Skip to content

Commit 84d2234

Browse files
committed
fix setContents on block embed ending
1 parent ff47bb0 commit 84d2234

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

core/quill.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -383,18 +383,13 @@ class Quill {
383383
() => {
384384
delta = new Delta(delta);
385385
const length = this.getLength();
386-
const deleted = this.editor.deleteText(0, length);
386+
// Quill will set empty editor to \n
387+
const delete1 = this.editor.deleteText(0, length);
388+
// delta always applied before existing content
387389
const applied = this.editor.applyDelta(delta);
388-
const lastOp = applied.ops[applied.ops.length - 1];
389-
if (
390-
lastOp != null &&
391-
typeof lastOp.insert === 'string' &&
392-
lastOp.insert[lastOp.insert.length - 1] === '\n'
393-
) {
394-
this.editor.deleteText(this.getLength() - 1, 1);
395-
applied.delete(1);
396-
}
397-
return deleted.compose(applied);
390+
// Remove extra \n from empty editor initialization
391+
const delete2 = this.editor.deleteText(this.getLength() - 1, 1);
392+
return delete1.compose(applied).compose(delete2);
398393
},
399394
source,
400395
);

test/unit/core/quill.js

+7
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,13 @@ describe('Quill', function() {
429429
expect(quill.getContents()).toEqual(contents);
430430
expect(delta).toEqual(contents.delete(contents.length()));
431431
});
432+
433+
it('block embed', function() {
434+
const quill = this.initialize(Quill, '<p>Hello World!</p>');
435+
const contents = new Delta().insert({ video: '#' });
436+
quill.setContents(contents);
437+
expect(quill.getContents()).toEqual(contents);
438+
});
432439
});
433440

434441
describe('setText()', function() {

0 commit comments

Comments
 (0)