From 4bc4736dbefe5d90739f8c3a2ca0fb8ba89d4482 Mon Sep 17 00:00:00 2001 From: Cameron Hessler Date: Thu, 27 Oct 2022 03:20:48 -0500 Subject: [PATCH] fix(extension-placeholder) Fix perf issue w/empty nodes --- packages/extension-placeholder/src/placeholder.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/extension-placeholder/src/placeholder.ts b/packages/extension-placeholder/src/placeholder.ts index b00b6b86844..5e475c74025 100644 --- a/packages/extension-placeholder/src/placeholder.ts +++ b/packages/extension-placeholder/src/placeholder.ts @@ -44,6 +44,10 @@ export const Placeholder = Extension.create({ return null } + // only calculate isEmpty once due to its performance impacts (see issue #3360) + const emptyDocInstance = doc.type.createAndFill() + const isEditorEmpty = emptyDocInstance?.sameMarkup(doc) && emptyDocInstance.content.findDiffStart(doc.content) === null + doc.descendants((node, pos) => { const hasAnchor = anchor >= pos && anchor <= (pos + node.nodeSize) const isEmpty = !node.isLeaf && !node.childCount @@ -51,7 +55,7 @@ export const Placeholder = Extension.create({ if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) { const classes = [this.options.emptyNodeClass] - if (this.editor.isEmpty) { + if (isEditorEmpty) { classes.push(this.options.emptyEditorClass) }