Skip to content

Commit

Permalink
fix insert text before block embed (slab#2278)
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiaoyan authored and jhchen committed Aug 29, 2018
1 parent fd40052 commit 8d9e3ab
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
8 changes: 6 additions & 2 deletions core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down
49 changes: 49 additions & 0 deletions test/unit/core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,55 @@ describe('Editor', function() {
);
});

it('insert text before block embed', function() {
const editor = this.initialize(
Editor,
'<p>0123</p><iframe src="#" class="ql-video" frameborder="0" allowfullscreen="true"></iframe>',
);
editor.applyDelta(new Delta().retain(5).insert('5678'));
expect(this.container).toEqualHTML(
'<p>0123</p><p>5678</p><iframe src="#" class="ql-video" frameborder="0" allowfullscreen="true"></iframe>',
);
});

it('insert attributed text before block embed', function() {
const editor = this.initialize(
Editor,
'<p>0123</p><iframe src="#" class="ql-video" frameborder="0" allowfullscreen="true"></iframe>',
);
editor.applyDelta(new Delta().retain(5).insert('5678', { bold: true }));
expect(this.container).toEqualHTML(
'<p>0123</p><p><strong>5678</strong></p><iframe src="#" class="ql-video" frameborder="0" allowfullscreen="true"></iframe>',
);
});

it('insert text with newline before block embed', function() {
const editor = this.initialize(
Editor,
'<p>0123</p><iframe src="#" class="ql-video" frameborder="0" allowfullscreen="true"></iframe>',
);
editor.applyDelta(new Delta().retain(5).insert('5678\n'));
expect(this.container).toEqualHTML(
'<p>0123</p><p>5678</p><iframe src="#" class="ql-video" frameborder="0" allowfullscreen="true"></iframe>',
);
});

it('insert attributed text with newline before block embed', function() {
const editor = this.initialize(
Editor,
'<p>0123</p><iframe src="#" class="ql-video" frameborder="0" allowfullscreen="true"></iframe>',
);
editor.applyDelta(
new Delta()
.retain(5)
.insert('5678', { bold: true })
.insert('\n'),
);
expect(this.container).toEqualHTML(
'<p>0123</p><p><strong>5678</strong></p><iframe src="#" class="ql-video" frameborder="0" allowfullscreen="true"></iframe>',
);
});

it('improper block embed insert', function() {
const editor = this.initialize(Editor, '<p>0123</p>');
editor.applyDelta(new Delta().retain(2).insert({ video: '#' }));
Expand Down

0 comments on commit 8d9e3ab

Please sign in to comment.