Skip to content

Commit

Permalink
fix(extension-placeholder) Fix perf issue w/empty nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
BTCameronHessler committed Oct 27, 2022
1 parent 2b69f34 commit 5b60278
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/extension-placeholder/src/placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ export const Placeholder = Extension.create<PlaceholderOptions>({
return null
}

// Calculate isEmpty once due to its performance impace (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

if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
const classes = [this.options.emptyNodeClass]

if (this.editor.isEmpty) {
if (isEditorEmpty) {
classes.push(this.options.emptyEditorClass)
}

Expand Down

0 comments on commit 5b60278

Please sign in to comment.