From a357cbaf8dc29f7ff4afb10a839fe82577fbf099 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 16 Oct 2023 19:28:41 -0400 Subject: [PATCH] perf: Improve performance of Placeholder extension --- shared/editor/extensions/Placeholder.ts | 27 ++++++++++++------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/shared/editor/extensions/Placeholder.ts b/shared/editor/extensions/Placeholder.ts index b6998f4ee..a85f71956 100644 --- a/shared/editor/extensions/Placeholder.ts +++ b/shared/editor/extensions/Placeholder.ts @@ -22,24 +22,23 @@ export default class Placeholder extends Extension { const { doc } = state; const decorations: Decoration[] = []; const completelyEmpty = - doc.textContent === "" && doc.childCount <= 1 && - doc.content.size <= 2; + doc.content.size <= 2 && + doc.textContent === ""; - doc.descendants((node, pos) => { - if (!completelyEmpty) { - return; - } - if (pos !== 0 || node.type.name !== "paragraph") { - return; - } + if (completelyEmpty) { + doc.descendants((node, pos) => { + if (pos !== 0 || node.type.name !== "paragraph") { + return; + } - const decoration = Decoration.node(pos, pos + node.nodeSize, { - class: this.options.emptyNodeClass, - "data-empty-text": this.options.placeholder, + const decoration = Decoration.node(pos, pos + node.nodeSize, { + class: this.options.emptyNodeClass, + "data-empty-text": this.options.placeholder, + }); + decorations.push(decoration); }); - decorations.push(decoration); - }); + } return DecorationSet.create(doc, decorations); },