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

[lexical] Feature: Add onUpdate function during update with $onUpdate (correct baselline) #6773

Merged
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
1 change: 1 addition & 0 deletions packages/lexical/flow/Lexical.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ declare export function $isParagraphNode(
export type EventHandler = (event: Event, editor: LexicalEditor) => void;
declare export function $hasUpdateTag(tag: string): boolean;
declare export function $addUpdateTag(tag: string): void;
declare export function $onUpdate(updateFn: () => void): void;
declare export function $getNearestNodeFromDOMNode(
startingDOM: Node,
): LexicalNode | null;
Expand Down
13 changes: 13 additions & 0 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,19 @@ export function $addUpdateTag(tag: string): void {
editor._updateTags.add(tag);
}

/**
* Add a function to run after the current update. This will run after any
* `onUpdate` function already supplied to `editor.update()`, as well as any
* functions added with previous calls to `$onUpdate`.
*
* @param updateFn The function to run after the current update.
*/
export function $onUpdate(updateFn: () => void): void {
errorOnReadOnly();
const editor = getActiveEditor();
editor._deferred.push(updateFn);
}

export function $maybeMoveChildrenSelectionToParent(
parentNode: LexicalNode,
): BaseSelection | null {
Expand Down
38 changes: 38 additions & 0 deletions packages/lexical/src/__tests__/unit/LexicalUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from 'lexical';

import {
$onUpdate,
emptyFunction,
generateRandomKey,
getCachedTypeToNodeMap,
Expand Down Expand Up @@ -242,6 +243,43 @@ describe('LexicalUtils tests', () => {
});
});

describe('$onUpdate', () => {
test('added fn runs after update, original onUpdate, and prior calls to $onUpdate', () => {
const {editor} = testEnv;
const runs: string[] = [];

editor.update(
() => {
$getRoot().append(
$createParagraphNode().append($createTextNode('foo')),
);
$onUpdate(() => {
runs.push('second');
});
$onUpdate(() => {
runs.push('third');
});
},
{
onUpdate: () => {
runs.push('first');
},
},
);

// Flush pending updates
editor.read(() => {});

expect(runs).toEqual(['first', 'second', 'third']);
});

test('adding fn throws outside update', () => {
expect(() => {
$onUpdate(() => {});
}).toThrow();
});
});

test('getCachedTypeToNodeMap', async () => {
const {editor} = testEnv;
const paragraphKeys: string[] = [];
Expand Down
1 change: 1 addition & 0 deletions packages/lexical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export {
$isRootOrShadowRoot,
$isTokenOrSegmented,
$nodesOfType,
$onUpdate,
$selectAll,
$setCompositionKey,
$setSelection,
Expand Down
Loading