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 inline embed insertions with applyDelta() #3793

Merged
merged 2 commits into from
Jun 15, 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
10 changes: 7 additions & 3 deletions core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ class Editor {
) {
isImplicitNewlineAppended = true;
}
const [leaf] = this.scroll.leaf(index);
const formats = merge({}, bubbleFormats(leaf));
attributes = AttributeMap.diff(formats, attributes) || {};
} else if (index > 0) {
// @ts-expect-error
const [leaf, offset] = this.scroll.descendant(LeafBlot, index - 1);
Expand All @@ -86,6 +83,13 @@ class Editor {
}
}
this.scroll.insertAt(index, key, op.insert[key]);

if (isInlineEmbed) {
// @ts-expect-error
const [leaf] = this.scroll.descendant(LeafBlot, index);
const formats = merge({}, bubbleFormats(leaf));
attributes = AttributeMap.diff(formats, attributes) || {};
}
}
scrollLength += length;
} else {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,22 @@ describe('Editor', function () {
);
});

it('insert inline embed between plain text and formatted content', function () {
const editor = this.initialize(Editor, '<p>a<strong>b</strong></p>');
editor.applyDelta(new Delta().retain(1).insert({ image: '#' }));
expect(this.container).toEqualHTML(
'<p>a<img src="#"><strong>b</strong></p>',
);
});

it('prepend inline embed to another inline embed with same attributes', function () {
const editor = this.initialize(Editor, '<p><img src="#" alt="hi"/></p>');
editor.applyDelta(new Delta().insert({ image: '#' }, { alt: 'hi' }));
expect(this.container).toEqualHTML(
'<p><img src="#" alt="hi"><img src="#" alt="hi"></p>',
);
});

it('insert block embed with delete before block embed', function () {
const editor = this.initialize(
Editor,
Expand Down