-
-
Notifications
You must be signed in to change notification settings - Fork 340
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
fix paste with new line #2696
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,7 +15,13 @@ import { | |||||
CompletionContext, | ||||||
completionKeymap, | ||||||
} from '@codemirror/autocomplete'; | ||||||
import { EditorState, Extension, StateEffect, StateField } from '@codemirror/state'; | ||||||
import { | ||||||
ChangeSpec, | ||||||
EditorState, | ||||||
Extension, | ||||||
StateEffect, | ||||||
StateField, | ||||||
} from '@codemirror/state'; | ||||||
import { | ||||||
Decoration, | ||||||
DecorationSet, | ||||||
|
@@ -126,7 +132,22 @@ export class XInputComponent implements AfterViewInit, ControlValueAccessor { | |||||
}, | ||||||
}, | ||||||
}); | ||||||
|
||||||
const filterNewLine = EditorState.transactionFilter.of((tr) => { | ||||||
if (tr.changes.empty) return tr; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): Use block braces for ifs, whiles, etc. (
Suggested change
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 |
||||||
|
||||||
if (tr.isUserEvent('input.paste')) { | ||||||
// For paste events, replace newlines with spaces | ||||||
const changes = [ | ||||||
{ | ||||||
from: 0, | ||||||
insert: tr.newDoc.toString().replace(/\n/g, ' '), | ||||||
}, | ||||||
]; | ||||||
return [{ changes }]; | ||||||
} | ||||||
|
||||||
// Block multi-line input from other sources | ||||||
return tr.newDoc.lines > 1 ? [] : [tr]; | ||||||
}); | ||||||
|
||||||
|
There was a problem hiding this comment.
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:
This achieves the same result more clearly by:
There was a problem hiding this comment.
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!