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

Avoid emitting change events for void operations #4195

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions packages/quill/src/core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ class Editor {
this.delta = this.getDelta();
if (!change || !isEqual(oldDelta.compose(change), this.delta)) {
change = oldDelta.diff(this.delta, selectionInfo);
} else if (change) {
change = new Delta(change.ops.slice()).chop();
}
}
return change;
Expand Down
2 changes: 1 addition & 1 deletion packages/quill/test/fuzz/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe('editor', () => {
const doc = quill.getContents();
const change = generateChange(doc, randomInt(4) + 1);
const diff = quill.updateContents(change);
expect(change).toEqual(diff);
expect(change.chop()).toEqual(diff);
}
});
});
Expand Down
7 changes: 7 additions & 0 deletions packages/quill/test/unit/core/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,13 @@ describe('Editor', () => {
});
});

describe('update()', () => {
test('chop off void ops', () => {
const editor = createEditor('<p><strong>a</strong></p>');
expect(editor.update(new Delta().retain(1))).toEqual(new Delta());
});
});

describe('insertContents', () => {
const video =
'<iframe src="#" class="ql-video" frameborder="0" allowfullscreen="true"></iframe>';
Expand Down
52 changes: 39 additions & 13 deletions packages/quill/test/unit/core/quill.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,45 @@ describe('Quill', () => {
return { quill, oldDelta };
};

test('deleteText()', () => {
const { quill, oldDelta } = setup();
quill.deleteText(3, 2);
const change = new Delta().retain(3).delete(2);
expect(quill.root.innerHTML).toMatchInlineSnapshot(
'"<p>012<em>5</em>67</p>"',
);
expect(quill.emitter.emit).toHaveBeenCalledWith(
Emitter.events.TEXT_CHANGE,
change,
oldDelta,
Emitter.sources.API,
);
describe('deleteText()', () => {
test('delete text', () => {
const { quill, oldDelta } = setup();
quill.deleteText(3, 2);
const change = new Delta().retain(3).delete(2);
expect(quill.root.innerHTML).toMatchInlineSnapshot(
'"<p>012<em>5</em>67</p>"',
);
expect(quill.emitter.emit).toHaveBeenCalledWith(
Emitter.events.TEXT_CHANGE,
change,
oldDelta,
Emitter.sources.API,
);
});

test('delete 0 length', () => {
const { quill } = setup();
const delta = quill.deleteText(3, 0);
expect(delta).toEqual(new Delta());
expect(quill.emitter.emit).not.toHaveBeenCalledWith(
Emitter.events.TEXT_CHANGE,
expect.anything(),
expect.anything(),
expect.anything(),
);
});

test('delete after trailing', () => {
const { quill } = setup();
const delta = quill.deleteText(100000, 10);
expect(delta).toEqual(new Delta());
expect(quill.emitter.emit).not.toHaveBeenCalledWith(
Emitter.events.TEXT_CHANGE,
expect.anything(),
expect.anything(),
expect.anything(),
);
});
});

test('format()', () => {
Expand Down
Loading