@tiptap/extensions 3.27.2 → 3.27.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.
@@ -28,13 +28,19 @@ module.exports = __toCommonJS(index_exports);
28
28
  var import_core = require("@tiptap/core");
29
29
  var import_state = require("@tiptap/pm/state");
30
30
  var import_view = require("@tiptap/pm/view");
31
- var selectionStyle = `.ProseMirror:not(.ProseMirror-focused) *::selection {
32
- background: transparent;
31
+ function shouldSyncDomSelection(state, editor) {
32
+ return !state.selection.empty && !(0, import_core.isNodeSelection)(state.selection) && editor.isEditable;
33
+ }
34
+ function shouldPreserveSelection(state, editor) {
35
+ return shouldSyncDomSelection(state, editor) && !editor.isFocused && !editor.view.dragging;
36
+ }
37
+ function clearDomSelection() {
38
+ var _a;
39
+ (_a = window.getSelection()) == null ? void 0 : _a.removeAllRanges();
40
+ }
41
+ function restoreDomSelection(view) {
42
+ view.focus();
33
43
  }
34
-
35
- .ProseMirror:not(.ProseMirror-focused) *::-moz-selection {
36
- background: transparent;
37
- }`;
38
44
  var Selection = import_core.Extension.create({
39
45
  name: "selection",
40
46
  addOptions() {
@@ -44,15 +50,12 @@ var Selection = import_core.Extension.create({
44
50
  },
45
51
  addProseMirrorPlugins() {
46
52
  const { editor, options } = this;
47
- if (editor.options.injectCSS && typeof document !== "undefined") {
48
- (0, import_core.createStyleTag)(selectionStyle, editor.options.injectNonce, "selection");
49
- }
50
53
  return [
51
54
  new import_state.Plugin({
52
55
  key: new import_state.PluginKey("selection"),
53
56
  props: {
54
57
  decorations(state) {
55
- if (state.selection.empty || editor.isFocused || !editor.isEditable || (0, import_core.isNodeSelection)(state.selection) || editor.view.dragging) {
58
+ if (!shouldPreserveSelection(state, editor)) {
56
59
  return null;
57
60
  }
58
61
  return import_view.DecorationSet.create(state.doc, [
@@ -60,6 +63,26 @@ var Selection = import_core.Extension.create({
60
63
  class: options.className
61
64
  })
62
65
  ]);
66
+ },
67
+ handleDOMEvents: {
68
+ blur(view) {
69
+ if (!shouldSyncDomSelection(view.state, editor)) {
70
+ return false;
71
+ }
72
+ clearDomSelection();
73
+ return false;
74
+ },
75
+ focus(view) {
76
+ if (!shouldSyncDomSelection(view.state, editor)) {
77
+ return false;
78
+ }
79
+ requestAnimationFrame(() => {
80
+ if (!editor.isDestroyed && view.hasFocus()) {
81
+ restoreDomSelection(view);
82
+ }
83
+ });
84
+ return false;
85
+ }
63
86
  }
64
87
  }
65
88
  })
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/selection/index.ts","../../src/selection/selection.ts"],"sourcesContent":["export * from './selection.js'\n","import { createStyleTag, Extension, isNodeSelection } from '@tiptap/core'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nconst selectionStyle = `.ProseMirror:not(.ProseMirror-focused) *::selection {\n background: transparent;\n}\n\n.ProseMirror:not(.ProseMirror-focused) *::-moz-selection {\n background: transparent;\n}`\n\nexport type SelectionOptions = {\n /**\n * The class name that should be added to the selected text.\n * @default 'selection'\n * @example 'is-selected'\n */\n className: string\n}\n\n/**\n * This extension allows you to add a class to the selected text.\n * @see https://www.tiptap.dev/api/extensions/selection\n */\nexport const Selection = Extension.create<SelectionOptions>({\n name: 'selection',\n\n addOptions() {\n return {\n className: 'selection',\n }\n },\n\n addProseMirrorPlugins() {\n const { editor, options } = this\n\n if (editor.options.injectCSS && typeof document !== 'undefined') {\n createStyleTag(selectionStyle, editor.options.injectNonce, 'selection')\n }\n\n return [\n new Plugin({\n key: new PluginKey('selection'),\n props: {\n decorations(state) {\n if (\n state.selection.empty ||\n editor.isFocused ||\n !editor.isEditable ||\n isNodeSelection(state.selection) ||\n editor.view.dragging\n ) {\n return null\n }\n\n return DecorationSet.create(state.doc, [\n Decoration.inline(state.selection.from, state.selection.to, {\n class: options.className,\n }),\n ])\n },\n },\n }),\n ]\n },\n})\n\nexport default Selection\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAA2D;AAC3D,mBAAkC;AAClC,kBAA0C;AAE1C,IAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAqBhB,IAAM,YAAY,sBAAU,OAAyB;AAAA,EAC1D,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,WAAW;AAAA,IACb;AAAA,EACF;AAAA,EAEA,wBAAwB;AACtB,UAAM,EAAE,QAAQ,QAAQ,IAAI;AAE5B,QAAI,OAAO,QAAQ,aAAa,OAAO,aAAa,aAAa;AAC/D,sCAAe,gBAAgB,OAAO,QAAQ,aAAa,WAAW;AAAA,IACxE;AAEA,WAAO;AAAA,MACL,IAAI,oBAAO;AAAA,QACT,KAAK,IAAI,uBAAU,WAAW;AAAA,QAC9B,OAAO;AAAA,UACL,YAAY,OAAO;AACjB,gBACE,MAAM,UAAU,SAChB,OAAO,aACP,CAAC,OAAO,kBACR,6BAAgB,MAAM,SAAS,KAC/B,OAAO,KAAK,UACZ;AACA,qBAAO;AAAA,YACT;AAEA,mBAAO,0BAAc,OAAO,MAAM,KAAK;AAAA,cACrC,uBAAW,OAAO,MAAM,UAAU,MAAM,MAAM,UAAU,IAAI;AAAA,gBAC1D,OAAO,QAAQ;AAAA,cACjB,CAAC;AAAA,YACH,CAAC;AAAA,UACH;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;","names":[]}
1
+ {"version":3,"sources":["../../src/selection/index.ts","../../src/selection/selection.ts"],"sourcesContent":["export * from './selection.js'\n","import { Extension, isNodeSelection, type Editor } from '@tiptap/core'\nimport { Plugin, PluginKey, type EditorState } from '@tiptap/pm/state'\nimport type { EditorView } from '@tiptap/pm/view'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nexport type SelectionOptions = {\n /**\n * The class name that should be added to the selected text.\n * @default 'selection'\n * @example 'is-selected'\n */\n className: string\n}\n\n/**\n * Whether the native browser selection should be cleared on blur and restored on focus.\n * Only applies to non-empty text selections in an editable editor.\n */\nfunction shouldSyncDomSelection(state: EditorState, editor: Editor): boolean {\n return !state.selection.empty && !isNodeSelection(state.selection) && editor.isEditable\n}\n\n/**\n * Whether the selection decoration should be rendered to keep the selection\n * visible while the editor is blurred (and not dragging).\n */\nfunction shouldPreserveSelection(state: EditorState, editor: Editor): boolean {\n return shouldSyncDomSelection(state, editor) && !editor.isFocused && !editor.view.dragging\n}\n\nfunction clearDomSelection() {\n window.getSelection()?.removeAllRanges()\n}\n\n/**\n * Sync the native selection from the editor state.\n * @see https://prosemirror.net/docs/ref/#view.EditorView.focus\n */\nfunction restoreDomSelection(view: EditorView) {\n view.focus()\n}\n\n/**\n * This extension allows you to add a class to the selected text when the editor is blurred.\n * It clears the native browser selection on blur (so `::selection` styles do not overlap the\n * decoration) and restores it when the editor is focused again.\n * @see https://www.tiptap.dev/api/extensions/selection\n */\nexport const Selection = Extension.create<SelectionOptions>({\n name: 'selection',\n\n addOptions() {\n return {\n className: 'selection',\n }\n },\n\n addProseMirrorPlugins() {\n const { editor, options } = this\n\n return [\n new Plugin({\n key: new PluginKey('selection'),\n props: {\n decorations(state) {\n if (!shouldPreserveSelection(state, editor)) {\n return null\n }\n\n return DecorationSet.create(state.doc, [\n Decoration.inline(state.selection.from, state.selection.to, {\n class: options.className,\n }),\n ])\n },\n handleDOMEvents: {\n blur(view) {\n if (!shouldSyncDomSelection(view.state, editor)) {\n return false\n }\n\n clearDomSelection()\n\n return false\n },\n focus(view) {\n if (!shouldSyncDomSelection(view.state, editor)) {\n return false\n }\n\n requestAnimationFrame(() => {\n if (!editor.isDestroyed && view.hasFocus()) {\n restoreDomSelection(view)\n }\n })\n\n return false\n },\n },\n },\n }),\n ]\n },\n})\n\nexport default Selection\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAAwD;AACxD,mBAAoD;AAEpD,kBAA0C;AAe1C,SAAS,uBAAuB,OAAoB,QAAyB;AAC3E,SAAO,CAAC,MAAM,UAAU,SAAS,KAAC,6BAAgB,MAAM,SAAS,KAAK,OAAO;AAC/E;AAMA,SAAS,wBAAwB,OAAoB,QAAyB;AAC5E,SAAO,uBAAuB,OAAO,MAAM,KAAK,CAAC,OAAO,aAAa,CAAC,OAAO,KAAK;AACpF;AAEA,SAAS,oBAAoB;AA9B7B;AA+BE,eAAO,aAAa,MAApB,mBAAuB;AACzB;AAMA,SAAS,oBAAoB,MAAkB;AAC7C,OAAK,MAAM;AACb;AAQO,IAAM,YAAY,sBAAU,OAAyB;AAAA,EAC1D,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,WAAW;AAAA,IACb;AAAA,EACF;AAAA,EAEA,wBAAwB;AACtB,UAAM,EAAE,QAAQ,QAAQ,IAAI;AAE5B,WAAO;AAAA,MACL,IAAI,oBAAO;AAAA,QACT,KAAK,IAAI,uBAAU,WAAW;AAAA,QAC9B,OAAO;AAAA,UACL,YAAY,OAAO;AACjB,gBAAI,CAAC,wBAAwB,OAAO,MAAM,GAAG;AAC3C,qBAAO;AAAA,YACT;AAEA,mBAAO,0BAAc,OAAO,MAAM,KAAK;AAAA,cACrC,uBAAW,OAAO,MAAM,UAAU,MAAM,MAAM,UAAU,IAAI;AAAA,gBAC1D,OAAO,QAAQ;AAAA,cACjB,CAAC;AAAA,YACH,CAAC;AAAA,UACH;AAAA,UACA,iBAAiB;AAAA,YACf,KAAK,MAAM;AACT,kBAAI,CAAC,uBAAuB,KAAK,OAAO,MAAM,GAAG;AAC/C,uBAAO;AAAA,cACT;AAEA,gCAAkB;AAElB,qBAAO;AAAA,YACT;AAAA,YACA,MAAM,MAAM;AACV,kBAAI,CAAC,uBAAuB,KAAK,OAAO,MAAM,GAAG;AAC/C,uBAAO;AAAA,cACT;AAEA,oCAAsB,MAAM;AAC1B,oBAAI,CAAC,OAAO,eAAe,KAAK,SAAS,GAAG;AAC1C,sCAAoB,IAAI;AAAA,gBAC1B;AAAA,cACF,CAAC;AAED,qBAAO;AAAA,YACT;AAAA,UACF;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;","names":[]}
@@ -9,7 +9,9 @@ type SelectionOptions = {
9
9
  className: string;
10
10
  };
11
11
  /**
12
- * This extension allows you to add a class to the selected text.
12
+ * This extension allows you to add a class to the selected text when the editor is blurred.
13
+ * It clears the native browser selection on blur (so `::selection` styles do not overlap the
14
+ * decoration) and restores it when the editor is focused again.
13
15
  * @see https://www.tiptap.dev/api/extensions/selection
14
16
  */
15
17
  declare const Selection: Extension<SelectionOptions, any>;
@@ -9,7 +9,9 @@ type SelectionOptions = {
9
9
  className: string;
10
10
  };
11
11
  /**
12
- * This extension allows you to add a class to the selected text.
12
+ * This extension allows you to add a class to the selected text when the editor is blurred.
13
+ * It clears the native browser selection on blur (so `::selection` styles do not overlap the
14
+ * decoration) and restores it when the editor is focused again.
13
15
  * @see https://www.tiptap.dev/api/extensions/selection
14
16
  */
15
17
  declare const Selection: Extension<SelectionOptions, any>;
@@ -1,14 +1,20 @@
1
1
  // src/selection/selection.ts
2
- import { createStyleTag, Extension, isNodeSelection } from "@tiptap/core";
2
+ import { Extension, isNodeSelection } from "@tiptap/core";
3
3
  import { Plugin, PluginKey } from "@tiptap/pm/state";
4
4
  import { Decoration, DecorationSet } from "@tiptap/pm/view";
5
- var selectionStyle = `.ProseMirror:not(.ProseMirror-focused) *::selection {
6
- background: transparent;
5
+ function shouldSyncDomSelection(state, editor) {
6
+ return !state.selection.empty && !isNodeSelection(state.selection) && editor.isEditable;
7
+ }
8
+ function shouldPreserveSelection(state, editor) {
9
+ return shouldSyncDomSelection(state, editor) && !editor.isFocused && !editor.view.dragging;
10
+ }
11
+ function clearDomSelection() {
12
+ var _a;
13
+ (_a = window.getSelection()) == null ? void 0 : _a.removeAllRanges();
14
+ }
15
+ function restoreDomSelection(view) {
16
+ view.focus();
7
17
  }
8
-
9
- .ProseMirror:not(.ProseMirror-focused) *::-moz-selection {
10
- background: transparent;
11
- }`;
12
18
  var Selection = Extension.create({
13
19
  name: "selection",
14
20
  addOptions() {
@@ -18,15 +24,12 @@ var Selection = Extension.create({
18
24
  },
19
25
  addProseMirrorPlugins() {
20
26
  const { editor, options } = this;
21
- if (editor.options.injectCSS && typeof document !== "undefined") {
22
- createStyleTag(selectionStyle, editor.options.injectNonce, "selection");
23
- }
24
27
  return [
25
28
  new Plugin({
26
29
  key: new PluginKey("selection"),
27
30
  props: {
28
31
  decorations(state) {
29
- if (state.selection.empty || editor.isFocused || !editor.isEditable || isNodeSelection(state.selection) || editor.view.dragging) {
32
+ if (!shouldPreserveSelection(state, editor)) {
30
33
  return null;
31
34
  }
32
35
  return DecorationSet.create(state.doc, [
@@ -34,6 +37,26 @@ var Selection = Extension.create({
34
37
  class: options.className
35
38
  })
36
39
  ]);
40
+ },
41
+ handleDOMEvents: {
42
+ blur(view) {
43
+ if (!shouldSyncDomSelection(view.state, editor)) {
44
+ return false;
45
+ }
46
+ clearDomSelection();
47
+ return false;
48
+ },
49
+ focus(view) {
50
+ if (!shouldSyncDomSelection(view.state, editor)) {
51
+ return false;
52
+ }
53
+ requestAnimationFrame(() => {
54
+ if (!editor.isDestroyed && view.hasFocus()) {
55
+ restoreDomSelection(view);
56
+ }
57
+ });
58
+ return false;
59
+ }
37
60
  }
38
61
  }
39
62
  })
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/selection/selection.ts"],"sourcesContent":["import { createStyleTag, Extension, isNodeSelection } from '@tiptap/core'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nconst selectionStyle = `.ProseMirror:not(.ProseMirror-focused) *::selection {\n background: transparent;\n}\n\n.ProseMirror:not(.ProseMirror-focused) *::-moz-selection {\n background: transparent;\n}`\n\nexport type SelectionOptions = {\n /**\n * The class name that should be added to the selected text.\n * @default 'selection'\n * @example 'is-selected'\n */\n className: string\n}\n\n/**\n * This extension allows you to add a class to the selected text.\n * @see https://www.tiptap.dev/api/extensions/selection\n */\nexport const Selection = Extension.create<SelectionOptions>({\n name: 'selection',\n\n addOptions() {\n return {\n className: 'selection',\n }\n },\n\n addProseMirrorPlugins() {\n const { editor, options } = this\n\n if (editor.options.injectCSS && typeof document !== 'undefined') {\n createStyleTag(selectionStyle, editor.options.injectNonce, 'selection')\n }\n\n return [\n new Plugin({\n key: new PluginKey('selection'),\n props: {\n decorations(state) {\n if (\n state.selection.empty ||\n editor.isFocused ||\n !editor.isEditable ||\n isNodeSelection(state.selection) ||\n editor.view.dragging\n ) {\n return null\n }\n\n return DecorationSet.create(state.doc, [\n Decoration.inline(state.selection.from, state.selection.to, {\n class: options.className,\n }),\n ])\n },\n },\n }),\n ]\n },\n})\n\nexport default Selection\n"],"mappings":";AAAA,SAAS,gBAAgB,WAAW,uBAAuB;AAC3D,SAAS,QAAQ,iBAAiB;AAClC,SAAS,YAAY,qBAAqB;AAE1C,IAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAqBhB,IAAM,YAAY,UAAU,OAAyB;AAAA,EAC1D,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,WAAW;AAAA,IACb;AAAA,EACF;AAAA,EAEA,wBAAwB;AACtB,UAAM,EAAE,QAAQ,QAAQ,IAAI;AAE5B,QAAI,OAAO,QAAQ,aAAa,OAAO,aAAa,aAAa;AAC/D,qBAAe,gBAAgB,OAAO,QAAQ,aAAa,WAAW;AAAA,IACxE;AAEA,WAAO;AAAA,MACL,IAAI,OAAO;AAAA,QACT,KAAK,IAAI,UAAU,WAAW;AAAA,QAC9B,OAAO;AAAA,UACL,YAAY,OAAO;AACjB,gBACE,MAAM,UAAU,SAChB,OAAO,aACP,CAAC,OAAO,cACR,gBAAgB,MAAM,SAAS,KAC/B,OAAO,KAAK,UACZ;AACA,qBAAO;AAAA,YACT;AAEA,mBAAO,cAAc,OAAO,MAAM,KAAK;AAAA,cACrC,WAAW,OAAO,MAAM,UAAU,MAAM,MAAM,UAAU,IAAI;AAAA,gBAC1D,OAAO,QAAQ;AAAA,cACjB,CAAC;AAAA,YACH,CAAC;AAAA,UACH;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;","names":[]}
1
+ {"version":3,"sources":["../../src/selection/selection.ts"],"sourcesContent":["import { Extension, isNodeSelection, type Editor } from '@tiptap/core'\nimport { Plugin, PluginKey, type EditorState } from '@tiptap/pm/state'\nimport type { EditorView } from '@tiptap/pm/view'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nexport type SelectionOptions = {\n /**\n * The class name that should be added to the selected text.\n * @default 'selection'\n * @example 'is-selected'\n */\n className: string\n}\n\n/**\n * Whether the native browser selection should be cleared on blur and restored on focus.\n * Only applies to non-empty text selections in an editable editor.\n */\nfunction shouldSyncDomSelection(state: EditorState, editor: Editor): boolean {\n return !state.selection.empty && !isNodeSelection(state.selection) && editor.isEditable\n}\n\n/**\n * Whether the selection decoration should be rendered to keep the selection\n * visible while the editor is blurred (and not dragging).\n */\nfunction shouldPreserveSelection(state: EditorState, editor: Editor): boolean {\n return shouldSyncDomSelection(state, editor) && !editor.isFocused && !editor.view.dragging\n}\n\nfunction clearDomSelection() {\n window.getSelection()?.removeAllRanges()\n}\n\n/**\n * Sync the native selection from the editor state.\n * @see https://prosemirror.net/docs/ref/#view.EditorView.focus\n */\nfunction restoreDomSelection(view: EditorView) {\n view.focus()\n}\n\n/**\n * This extension allows you to add a class to the selected text when the editor is blurred.\n * It clears the native browser selection on blur (so `::selection` styles do not overlap the\n * decoration) and restores it when the editor is focused again.\n * @see https://www.tiptap.dev/api/extensions/selection\n */\nexport const Selection = Extension.create<SelectionOptions>({\n name: 'selection',\n\n addOptions() {\n return {\n className: 'selection',\n }\n },\n\n addProseMirrorPlugins() {\n const { editor, options } = this\n\n return [\n new Plugin({\n key: new PluginKey('selection'),\n props: {\n decorations(state) {\n if (!shouldPreserveSelection(state, editor)) {\n return null\n }\n\n return DecorationSet.create(state.doc, [\n Decoration.inline(state.selection.from, state.selection.to, {\n class: options.className,\n }),\n ])\n },\n handleDOMEvents: {\n blur(view) {\n if (!shouldSyncDomSelection(view.state, editor)) {\n return false\n }\n\n clearDomSelection()\n\n return false\n },\n focus(view) {\n if (!shouldSyncDomSelection(view.state, editor)) {\n return false\n }\n\n requestAnimationFrame(() => {\n if (!editor.isDestroyed && view.hasFocus()) {\n restoreDomSelection(view)\n }\n })\n\n return false\n },\n },\n },\n }),\n ]\n },\n})\n\nexport default Selection\n"],"mappings":";AAAA,SAAS,WAAW,uBAAoC;AACxD,SAAS,QAAQ,iBAAmC;AAEpD,SAAS,YAAY,qBAAqB;AAe1C,SAAS,uBAAuB,OAAoB,QAAyB;AAC3E,SAAO,CAAC,MAAM,UAAU,SAAS,CAAC,gBAAgB,MAAM,SAAS,KAAK,OAAO;AAC/E;AAMA,SAAS,wBAAwB,OAAoB,QAAyB;AAC5E,SAAO,uBAAuB,OAAO,MAAM,KAAK,CAAC,OAAO,aAAa,CAAC,OAAO,KAAK;AACpF;AAEA,SAAS,oBAAoB;AA9B7B;AA+BE,eAAO,aAAa,MAApB,mBAAuB;AACzB;AAMA,SAAS,oBAAoB,MAAkB;AAC7C,OAAK,MAAM;AACb;AAQO,IAAM,YAAY,UAAU,OAAyB;AAAA,EAC1D,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,WAAW;AAAA,IACb;AAAA,EACF;AAAA,EAEA,wBAAwB;AACtB,UAAM,EAAE,QAAQ,QAAQ,IAAI;AAE5B,WAAO;AAAA,MACL,IAAI,OAAO;AAAA,QACT,KAAK,IAAI,UAAU,WAAW;AAAA,QAC9B,OAAO;AAAA,UACL,YAAY,OAAO;AACjB,gBAAI,CAAC,wBAAwB,OAAO,MAAM,GAAG;AAC3C,qBAAO;AAAA,YACT;AAEA,mBAAO,cAAc,OAAO,MAAM,KAAK;AAAA,cACrC,WAAW,OAAO,MAAM,UAAU,MAAM,MAAM,UAAU,IAAI;AAAA,gBAC1D,OAAO,QAAQ;AAAA,cACjB,CAAC;AAAA,YACH,CAAC;AAAA,UACH;AAAA,UACA,iBAAiB;AAAA,YACf,KAAK,MAAM;AACT,kBAAI,CAAC,uBAAuB,KAAK,OAAO,MAAM,GAAG;AAC/C,uBAAO;AAAA,cACT;AAEA,gCAAkB;AAElB,qBAAO;AAAA,YACT;AAAA,YACA,MAAM,MAAM;AACV,kBAAI,CAAC,uBAAuB,KAAK,OAAO,MAAM,GAAG;AAC/C,uBAAO;AAAA,cACT;AAEA,oCAAsB,MAAM;AAC1B,oBAAI,CAAC,OAAO,eAAe,KAAK,SAAS,GAAG;AAC1C,sCAAoB,IAAI;AAAA,gBAC1B;AAAA,cACF,CAAC;AAED,qBAAO;AAAA,YACT;AAAA,UACF;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiptap/extensions",
3
- "version": "3.27.2",
3
+ "version": "3.27.4",
4
4
  "description": "various extensions for tiptap",
5
5
  "keywords": [
6
6
  "tiptap",
@@ -103,12 +103,12 @@
103
103
  }
104
104
  },
105
105
  "devDependencies": {
106
- "@tiptap/core": "^3.27.2",
107
- "@tiptap/pm": "^3.27.2"
106
+ "@tiptap/core": "^3.27.4",
107
+ "@tiptap/pm": "^3.27.4"
108
108
  },
109
109
  "peerDependencies": {
110
- "@tiptap/core": "3.27.2",
111
- "@tiptap/pm": "3.27.2"
110
+ "@tiptap/core": "3.27.4",
111
+ "@tiptap/pm": "3.27.4"
112
112
  },
113
113
  "scripts": {
114
114
  "build": "tsup"
@@ -1,17 +1,8 @@
1
1
  import { PluginKey } from '@tiptap/pm/state'
2
-
3
- import type { ViewportState } from './types.js'
2
+ import type { DecorationSet } from '@tiptap/pm/view'
4
3
 
5
4
  /** The default data attribute label */
6
5
  export const DEFAULT_DATA_ATTRIBUTE = 'placeholder'
7
6
 
8
- /** The plugin key used to store and read the placeholder viewport state */
9
- export const PLUGIN_KEY = new PluginKey<ViewportState>('tiptap__placeholder')
10
-
11
- /**
12
- * Extra pixels added above and below the visible viewport when computing the
13
- * range of nodes to decorate. Decorating slightly beyond the fold means a
14
- * node already has its placeholder before it scrolls into view, which hides
15
- * the latency introduced by the throttled viewport recompute.
16
- */
17
- export const VIEWPORT_OVERSCAN_PX = 200
7
+ /** The plugin key used to store and read the placeholder decoration set */
8
+ export const PLUGIN_KEY = new PluginKey<DecorationSet>('tiptap__placeholder')
@@ -1,11 +1,12 @@
1
1
  import type { Editor } from '@tiptap/core'
2
2
  import { Plugin } from '@tiptap/pm/state'
3
+ import { DecorationSet } from '@tiptap/pm/view'
3
4
 
4
5
  import { DEFAULT_DATA_ATTRIBUTE, PLUGIN_KEY } from '../constants.js'
5
6
  import type { PlaceholderOptions } from '../types.js'
6
7
  import { buildPlaceholderDecorations } from '../utils/buildPlaceholderDecorations.js'
8
+ import { createPlaceholderStateField } from '../utils/placeholderStateField.js'
7
9
  import { preparePlaceholderAttribute } from '../utils/preparePlaceholderAttribute.js'
8
- import { createViewportPluginView, viewportPluginState } from '../utils/viewportTracking.js'
9
10
 
10
11
  export type CreatePluginOptions = {
11
12
  editor: Editor
@@ -23,13 +24,26 @@ export function createPlaceholderPlugin({ editor, options }: CreatePluginOptions
23
24
  ? `data-${preparePlaceholderAttribute(options.dataAttribute)}`
24
25
  : `data-${DEFAULT_DATA_ATTRIBUTE}`
25
26
 
27
+ const useResolvedPath = options.showOnlyCurrent && !options.includeChildren
28
+
26
29
  return new Plugin({
27
30
  key: PLUGIN_KEY,
28
- state: viewportPluginState,
29
- view: createViewportPluginView,
31
+ ...(useResolvedPath
32
+ ? {}
33
+ : {
34
+ state: createPlaceholderStateField({ editor, options, dataAttribute }),
35
+ }),
30
36
  props: {
31
- decorations: ({ doc, selection }) =>
32
- buildPlaceholderDecorations({ editor, options, dataAttribute, doc, selection }),
37
+ decorations: useResolvedPath
38
+ ? ({ doc, selection }) =>
39
+ buildPlaceholderDecorations({ editor, options, dataAttribute, doc, selection })
40
+ : state => {
41
+ if (options.showOnlyWhenEditable && !editor.isEditable) {
42
+ return DecorationSet.empty
43
+ }
44
+
45
+ return PLUGIN_KEY.getState(state) ?? DecorationSet.empty
46
+ },
33
47
  },
34
48
  })
35
49
  }
@@ -1,16 +1,6 @@
1
1
  import type { Editor } from '@tiptap/core'
2
2
  import type { Node as ProsemirrorNode } from '@tiptap/pm/model'
3
3
 
4
- /**
5
- * The viewport positions tracked by the placeholder plugin.
6
- * `null` means "no viewport info yet" — the decoration callback falls back to
7
- * a full document scan until the scroll handler fires.
8
- */
9
- export interface ViewportState {
10
- topPos: number | null
11
- bottomPos: number | null
12
- }
13
-
14
4
  export interface PlaceholderOptions {
15
5
  /**
16
6
  * **The class name for the empty editor**
@@ -5,7 +5,6 @@ import type { Selection } from '@tiptap/pm/state'
5
5
  import type { Decoration } from '@tiptap/pm/view'
6
6
  import { DecorationSet } from '@tiptap/pm/view'
7
7
 
8
- import { PLUGIN_KEY } from '../constants.js'
9
8
  import type { PlaceholderOptions } from '../types.js'
10
9
  import { createPlaceholderDecoration } from './createPlaceholderDecoration.js'
11
10
 
@@ -16,6 +15,68 @@ function resolveEmptyNodeClass(
16
15
  return typeof emptyNodeClass === 'function' ? emptyNodeClass(props) : emptyNodeClass
17
16
  }
18
17
 
18
+ /**
19
+ * Scans a document range for empty textblocks that should receive placeholder
20
+ * decorations. Used by the slow path and incremental state updates.
21
+ */
22
+ export function scanRangeForDecorations({
23
+ editor,
24
+ options,
25
+ dataAttribute,
26
+ doc,
27
+ selection,
28
+ from,
29
+ to,
30
+ }: {
31
+ editor: Editor
32
+ options: PlaceholderOptions
33
+ dataAttribute: string
34
+ doc: Node
35
+ selection: Selection
36
+ from: number
37
+ to: number
38
+ }): Decoration[] {
39
+ const { anchor } = selection
40
+ const decorations: Decoration[] = []
41
+ const isEmptyDoc = editor.isEmpty
42
+
43
+ doc.nodesBetween(from, to, (node, pos) => {
44
+ const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize
45
+ const isEmpty = !node.isLeaf && isNodeEmpty(node)
46
+
47
+ if (!node.type.isTextblock) {
48
+ return options.includeChildren
49
+ }
50
+
51
+ if ((hasAnchor || !options.showOnlyCurrent) && isEmpty) {
52
+ decorations.push(
53
+ createPlaceholderDecoration({
54
+ editor,
55
+ isEmptyDoc,
56
+ dataAttribute,
57
+ hasAnchor,
58
+ placeholder: options.placeholder,
59
+ classes: {
60
+ emptyEditor: options.emptyEditorClass,
61
+ emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
62
+ editor,
63
+ node,
64
+ pos,
65
+ hasAnchor,
66
+ }),
67
+ },
68
+ node,
69
+ pos,
70
+ }),
71
+ )
72
+ }
73
+
74
+ return options.includeChildren
75
+ })
76
+
77
+ return decorations
78
+ }
79
+
19
80
  /**
20
81
  * Builds the placeholder decorations for the current document state.
21
82
  * @param options.editor - The editor instance.
@@ -86,43 +147,17 @@ export function buildPlaceholderDecorations({
86
147
  )
87
148
  }
88
149
  } else {
89
- const pluginState = PLUGIN_KEY.getState(editor.state)
90
- const from = pluginState?.topPos ?? 0
91
- const to = pluginState?.bottomPos ?? doc.content.size
92
-
93
- doc.nodesBetween(from, to, (node, pos) => {
94
- const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize
95
- const isEmpty = !node.isLeaf && isNodeEmpty(node)
96
-
97
- if (!node.type.isTextblock) {
98
- return options.includeChildren
99
- }
100
-
101
- if ((hasAnchor || !options.showOnlyCurrent) && isEmpty) {
102
- decorations.push(
103
- createPlaceholderDecoration({
104
- editor,
105
- isEmptyDoc,
106
- dataAttribute,
107
- hasAnchor,
108
- placeholder: options.placeholder,
109
- classes: {
110
- emptyEditor: options.emptyEditorClass,
111
- emptyNode: resolveEmptyNodeClass(options.emptyNodeClass, {
112
- editor,
113
- node,
114
- pos,
115
- hasAnchor,
116
- }),
117
- },
118
- node,
119
- pos,
120
- }),
121
- )
122
- }
123
-
124
- return options.includeChildren
125
- })
150
+ decorations.push(
151
+ ...scanRangeForDecorations({
152
+ editor,
153
+ options,
154
+ dataAttribute,
155
+ doc,
156
+ selection,
157
+ from: 0,
158
+ to: doc.content.size,
159
+ }),
160
+ )
126
161
  }
127
162
 
128
163
  return DecorationSet.create(doc, decorations)
@@ -1,6 +1,3 @@
1
1
  export * from './buildPlaceholderDecorations.js'
2
2
  export * from './createPlaceholderDecoration.js'
3
- export * from './findScrollParent.js'
4
- export * from './getViewportBoundaryPositions.js'
5
3
  export * from './preparePlaceholderAttribute.js'
6
- export * from './viewportTracking.js'
@@ -0,0 +1,204 @@
1
+ import type { Editor } from '@tiptap/core'
2
+ import { getChangedRanges } from '@tiptap/core'
3
+ import type { Node } from '@tiptap/pm/model'
4
+ import type { EditorState, StateField, Transaction } from '@tiptap/pm/state'
5
+ import type { Selection } from '@tiptap/pm/state'
6
+ import { DecorationSet } from '@tiptap/pm/view'
7
+
8
+ import type { PlaceholderOptions } from '../types.js'
9
+ import {
10
+ buildPlaceholderDecorations,
11
+ scanRangeForDecorations,
12
+ } from './buildPlaceholderDecorations.js'
13
+ import {
14
+ getTopLevelBlocksInRange,
15
+ mergeRanges,
16
+ resolveTopLevelRange,
17
+ toContentRelativeRange,
18
+ } from './resolveTopLevelRange.js'
19
+
20
+ /** Options passed to {@link createPlaceholderStateField}. */
21
+ export type CreatePlaceholderStateFieldOptions = {
22
+ editor: Editor
23
+ options: PlaceholderOptions
24
+ dataAttribute: string
25
+ }
26
+
27
+ /**
28
+ * Expands a single changed range to the top-level blocks it touches.
29
+ * Also resolves blocks at range boundaries so split/merge edits update
30
+ * adjacent empty nodes (e.g. a new paragraph after Enter).
31
+ */
32
+ function collectBlocksForChange(
33
+ doc: Node,
34
+ change: { from: number; to: number },
35
+ ): Array<{ from: number; to: number }> {
36
+ const ranges = getTopLevelBlocksInRange(doc, change.from, change.to)
37
+
38
+ ranges.push(toContentRelativeRange(doc, resolveTopLevelRange(doc, change.from)))
39
+
40
+ if (change.to > change.from) {
41
+ ranges.push(
42
+ toContentRelativeRange(
43
+ doc,
44
+ resolveTopLevelRange(doc, Math.min(change.to, doc.content.size + 1) - 1),
45
+ ),
46
+ )
47
+ } else if (change.from < doc.content.size + 1) {
48
+ ranges.push(
49
+ toContentRelativeRange(
50
+ doc,
51
+ resolveTopLevelRange(doc, Math.min(change.from + 1, doc.content.size)),
52
+ ),
53
+ )
54
+ }
55
+
56
+ return ranges
57
+ }
58
+
59
+ /**
60
+ * Collects content-relative top-level block ranges that need placeholder
61
+ * decorations recomputed after a transaction.
62
+ */
63
+ function collectRescanRanges(
64
+ tr: Transaction,
65
+ oldState: EditorState,
66
+ newState: EditorState,
67
+ ): Array<{ from: number; to: number }> {
68
+ const ranges: Array<{ from: number; to: number }> = []
69
+
70
+ if (tr.docChanged) {
71
+ const changes = getChangedRanges(tr)
72
+
73
+ for (const change of changes) {
74
+ ranges.push(...collectBlocksForChange(newState.doc, change.newRange))
75
+ }
76
+ }
77
+
78
+ if (tr.selectionSet) {
79
+ ranges.push(
80
+ toContentRelativeRange(
81
+ newState.doc,
82
+ resolveTopLevelRange(newState.doc, tr.mapping.map(oldState.selection.anchor)),
83
+ ),
84
+ )
85
+ ranges.push(
86
+ toContentRelativeRange(
87
+ newState.doc,
88
+ resolveTopLevelRange(newState.doc, newState.selection.anchor),
89
+ ),
90
+ )
91
+ }
92
+
93
+ return mergeRanges(ranges)
94
+ }
95
+
96
+ /** Clamps a content-relative range to `[0, doc.content.size]`. */
97
+ function clampRange(from: number, to: number, doc: Node): { from: number; to: number } {
98
+ const clampedFrom = Math.max(0, Math.min(from, doc.content.size))
99
+ const clampedTo = Math.max(clampedFrom, Math.min(to, doc.content.size))
100
+
101
+ return { from: clampedFrom, to: clampedTo }
102
+ }
103
+
104
+ /**
105
+ * Removes and rebuilds placeholder decorations within the given ranges.
106
+ * Only drops decorations fully contained in a range so mapped decorations
107
+ * on neighbouring blocks (e.g. at a block boundary) are kept intact.
108
+ */
109
+ function updateDecorationsInRanges({
110
+ decorations,
111
+ ranges,
112
+ editor,
113
+ options,
114
+ dataAttribute,
115
+ doc,
116
+ selection,
117
+ }: {
118
+ decorations: DecorationSet
119
+ ranges: Array<{ from: number; to: number }>
120
+ editor: Editor
121
+ options: PlaceholderOptions
122
+ dataAttribute: string
123
+ doc: Node
124
+ selection: Selection
125
+ }): DecorationSet {
126
+ let next = decorations
127
+
128
+ for (const range of ranges) {
129
+ const { from, to } = clampRange(range.from, range.to, doc)
130
+ const existing = next
131
+ .find(from, to)
132
+ .filter(decoration => decoration.from >= from && decoration.to <= to)
133
+
134
+ if (existing.length) {
135
+ next = next.remove(existing)
136
+ }
137
+
138
+ const newDecos = scanRangeForDecorations({
139
+ editor,
140
+ options,
141
+ dataAttribute,
142
+ doc,
143
+ selection,
144
+ from,
145
+ to,
146
+ })
147
+
148
+ if (newDecos.length) {
149
+ next = next.add(doc, newDecos)
150
+ }
151
+ }
152
+
153
+ return next
154
+ }
155
+
156
+ /**
157
+ * Creates the incremental `StateField<DecorationSet>` used by the slow path
158
+ * (`showOnlyCurrent: false` or `includeChildren: true`).
159
+ *
160
+ * Decorations are mapped through each transaction and only recomputed for
161
+ * top-level blocks touched by document or selection changes.
162
+ * @param options.editor - The editor instance.
163
+ * @param options.options - The resolved placeholder options.
164
+ * @param options.dataAttribute - The prepared `data-*` attribute name.
165
+ * @returns A ProseMirror state field storing the placeholder decoration set.
166
+ */
167
+ export function createPlaceholderStateField({
168
+ editor,
169
+ options,
170
+ dataAttribute,
171
+ }: CreatePlaceholderStateFieldOptions): StateField<DecorationSet> {
172
+ return {
173
+ init(_config, state: EditorState) {
174
+ const decorations = buildPlaceholderDecorations({
175
+ editor,
176
+ options,
177
+ dataAttribute,
178
+ doc: state.doc,
179
+ selection: state.selection,
180
+ })
181
+
182
+ return decorations ?? DecorationSet.empty
183
+ },
184
+
185
+ apply(tr: Transaction, prev: DecorationSet, oldState: EditorState, newState: EditorState) {
186
+ if (!tr.docChanged && !tr.selectionSet) {
187
+ return prev
188
+ }
189
+
190
+ const mapped = prev.map(tr.mapping, tr.doc)
191
+ const ranges = collectRescanRanges(tr, oldState, newState)
192
+
193
+ return updateDecorationsInRanges({
194
+ decorations: mapped,
195
+ ranges,
196
+ editor,
197
+ options,
198
+ dataAttribute,
199
+ doc: newState.doc,
200
+ selection: newState.selection,
201
+ })
202
+ },
203
+ }
204
+ }