From 84d22342d5e3eb33453d4601d2c7f55d26b47495 Mon Sep 17 00:00:00 2001 From: Jason Chen Date: Wed, 15 Jul 2020 18:18:28 -0700 Subject: [PATCH] fix setContents on block embed ending --- core/quill.js | 17 ++++++----------- test/unit/core/quill.js | 7 +++++++ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/core/quill.js b/core/quill.js index 2ee1578e1e..44bf15dd97 100644 --- a/core/quill.js +++ b/core/quill.js @@ -383,18 +383,13 @@ class Quill { () => { delta = new Delta(delta); const length = this.getLength(); - const deleted = this.editor.deleteText(0, length); + // Quill will set empty editor to \n + const delete1 = this.editor.deleteText(0, length); + // delta always applied before existing content const applied = this.editor.applyDelta(delta); - const lastOp = applied.ops[applied.ops.length - 1]; - if ( - lastOp != null && - typeof lastOp.insert === 'string' && - lastOp.insert[lastOp.insert.length - 1] === '\n' - ) { - this.editor.deleteText(this.getLength() - 1, 1); - applied.delete(1); - } - return deleted.compose(applied); + // Remove extra \n from empty editor initialization + const delete2 = this.editor.deleteText(this.getLength() - 1, 1); + return delete1.compose(applied).compose(delete2); }, source, ); diff --git a/test/unit/core/quill.js b/test/unit/core/quill.js index c508599824..424a979f82 100644 --- a/test/unit/core/quill.js +++ b/test/unit/core/quill.js @@ -429,6 +429,13 @@ describe('Quill', function() { expect(quill.getContents()).toEqual(contents); expect(delta).toEqual(contents.delete(contents.length())); }); + + it('block embed', function() { + const quill = this.initialize(Quill, '

Hello World!

'); + const contents = new Delta().insert({ video: '#' }); + quill.setContents(contents); + expect(quill.getContents()).toEqual(contents); + }); }); describe('setText()', function() {