Skip to content

Commit 83d3cee

Browse files
zurfyxetrepumivailop7
authored
Multiple update tags (#6507)
Co-authored-by: Bob Ippolito <[email protected]> Co-authored-by: Ivaylo Pavlov <[email protected]>
1 parent 7646a85 commit 83d3cee

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

packages/lexical/flow/Lexical.js.flow

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ type EditorReadOptions = {
215215
};
216216
type EditorUpdateOptions = {
217217
onUpdate?: () => void,
218-
tag?: string,
218+
tag?: string | Array<string>,
219219
skipTransforms?: true,
220220
discrete?: true,
221221
};

packages/lexical/src/LexicalEditor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export type TextNodeThemeClasses = {
8080
export type EditorUpdateOptions = {
8181
onUpdate?: () => void;
8282
skipTransforms?: true;
83-
tag?: string;
83+
tag?: string | Array<string>;
8484
discrete?: true;
8585
};
8686

packages/lexical/src/LexicalUpdates.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,20 @@ function $normalizeAllDirtyTextNodes(
219219
}
220220
}
221221

222+
function addTags(editor: LexicalEditor, tags: undefined | string | string[]) {
223+
if (!tags) {
224+
return;
225+
}
226+
const updateTags = editor._updateTags;
227+
let tags_ = tags;
228+
if (!Array.isArray(tags)) {
229+
tags_ = [tags];
230+
}
231+
for (const tag of tags_) {
232+
updateTags.add(tag);
233+
}
234+
}
235+
222236
/**
223237
* Transform heuristic:
224238
* 1. We transform leaves first. If transforms generate additional dirty nodes we repeat step 1.
@@ -829,11 +843,9 @@ function processNestedUpdates(
829843
const [nextUpdateFn, options] = queuedUpdate;
830844

831845
let onUpdate;
832-
let tag;
833846

834847
if (options !== undefined) {
835848
onUpdate = options.onUpdate;
836-
tag = options.tag;
837849

838850
if (options.skipTransforms) {
839851
skipTransforms = true;
@@ -851,9 +863,7 @@ function processNestedUpdates(
851863
editor._deferred.push(onUpdate);
852864
}
853865

854-
if (tag) {
855-
editor._updateTags.add(tag);
856-
}
866+
addTags(editor, options.tag);
857867
}
858868

859869
nextUpdateFn();
@@ -870,17 +880,12 @@ function $beginUpdate(
870880
): void {
871881
const updateTags = editor._updateTags;
872882
let onUpdate;
873-
let tag;
874883
let skipTransforms = false;
875884
let discrete = false;
876885

877886
if (options !== undefined) {
878887
onUpdate = options.onUpdate;
879-
tag = options.tag;
880-
881-
if (tag != null) {
882-
updateTags.add(tag);
883-
}
888+
addTags(editor, options.tag);
884889

885890
skipTransforms = options.skipTransforms || false;
886891
discrete = options.discrete || false;

packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {createRoot, Root} from 'react-dom/client';
6161
import invariant from 'shared/invariant';
6262
import * as ReactTestUtils from 'shared/react-test-utils';
6363

64+
import {emptyFunction} from '../../LexicalUtils';
6465
import {
6566
$createTestDecoratorNode,
6667
$createTestElementNode,
@@ -2041,6 +2042,28 @@ describe('LexicalEditor tests', () => {
20412042
]);
20422043
});
20432044

2045+
it('multiple update tags', async () => {
2046+
init();
2047+
const $mutateSomething = $createTextNode;
2048+
2049+
editor.update($mutateSomething, {
2050+
tag: ['a', 'b'],
2051+
});
2052+
expect(editor._updateTags).toEqual(new Set(['a', 'b']));
2053+
editor.update(
2054+
() => {
2055+
editor.update(emptyFunction, {tag: ['e', 'f']});
2056+
},
2057+
{
2058+
tag: ['c', 'd'],
2059+
},
2060+
);
2061+
expect(editor._updateTags).toEqual(new Set(['a', 'b', 'c', 'd', 'e', 'f']));
2062+
2063+
await Promise.resolve();
2064+
expect(editor._updateTags).toEqual(new Set([]));
2065+
});
2066+
20442067
it('mutation listeners does not trigger when other node types are mutated', async () => {
20452068
init();
20462069

0 commit comments

Comments
 (0)