Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HTML and Delta not matching #3784

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions blots/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Block extends BlockBlot {
this.cache = {};
}

insertAt(index, value, def?: unknown) {
insertAt(index: number, value: string, def?: unknown) {
if (def != null) {
super.insertAt(index, value, def);
this.cache = {};
Expand Down Expand Up @@ -156,12 +156,23 @@ class BlockEmbed extends EmbedBlot {
}

insertAt(index: number, value: string, def?: unknown) {
if (typeof value === 'string' && value.endsWith('\n')) {
const block = this.scroll.create(Block.blotName);
this.parent.insertBefore(block, index === 0 ? this : this.next);
block.insertAt(0, value.slice(0, -1));
} else {
if (def != null) {
super.insertAt(index, value, def);
return;
}
const lines = value.split('\n');
const text = lines.pop();
const blocks = lines.map(line => {
const block = this.scroll.create(Block.blotName);
block.insertAt(0, line);
return block;
});
const ref = this.split(index);
blocks.forEach(block => {
this.parent.insertBefore(block, ref);
});
if (text) {
this.parent.insertBefore(this.scroll.create('text', text), ref);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/unit/blots/block-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ describe('Block Embed', function () {
`);
});

it('insert multiple newlines before', function () {
const scroll = this.initialize(
Scroll,
'<iframe src="#" class="ql-video" frameborder="0" allowfullscreen="true"></iframe>',
);
scroll.insertAt(0, '\n\n\n');
scroll.optimize();
expect(scroll.domNode).toEqualHTML(`
<p><br></p>
<p><br></p>
<p><br></p>
<iframe src="#" class="ql-video" frameborder="0" allowfullscreen="true"></iframe>
`);
});

it('insert newline after', function () {
const scroll = this.initialize(
Scroll,
Expand Down
13 changes: 13 additions & 0 deletions test/unit/core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,19 @@ describe('Editor', function () {
);
});

it('insert formatted lines 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('a\nb').insert('\n', { header: 1 }),
);
expect(this.container).toEqualHTML(
'<p>0123</p><p>a</p><h1>b</h1><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,
Expand Down