Skip to content

Commit

Permalink
prevent layout item from being empty which would leed into a 100% cpu…
Browse files Browse the repository at this point in the history
… use
  • Loading branch information
basile-savouret committed Dec 4, 2024
1 parent b7fa4cf commit 3c93329
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
*
*/

import type {ElementNode, LexicalCommand, LexicalNode, NodeKey} from 'lexical';
import type {
ElementNode,
LexicalCommand,
LexicalNode,
NodeKey,
NodeMutation,
} from 'lexical';

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {
Expand Down Expand Up @@ -97,7 +103,23 @@ export function LayoutPlugin(): null {
return false;
};

const $fillLayoutItemIfEmpty = (nodes: Map<NodeKey, NodeMutation>) => {
editor.update(() => {
nodes.forEach((_, key) => {
const layoutItem = $getNodeByKey(key) as LayoutItemNode;
if (layoutItem) {
if (layoutItem.isEmpty()) {
layoutItem.append($createParagraphNode());
}
}
});
});
};

return mergeRegister(
// Layout item should always have a child. this function will listen
// for any empty layout item and fill it with a paragraph node
editor.registerMutationListener(LayoutItemNode, $fillLayoutItemIfEmpty),
// When layout is the last child pressing down/right arrow will insert paragraph
// below it to allow adding more content. It's similar what $insertBlockNode
// (mainly for decorators), except it'll always be possible to continue adding
Expand Down

0 comments on commit 3c93329

Please sign in to comment.