Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix paste with new line #2696

Merged
merged 2 commits into from
Nov 7, 2024
Merged

fix paste with new line #2696

merged 2 commits into from
Nov 7, 2024

Conversation

imolorhe
Copy link
Collaborator

@imolorhe imolorhe commented Nov 7, 2024

Fixes

Closes #2681

Checks

  • Ran yarn test-build
  • Updated relevant documentations
  • Updated matching config options in altair-static

Changes proposed in this pull request:

Summary by Sourcery

Bug Fixes:

  • Fix the issue where pasting text with new lines was not handled correctly by filtering out new lines only when the input is not a paste event.

Copy link

sourcery-ai bot commented Nov 7, 2024

Reviewer's Guide by Sourcery

This PR modifies the line filtering behavior in the x-input component to properly handle pasted text containing newlines. Instead of completely blocking multi-line input when pasting, it now removes newline characters while preserving the content.

Sequence diagram for paste handling in x-input component

sequenceDiagram
    participant User
    participant XInputComponent
    participant EditorState

    User->>XInputComponent: Paste text
    XInputComponent->>EditorState: Create transaction
    EditorState->>XInputComponent: Transaction with changes
    XInputComponent->>XInputComponent: Check if paste event
    alt Paste event with new lines
        XInputComponent->>XInputComponent: Remove newlines
    else No new lines or not paste event
        XInputComponent->>XInputComponent: Allow transaction
    end
    XInputComponent->>EditorState: Apply changes
    EditorState->>User: Updated text without newlines
Loading

File-Level Changes

Change Details Files
Improved newline handling in text input filtering
  • Added check for paste events to allow multi-line content during paste operations
  • Implemented iterative processing of pasted content to remove newline characters
  • Preserved first line content and removed only leading newlines from subsequent lines
  • Added support for handling empty transactions
packages/altair-app/src/app/modules/altair/components/x-input/x-input.component.ts

Assessment against linked issues

Issue Objective Addressed Explanation
#2681 Fix the inability to paste values with trailing newlines into Header value fields

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @imolorhe - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Please complete the PR checklist (running tests, updating documentation, etc) and mark the items when done.
  • Could you expand the PR description to explain the specific changes made and how they fix the paste behavior?
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -126,8 +132,28 @@ export class XInputComponent implements AfterViewInit, ControlValueAccessor {
},
},
});

const filterNewLine = EditorState.transactionFilter.of((tr) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider simplifying the newline filtering logic by using string replacement instead of iterative change tracking

The current implementation is unnecessarily complex with nested iterations and change tracking. Here's a simpler approach that maintains the same functionality:

const filterNewLine = EditorState.transactionFilter.of((tr) => {
  if (tr.changes.empty) return tr;

  if (tr.isUserEvent('input.paste')) {
    // For paste events, replace newlines with spaces
    const changes = [{
      from: 0,
      to: tr.newDoc.length,
      insert: tr.newDoc.toString().replace(/\n/g, ' ')
    }];
    return [{ changes }];
  }

  // Block multi-line input from other sources
  return tr.newDoc.lines > 1 ? [] : [tr];
});

This achieves the same result more clearly by:

  1. Handling paste events with a simple string replacement
  2. Blocking other multi-line input directly
  3. Eliminating the need for nested iterations and change tracking

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's brilliant! It's a much simpler logic to the problem. I only had to tweak a bit but most of the logic worked!

const filterNewLine = EditorState.transactionFilter.of((tr) => {
return tr.newDoc.lines > 1 ? [] : [tr];
if (tr.changes.empty) return tr;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): Use block braces for ifs, whiles, etc. (use-braces)

Suggested change
if (tr.changes.empty) return tr;
if (tr.changes.empty) {


ExplanationIt is recommended to always use braces and create explicit statement blocks.

Using the allowed syntax to just write a single statement can lead to very confusing
situations, especially where subsequently a developer might add another statement
while forgetting to add the braces (meaning that this wouldn't be included in the condition).

const removeNLs: ChangeSpec[] = [];
tr.changes.iterChanges((fromA, toA, fromB, toB, ins) => {
const lineIter = ins.iterLines().next();
if (ins.lines <= 1) return;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): Use block braces for ifs, whiles, etc. (use-braces)

Suggested change
if (ins.lines <= 1) return;
if (ins.lines <= 1) {


ExplanationIt is recommended to always use braces and create explicit statement blocks.

Using the allowed syntax to just write a single statement can lead to very confusing
situations, especially where subsequently a developer might add another statement
while forgetting to add the braces (meaning that this wouldn't be included in the condition).

Copy link

github-actions bot commented Nov 7, 2024

Visit the preview URL for this PR (updated for commit 22188f2):

https://altair-gql--pr2696-imolorhe-fix-paste-w-lcn91wnm.web.app

(expires Thu, 14 Nov 2024 10:22:41 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 02d6323d75a99e532a38922862e269d63351a6cf

@imolorhe imolorhe added this pull request to the merge queue Nov 7, 2024
Merged via the queue into master with commit a82bc79 Nov 7, 2024
14 checks passed
@imolorhe imolorhe deleted the imolorhe/fix-paste-with-new-line branch November 7, 2024 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Cannot paste value with trailing newline in Header values anymore
1 participant