Skip to content

Commit

Permalink
used recommended solution
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Nov 7, 2024
1 parent 620ae19 commit a82bc79
Showing 1 changed file with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,20 @@ export class XInputComponent implements AfterViewInit, ControlValueAccessor {

const filterNewLine = EditorState.transactionFilter.of((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;
}
});
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 }];
}

return [tr, { changes: removeNLs, sequential: true }];
// Block multi-line input from other sources
return tr.newDoc.lines > 1 ? [] : [tr];
});

return [
Expand Down

0 comments on commit a82bc79

Please sign in to comment.