From 620ae19d7bd654a1b7ac982a875069314c8eba17 Mon Sep 17 00:00:00 2001 From: Samuel Imolorhe Date: Thu, 7 Nov 2024 02:32:21 +0100 Subject: [PATCH] fix paste with new line --- .../components/x-input/x-input.component.ts | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 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 218731e686..08f2744e58 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 @@ -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; + 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 [