From a82bc798e2168a06e2410041519a71b8b5bed67f Mon Sep 17 00:00:00 2001 From: Samuel Imolorhe Date: Thu, 7 Nov 2024 11:15:11 +0100 Subject: [PATCH] used recommended solution --- .../components/x-input/x-input.component.ts | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/packages/altair-app/src/app/modules/altair/components/x-input/x-input.component.ts b/packages/altair-app/src/app/modules/altair/components/x-input/x-input.component.ts index 08f2744e58..e68a1a8e97 100644 --- a/packages/altair-app/src/app/modules/altair/components/x-input/x-input.component.ts +++ b/packages/altair-app/src/app/modules/altair/components/x-input/x-input.component.ts @@ -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 [