From cf101f681158a48b1b477c1544680cc9614425ae Mon Sep 17 00:00:00 2001 From: Jason Chen Date: Mon, 16 Aug 2021 17:57:22 -0700 Subject: [PATCH] track all implicit newline indexes and shift for delete --- core/editor.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/core/editor.js b/core/editor.js index 414bd81a7a..5d1dfafc9f 100644 --- a/core/editor.js +++ b/core/editor.js @@ -18,10 +18,12 @@ class Editor { } applyDelta(delta) { - let consumeNextNewline = false; this.scroll.update(); let scrollLength = this.scroll.length(); this.scroll.batchStart(); + // Track indexes of violations of newline requirements + // (at end of doc and before block embeds) + const implicitNewlines = []; const normalizedDelta = normalizeDelta(delta); normalizedDelta.reduce((index, op) => { const length = Op.length(op); @@ -29,8 +31,8 @@ class Editor { if (op.insert != null) { if (typeof op.insert === 'string') { let text = op.insert; - if (text.endsWith('\n') && consumeNextNewline) { - consumeNextNewline = false; + if (text.endsWith('\n') && implicitNewlines.length > 0) { + implicitNewlines.shift(); text = text.slice(0, -1); } if ( @@ -38,7 +40,7 @@ class Editor { this.scroll.descendant(BlockEmbed, index)[0]) && !text.endsWith('\n') ) { - consumeNextNewline = true; + implicitNewlines.push(index + length); } this.scroll.insertAt(index, text); const [line, offset] = this.scroll.line(index); @@ -55,7 +57,7 @@ class Editor { this.scroll.query(key, Scope.INLINE) != null && this.scroll.descendant(BlockEmbed, index)[0] ) { - consumeNextNewline = true; + implicitNewlines.push(index); } this.scroll.insertAt(index, key, op.insert[key]); } @@ -66,7 +68,10 @@ class Editor { }); return index + length; }, 0); - normalizedDelta.reduce((index, op) => { + const implicitDelta = implicitNewlines.reduce((delta, index) => { + return delta.retain(index).delete(1); + }, new Delta()); + normalizedDelta.compose(implicitDelta).reduce((index, op) => { if (typeof op.delete === 'number') { this.scroll.deleteAt(index, op.delete); return index;