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

Multiple update tags #6507

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion packages/lexical/flow/Lexical.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ type EditorReadOptions = {
};
type EditorUpdateOptions = {
onUpdate?: () => void,
tag?: string,
tag?: string | Array<string>,
skipTransforms?: true,
discrete?: true,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export type TextNodeThemeClasses = {
export type EditorUpdateOptions = {
onUpdate?: () => void;
skipTransforms?: true;
tag?: string;
tag?: string | Array<string>;
discrete?: true;
};

Expand Down
27 changes: 16 additions & 11 deletions packages/lexical/src/LexicalUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ function $normalizeAllDirtyTextNodes(
}
}

function addTags(editor: LexicalEditor, tags: undefined | string | string[]) {
if (!tags) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be strict here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!tags) {
if (tags === undefined || tags.length === 0) {

return;
}
const updateTags = editor._updateTags;
let tags_ = tags;
if (!Array.isArray(tags)) {
tags_ = [tags];
}
for (const tag of tags_) {
updateTags.add(tag);
}
}

/**
* Transform heuristic:
* 1. We transform leaves first. If transforms generate additional dirty nodes we repeat step 1.
Expand Down Expand Up @@ -829,11 +843,9 @@ function processNestedUpdates(
const [nextUpdateFn, options] = queuedUpdate;

let onUpdate;
let tag;

if (options !== undefined) {
onUpdate = options.onUpdate;
tag = options.tag;

if (options.skipTransforms) {
skipTransforms = true;
Expand All @@ -851,9 +863,7 @@ function processNestedUpdates(
editor._deferred.push(onUpdate);
}

if (tag) {
editor._updateTags.add(tag);
}
addTags(editor, options.tag);
}

nextUpdateFn();
Expand All @@ -870,17 +880,12 @@ function $beginUpdate(
): void {
const updateTags = editor._updateTags;
let onUpdate;
let tag;
let skipTransforms = false;
let discrete = false;

if (options !== undefined) {
onUpdate = options.onUpdate;
tag = options.tag;

if (tag != null) {
updateTags.add(tag);
}
addTags(editor, options.tag);

skipTransforms = options.skipTransforms || false;
discrete = options.discrete || false;
Expand Down
23 changes: 23 additions & 0 deletions packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {createPortal} from 'react-dom';
import {createRoot, Root} from 'react-dom/client';
import * as ReactTestUtils from 'shared/react-test-utils';

import {emptyFunction} from '../../LexicalUtils';
import {
$createTestDecoratorNode,
$createTestElementNode,
Expand Down Expand Up @@ -2039,6 +2040,28 @@ describe('LexicalEditor tests', () => {
]);
});

it('multiple update tags', async () => {
init();
const $mutateSomething = $createTextNode;

editor.update($mutateSomething, {
tag: ['a', 'b'],
});
expect(editor._updateTags).toEqual(new Set(['a', 'b']));
editor.update(
() => {
editor.update(emptyFunction, {tag: ['e', 'f']});
},
{
tag: ['c', 'd'],
},
);
expect(editor._updateTags).toEqual(new Set(['a', 'b', 'c', 'd', 'e', 'f']));

await Promise.resolve();
expect(editor._updateTags).toEqual(new Set([]));
});

it('mutation listeners does not trigger when other node types are mutated', async () => {
init();

Expand Down
Loading