@tiptap/extension-placeholder 2.0.0-beta.8 → 2.0.0-rc.1

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/README.md CHANGED
@@ -5,10 +5,10 @@
5
5
  [![Sponsor](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub)](https://github.com/sponsors/ueberdosis)
6
6
 
7
7
  ## Introduction
8
- tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as *New York Times*, *The Guardian* or *Atlassian*.
8
+ Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as *New York Times*, *The Guardian* or *Atlassian*.
9
9
 
10
- ## Offical Documentation
11
- Documentation can be found on the [tiptap website](https://tiptap.dev).
10
+ ## Official Documentation
11
+ Documentation can be found on the [Tiptap website](https://tiptap.dev).
12
12
 
13
13
  ## License
14
- tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
14
+ Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
@@ -3,51 +3,61 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var core = require('@tiptap/core');
6
- var prosemirrorView = require('prosemirror-view');
7
- var prosemirrorState = require('prosemirror-state');
6
+ var state = require('@tiptap/pm/state');
7
+ var view = require('@tiptap/pm/view');
8
8
 
9
9
  const Placeholder = core.Extension.create({
10
10
  name: 'placeholder',
11
- defaultOptions: {
12
- emptyEditorClass: 'is-editor-empty',
13
- emptyNodeClass: 'is-empty',
14
- placeholder: 'Write something …',
15
- showOnlyWhenEditable: true,
16
- showOnlyCurrent: true,
11
+ addOptions() {
12
+ return {
13
+ emptyEditorClass: 'is-editor-empty',
14
+ emptyNodeClass: 'is-empty',
15
+ placeholder: 'Write something …',
16
+ showOnlyWhenEditable: true,
17
+ showOnlyCurrent: true,
18
+ includeChildren: false,
19
+ };
17
20
  },
18
21
  addProseMirrorPlugins() {
19
22
  return [
20
- new prosemirrorState.Plugin({
23
+ new state.Plugin({
24
+ key: new state.PluginKey('placeholder'),
21
25
  props: {
22
26
  decorations: ({ doc, selection }) => {
23
27
  const active = this.editor.isEditable || !this.options.showOnlyWhenEditable;
24
28
  const { anchor } = selection;
25
29
  const decorations = [];
26
30
  if (!active) {
27
- return;
31
+ return null;
28
32
  }
33
+ // 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;
29
37
  doc.descendants((node, pos) => {
30
- const hasAnchor = anchor >= pos && anchor <= (pos + node.nodeSize);
31
- const isEmpty = !node.isLeaf && core.isNodeEmpty(node);
38
+ const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize;
39
+ const isEmpty = !node.isLeaf && !node.childCount;
32
40
  if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
33
41
  const classes = [this.options.emptyNodeClass];
34
- if (this.editor.isEmpty) {
42
+ if (isEditorEmpty) {
35
43
  classes.push(this.options.emptyEditorClass);
36
44
  }
37
- const decoration = prosemirrorView.Decoration.node(pos, pos + node.nodeSize, {
45
+ const decoration = view.Decoration.node(pos, pos + node.nodeSize, {
38
46
  class: classes.join(' '),
39
47
  'data-placeholder': typeof this.options.placeholder === 'function'
40
48
  ? this.options.placeholder({
41
49
  editor: this.editor,
42
50
  node,
51
+ pos,
52
+ hasAnchor,
43
53
  })
44
54
  : this.options.placeholder,
45
55
  });
46
56
  decorations.push(decoration);
47
57
  }
48
- return false;
58
+ return this.options.includeChildren;
49
59
  });
50
- return prosemirrorView.DecorationSet.create(doc, decorations);
60
+ return view.DecorationSet.create(doc, decorations);
51
61
  },
52
62
  },
53
63
  }),
@@ -56,5 +66,5 @@ const Placeholder = core.Extension.create({
56
66
  });
57
67
 
58
68
  exports.Placeholder = Placeholder;
59
- exports.default = Placeholder;
60
- //# sourceMappingURL=tiptap-extension-placeholder.cjs.js.map
69
+ exports["default"] = Placeholder;
70
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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,C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,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,OAAOC,kBAAa,CAAC,MAAM,CAAC,GAAG,EAAE,WAAW,CAAC,CAAA;qBAC9C;AACF,iBAAA;aACF,CAAC;SACH,CAAA;KACF;AACF,CAAA;;;;;"}
@@ -1,33 +1,41 @@
1
- import { Extension, isNodeEmpty } from '@tiptap/core';
2
- import { Decoration, DecorationSet } from 'prosemirror-view';
3
- import { Plugin } from 'prosemirror-state';
1
+ import { Extension } from '@tiptap/core';
2
+ import { Plugin, PluginKey } from '@tiptap/pm/state';
3
+ import { Decoration, DecorationSet } from '@tiptap/pm/view';
4
4
 
5
5
  const Placeholder = Extension.create({
6
6
  name: 'placeholder',
7
- defaultOptions: {
8
- emptyEditorClass: 'is-editor-empty',
9
- emptyNodeClass: 'is-empty',
10
- placeholder: 'Write something …',
11
- showOnlyWhenEditable: true,
12
- showOnlyCurrent: true,
7
+ addOptions() {
8
+ return {
9
+ emptyEditorClass: 'is-editor-empty',
10
+ emptyNodeClass: 'is-empty',
11
+ placeholder: 'Write something …',
12
+ showOnlyWhenEditable: true,
13
+ showOnlyCurrent: true,
14
+ includeChildren: false,
15
+ };
13
16
  },
14
17
  addProseMirrorPlugins() {
15
18
  return [
16
19
  new Plugin({
20
+ key: new PluginKey('placeholder'),
17
21
  props: {
18
22
  decorations: ({ doc, selection }) => {
19
23
  const active = this.editor.isEditable || !this.options.showOnlyWhenEditable;
20
24
  const { anchor } = selection;
21
25
  const decorations = [];
22
26
  if (!active) {
23
- return;
27
+ return null;
24
28
  }
29
+ // 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;
25
33
  doc.descendants((node, pos) => {
26
- const hasAnchor = anchor >= pos && anchor <= (pos + node.nodeSize);
27
- const isEmpty = !node.isLeaf && isNodeEmpty(node);
34
+ const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize;
35
+ const isEmpty = !node.isLeaf && !node.childCount;
28
36
  if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
29
37
  const classes = [this.options.emptyNodeClass];
30
- if (this.editor.isEmpty) {
38
+ if (isEditorEmpty) {
31
39
  classes.push(this.options.emptyEditorClass);
32
40
  }
33
41
  const decoration = Decoration.node(pos, pos + node.nodeSize, {
@@ -36,12 +44,14 @@ const Placeholder = Extension.create({
36
44
  ? this.options.placeholder({
37
45
  editor: this.editor,
38
46
  node,
47
+ pos,
48
+ hasAnchor,
39
49
  })
40
50
  : this.options.placeholder,
41
51
  });
42
52
  decorations.push(decoration);
43
53
  }
44
- return false;
54
+ return this.options.includeChildren;
45
55
  });
46
56
  return DecorationSet.create(doc, decorations);
47
57
  },
@@ -51,6 +61,5 @@ const Placeholder = Extension.create({
51
61
  },
52
62
  });
53
63
 
54
- export default Placeholder;
55
- export { Placeholder };
56
- //# sourceMappingURL=tiptap-extension-placeholder.esm.js.map
64
+ export { Placeholder, Placeholder as default };
65
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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,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C3D,MAAM,OAAO,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,CAAA;AAE7C,gCAAA,IAAI,aAAa,EAAE;oCACjB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAA;AAC5C,iCAAA;AAED,gCAAA,MAAM,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC,QAAQ,EAAE;AAC3D,oCAAA,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC;oCACxB,kBAAkB,EAChB,OAAO,IAAI,CAAC,OAAO,CAAC,WAAW,KAAK,UAAU;AAC5C,0CAAE,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC;4CACzB,MAAM,EAAE,IAAI,CAAC,MAAM;4CACnB,IAAI;4CACJ,GAAG;4CACH,SAAS;yCACV,CAAC;AACF,0CAAE,IAAI,CAAC,OAAO,CAAC,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,51 +1,61 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tiptap/core'), require('prosemirror-view'), require('prosemirror-state')) :
3
- typeof define === 'function' && define.amd ? define(['exports', '@tiptap/core', 'prosemirror-view', 'prosemirror-state'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['@tiptap/extension-placeholder'] = {}, global.core, global.prosemirrorView, global.prosemirrorState));
5
- }(this, (function (exports, core, prosemirrorView, prosemirrorState) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tiptap/core'), require('@tiptap/pm/state'), require('@tiptap/pm/view')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@tiptap/core', '@tiptap/pm/state', '@tiptap/pm/view'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@tiptap/extension-placeholder"] = {}, global.core, global.state, global.view));
5
+ })(this, (function (exports, core, state, view) { 'use strict';
6
6
 
7
7
  const Placeholder = core.Extension.create({
8
8
  name: 'placeholder',
9
- defaultOptions: {
10
- emptyEditorClass: 'is-editor-empty',
11
- emptyNodeClass: 'is-empty',
12
- placeholder: 'Write something …',
13
- showOnlyWhenEditable: true,
14
- showOnlyCurrent: true,
9
+ addOptions() {
10
+ return {
11
+ emptyEditorClass: 'is-editor-empty',
12
+ emptyNodeClass: 'is-empty',
13
+ placeholder: 'Write something …',
14
+ showOnlyWhenEditable: true,
15
+ showOnlyCurrent: true,
16
+ includeChildren: false,
17
+ };
15
18
  },
16
19
  addProseMirrorPlugins() {
17
20
  return [
18
- new prosemirrorState.Plugin({
21
+ new state.Plugin({
22
+ key: new state.PluginKey('placeholder'),
19
23
  props: {
20
24
  decorations: ({ doc, selection }) => {
21
25
  const active = this.editor.isEditable || !this.options.showOnlyWhenEditable;
22
26
  const { anchor } = selection;
23
27
  const decorations = [];
24
28
  if (!active) {
25
- return;
29
+ return null;
26
30
  }
31
+ // 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;
27
35
  doc.descendants((node, pos) => {
28
- const hasAnchor = anchor >= pos && anchor <= (pos + node.nodeSize);
29
- const isEmpty = !node.isLeaf && core.isNodeEmpty(node);
36
+ const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize;
37
+ const isEmpty = !node.isLeaf && !node.childCount;
30
38
  if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
31
39
  const classes = [this.options.emptyNodeClass];
32
- if (this.editor.isEmpty) {
40
+ if (isEditorEmpty) {
33
41
  classes.push(this.options.emptyEditorClass);
34
42
  }
35
- const decoration = prosemirrorView.Decoration.node(pos, pos + node.nodeSize, {
43
+ const decoration = view.Decoration.node(pos, pos + node.nodeSize, {
36
44
  class: classes.join(' '),
37
45
  'data-placeholder': typeof this.options.placeholder === 'function'
38
46
  ? this.options.placeholder({
39
47
  editor: this.editor,
40
48
  node,
49
+ pos,
50
+ hasAnchor,
41
51
  })
42
52
  : this.options.placeholder,
43
53
  });
44
54
  decorations.push(decoration);
45
55
  }
46
- return false;
56
+ return this.options.includeChildren;
47
57
  });
48
- return prosemirrorView.DecorationSet.create(doc, decorations);
58
+ return view.DecorationSet.create(doc, decorations);
49
59
  },
50
60
  },
51
61
  }),
@@ -54,9 +64,9 @@
54
64
  });
55
65
 
56
66
  exports.Placeholder = Placeholder;
57
- exports.default = Placeholder;
67
+ exports["default"] = Placeholder;
58
68
 
59
69
  Object.defineProperty(exports, '__esModule', { value: true });
60
70
 
61
- })));
62
- //# sourceMappingURL=tiptap-extension-placeholder.umd.js.map
71
+ }));
72
+ //# sourceMappingURL=index.umd.js.map
@@ -0,0 +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,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,KAAK,OAAO,EAAE;kCAC3D,MAAM,OAAO,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,CAAA;EAE7C,gCAAA,IAAI,aAAa,EAAE;sCACjB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAA;EAC5C,iCAAA;EAED,gCAAA,MAAM,UAAU,GAAGC,eAAU,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC,QAAQ,EAAE;EAC3D,oCAAA,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC;sCACxB,kBAAkB,EAChB,OAAO,IAAI,CAAC,OAAO,CAAC,WAAW,KAAK,UAAU;EAC5C,0CAAE,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC;8CACzB,MAAM,EAAE,IAAI,CAAC,MAAM;8CACnB,IAAI;8CACJ,GAAG;8CACH,SAAS;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,13 +1,16 @@
1
1
  import { Editor, Extension } from '@tiptap/core';
2
- import { Node as ProsemirrorNode } from 'prosemirror-model';
2
+ import { Node as ProsemirrorNode } from '@tiptap/pm/model';
3
3
  export interface PlaceholderOptions {
4
4
  emptyEditorClass: string;
5
5
  emptyNodeClass: string;
6
6
  placeholder: ((PlaceholderProps: {
7
7
  editor: Editor;
8
8
  node: ProsemirrorNode;
9
+ pos: number;
10
+ hasAnchor: boolean;
9
11
  }) => string) | string;
10
12
  showOnlyWhenEditable: boolean;
11
13
  showOnlyCurrent: boolean;
14
+ includeChildren: boolean;
12
15
  }
13
- export declare const Placeholder: Extension<PlaceholderOptions>;
16
+ 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.0.0-beta.8",
4
+ "version": "2.0.0-rc.1",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -12,22 +12,37 @@
12
12
  "type": "github",
13
13
  "url": "https://github.com/sponsors/ueberdosis"
14
14
  },
15
- "main": "dist/tiptap-extension-placeholder.cjs.js",
16
- "umd": "dist/tiptap-extension-placeholder.umd.js",
17
- "module": "dist/tiptap-extension-placeholder.esm.js",
18
- "unpkg": "dist/tiptap-extension-placeholder.bundle.umd.min.js",
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/packages/extension-placeholder/src/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.cjs"
21
+ }
22
+ },
23
+ "main": "dist/index.cjs",
24
+ "module": "dist/index.js",
25
+ "umd": "dist/index.umd.js",
19
26
  "types": "dist/packages/extension-placeholder/src/index.d.ts",
20
27
  "files": [
21
28
  "src",
22
29
  "dist"
23
30
  ],
24
31
  "peerDependencies": {
25
- "@tiptap/core": "^2.0.0-beta.1"
32
+ "@tiptap/core": "2.0.0-rc.1",
33
+ "@tiptap/pm": "2.0.0-rc.1"
34
+ },
35
+ "devDependencies": {
36
+ "@tiptap/core": "2.0.0-rc.1",
37
+ "@tiptap/pm": "2.0.0-rc.1"
26
38
  },
27
- "dependencies": {
28
- "prosemirror-model": "^1.14.0",
29
- "prosemirror-state": "^1.3.4",
30
- "prosemirror-view": "^1.18.2"
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "https://github.com/ueberdosis/tiptap",
42
+ "directory": "packages/extension-placeholder"
31
43
  },
32
- "gitHead": "bd9e15d78f9159afb05d09e7ba279ff7d74a85ea"
33
- }
44
+ "scripts": {
45
+ "clean": "rm -rf dist",
46
+ "build": "npm run clean && rollup -c"
47
+ }
48
+ }
@@ -1,33 +1,42 @@
1
- import { Editor, Extension, isNodeEmpty } from '@tiptap/core'
2
- import { Node as ProsemirrorNode } from 'prosemirror-model'
3
- import { Decoration, DecorationSet } from 'prosemirror-view'
4
- import { Plugin } from 'prosemirror-state'
1
+ import { Editor, Extension } from '@tiptap/core'
2
+ import { Node as ProsemirrorNode } from '@tiptap/pm/model'
3
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
4
+ import { Decoration, DecorationSet } from '@tiptap/pm/view'
5
5
 
6
6
  export interface PlaceholderOptions {
7
- emptyEditorClass: string,
8
- emptyNodeClass: string,
9
- placeholder: ((PlaceholderProps: {
10
- editor: Editor,
11
- node: ProsemirrorNode,
12
- }) => string) | string,
13
- showOnlyWhenEditable: boolean,
14
- showOnlyCurrent: boolean,
7
+ emptyEditorClass: string
8
+ emptyNodeClass: string
9
+ placeholder:
10
+ | ((PlaceholderProps: {
11
+ editor: Editor
12
+ node: ProsemirrorNode
13
+ pos: number
14
+ hasAnchor: boolean
15
+ }) => string)
16
+ | string
17
+ showOnlyWhenEditable: boolean
18
+ showOnlyCurrent: boolean
19
+ includeChildren: boolean
15
20
  }
16
21
 
17
22
  export const Placeholder = Extension.create<PlaceholderOptions>({
18
23
  name: 'placeholder',
19
24
 
20
- defaultOptions: {
21
- emptyEditorClass: 'is-editor-empty',
22
- emptyNodeClass: 'is-empty',
23
- placeholder: 'Write something …',
24
- showOnlyWhenEditable: true,
25
- showOnlyCurrent: true,
25
+ addOptions() {
26
+ return {
27
+ emptyEditorClass: 'is-editor-empty',
28
+ emptyNodeClass: 'is-empty',
29
+ placeholder: 'Write something …',
30
+ showOnlyWhenEditable: true,
31
+ showOnlyCurrent: true,
32
+ includeChildren: false,
33
+ }
26
34
  },
27
35
 
28
36
  addProseMirrorPlugins() {
29
37
  return [
30
38
  new Plugin({
39
+ key: new PluginKey('placeholder'),
31
40
  props: {
32
41
  decorations: ({ doc, selection }) => {
33
42
  const active = this.editor.isEditable || !this.options.showOnlyWhenEditable
@@ -35,34 +44,42 @@ export const Placeholder = Extension.create<PlaceholderOptions>({
35
44
  const decorations: Decoration[] = []
36
45
 
37
46
  if (!active) {
38
- return
47
+ return null
39
48
  }
40
49
 
50
+ // 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
54
+
41
55
  doc.descendants((node, pos) => {
42
- const hasAnchor = anchor >= pos && anchor <= (pos + node.nodeSize)
43
- const isEmpty = !node.isLeaf && isNodeEmpty(node)
56
+ const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize
57
+ const isEmpty = !node.isLeaf && !node.childCount
44
58
 
45
59
  if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
46
60
  const classes = [this.options.emptyNodeClass]
47
61
 
48
- if (this.editor.isEmpty) {
62
+ if (isEditorEmpty) {
49
63
  classes.push(this.options.emptyEditorClass)
50
64
  }
51
65
 
52
66
  const decoration = Decoration.node(pos, pos + node.nodeSize, {
53
67
  class: classes.join(' '),
54
- 'data-placeholder': typeof this.options.placeholder === 'function'
55
- ? this.options.placeholder({
56
- editor: this.editor,
57
- node,
58
- })
59
- : this.options.placeholder,
68
+ 'data-placeholder':
69
+ typeof this.options.placeholder === 'function'
70
+ ? this.options.placeholder({
71
+ editor: this.editor,
72
+ node,
73
+ pos,
74
+ hasAnchor,
75
+ })
76
+ : this.options.placeholder,
60
77
  })
61
78
 
62
79
  decorations.push(decoration)
63
80
  }
64
81
 
65
- return false
82
+ return this.options.includeChildren
66
83
  })
67
84
 
68
85
  return DecorationSet.create(doc, decorations)
package/CHANGELOG.md DELETED
@@ -1,64 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- # [2.0.0-beta.8](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-placeholder@2.0.0-beta.7...@tiptap/extension-placeholder@2.0.0-beta.8) (2021-04-23)
7
-
8
- **Note:** Version bump only for package @tiptap/extension-placeholder
9
-
10
-
11
-
12
-
13
-
14
- # [2.0.0-beta.7](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-placeholder@2.0.0-beta.6...@tiptap/extension-placeholder@2.0.0-beta.7) (2021-04-22)
15
-
16
- **Note:** Version bump only for package @tiptap/extension-placeholder
17
-
18
-
19
-
20
-
21
-
22
- # [2.0.0-beta.6](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-placeholder@2.0.0-beta.5...@tiptap/extension-placeholder@2.0.0-beta.6) (2021-04-16)
23
-
24
- **Note:** Version bump only for package @tiptap/extension-placeholder
25
-
26
-
27
-
28
-
29
-
30
- # [2.0.0-beta.5](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-placeholder@2.0.0-beta.4...@tiptap/extension-placeholder@2.0.0-beta.5) (2021-04-15)
31
-
32
- **Note:** Version bump only for package @tiptap/extension-placeholder
33
-
34
-
35
-
36
-
37
-
38
- # [2.0.0-beta.4](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-placeholder@2.0.0-beta.3...@tiptap/extension-placeholder@2.0.0-beta.4) (2021-04-06)
39
-
40
- **Note:** Version bump only for package @tiptap/extension-placeholder
41
-
42
-
43
-
44
-
45
-
46
- # [2.0.0-beta.3](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-placeholder@2.0.0-beta.2...@tiptap/extension-placeholder@2.0.0-beta.3) (2021-03-31)
47
-
48
- **Note:** Version bump only for package @tiptap/extension-placeholder
49
-
50
-
51
-
52
-
53
-
54
- # [2.0.0-beta.2](https://github.com/ueberdosis/tiptap/compare/@tiptap/extension-placeholder@2.0.0-beta.1...@tiptap/extension-placeholder@2.0.0-beta.2) (2021-03-28)
55
-
56
- **Note:** Version bump only for package @tiptap/extension-placeholder
57
-
58
-
59
-
60
-
61
-
62
- # 2.0.0-beta.1 (2021-03-24)
63
-
64
- **Note:** Version bump only for package @tiptap/extension-placeholder
package/LICENSE.md DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2020, überdosis GbR
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.