From 99c436a1c6fa7a0badd200a2db152e00f9624a46 Mon Sep 17 00:00:00 2001 From: XiaoYan Li Date: Mon, 27 Aug 2018 16:56:39 +0800 Subject: [PATCH] fix insert text before block embed --- core/editor.js | 8 +++++-- test/unit/core/editor.js | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/core/editor.js b/core/editor.js index 2df22da909..65954c11ae 100644 --- a/core/editor.js +++ b/core/editor.js @@ -6,7 +6,7 @@ import DeltaOp from 'quill-delta/lib/op'; import { LeafBlot } from 'parchment'; import { Range } from './selection'; import CursorBlot from '../blots/cursor'; -import Block, { bubbleFormats } from '../blots/block'; +import Block, { BlockEmbed, bubbleFormats } from '../blots/block'; import Break from '../blots/break'; import TextBlot, { escapeText } from '../blots/text'; @@ -34,7 +34,11 @@ class Editor { consumeNextNewline = false; text = text.slice(0, -1); } - if (index >= scrollLength && !text.endsWith('\n')) { + if ( + (index >= scrollLength || + this.scroll.descendant(BlockEmbed, index)[0]) && + !text.endsWith('\n') + ) { consumeNextNewline = true; } this.scroll.insertAt(index, text); diff --git a/test/unit/core/editor.js b/test/unit/core/editor.js index 72b855a52f..6a35a6381b 100644 --- a/test/unit/core/editor.js +++ b/test/unit/core/editor.js @@ -357,6 +357,55 @@ describe('Editor', function() { ); }); + it('insert text before block embed', function() { + const editor = this.initialize( + Editor, + '

0123

', + ); + editor.applyDelta(new Delta().retain(5).insert('5678')); + expect(this.container).toEqualHTML( + '

0123

5678

', + ); + }); + + it('insert attributed text before block embed', function() { + const editor = this.initialize( + Editor, + '

0123

', + ); + editor.applyDelta(new Delta().retain(5).insert('5678', { bold: true })); + expect(this.container).toEqualHTML( + '

0123

5678

', + ); + }); + + it('insert text with newline before block embed', function() { + const editor = this.initialize( + Editor, + '

0123

', + ); + editor.applyDelta(new Delta().retain(5).insert('5678\n')); + expect(this.container).toEqualHTML( + '

0123

5678

', + ); + }); + + it('insert attributed text with newline before block embed', function() { + const editor = this.initialize( + Editor, + '

0123

', + ); + editor.applyDelta( + new Delta() + .retain(5) + .insert('5678', { bold: true }) + .insert('\n'), + ); + expect(this.container).toEqualHTML( + '

0123

5678

', + ); + }); + it('improper block embed insert', function() { const editor = this.initialize(Editor, '

0123

'); editor.applyDelta(new Delta().retain(2).insert({ video: '#' }));