@tiptap/extension-placeholder 2.1.11 → 2.2.0-rc.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -14,6 +14,7 @@ const Placeholder = core.Extension.create({
14
14
  emptyNodeClass: 'is-empty',
15
15
  placeholder: 'Write something …',
16
16
  showOnlyWhenEditable: true,
17
+ considerAnyAsEmpty: false,
17
18
  showOnlyCurrent: true,
18
19
  includeChildren: false,
19
20
  };
@@ -24,6 +25,7 @@ const Placeholder = core.Extension.create({
24
25
  key: new state.PluginKey('placeholder'),
25
26
  props: {
26
27
  decorations: ({ doc, selection }) => {
28
+ var _a;
27
29
  const active = this.editor.isEditable || !this.options.showOnlyWhenEditable;
28
30
  const { anchor } = selection;
29
31
  const decorations = [];
@@ -31,15 +33,22 @@ const Placeholder = core.Extension.create({
31
33
  return null;
32
34
  }
33
35
  // only calculate isEmpty once due to its performance impacts (see issue #3360)
34
- const emptyDocInstance = doc.type.createAndFill();
35
- const isEditorEmpty = (emptyDocInstance === null || emptyDocInstance === void 0 ? void 0 : emptyDocInstance.sameMarkup(doc))
36
- && emptyDocInstance.content.findDiffStart(doc.content) === null;
36
+ const { firstChild } = doc.content;
37
+ const isLeaf = firstChild && firstChild.type.isLeaf;
38
+ const isAtom = firstChild && firstChild.isAtom;
39
+ const isValidNode = this.options.considerAnyAsEmpty
40
+ ? true
41
+ : firstChild && firstChild.type.name === ((_a = doc.type.contentMatch.defaultType) === null || _a === void 0 ? void 0 : _a.name);
42
+ const isEmptyDoc = doc.content.childCount <= 1
43
+ && firstChild
44
+ && isValidNode
45
+ && (firstChild.nodeSize <= 2 && (!isLeaf || !isAtom));
37
46
  doc.descendants((node, pos) => {
38
47
  const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize;
39
48
  const isEmpty = !node.isLeaf && !node.childCount;
40
49
  if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
41
50
  const classes = [this.options.emptyNodeClass];
42
- if (isEditorEmpty) {
51
+ if (isEmptyDoc) {
43
52
  classes.push(this.options.emptyEditorClass);
44
53
  }
45
54
  const decoration = view.Decoration.node(pos, pos + node.nodeSize, {
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/placeholder.ts"],"sourcesContent":["import { Editor, Extension } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nexport interface PlaceholderOptions {\n emptyEditorClass: string\n emptyNodeClass: string\n placeholder:\n | ((PlaceholderProps: {\n editor: Editor\n node: ProsemirrorNode\n pos: number\n hasAnchor: boolean\n }) => string)\n | string\n showOnlyWhenEditable: boolean\n showOnlyCurrent: boolean\n includeChildren: boolean\n}\n\nexport const Placeholder = Extension.create<PlaceholderOptions>({\n name: 'placeholder',\n\n addOptions() {\n return {\n emptyEditorClass: 'is-editor-empty',\n emptyNodeClass: 'is-empty',\n placeholder: 'Write something …',\n showOnlyWhenEditable: true,\n showOnlyCurrent: true,\n includeChildren: false,\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('placeholder'),\n props: {\n decorations: ({ doc, selection }) => {\n const active = this.editor.isEditable || !this.options.showOnlyWhenEditable\n const { anchor } = selection\n const decorations: Decoration[] = []\n\n if (!active) {\n return null\n }\n\n // only calculate isEmpty once due to its performance impacts (see issue #3360)\n const emptyDocInstance = doc.type.createAndFill()\n const isEditorEmpty = emptyDocInstance?.sameMarkup(doc)\n && emptyDocInstance.content.findDiffStart(doc.content) === null\n\n doc.descendants((node, pos) => {\n const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize\n const isEmpty = !node.isLeaf && !node.childCount\n\n if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {\n const classes = [this.options.emptyNodeClass]\n\n if (isEditorEmpty) {\n classes.push(this.options.emptyEditorClass)\n }\n\n const decoration = Decoration.node(pos, pos + node.nodeSize, {\n class: classes.join(' '),\n 'data-placeholder':\n typeof this.options.placeholder === 'function'\n ? this.options.placeholder({\n editor: this.editor,\n node,\n pos,\n hasAnchor,\n })\n : this.options.placeholder,\n })\n\n decorations.push(decoration)\n }\n\n return this.options.includeChildren\n })\n\n return DecorationSet.create(doc, decorations)\n },\n },\n }),\n ]\n },\n})\n"],"names":["Extension","Plugin","PluginKey","Decoration","DecorationSet"],"mappings":";;;;;;;;AAqBa,MAAA,WAAW,GAAGA,cAAS,CAAC,MAAM,CAAqB;AAC9D,IAAA,IAAI,EAAE,aAAa;IAEnB,UAAU,GAAA;QACR,OAAO;AACL,YAAA,gBAAgB,EAAE,iBAAiB;AACnC,YAAA,cAAc,EAAE,UAAU;AAC1B,YAAA,WAAW,EAAE,mBAAmB;AAChC,YAAA,oBAAoB,EAAE,IAAI;AAC1B,YAAA,eAAe,EAAE,IAAI;AACrB,YAAA,eAAe,EAAE,KAAK;SACvB,CAAA;KACF;IAED,qBAAqB,GAAA;QACnB,OAAO;AACL,YAAA,IAAIC,YAAM,CAAC;AACT,gBAAA,GAAG,EAAE,IAAIC,eAAS,CAAC,aAAa,CAAC;AACjC,gBAAA,KAAK,EAAE;oBACL,WAAW,EAAE,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,KAAI;AAClC,wBAAA,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAA;AAC3E,wBAAA,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAA;wBAC5B,MAAM,WAAW,GAAiB,EAAE,CAAA;wBAEpC,IAAI,CAAC,MAAM,EAAE;AACX,4BAAA,OAAO,IAAI,CAAA;AACZ,yBAAA;;wBAGD,MAAM,gBAAgB,GAAG,GAAG,CAAC,IAAI,CAAC,aAAa,EAAE,CAAA;AACjD,wBAAA,MAAM,aAAa,GAAG,CAAA,gBAAgB,KAAhB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A;AAC7B,6BAAA;AAED,4BAAA,OAAO,IAAI,CAAC,OAAO,CAAC,eAAe,CAAA;AACrC,yBAAC,CAAC,CAAA;wBAEF,OAAOC,kBAAa,CAAC,MAAM,CAAC,GAAG,EAAE,WAAW,CAAC,CAAA;qBAC9C;AACF,iBAAA;aACF,CAAC;SACH,CAAA;KACF;AACF,CAAA;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/placeholder.ts"],"sourcesContent":["import { Editor, Extension } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nexport interface PlaceholderOptions {\n /**\n * **The class name for the empty editor**\n * @default 'is-editor-empty'\n */\n emptyEditorClass: string\n\n /**\n * **The class name for empty nodes**\n * @default 'is-empty'\n */\n emptyNodeClass: string\n\n /**\n * **The placeholder content**\n *\n * You can use a function to return a dynamic placeholder or a string.\n * @default 'Write something …'\n */\n placeholder:\n | ((PlaceholderProps: {\n editor: Editor\n node: ProsemirrorNode\n pos: number\n hasAnchor: boolean\n }) => string)\n | string\n\n /**\n * **Used for empty check on the document.**\n *\n * If true, any node that is not a leaf or atom will be considered for empty check.\n * If false, only default nodes (paragraphs) will be considered for empty check.\n * @default false\n */\n considerAnyAsEmpty: boolean\n\n /**\n * **Checks if the placeholder should be only shown when the editor is editable.**\n *\n * If true, the placeholder will only be shown when the editor is editable.\n * If false, the placeholder will always be shown.\n * @default true\n */\n showOnlyWhenEditable: boolean\n\n /**\n * **Checks if the placeholder should be only shown when the current node is empty.**\n *\n * If true, the placeholder will only be shown when the current node is empty.\n * If false, the placeholder will be shown when any node is empty.\n * @default true\n */\n showOnlyCurrent: boolean\n\n /**\n * **Controls if the placeholder should be shown for all descendents.**\n *\n * If true, the placeholder will be shown for all descendents.\n * If false, the placeholder will only be shown for the current node.\n * @default false\n */\n includeChildren: boolean\n}\n\nexport const Placeholder = Extension.create<PlaceholderOptions>({\n name: 'placeholder',\n\n addOptions() {\n return {\n emptyEditorClass: 'is-editor-empty',\n emptyNodeClass: 'is-empty',\n placeholder: 'Write something …',\n showOnlyWhenEditable: true,\n considerAnyAsEmpty: false,\n showOnlyCurrent: true,\n includeChildren: false,\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('placeholder'),\n props: {\n decorations: ({ doc, selection }) => {\n const active = this.editor.isEditable || !this.options.showOnlyWhenEditable\n const { anchor } = selection\n const decorations: Decoration[] = []\n\n if (!active) {\n return null\n }\n\n // only calculate isEmpty once due to its performance impacts (see issue #3360)\n const { firstChild } = doc.content\n const isLeaf = firstChild && firstChild.type.isLeaf\n const isAtom = firstChild && firstChild.isAtom\n const isValidNode = this.options.considerAnyAsEmpty\n ? true\n : firstChild && firstChild.type.name === doc.type.contentMatch.defaultType?.name\n const isEmptyDoc = doc.content.childCount <= 1\n && firstChild\n && isValidNode\n && (firstChild.nodeSize <= 2 && (!isLeaf || !isAtom))\n\n doc.descendants((node, pos) => {\n const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize\n const isEmpty = !node.isLeaf && !node.childCount\n\n if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {\n const classes = [this.options.emptyNodeClass]\n\n if (isEmptyDoc) {\n classes.push(this.options.emptyEditorClass)\n }\n\n const decoration = Decoration.node(pos, pos + node.nodeSize, {\n class: classes.join(' '),\n 'data-placeholder':\n typeof this.options.placeholder === 'function'\n ? this.options.placeholder({\n editor: this.editor,\n node,\n pos,\n hasAnchor,\n })\n : this.options.placeholder,\n })\n\n decorations.push(decoration)\n }\n\n return this.options.includeChildren\n })\n\n return DecorationSet.create(doc, decorations)\n },\n },\n }),\n ]\n },\n})\n"],"names":["Extension","Plugin","PluginKey","Decoration","DecorationSet"],"mappings":";;;;;;;;AAsEa,MAAA,WAAW,GAAGA,cAAS,CAAC,MAAM,CAAqB;AAC9D,IAAA,IAAI,EAAE,aAAa;IAEnB,UA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AC,UAAU,CAAC,CAAA;AAC7B,6BAAA;AAED,4BAAA,OAAO,IAAI,CAAC,OAAO,CAAC,eAAe,CAAA;AACrC,yBAAC,CAAC,CAAA;wBAEF,OAAOC,kBAAa,CAAC,MAAM,CAAC,GAAG,EAAE,WAAW,CAAC,CAAA;qBAC9C;AACF,iBAAA;aACF,CAAC;SACH,CAAA;KACF;AACF,CAAA;;;;;"}
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ const Placeholder = Extension.create({
10
10
  emptyNodeClass: 'is-empty',
11
11
  placeholder: 'Write something …',
12
12
  showOnlyWhenEditable: true,
13
+ considerAnyAsEmpty: false,
13
14
  showOnlyCurrent: true,
14
15
  includeChildren: false,
15
16
  };
@@ -20,6 +21,7 @@ const Placeholder = Extension.create({
20
21
  key: new PluginKey('placeholder'),
21
22
  props: {
22
23
  decorations: ({ doc, selection }) => {
24
+ var _a;
23
25
  const active = this.editor.isEditable || !this.options.showOnlyWhenEditable;
24
26
  const { anchor } = selection;
25
27
  const decorations = [];
@@ -27,15 +29,22 @@ const Placeholder = Extension.create({
27
29
  return null;
28
30
  }
29
31
  // only calculate isEmpty once due to its performance impacts (see issue #3360)
30
- const emptyDocInstance = doc.type.createAndFill();
31
- const isEditorEmpty = (emptyDocInstance === null || emptyDocInstance === void 0 ? void 0 : emptyDocInstance.sameMarkup(doc))
32
- && emptyDocInstance.content.findDiffStart(doc.content) === null;
32
+ const { firstChild } = doc.content;
33
+ const isLeaf = firstChild && firstChild.type.isLeaf;
34
+ const isAtom = firstChild && firstChild.isAtom;
35
+ const isValidNode = this.options.considerAnyAsEmpty
36
+ ? true
37
+ : firstChild && firstChild.type.name === ((_a = doc.type.contentMatch.defaultType) === null || _a === void 0 ? void 0 : _a.name);
38
+ const isEmptyDoc = doc.content.childCount <= 1
39
+ && firstChild
40
+ && isValidNode
41
+ && (firstChild.nodeSize <= 2 && (!isLeaf || !isAtom));
33
42
  doc.descendants((node, pos) => {
34
43
  const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize;
35
44
  const isEmpty = !node.isLeaf && !node.childCount;
36
45
  if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
37
46
  const classes = [this.options.emptyNodeClass];
38
- if (isEditorEmpty) {
47
+ if (isEmptyDoc) {
39
48
  classes.push(this.options.emptyEditorClass);
40
49
  }
41
50
  const decoration = Decoration.node(pos, pos + node.nodeSize, {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/placeholder.ts"],"sourcesContent":["import { Editor, Extension } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nexport interface PlaceholderOptions {\n emptyEditorClass: string\n emptyNodeClass: string\n placeholder:\n | ((PlaceholderProps: {\n editor: Editor\n node: ProsemirrorNode\n pos: number\n hasAnchor: boolean\n }) => string)\n | string\n showOnlyWhenEditable: boolean\n showOnlyCurrent: boolean\n includeChildren: boolean\n}\n\nexport const Placeholder = Extension.create<PlaceholderOptions>({\n name: 'placeholder',\n\n addOptions() {\n return {\n emptyEditorClass: 'is-editor-empty',\n emptyNodeClass: 'is-empty',\n placeholder: 'Write something …',\n showOnlyWhenEditable: true,\n showOnlyCurrent: true,\n includeChildren: false,\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('placeholder'),\n props: {\n decorations: ({ doc, selection }) => {\n const active = this.editor.isEditable || !this.options.showOnlyWhenEditable\n const { anchor } = selection\n const decorations: Decoration[] = []\n\n if (!active) {\n return null\n }\n\n // only calculate isEmpty once due to its performance impacts (see issue #3360)\n const emptyDocInstance = doc.type.createAndFill()\n const isEditorEmpty = emptyDocInstance?.sameMarkup(doc)\n && emptyDocInstance.content.findDiffStart(doc.content) === null\n\n doc.descendants((node, pos) => {\n const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize\n const isEmpty = !node.isLeaf && !node.childCount\n\n if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {\n const classes = [this.options.emptyNodeClass]\n\n if (isEditorEmpty) {\n classes.push(this.options.emptyEditorClass)\n }\n\n const decoration = Decoration.node(pos, pos + node.nodeSize, {\n class: classes.join(' '),\n 'data-placeholder':\n typeof this.options.placeholder === 'function'\n ? this.options.placeholder({\n editor: this.editor,\n node,\n pos,\n hasAnchor,\n })\n : this.options.placeholder,\n })\n\n decorations.push(decoration)\n }\n\n return this.options.includeChildren\n })\n\n return DecorationSet.create(doc, decorations)\n },\n },\n }),\n ]\n },\n})\n"],"names":[],"mappings":";;;;AAqBa,MAAA,WAAW,GAAG,SAAS,CAAC,MAAM,CAAqB;AAC9D,IAAA,IAAI,EAAE,aAAa;IAEnB,UAAU,GAAA;QACR,OAAO;AACL,YAAA,gBAAgB,EAAE,iBAAiB;AACnC,YAAA,cAAc,EAAE,UAAU;AAC1B,YAAA,WAAW,EAAE,mBAAmB;AAChC,YAAA,oBAAoB,EAAE,IAAI;AAC1B,YAAA,eAAe,EAAE,IAAI;AACrB,YAAA,eAAe,EAAE,KAAK;SACvB,CAAA;KACF;IAED,qBAAqB,GAAA;QACnB,OAAO;AACL,YAAA,IAAI,MAAM,CAAC;AACT,gBAAA,GAAG,EAAE,IAAI,SAAS,CAAC,aAAa,CAAC;AACjC,gBAAA,KAAK,EAAE;oBACL,WAAW,EAAE,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,KAAI;AAClC,wBAAA,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAA;AAC3E,wBAAA,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAA;wBAC5B,MAAM,WAAW,GAAiB,EAAE,CAAA;wBAEpC,IAAI,CAAC,MAAM,EAAE;AACX,4BAAA,OAAO,IAAI,CAAA;AACZ,yBAAA;;wBAGD,MAAM,gBAAgB,GAAG,GAA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AC,WAAW;AAC/B,iCAAA,CAAC,CAAA;AAEF,gCAAA,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;AAC7B,6BAAA;AAED,4BAAA,OAAO,IAAI,CAAC,OAAO,CAAC,eAAe,CAAA;AACrC,yBAAC,CAAC,CAAA;wBAEF,OAAO,aAAa,CAAC,MAAM,CAAC,GAAG,EAAE,WAAW,CAAC,CAAA;qBAC9C;AACF,iBAAA;aACF,CAAC;SACH,CAAA;KACF;AACF,CAAA;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/placeholder.ts"],"sourcesContent":["import { Editor, Extension } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nexport interface PlaceholderOptions {\n /**\n * **The class name for the empty editor**\n * @default 'is-editor-empty'\n */\n emptyEditorClass: string\n\n /**\n * **The class name for empty nodes**\n * @default 'is-empty'\n */\n emptyNodeClass: string\n\n /**\n * **The placeholder content**\n *\n * You can use a function to return a dynamic placeholder or a string.\n * @default 'Write something …'\n */\n placeholder:\n | ((PlaceholderProps: {\n editor: Editor\n node: ProsemirrorNode\n pos: number\n hasAnchor: boolean\n }) => string)\n | string\n\n /**\n * **Used for empty check on the document.**\n *\n * If true, any node that is not a leaf or atom will be considered for empty check.\n * If false, only default nodes (paragraphs) will be considered for empty check.\n * @default false\n */\n considerAnyAsEmpty: boolean\n\n /**\n * **Checks if the placeholder should be only shown when the editor is editable.**\n *\n * If true, the placeholder will only be shown when the editor is editable.\n * If false, the placeholder will always be shown.\n * @default true\n */\n showOnlyWhenEditable: boolean\n\n /**\n * **Checks if the placeholder should be only shown when the current node is empty.**\n *\n * If true, the placeholder will only be shown when the current node is empty.\n * If false, the placeholder will be shown when any node is empty.\n * @default true\n */\n showOnlyCurrent: boolean\n\n /**\n * **Controls if the placeholder should be shown for all descendents.**\n *\n * If true, the placeholder will be shown for all descendents.\n * If false, the placeholder will only be shown for the current node.\n * @default false\n */\n includeChildren: boolean\n}\n\nexport const Placeholder = Extension.create<PlaceholderOptions>({\n name: 'placeholder',\n\n addOptions() {\n return {\n emptyEditorClass: 'is-editor-empty',\n emptyNodeClass: 'is-empty',\n placeholder: 'Write something …',\n showOnlyWhenEditable: true,\n considerAnyAsEmpty: false,\n showOnlyCurrent: true,\n includeChildren: false,\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('placeholder'),\n props: {\n decorations: ({ doc, selection }) => {\n const active = this.editor.isEditable || !this.options.showOnlyWhenEditable\n const { anchor } = selection\n const decorations: Decoration[] = []\n\n if (!active) {\n return null\n }\n\n // only calculate isEmpty once due to its performance impacts (see issue #3360)\n const { firstChild } = doc.content\n const isLeaf = firstChild && firstChild.type.isLeaf\n const isAtom = firstChild && firstChild.isAtom\n const isValidNode = this.options.considerAnyAsEmpty\n ? true\n : firstChild && firstChild.type.name === doc.type.contentMatch.defaultType?.name\n const isEmptyDoc = doc.content.childCount <= 1\n && firstChild\n && isValidNode\n && (firstChild.nodeSize <= 2 && (!isLeaf || !isAtom))\n\n doc.descendants((node, pos) => {\n const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize\n const isEmpty = !node.isLeaf && !node.childCount\n\n if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {\n const classes = [this.options.emptyNodeClass]\n\n if (isEmptyDoc) {\n classes.push(this.options.emptyEditorClass)\n }\n\n const decoration = Decoration.node(pos, pos + node.nodeSize, {\n class: classes.join(' '),\n 'data-placeholder':\n typeof this.options.placeholder === 'function'\n ? this.options.placeholder({\n editor: this.editor,\n node,\n pos,\n hasAnchor,\n })\n : this.options.placeholder,\n })\n\n decorations.push(decoration)\n }\n\n return this.options.includeChildren\n })\n\n return DecorationSet.create(doc, decorations)\n },\n },\n }),\n ]\n },\n})\n"],"names":[],"mappings":";;;;AAsEa,MAAA,WAAW,GAAG,SAAS,CAAC,MAAM,CAA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,CAAA;AAEF,gCAAA,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;AAC7B,6BAAA;AAED,4BAAA,OAAO,IAAI,CAAC,OAAO,CAAC,eAAe,CAAA;AACrC,yBAAC,CAAC,CAAA;wBAEF,OAAO,aAAa,CAAC,MAAM,CAAC,GAAG,EAAE,WAAW,CAAC,CAAA;qBAC9C;AACF,iBAAA;aACF,CAAC;SACH,CAAA;KACF;AACF,CAAA;;;;"}
package/dist/index.umd.js CHANGED
@@ -12,6 +12,7 @@
12
12
  emptyNodeClass: 'is-empty',
13
13
  placeholder: 'Write something …',
14
14
  showOnlyWhenEditable: true,
15
+ considerAnyAsEmpty: false,
15
16
  showOnlyCurrent: true,
16
17
  includeChildren: false,
17
18
  };
@@ -22,6 +23,7 @@
22
23
  key: new state.PluginKey('placeholder'),
23
24
  props: {
24
25
  decorations: ({ doc, selection }) => {
26
+ var _a;
25
27
  const active = this.editor.isEditable || !this.options.showOnlyWhenEditable;
26
28
  const { anchor } = selection;
27
29
  const decorations = [];
@@ -29,15 +31,22 @@
29
31
  return null;
30
32
  }
31
33
  // only calculate isEmpty once due to its performance impacts (see issue #3360)
32
- const emptyDocInstance = doc.type.createAndFill();
33
- const isEditorEmpty = (emptyDocInstance === null || emptyDocInstance === void 0 ? void 0 : emptyDocInstance.sameMarkup(doc))
34
- && emptyDocInstance.content.findDiffStart(doc.content) === null;
34
+ const { firstChild } = doc.content;
35
+ const isLeaf = firstChild && firstChild.type.isLeaf;
36
+ const isAtom = firstChild && firstChild.isAtom;
37
+ const isValidNode = this.options.considerAnyAsEmpty
38
+ ? true
39
+ : firstChild && firstChild.type.name === ((_a = doc.type.contentMatch.defaultType) === null || _a === void 0 ? void 0 : _a.name);
40
+ const isEmptyDoc = doc.content.childCount <= 1
41
+ && firstChild
42
+ && isValidNode
43
+ && (firstChild.nodeSize <= 2 && (!isLeaf || !isAtom));
35
44
  doc.descendants((node, pos) => {
36
45
  const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize;
37
46
  const isEmpty = !node.isLeaf && !node.childCount;
38
47
  if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
39
48
  const classes = [this.options.emptyNodeClass];
40
- if (isEditorEmpty) {
49
+ if (isEmptyDoc) {
41
50
  classes.push(this.options.emptyEditorClass);
42
51
  }
43
52
  const decoration = view.Decoration.node(pos, pos + node.nodeSize, {
@@ -1 +1 @@
1
- {"version":3,"file":"index.umd.js","sources":["../src/placeholder.ts"],"sourcesContent":["import { Editor, Extension } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nexport interface PlaceholderOptions {\n emptyEditorClass: string\n emptyNodeClass: string\n placeholder:\n | ((PlaceholderProps: {\n editor: Editor\n node: ProsemirrorNode\n pos: number\n hasAnchor: boolean\n }) => string)\n | string\n showOnlyWhenEditable: boolean\n showOnlyCurrent: boolean\n includeChildren: boolean\n}\n\nexport const Placeholder = Extension.create<PlaceholderOptions>({\n name: 'placeholder',\n\n addOptions() {\n return {\n emptyEditorClass: 'is-editor-empty',\n emptyNodeClass: 'is-empty',\n placeholder: 'Write something …',\n showOnlyWhenEditable: true,\n showOnlyCurrent: true,\n includeChildren: false,\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('placeholder'),\n props: {\n decorations: ({ doc, selection }) => {\n const active = this.editor.isEditable || !this.options.showOnlyWhenEditable\n const { anchor } = selection\n const decorations: Decoration[] = []\n\n if (!active) {\n return null\n }\n\n // only calculate isEmpty once due to its performance impacts (see issue #3360)\n const emptyDocInstance = doc.type.createAndFill()\n const isEditorEmpty = emptyDocInstance?.sameMarkup(doc)\n && emptyDocInstance.content.findDiffStart(doc.content) === null\n\n doc.descendants((node, pos) => {\n const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize\n const isEmpty = !node.isLeaf && !node.childCount\n\n if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {\n const classes = [this.options.emptyNodeClass]\n\n if (isEditorEmpty) {\n classes.push(this.options.emptyEditorClass)\n }\n\n const decoration = Decoration.node(pos, pos + node.nodeSize, {\n class: classes.join(' '),\n 'data-placeholder':\n typeof this.options.placeholder === 'function'\n ? this.options.placeholder({\n editor: this.editor,\n node,\n pos,\n hasAnchor,\n })\n : this.options.placeholder,\n })\n\n decorations.push(decoration)\n }\n\n return this.options.includeChildren\n })\n\n return DecorationSet.create(doc, decorations)\n },\n },\n }),\n ]\n },\n})\n"],"names":["Extension","Plugin","PluginKey","Decoration","DecorationSet"],"mappings":";;;;;;AAqBa,QAAA,WAAW,GAAGA,cAAS,CAAC,MAAM,CAAqB;EAC9D,IAAA,IAAI,EAAE,aAAa;MAEnB,UAAU,GAAA;UACR,OAAO;EACL,YAAA,gBAAgB,EAAE,iBAAiB;EACnC,YAAA,cAAc,EAAE,UAAU;EAC1B,YAAA,WAAW,EAAE,mBAAmB;EAChC,YAAA,oBAAoB,EAAE,IAAI;EAC1B,YAAA,eAAe,EAAE,IAAI;EACrB,YAAA,eAAe,EAAE,KAAK;WACvB,CAAA;OACF;MAED,qBAAqB,GAAA;UACnB,OAAO;EACL,YAAA,IAAIC,YAAM,CAAC;EACT,gBAAA,GAAG,EAAE,IAAIC,eAAS,CAAC,aAAa,CAAC;EACjC,gBAAA,KAAK,EAAE;sBACL,WAAW,EAAE,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,KAAI;EAClC,wBAAA,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAA;EAC3E,wBAAA,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAA;0BAC5B,MAAM,WAAW,GAAiB,EAAE,CAAA;0BAEpC,IAAI,CAAC,MAAM,EAAE;EACX,4BAAA,OAAO,IAAI,CAAA;EACZ,yBAAA;;0BAGD,MAAM,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AAI,CAAC,OAAO,CAAC,WAAW;EAC/B,iCAAA,CAAC,CAAA;EAEF,gCAAA,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;EAC7B,6BAAA;EAED,4BAAA,OAAO,IAAI,CAAC,OAAO,CAAC,eAAe,CAAA;EACrC,yBAAC,CAAC,CAAA;0BAEF,OAAOC,kBAAa,CAAC,MAAM,CAAC,GAAG,EAAE,WAAW,CAAC,CAAA;uBAC9C;EACF,iBAAA;eACF,CAAC;WACH,CAAA;OACF;EACF,CAAA;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.umd.js","sources":["../src/placeholder.ts"],"sourcesContent":["import { Editor, Extension } from '@tiptap/core'\nimport { Node as ProsemirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nexport interface PlaceholderOptions {\n /**\n * **The class name for the empty editor**\n * @default 'is-editor-empty'\n */\n emptyEditorClass: string\n\n /**\n * **The class name for empty nodes**\n * @default 'is-empty'\n */\n emptyNodeClass: string\n\n /**\n * **The placeholder content**\n *\n * You can use a function to return a dynamic placeholder or a string.\n * @default 'Write something …'\n */\n placeholder:\n | ((PlaceholderProps: {\n editor: Editor\n node: ProsemirrorNode\n pos: number\n hasAnchor: boolean\n }) => string)\n | string\n\n /**\n * **Used for empty check on the document.**\n *\n * If true, any node that is not a leaf or atom will be considered for empty check.\n * If false, only default nodes (paragraphs) will be considered for empty check.\n * @default false\n */\n considerAnyAsEmpty: boolean\n\n /**\n * **Checks if the placeholder should be only shown when the editor is editable.**\n *\n * If true, the placeholder will only be shown when the editor is editable.\n * If false, the placeholder will always be shown.\n * @default true\n */\n showOnlyWhenEditable: boolean\n\n /**\n * **Checks if the placeholder should be only shown when the current node is empty.**\n *\n * If true, the placeholder will only be shown when the current node is empty.\n * If false, the placeholder will be shown when any node is empty.\n * @default true\n */\n showOnlyCurrent: boolean\n\n /**\n * **Controls if the placeholder should be shown for all descendents.**\n *\n * If true, the placeholder will be shown for all descendents.\n * If false, the placeholder will only be shown for the current node.\n * @default false\n */\n includeChildren: boolean\n}\n\nexport const Placeholder = Extension.create<PlaceholderOptions>({\n name: 'placeholder',\n\n addOptions() {\n return {\n emptyEditorClass: 'is-editor-empty',\n emptyNodeClass: 'is-empty',\n placeholder: 'Write something …',\n showOnlyWhenEditable: true,\n considerAnyAsEmpty: false,\n showOnlyCurrent: true,\n includeChildren: false,\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('placeholder'),\n props: {\n decorations: ({ doc, selection }) => {\n const active = this.editor.isEditable || !this.options.showOnlyWhenEditable\n const { anchor } = selection\n const decorations: Decoration[] = []\n\n if (!active) {\n return null\n }\n\n // only calculate isEmpty once due to its performance impacts (see issue #3360)\n const { firstChild } = doc.content\n const isLeaf = firstChild && firstChild.type.isLeaf\n const isAtom = firstChild && firstChild.isAtom\n const isValidNode = this.options.considerAnyAsEmpty\n ? true\n : firstChild && firstChild.type.name === doc.type.contentMatch.defaultType?.name\n const isEmptyDoc = doc.content.childCount <= 1\n && firstChild\n && isValidNode\n && (firstChild.nodeSize <= 2 && (!isLeaf || !isAtom))\n\n doc.descendants((node, pos) => {\n const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize\n const isEmpty = !node.isLeaf && !node.childCount\n\n if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {\n const classes = [this.options.emptyNodeClass]\n\n if (isEmptyDoc) {\n classes.push(this.options.emptyEditorClass)\n }\n\n const decoration = Decoration.node(pos, pos + node.nodeSize, {\n class: classes.join(' '),\n 'data-placeholder':\n typeof this.options.placeholder === 'function'\n ? this.options.placeholder({\n editor: this.editor,\n node,\n pos,\n hasAnchor,\n })\n : this.options.placeholder,\n })\n\n decorations.push(decoration)\n }\n\n return this.options.includeChildren\n })\n\n return DecorationSet.create(doc, decorations)\n },\n },\n }),\n ]\n },\n})\n"],"names":["Extension","Plugin","PluginKey","Decoration","D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2CACV,CAAC;EACF,0CAAE,IAAI,CAAC,OAAO,CAAC,WAAW;EAC/B,iCAAA,CAAC,CAAA;EAEF,gCAAA,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;EAC7B,6BAAA;EAED,4BAAA,OAAO,IAAI,CAAC,OAAO,CAAC,eAAe,CAAA;EACrC,yBAAC,CAAC,CAAA;0BAEF,OAAOC,kBAAa,CAAC,MAAM,CAAC,GAAG,EAAE,WAAW,CAAC,CAAA;uBAC9C;EACF,iBAAA;eACF,CAAC;WACH,CAAA;OACF;EACF,CAAA;;;;;;;;;;;"}
@@ -1,16 +1,59 @@
1
1
  import { Editor, Extension } from '@tiptap/core';
2
2
  import { Node as ProsemirrorNode } from '@tiptap/pm/model';
3
3
  export interface PlaceholderOptions {
4
+ /**
5
+ * **The class name for the empty editor**
6
+ * @default 'is-editor-empty'
7
+ */
4
8
  emptyEditorClass: string;
9
+ /**
10
+ * **The class name for empty nodes**
11
+ * @default 'is-empty'
12
+ */
5
13
  emptyNodeClass: string;
14
+ /**
15
+ * **The placeholder content**
16
+ *
17
+ * You can use a function to return a dynamic placeholder or a string.
18
+ * @default 'Write something …'
19
+ */
6
20
  placeholder: ((PlaceholderProps: {
7
21
  editor: Editor;
8
22
  node: ProsemirrorNode;
9
23
  pos: number;
10
24
  hasAnchor: boolean;
11
25
  }) => string) | string;
26
+ /**
27
+ * **Used for empty check on the document.**
28
+ *
29
+ * If true, any node that is not a leaf or atom will be considered for empty check.
30
+ * If false, only default nodes (paragraphs) will be considered for empty check.
31
+ * @default false
32
+ */
33
+ considerAnyAsEmpty: boolean;
34
+ /**
35
+ * **Checks if the placeholder should be only shown when the editor is editable.**
36
+ *
37
+ * If true, the placeholder will only be shown when the editor is editable.
38
+ * If false, the placeholder will always be shown.
39
+ * @default true
40
+ */
12
41
  showOnlyWhenEditable: boolean;
42
+ /**
43
+ * **Checks if the placeholder should be only shown when the current node is empty.**
44
+ *
45
+ * If true, the placeholder will only be shown when the current node is empty.
46
+ * If false, the placeholder will be shown when any node is empty.
47
+ * @default true
48
+ */
13
49
  showOnlyCurrent: boolean;
50
+ /**
51
+ * **Controls if the placeholder should be shown for all descendents.**
52
+ *
53
+ * If true, the placeholder will be shown for all descendents.
54
+ * If false, the placeholder will only be shown for the current node.
55
+ * @default false
56
+ */
14
57
  includeChildren: boolean;
15
58
  }
16
59
  export declare const Placeholder: Extension<PlaceholderOptions, any>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiptap/extension-placeholder",
3
3
  "description": "placeholder extension for tiptap",
4
- "version": "2.1.11",
4
+ "version": "2.2.0-rc.4",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -29,8 +29,8 @@
29
29
  "dist"
30
30
  ],
31
31
  "devDependencies": {
32
- "@tiptap/core": "^2.1.11",
33
- "@tiptap/pm": "^2.1.11"
32
+ "@tiptap/core": "^2.2.0-rc.4",
33
+ "@tiptap/pm": "^2.2.0-rc.4"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "@tiptap/core": "^2.0.0",
@@ -4,8 +4,24 @@ import { Plugin, PluginKey } from '@tiptap/pm/state'
4
4
  import { Decoration, DecorationSet } from '@tiptap/pm/view'
5
5
 
6
6
  export interface PlaceholderOptions {
7
+ /**
8
+ * **The class name for the empty editor**
9
+ * @default 'is-editor-empty'
10
+ */
7
11
  emptyEditorClass: string
12
+
13
+ /**
14
+ * **The class name for empty nodes**
15
+ * @default 'is-empty'
16
+ */
8
17
  emptyNodeClass: string
18
+
19
+ /**
20
+ * **The placeholder content**
21
+ *
22
+ * You can use a function to return a dynamic placeholder or a string.
23
+ * @default 'Write something …'
24
+ */
9
25
  placeholder:
10
26
  | ((PlaceholderProps: {
11
27
  editor: Editor
@@ -14,8 +30,41 @@ export interface PlaceholderOptions {
14
30
  hasAnchor: boolean
15
31
  }) => string)
16
32
  | string
33
+
34
+ /**
35
+ * **Used for empty check on the document.**
36
+ *
37
+ * If true, any node that is not a leaf or atom will be considered for empty check.
38
+ * If false, only default nodes (paragraphs) will be considered for empty check.
39
+ * @default false
40
+ */
41
+ considerAnyAsEmpty: boolean
42
+
43
+ /**
44
+ * **Checks if the placeholder should be only shown when the editor is editable.**
45
+ *
46
+ * If true, the placeholder will only be shown when the editor is editable.
47
+ * If false, the placeholder will always be shown.
48
+ * @default true
49
+ */
17
50
  showOnlyWhenEditable: boolean
51
+
52
+ /**
53
+ * **Checks if the placeholder should be only shown when the current node is empty.**
54
+ *
55
+ * If true, the placeholder will only be shown when the current node is empty.
56
+ * If false, the placeholder will be shown when any node is empty.
57
+ * @default true
58
+ */
18
59
  showOnlyCurrent: boolean
60
+
61
+ /**
62
+ * **Controls if the placeholder should be shown for all descendents.**
63
+ *
64
+ * If true, the placeholder will be shown for all descendents.
65
+ * If false, the placeholder will only be shown for the current node.
66
+ * @default false
67
+ */
19
68
  includeChildren: boolean
20
69
  }
21
70
 
@@ -28,6 +77,7 @@ export const Placeholder = Extension.create<PlaceholderOptions>({
28
77
  emptyNodeClass: 'is-empty',
29
78
  placeholder: 'Write something …',
30
79
  showOnlyWhenEditable: true,
80
+ considerAnyAsEmpty: false,
31
81
  showOnlyCurrent: true,
32
82
  includeChildren: false,
33
83
  }
@@ -48,9 +98,16 @@ export const Placeholder = Extension.create<PlaceholderOptions>({
48
98
  }
49
99
 
50
100
  // only calculate isEmpty once due to its performance impacts (see issue #3360)
51
- const emptyDocInstance = doc.type.createAndFill()
52
- const isEditorEmpty = emptyDocInstance?.sameMarkup(doc)
53
- && emptyDocInstance.content.findDiffStart(doc.content) === null
101
+ const { firstChild } = doc.content
102
+ const isLeaf = firstChild && firstChild.type.isLeaf
103
+ const isAtom = firstChild && firstChild.isAtom
104
+ const isValidNode = this.options.considerAnyAsEmpty
105
+ ? true
106
+ : firstChild && firstChild.type.name === doc.type.contentMatch.defaultType?.name
107
+ const isEmptyDoc = doc.content.childCount <= 1
108
+ && firstChild
109
+ && isValidNode
110
+ && (firstChild.nodeSize <= 2 && (!isLeaf || !isAtom))
54
111
 
55
112
  doc.descendants((node, pos) => {
56
113
  const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize
@@ -59,7 +116,7 @@ export const Placeholder = Extension.create<PlaceholderOptions>({
59
116
  if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
60
117
  const classes = [this.options.emptyNodeClass]
61
118
 
62
- if (isEditorEmpty) {
119
+ if (isEmptyDoc) {
63
120
  classes.push(this.options.emptyEditorClass)
64
121
  }
65
122