@blocknote/core 0.48.1 → 0.50.0

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.
Files changed (115) hide show
  1. package/dist/blocknote.cjs +1 -1
  2. package/dist/blocknote.js +3 -3
  3. package/dist/{blocks-BEvIOzln.cjs → blocks-BFnTypT-.cjs} +3 -3
  4. package/dist/blocks-BFnTypT-.cjs.map +1 -0
  5. package/dist/{blocks-DYl7UTS4.js → blocks-UU7EM-QM.js} +601 -570
  6. package/dist/blocks-UU7EM-QM.js.map +1 -0
  7. package/dist/blocks.cjs +1 -1
  8. package/dist/blocks.js +1 -1
  9. package/dist/comments.cjs +1 -1
  10. package/dist/comments.cjs.map +1 -1
  11. package/dist/comments.js +19 -16
  12. package/dist/comments.js.map +1 -1
  13. package/dist/{en-QsSI16s5.js → en-B9hMApn4.js} +4 -2
  14. package/dist/en-B9hMApn4.js.map +1 -0
  15. package/dist/{en-C4ykW581.cjs → en-Cj5r8sW_.cjs} +2 -2
  16. package/dist/en-Cj5r8sW_.cjs.map +1 -0
  17. package/dist/extensions-CYsFi5Is.cjs +2 -0
  18. package/dist/extensions-CYsFi5Is.cjs.map +1 -0
  19. package/dist/{extensions-3nf9PPhA.js → extensions-CkLT--Nc.js} +244 -250
  20. package/dist/extensions-CkLT--Nc.js.map +1 -0
  21. package/dist/extensions.cjs +1 -1
  22. package/dist/extensions.js +2 -2
  23. package/dist/locales.cjs +1 -1
  24. package/dist/locales.cjs.map +1 -1
  25. package/dist/locales.js +67 -23
  26. package/dist/locales.js.map +1 -1
  27. package/dist/src-DsJ6yzs9.cjs +5 -0
  28. package/dist/src-DsJ6yzs9.cjs.map +1 -0
  29. package/dist/{src-Hjv1f5lc.js → src-SVFpMAWE.js} +754 -366
  30. package/dist/src-SVFpMAWE.js.map +1 -0
  31. package/dist/style.css +1 -1
  32. package/dist/webpack-stats.json +1 -1
  33. package/dist/yjs.cjs +1 -1
  34. package/dist/yjs.js +1 -1
  35. package/package.json +6 -8
  36. package/src/api/clipboard/toClipboard/copyExtension.ts +21 -16
  37. package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.test.ts +14 -1
  38. package/src/blocks/Quote/block.ts +9 -0
  39. package/src/blocks/Table/TableExtension.ts +38 -9
  40. package/src/blocks/Table/block.ts +26 -0
  41. package/src/blocks/defaultBlocks.ts +34 -1
  42. package/src/comments/extension.ts +35 -11
  43. package/src/comments/threadstore/yjs/YjsThreadStore.test.ts +3 -2
  44. package/src/comments/threadstore/yjs/YjsThreadStore.ts +4 -4
  45. package/src/editor/Block.css +14 -18
  46. package/src/editor/BlockNoteEditor.ts +84 -5
  47. package/src/editor/editor.css +5 -0
  48. package/src/editor/managers/ExtensionManager/extensions.ts +8 -27
  49. package/src/editor/managers/StyleManager.ts +90 -10
  50. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +23 -16
  51. package/src/extensions/LinkToolbar/LinkToolbar.ts +30 -75
  52. package/src/extensions/Placeholder/Placeholder.ts +2 -2
  53. package/src/extensions/SideMenu/SideMenu.ts +2 -2
  54. package/src/extensions/TableHandles/TableHandles.ts +2 -0
  55. package/src/extensions/tiptap-extensions/Link/helpers/autolink.ts +179 -0
  56. package/src/extensions/tiptap-extensions/Link/helpers/clickHandler.ts +82 -0
  57. package/src/extensions/tiptap-extensions/Link/helpers/linkDetector.ts +414 -0
  58. package/src/extensions/tiptap-extensions/Link/helpers/pasteHandler.ts +53 -0
  59. package/src/extensions/tiptap-extensions/Link/helpers/tlds.ts +7 -0
  60. package/src/extensions/tiptap-extensions/Link/helpers/whitespace.ts +13 -0
  61. package/src/extensions/tiptap-extensions/Link/index.ts +2 -0
  62. package/src/extensions/tiptap-extensions/Link/link.test.ts +941 -0
  63. package/src/extensions/tiptap-extensions/Link/link.ts +238 -0
  64. package/src/extensions/tiptap-extensions/UniqueID/UniqueID.ts +3 -5
  65. package/src/i18n/locales/ar.ts +3 -1
  66. package/src/i18n/locales/de.ts +3 -1
  67. package/src/i18n/locales/en.ts +3 -1
  68. package/src/i18n/locales/es.ts +3 -1
  69. package/src/i18n/locales/fa.ts +3 -1
  70. package/src/i18n/locales/fr.ts +3 -1
  71. package/src/i18n/locales/he.ts +3 -1
  72. package/src/i18n/locales/hr.ts +3 -1
  73. package/src/i18n/locales/is.ts +3 -1
  74. package/src/i18n/locales/it.ts +3 -1
  75. package/src/i18n/locales/ja.ts +3 -1
  76. package/src/i18n/locales/ko.ts +3 -1
  77. package/src/i18n/locales/nl.ts +3 -1
  78. package/src/i18n/locales/no.ts +3 -1
  79. package/src/i18n/locales/pl.ts +3 -1
  80. package/src/i18n/locales/pt.ts +3 -1
  81. package/src/i18n/locales/ru.ts +3 -1
  82. package/src/i18n/locales/sk.ts +3 -1
  83. package/src/i18n/locales/uk.ts +3 -1
  84. package/src/i18n/locales/uz.ts +3 -1
  85. package/src/i18n/locales/vi.ts +3 -1
  86. package/src/i18n/locales/zh-tw.ts +3 -1
  87. package/src/i18n/locales/zh.ts +3 -1
  88. package/src/schema/inlineContent/types.ts +1 -1
  89. package/types/src/comments/extension.d.ts +2 -1
  90. package/types/src/editor/BlockNoteEditor.d.ts +69 -3
  91. package/types/src/editor/managers/StyleManager.d.ts +22 -0
  92. package/types/src/extensions/LinkToolbar/LinkToolbar.d.ts +28 -7
  93. package/types/src/extensions/tiptap-extensions/Link/helpers/autolink.d.ts +13 -0
  94. package/types/src/extensions/tiptap-extensions/Link/helpers/clickHandler.d.ts +12 -0
  95. package/types/src/extensions/tiptap-extensions/Link/helpers/linkDetector.d.ts +32 -0
  96. package/types/src/extensions/tiptap-extensions/Link/helpers/pasteHandler.d.ts +12 -0
  97. package/types/src/extensions/tiptap-extensions/Link/helpers/tlds.d.ts +1 -0
  98. package/types/src/extensions/tiptap-extensions/Link/helpers/whitespace.d.ts +4 -0
  99. package/types/src/extensions/tiptap-extensions/Link/index.d.ts +2 -0
  100. package/types/src/extensions/tiptap-extensions/Link/link.d.ts +25 -0
  101. package/types/src/extensions/tiptap-extensions/Link/link.test.d.ts +1 -0
  102. package/types/src/i18n/locales/en.d.ts +2 -0
  103. package/types/src/i18n/locales/fa.d.ts +2 -0
  104. package/types/src/i18n/locales/sk.d.ts +2 -0
  105. package/types/src/schema/inlineContent/types.d.ts +1 -2
  106. package/dist/blocks-BEvIOzln.cjs.map +0 -1
  107. package/dist/blocks-DYl7UTS4.js.map +0 -1
  108. package/dist/en-C4ykW581.cjs.map +0 -1
  109. package/dist/en-QsSI16s5.js.map +0 -1
  110. package/dist/extensions-3nf9PPhA.js.map +0 -1
  111. package/dist/extensions-D9y6evn6.cjs +0 -2
  112. package/dist/extensions-D9y6evn6.cjs.map +0 -1
  113. package/dist/src-BKJH7Jla.cjs +0 -5
  114. package/dist/src-BKJH7Jla.cjs.map +0 -1
  115. package/dist/src-Hjv1f5lc.js.map +0 -1
@@ -1,4 +1,4 @@
1
- import { NodeSelection, TextSelection } from "prosemirror-state";
1
+ import { TextSelection } from "prosemirror-state";
2
2
 
3
3
  import {
4
4
  createExtension,
@@ -16,15 +16,6 @@ export const FormattingToolbarExtension = createExtension(({ editor }) => {
16
16
  return false;
17
17
  }
18
18
 
19
- // Don't show if a block with inline content is selected.
20
- if (
21
- tr.selection instanceof NodeSelection &&
22
- (tr.selection.node.type.spec.content === "inline*" ||
23
- tr.selection.node.firstChild?.type.spec.content === "inline*")
24
- ) {
25
- return false;
26
- }
27
-
28
19
  // Don't show if the selection is a text selection but contains no text.
29
20
  if (
30
21
  tr.selection instanceof TextSelection &&
@@ -61,16 +52,17 @@ export const FormattingToolbarExtension = createExtension(({ editor }) => {
61
52
  * We want to mimic the Notion behavior of not showing the toolbar while the user is holding down the mouse button (to create a selection)
62
53
  */
63
54
  let preventShowWhileMouseDown = false;
55
+ let preventShowWhileDragging = false;
64
56
 
65
57
  const unsubscribeOnChange = editor.onChange(() => {
66
- if (preventShowWhileMouseDown) {
58
+ if (preventShowWhileMouseDown || preventShowWhileDragging) {
67
59
  return;
68
60
  }
69
61
  // re-evaluate whether the toolbar should be shown
70
62
  store.setState(shouldShow());
71
63
  });
72
64
  const unsubscribeOnSelectionChange = editor.onSelectionChange(() => {
73
- if (preventShowWhileMouseDown) {
65
+ if (preventShowWhileMouseDown || preventShowWhileDragging) {
74
66
  return;
75
67
  }
76
68
  // re-evaluate whether the toolbar should be shown
@@ -91,6 +83,7 @@ export const FormattingToolbarExtension = createExtension(({ editor }) => {
91
83
  "pointerup",
92
84
  () => {
93
85
  preventShowWhileMouseDown = false;
86
+
94
87
  // We only want to re-show the toolbar if the mouse made the selection
95
88
  if (editor.isFocused()) {
96
89
  store.setState(shouldShow());
@@ -102,12 +95,26 @@ export const FormattingToolbarExtension = createExtension(({ editor }) => {
102
95
  dom.addEventListener(
103
96
  "pointercancel",
104
97
  () => {
105
- preventShowWhileMouseDown = false;
98
+ preventShowWhileMouseDown = true;
106
99
  },
107
- {
108
- signal,
109
- capture: true,
100
+ { signal, capture: true },
101
+ );
102
+
103
+ editor.prosemirrorView.root.addEventListener(
104
+ "dragstart",
105
+ () => {
106
+ preventShowWhileDragging = true;
107
+ store.setState(false);
108
+ },
109
+ { signal },
110
+ );
111
+
112
+ editor.prosemirrorView.root.addEventListener(
113
+ "dragend",
114
+ () => {
115
+ preventShowWhileDragging = false;
110
116
  },
117
+ { signal },
111
118
  );
112
119
 
113
120
  signal.addEventListener("abort", () => {
@@ -1,5 +1,4 @@
1
- import { getMarkRange, posToDOMRect } from "@tiptap/core";
2
- import { getPmSchema } from "../../api/pmUtil.js";
1
+ import { posToDOMRect } from "@tiptap/core";
3
2
  import { createExtension } from "../../editor/BlockNoteExtension.js";
4
3
 
5
4
  export const LinkToolbarExtension = createExtension(({ editor }) => {
@@ -14,47 +13,35 @@ export const LinkToolbarExtension = createExtension(({ editor }) => {
14
13
  return null;
15
14
  }
16
15
 
17
- function getMarkAtPos(pos: number, markType: string) {
18
- return editor.transact((tr) => {
19
- const resolvedPos = tr.doc.resolve(pos);
20
- const mark = resolvedPos
21
- .marks()
22
- .find((mark) => mark.type.name === markType);
23
-
24
- if (!mark) {
25
- return;
26
- }
27
-
28
- const markRange = getMarkRange(resolvedPos, mark.type);
29
- if (!markRange) {
30
- return;
31
- }
16
+ function getLinkAtPos(pos: number) {
17
+ const linkData = editor.getLinkMarkAtPos(pos);
18
+ if (!linkData) {
19
+ return undefined;
20
+ }
32
21
 
33
- return {
34
- range: markRange,
35
- mark,
36
- get text() {
37
- return tr.doc.textBetween(markRange.from, markRange.to);
38
- },
39
- get position() {
40
- // to minimize re-renders, we convert to JSON, which is the same shape anyway
41
- return posToDOMRect(
42
- editor.prosemirrorView,
43
- markRange.from,
44
- markRange.to,
45
- ).toJSON() as DOMRect;
46
- },
47
- };
48
- });
22
+ return {
23
+ range: { from: linkData.from, to: linkData.to },
24
+ // Expose mark-like attrs for backward compat with React LinkToolbarController
25
+ mark: { attrs: { href: linkData.href } },
26
+ get text() {
27
+ return linkData.text;
28
+ },
29
+ get position() {
30
+ return posToDOMRect(
31
+ editor.prosemirrorView,
32
+ linkData.from,
33
+ linkData.to,
34
+ ).toJSON() as DOMRect;
35
+ },
36
+ };
49
37
  }
50
38
 
51
39
  function getLinkAtSelection() {
52
40
  return editor.transact((tr) => {
53
- const selection = tr.selection;
54
- if (!selection.empty) {
41
+ if (!tr.selection.empty) {
55
42
  return undefined;
56
43
  }
57
- return getMarkAtPos(selection.anchor, "link");
44
+ return getLinkAtPos(tr.selection.anchor);
58
45
  });
59
46
  }
60
47
 
@@ -63,12 +50,14 @@ export const LinkToolbarExtension = createExtension(({ editor }) => {
63
50
 
64
51
  getLinkAtSelection,
65
52
  getLinkElementAtPos,
66
- getMarkAtPos,
53
+ getMarkAtPos(pos: number, _markType: string) {
54
+ return getLinkAtPos(pos);
55
+ },
67
56
 
68
57
  getLinkAtElement(element: HTMLElement) {
69
58
  return editor.transact(() => {
70
59
  const posAtElement = editor.prosemirrorView.posAtDOM(element, 0) + 1;
71
- return getMarkAtPos(posAtElement, "link");
60
+ return getLinkAtPos(posAtElement);
72
61
  });
73
62
  },
74
63
 
@@ -77,45 +66,11 @@ export const LinkToolbarExtension = createExtension(({ editor }) => {
77
66
  text: string,
78
67
  position = editor.transact((tr) => tr.selection.anchor),
79
68
  ) {
80
- editor.transact((tr) => {
81
- const pmSchema = getPmSchema(tr);
82
- const { range } = getMarkAtPos(position + 1, "link") || {
83
- range: {
84
- from: tr.selection.from,
85
- to: tr.selection.to,
86
- },
87
- };
88
- if (!range) {
89
- return;
90
- }
91
- tr.insertText(text, range.from, range.to);
92
- tr.addMark(
93
- range.from,
94
- range.from + text.length,
95
- pmSchema.mark("link", { href: url }),
96
- );
97
- });
98
- editor.prosemirrorView.focus();
69
+ editor.editLink(url, text, position);
99
70
  },
100
- deleteLink(position = editor.transact((tr) => tr.selection.anchor)) {
101
- editor.transact((tr) => {
102
- const pmSchema = getPmSchema(tr);
103
- const { range } = getMarkAtPos(position + 1, "link") || {
104
- range: {
105
- from: tr.selection.from,
106
- to: tr.selection.to,
107
- },
108
- };
109
- if (!range) {
110
- return;
111
- }
112
71
 
113
- tr.removeMark(range.from, range.to, pmSchema.marks["link"]).setMeta(
114
- "preventAutolink",
115
- true,
116
- );
117
- });
118
- editor.prosemirrorView.focus();
72
+ deleteLink(position = editor.transact((tr) => tr.selection.anchor)) {
73
+ editor.deleteLink(position);
119
74
  },
120
75
  } as const;
121
76
  });
@@ -1,6 +1,6 @@
1
1
  import { Plugin, PluginKey } from "prosemirror-state";
2
2
  import { Decoration, DecorationSet } from "prosemirror-view";
3
- import { v4 } from "uuid";
3
+ import { uuidv4 } from "lib0/random";
4
4
  import {
5
5
  createExtension,
6
6
  ExtensionOptions,
@@ -23,7 +23,7 @@ export const PlaceholderExtension = createExtension(
23
23
  new Plugin({
24
24
  key: PLUGIN_KEY,
25
25
  view: (view) => {
26
- const uniqueEditorSelector = `placeholder-selector-${v4()}`;
26
+ const uniqueEditorSelector = `placeholder-selector-${uuidv4()}`;
27
27
  view.dom.classList.add(uniqueEditorSelector);
28
28
  const styleEl = document.createElement("style");
29
29
 
@@ -785,8 +785,8 @@ export const SideMenuExtension = createExtension(({ editor }) => {
785
785
  * interfering with open submenus.
786
786
  */
787
787
  hideMenuIfNotFrozen() {
788
- if (!view!.menuFrozen && view!.state!.show) {
789
- view!.state!.show = false;
788
+ if (!view!.menuFrozen && view!.state?.show) {
789
+ view!.state.show = false;
790
790
  view!.emitUpdate(view!.state!);
791
791
  }
792
792
  },
@@ -466,6 +466,8 @@ export class TableHandlesView implements PluginView {
466
466
  event.preventDefault();
467
467
 
468
468
  const { draggingState, colIndex, rowIndex } = this.state;
469
+ // Clear so a re-dispatched drop short-circuits above (issue #2691).
470
+ this.state.draggingState = undefined;
469
471
 
470
472
  const columnWidths = this.state.block.content.columnWidths;
471
473
 
@@ -0,0 +1,179 @@
1
+ import type { NodeWithPos } from "@tiptap/core";
2
+ import {
3
+ combineTransactionSteps,
4
+ findChildrenInRange,
5
+ getChangedRanges,
6
+ getMarksBetween,
7
+ } from "@tiptap/core";
8
+ import type { MarkType } from "@tiptap/pm/model";
9
+ import { Plugin, PluginKey } from "@tiptap/pm/state";
10
+ import type { LinkMatch } from "./linkDetector.js";
11
+ import { tokenizeLink } from "./linkDetector.js";
12
+
13
+ import {
14
+ UNICODE_WHITESPACE_REGEX,
15
+ UNICODE_WHITESPACE_REGEX_END,
16
+ } from "./whitespace.js";
17
+
18
+ /**
19
+ * Check if the provided tokens form a valid link structure, which can either be a single link token
20
+ * or a link token surrounded by parentheses or square brackets.
21
+ */
22
+ function isValidLinkStructure(tokens: LinkMatch[]) {
23
+ if (tokens.length === 1) {
24
+ return tokens[0].isLink;
25
+ }
26
+
27
+ if (tokens.length === 3 && tokens[1].isLink) {
28
+ return ["()", "[]"].includes(tokens[0].value + tokens[2].value);
29
+ }
30
+
31
+ return false;
32
+ }
33
+
34
+ type AutolinkOptions = {
35
+ type: MarkType;
36
+ defaultProtocol: string;
37
+ validate: (url: string) => boolean;
38
+ shouldAutoLink: (url: string) => boolean;
39
+ };
40
+
41
+ /**
42
+ * Plugin that automatically adds link marks when typing URLs.
43
+ */
44
+ export function autolink(options: AutolinkOptions): Plugin {
45
+ return new Plugin({
46
+ key: new PluginKey("autolink"),
47
+ appendTransaction: (transactions, oldState, newState) => {
48
+ const docChanges =
49
+ transactions.some((transaction) => transaction.docChanged) &&
50
+ !oldState.doc.eq(newState.doc);
51
+
52
+ const preventAutolink = transactions.some((transaction) =>
53
+ transaction.getMeta("preventAutolink")
54
+ );
55
+
56
+ if (!docChanges || preventAutolink) {
57
+ return;
58
+ }
59
+
60
+ const { tr } = newState;
61
+ const transform = combineTransactionSteps(oldState.doc, [
62
+ ...transactions,
63
+ ]);
64
+ const changes = getChangedRanges(transform);
65
+
66
+ changes.forEach(({ newRange }) => {
67
+ const nodesInChangedRanges = findChildrenInRange(
68
+ newState.doc,
69
+ newRange,
70
+ (node) => node.isTextblock
71
+ );
72
+
73
+ let textBlock: NodeWithPos | undefined;
74
+ let textBeforeWhitespace: string | undefined;
75
+
76
+ if (nodesInChangedRanges.length > 1) {
77
+ textBlock = nodesInChangedRanges[0];
78
+ textBeforeWhitespace = newState.doc.textBetween(
79
+ textBlock.pos,
80
+ textBlock.pos + textBlock.node.nodeSize,
81
+ undefined,
82
+ " "
83
+ );
84
+ } else if (nodesInChangedRanges.length) {
85
+ const endText = newState.doc.textBetween(
86
+ newRange.from,
87
+ newRange.to,
88
+ " ",
89
+ " "
90
+ );
91
+ if (!UNICODE_WHITESPACE_REGEX_END.test(endText)) {
92
+ return;
93
+ }
94
+ textBlock = nodesInChangedRanges[0];
95
+ textBeforeWhitespace = newState.doc.textBetween(
96
+ textBlock.pos,
97
+ newRange.to,
98
+ undefined,
99
+ " "
100
+ );
101
+ }
102
+
103
+ if (textBlock && textBeforeWhitespace) {
104
+ const wordsBeforeWhitespace = textBeforeWhitespace
105
+ .split(UNICODE_WHITESPACE_REGEX)
106
+ .filter(Boolean);
107
+
108
+ if (wordsBeforeWhitespace.length <= 0) {
109
+ return;
110
+ }
111
+
112
+ const lastWordBeforeSpace =
113
+ wordsBeforeWhitespace[wordsBeforeWhitespace.length - 1];
114
+ const lastWordAndBlockOffset =
115
+ textBlock.pos +
116
+ textBeforeWhitespace.lastIndexOf(lastWordBeforeSpace);
117
+
118
+ if (!lastWordBeforeSpace) {
119
+ return;
120
+ }
121
+
122
+ const linksBeforeSpace = tokenizeLink(
123
+ lastWordBeforeSpace,
124
+ options.defaultProtocol
125
+ );
126
+
127
+ if (!isValidLinkStructure(linksBeforeSpace)) {
128
+ return;
129
+ }
130
+
131
+ linksBeforeSpace
132
+ .filter((link) => link.isLink)
133
+ .map((link) => ({
134
+ ...link,
135
+ from: lastWordAndBlockOffset + link.start + 1,
136
+ to: lastWordAndBlockOffset + link.end + 1,
137
+ }))
138
+ // ignore link inside code mark
139
+ .filter((link) => {
140
+ if (!newState.schema.marks.code) {
141
+ return true;
142
+ }
143
+
144
+ return !newState.doc.rangeHasMark(
145
+ link.from,
146
+ link.to,
147
+ newState.schema.marks.code
148
+ );
149
+ })
150
+ .filter((link) => options.validate(link.value))
151
+ .filter((link) => options.shouldAutoLink(link.value))
152
+ .forEach((link) => {
153
+ if (
154
+ getMarksBetween(link.from, link.to, newState.doc).some(
155
+ (item) => item.mark.type === options.type
156
+ )
157
+ ) {
158
+ return;
159
+ }
160
+
161
+ tr.addMark(
162
+ link.from,
163
+ link.to,
164
+ options.type.create({
165
+ href: link.href,
166
+ })
167
+ );
168
+ });
169
+ }
170
+ });
171
+
172
+ if (!tr.steps.length) {
173
+ return;
174
+ }
175
+
176
+ return tr;
177
+ },
178
+ });
179
+ }
@@ -0,0 +1,82 @@
1
+ import type { Editor } from "@tiptap/core";
2
+ import { getAttributes } from "@tiptap/core";
3
+ import type { MarkType } from "@tiptap/pm/model";
4
+ import { Plugin, PluginKey } from "@tiptap/pm/state";
5
+ import type { BlockNoteEditor } from "../../../../editor/BlockNoteEditor.js";
6
+
7
+ type ClickHandlerOptions = {
8
+ type: MarkType;
9
+ tiptapEditor: Editor;
10
+ editor?: BlockNoteEditor<any, any, any>;
11
+ onClick?: (
12
+ event: MouseEvent,
13
+ editor: BlockNoteEditor<any, any, any>,
14
+ ) => boolean | void;
15
+ };
16
+
17
+ export function clickHandler(options: ClickHandlerOptions): Plugin {
18
+ return new Plugin({
19
+ key: new PluginKey("handleClickLink"),
20
+ props: {
21
+ handleClick: (view, _pos, event) => {
22
+ if (event.button !== 0) {
23
+ return false;
24
+ }
25
+
26
+ if (!view.editable) {
27
+ return false;
28
+ }
29
+
30
+ let link: HTMLAnchorElement | null = null;
31
+
32
+ if (
33
+ event.target instanceof HTMLAnchorElement &&
34
+ // Differentiate between link inline content and read-only links.
35
+ event.target.getAttribute("data-inline-content-type") === "link"
36
+ ) {
37
+ link = event.target;
38
+ } else {
39
+ const target = event.target as HTMLElement | null;
40
+ if (!target) {
41
+ return false;
42
+ }
43
+
44
+ const root = options.tiptapEditor.view.dom;
45
+
46
+ // Intentionally limit the lookup to the editor root.
47
+ // Using tag names like DIV as boundaries breaks with custom NodeViews,
48
+ link = target.closest<HTMLAnchorElement>(
49
+ 'a[data-inline-content-type="link"]',
50
+ );
51
+
52
+ if (link && !root.contains(link)) {
53
+ link = null;
54
+ }
55
+ }
56
+
57
+ if (!link) {
58
+ return false;
59
+ }
60
+
61
+ if (options.onClick) {
62
+ if (!options.editor) {
63
+ throw new Error("BlockNoteEditor not found in Link click handler");
64
+ }
65
+ const result = options.onClick(event, options.editor);
66
+ return result ?? true;
67
+ }
68
+
69
+ const attrs = getAttributes(view.state, options.type.name);
70
+ const href = link.href ?? attrs.href;
71
+ const target = link.target ?? attrs.target;
72
+
73
+ if (href) {
74
+ window.open(href, target);
75
+ return true;
76
+ }
77
+
78
+ return false;
79
+ },
80
+ },
81
+ });
82
+ }