Skip to content

Commit

Permalink
fix paste with new line
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Nov 7, 2024
1 parent 0f9d967 commit 7b34839
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
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;
// 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 [
Expand Down

0 comments on commit 7b34839

Please sign in to comment.