-
-
Notifications
You must be signed in to change notification settings - Fork 333
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 1 commit
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,8 +132,28 @@ export class XInputComponent implements AfterViewInit, ControlValueAccessor { | |||||
}, | ||||||
}, | ||||||
}); | ||||||
|
||||||
const filterNewLine = EditorState.transactionFilter.of((tr) => { | ||||||
return tr.newDoc.lines > 1 ? [] : [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.newDoc.lines > 1 && !tr.isUserEvent('input.paste')) { | ||||||
return []; | ||||||
} | ||||||
|
||||||
const removeNLs: ChangeSpec[] = []; | ||||||
tr.changes.iterChanges((fromA, toA, fromB, toB, ins) => { | ||||||
const lineIter = ins.iterLines().next(); | ||||||
if (ins.lines <= 1) return; | ||||||
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 |
||||||
// skip the first line | ||||||
let len = fromB + lineIter.value.length; | ||||||
lineIter.next(); | ||||||
// for the next lines, remove the leading NL | ||||||
for (; !lineIter.done; lineIter.next()) { | ||||||
removeNLs.push({ from: len, to: len + 1 }); | ||||||
len += lineIter.value.length + 1; | ||||||
} | ||||||
}); | ||||||
|
||||||
return [tr, { changes: removeNLs, sequential: true }]; | ||||||
}); | ||||||
|
||||||
return [ | ||||||
|
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!