From ebddbadc1b4bfa4d4222d74427458ba78a542fbf Mon Sep 17 00:00:00 2001 From: BrianHung Date: Wed, 30 Mar 2022 01:24:02 -0700 Subject: [PATCH 1/3] add-empty-editor-class-to-root-div --- packages/extension-placeholder/src/placeholder.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/extension-placeholder/src/placeholder.ts b/packages/extension-placeholder/src/placeholder.ts index 1ecd8398..d05e4145 100644 --- a/packages/extension-placeholder/src/placeholder.ts +++ b/packages/extension-placeholder/src/placeholder.ts @@ -32,6 +32,7 @@ export const Placeholder = Extension.create({ }, addProseMirrorPlugins() { + let cachedEmptyTopNode: ProsemirrorNode; return [ new Plugin({ props: { @@ -44,14 +45,18 @@ export const Placeholder = Extension.create({ return } + cachedEmptyTopNode = cachedEmptyTopNode || state.doc.type.createAndFill() + let isEditorEmpty = cachedEmptyTopNode.content.findDiffStart(state.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) } @@ -76,6 +81,14 @@ export const Placeholder = Extension.create({ return DecorationSet.create(doc, decorations) }, }, + + attributes(state) { + cachedEmptyTopNode = cachedEmptyTopNode || state.doc.type.createAndFill() + let isEditorEmpty = cachedEmptyTopNode.content.findDiffStart(state.doc.content) === null + if (isEditorEmpty) { + return {class: this.options.emptyEditorClass} + } + } }), ] }, From 1d71877ffd9cd5b6d4ace6b9d97e1d8b84776132 Mon Sep 17 00:00:00 2001 From: BrianHung Date: Wed, 30 Mar 2022 02:02:11 -0700 Subject: [PATCH 2/3] fix-decorations-and-attribute-editor-props --- .../extension-placeholder/src/placeholder.ts | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/extension-placeholder/src/placeholder.ts b/packages/extension-placeholder/src/placeholder.ts index d05e4145..85c22168 100644 --- a/packages/extension-placeholder/src/placeholder.ts +++ b/packages/extension-placeholder/src/placeholder.ts @@ -45,8 +45,8 @@ export const Placeholder = Extension.create({ return } - cachedEmptyTopNode = cachedEmptyTopNode || state.doc.type.createAndFill() - let isEditorEmpty = cachedEmptyTopNode.content.findDiffStart(state.doc.content) === null + cachedEmptyTopNode = cachedEmptyTopNode || doc.type.createAndFill() + let isEditorEmpty = cachedEmptyTopNode.sameMarkup(doc) && cachedEmptyTopNode.content.findDiffStart(doc.content) === null doc.descendants((node, pos) => { const hasAnchor = anchor >= pos && anchor <= (pos + node.nodeSize) @@ -80,16 +80,28 @@ export const Placeholder = Extension.create({ return DecorationSet.create(doc, decorations) }, - }, - attributes(state) { - cachedEmptyTopNode = cachedEmptyTopNode || state.doc.type.createAndFill() - let isEditorEmpty = cachedEmptyTopNode.content.findDiffStart(state.doc.content) === null - if (isEditorEmpty) { - return {class: this.options.emptyEditorClass} + attributes: ({ doc }) => { + cachedEmptyTopNode = cachedEmptyTopNode || doc.type.createAndFill() + let isEditorEmpty = cachedEmptyTopNode.sameMarkup(doc) && cachedEmptyTopNode.content.findDiffStart(doc.content) === null + if (isEditorEmpty) { + return { + class: this.options.emptyEditorClass, + 'data-placeholder': typeof this.options.placeholder === 'function' + ? this.options.placeholder({ + editor: this.editor, + node: doc, + pos: 0, + hasAnchor: true, + }) + : this.options.placeholder, + } + } } - } + }, }), ] }, }) + +export default Placeholder \ No newline at end of file From 6a85f7365984b54550fbc66f02b787660935e4ed Mon Sep 17 00:00:00 2001 From: Dominik Biedebach Date: Sat, 10 Sep 2022 15:31:54 +0200 Subject: [PATCH 3/3] style(extension/placeholder): fix linting issues --- packages/extension-placeholder/src/placeholder.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/extension-placeholder/src/placeholder.ts b/packages/extension-placeholder/src/placeholder.ts index 67185408..df52cf1a 100644 --- a/packages/extension-placeholder/src/placeholder.ts +++ b/packages/extension-placeholder/src/placeholder.ts @@ -32,7 +32,8 @@ export const Placeholder = Extension.create({ }, addProseMirrorPlugins() { - let cachedEmptyTopNode: ProsemirrorNode; + let cachedEmptyTopNode: ProsemirrorNode + return [ new Plugin({ props: { @@ -46,12 +47,11 @@ export const Placeholder = Extension.create({ } cachedEmptyTopNode = cachedEmptyTopNode || doc.type.createAndFill() - let isEditorEmpty = cachedEmptyTopNode.sameMarkup(doc) && cachedEmptyTopNode.content.findDiffStart(doc.content) === null + const isEditorEmpty = cachedEmptyTopNode.sameMarkup(doc) && cachedEmptyTopNode.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] @@ -83,7 +83,8 @@ export const Placeholder = Extension.create({ attributes: ({ doc }) => { cachedEmptyTopNode = cachedEmptyTopNode || doc.type.createAndFill() - let isEditorEmpty = cachedEmptyTopNode.sameMarkup(doc) && cachedEmptyTopNode.content.findDiffStart(doc.content) === null + const isEditorEmpty = cachedEmptyTopNode.sameMarkup(doc) && cachedEmptyTopNode.content.findDiffStart(doc.content) === null + if (isEditorEmpty) { return { class: this.options.emptyEditorClass, @@ -95,13 +96,13 @@ export const Placeholder = Extension.create({ hasAnchor: true, }) : this.options.placeholder, - } + } } - } + }, }, }), ] }, }) -export default Placeholder \ No newline at end of file +export default Placeholder