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 insertContents for prepending block embeds #3842

Merged
merged 1 commit into from
Jul 25, 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
9 changes: 5 additions & 4 deletions blots/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,19 @@ class Scroll extends ScrollBlot {
? first.delta
: new Delta().insert({ [first.key]: first.value });
insertInlineContents(this, index, delta);
const newlineCharIndex = index + delta.length();
const newlineCharLength = first.type === 'block' ? 1 : 0;
const lineEndIndex = index + delta.length() + newlineCharLength;
if (shouldInsertNewlineChar) {
this.insertAt(newlineCharIndex, '\n');
this.insertAt(lineEndIndex - 1, '\n');
}

const formats = bubbleFormats(this.line(index)[0]);
const attributes = AttributeMap.diff(formats, first.attributes) || {};
Object.keys(attributes).forEach(name => {
this.formatAt(newlineCharIndex, 1, name, attributes[name]);
this.formatAt(lineEndIndex - 1, 1, name, attributes[name]);
});

index = newlineCharIndex + 1;
index = lineEndIndex;
}

let [refBlot, refBlotOffset] = this.children.find(index);
Expand Down
12 changes: 8 additions & 4 deletions test/fuzz/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ const generateSingleInsertDelta = (): Delta['ops'][number] & {
return op;
};

const safePushInsert = (delta: Delta) => {
const safePushInsert = (delta: Delta, isDoc: boolean) => {
const op = generateSingleInsertDelta();
if (typeof op.insert === 'object' && op.insert[BLOCK_EMBED_NAME]) {
if (
typeof op.insert === 'object' &&
op.insert[BLOCK_EMBED_NAME] &&
(!isDoc || (delta.ops.length && !isLineFinished(delta)))
) {
delta.insert('\n');
}
delta.push(op);
Expand All @@ -134,7 +138,7 @@ const generateDocument = () => {
const delta = new Delta();
const operationCount = 2 + randomInt(20);
for (let i = 0; i < operationCount; i += 1) {
safePushInsert(delta);
safePushInsert(delta, true);
}
if (!isLineFinished(delta)) {
delta.insert('\n');
Expand All @@ -161,7 +165,7 @@ const generateChange = (
const delta = new Delta();
const operationCount = randomInt(5) + 1;
for (let i = 0; i < operationCount; i += 1) {
safePushInsert(delta);
safePushInsert(delta, false);
}
if (
needNewline ||
Expand Down
27 changes: 27 additions & 0 deletions test/unit/core/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,33 @@ describe('Editor', () => {
});
});

test('prepends a formatted block embed', () => {
const editor = createEditor(`<p></p>`);
editor.insertContents(
0,
new Delta().insert({ video: '#' }, { width: '300' }),
);
expect(editor.getDelta().ops).toEqual([
{ insert: { video: '#' }, attributes: { width: '300' } },
{ insert: '\n' },
]);
});

test('prepends two formatted block embeds', () => {
const editor = createEditor(`<p></p>`);
editor.insertContents(
0,
new Delta()
.insert({ video: '#' }, { width: '300' })
.insert({ video: '#' }, { width: '600' }),
);
expect(editor.getDelta().ops).toEqual([
{ insert: { video: '#' }, attributes: { width: '300' } },
{ insert: { video: '#' }, attributes: { width: '600' } },
{ insert: '\n' },
]);
});

test('inserts formatted block embeds (styles)', () => {
const editor = createEditor(`<p></p>`);
editor.insertContents(
Expand Down