@tiptap/extension-link 3.0.0-next.0 → 3.0.0-next.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.
Files changed (175) hide show
  1. package/dist/index.cjs +338 -322
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/{packages/extension-link/src/link.d.ts → index.d.cts} +7 -5
  4. package/dist/index.d.ts +105 -0
  5. package/dist/index.js +321 -318
  6. package/dist/index.js.map +1 -1
  7. package/package.json +8 -10
  8. package/src/link.ts +4 -1
  9. package/dist/index.umd.js +0 -347
  10. package/dist/index.umd.js.map +0 -1
  11. package/dist/packages/core/src/CommandManager.d.ts +0 -20
  12. package/dist/packages/core/src/Editor.d.ts +0 -159
  13. package/dist/packages/core/src/EventEmitter.d.ts +0 -11
  14. package/dist/packages/core/src/Extension.d.ts +0 -343
  15. package/dist/packages/core/src/ExtensionManager.d.ts +0 -55
  16. package/dist/packages/core/src/InputRule.d.ts +0 -42
  17. package/dist/packages/core/src/Mark.d.ts +0 -451
  18. package/dist/packages/core/src/Node.d.ts +0 -611
  19. package/dist/packages/core/src/NodePos.d.ts +0 -44
  20. package/dist/packages/core/src/NodeView.d.ts +0 -31
  21. package/dist/packages/core/src/PasteRule.d.ts +0 -50
  22. package/dist/packages/core/src/Tracker.d.ts +0 -11
  23. package/dist/packages/core/src/commands/blur.d.ts +0 -13
  24. package/dist/packages/core/src/commands/clearContent.d.ts +0 -14
  25. package/dist/packages/core/src/commands/clearNodes.d.ts +0 -13
  26. package/dist/packages/core/src/commands/command.d.ts +0 -18
  27. package/dist/packages/core/src/commands/createParagraphNear.d.ts +0 -13
  28. package/dist/packages/core/src/commands/cut.d.ts +0 -20
  29. package/dist/packages/core/src/commands/deleteCurrentNode.d.ts +0 -13
  30. package/dist/packages/core/src/commands/deleteNode.d.ts +0 -15
  31. package/dist/packages/core/src/commands/deleteRange.d.ts +0 -14
  32. package/dist/packages/core/src/commands/deleteSelection.d.ts +0 -13
  33. package/dist/packages/core/src/commands/enter.d.ts +0 -13
  34. package/dist/packages/core/src/commands/exitCode.d.ts +0 -13
  35. package/dist/packages/core/src/commands/extendMarkRange.d.ts +0 -25
  36. package/dist/packages/core/src/commands/first.d.ts +0 -14
  37. package/dist/packages/core/src/commands/focus.d.ts +0 -27
  38. package/dist/packages/core/src/commands/forEach.d.ts +0 -14
  39. package/dist/packages/core/src/commands/index.d.ts +0 -55
  40. package/dist/packages/core/src/commands/insertContent.d.ts +0 -34
  41. package/dist/packages/core/src/commands/insertContentAt.d.ts +0 -47
  42. package/dist/packages/core/src/commands/join.d.ts +0 -41
  43. package/dist/packages/core/src/commands/joinItemBackward.d.ts +0 -13
  44. package/dist/packages/core/src/commands/joinItemForward.d.ts +0 -13
  45. package/dist/packages/core/src/commands/joinTextblockBackward.d.ts +0 -12
  46. package/dist/packages/core/src/commands/joinTextblockForward.d.ts +0 -12
  47. package/dist/packages/core/src/commands/keyboardShortcut.d.ts +0 -14
  48. package/dist/packages/core/src/commands/lift.d.ts +0 -17
  49. package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +0 -13
  50. package/dist/packages/core/src/commands/liftListItem.d.ts +0 -15
  51. package/dist/packages/core/src/commands/newlineInCode.d.ts +0 -13
  52. package/dist/packages/core/src/commands/resetAttributes.d.ts +0 -16
  53. package/dist/packages/core/src/commands/scrollIntoView.d.ts +0 -13
  54. package/dist/packages/core/src/commands/selectAll.d.ts +0 -13
  55. package/dist/packages/core/src/commands/selectNodeBackward.d.ts +0 -13
  56. package/dist/packages/core/src/commands/selectNodeForward.d.ts +0 -13
  57. package/dist/packages/core/src/commands/selectParentNode.d.ts +0 -13
  58. package/dist/packages/core/src/commands/selectTextblockEnd.d.ts +0 -13
  59. package/dist/packages/core/src/commands/selectTextblockStart.d.ts +0 -13
  60. package/dist/packages/core/src/commands/setContent.d.ts +0 -40
  61. package/dist/packages/core/src/commands/setMark.d.ts +0 -15
  62. package/dist/packages/core/src/commands/setMeta.d.ts +0 -15
  63. package/dist/packages/core/src/commands/setNode.d.ts +0 -16
  64. package/dist/packages/core/src/commands/setNodeSelection.d.ts +0 -14
  65. package/dist/packages/core/src/commands/setTextSelection.d.ts +0 -14
  66. package/dist/packages/core/src/commands/sinkListItem.d.ts +0 -15
  67. package/dist/packages/core/src/commands/splitBlock.d.ts +0 -17
  68. package/dist/packages/core/src/commands/splitListItem.d.ts +0 -15
  69. package/dist/packages/core/src/commands/toggleList.d.ts +0 -18
  70. package/dist/packages/core/src/commands/toggleMark.d.ts +0 -30
  71. package/dist/packages/core/src/commands/toggleNode.d.ts +0 -17
  72. package/dist/packages/core/src/commands/toggleWrap.d.ts +0 -16
  73. package/dist/packages/core/src/commands/undoInputRule.d.ts +0 -13
  74. package/dist/packages/core/src/commands/unsetAllMarks.d.ts +0 -13
  75. package/dist/packages/core/src/commands/unsetMark.d.ts +0 -25
  76. package/dist/packages/core/src/commands/updateAttributes.d.ts +0 -24
  77. package/dist/packages/core/src/commands/wrapIn.d.ts +0 -16
  78. package/dist/packages/core/src/commands/wrapInList.d.ts +0 -16
  79. package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +0 -5
  80. package/dist/packages/core/src/extensions/commands.d.ts +0 -3
  81. package/dist/packages/core/src/extensions/editable.d.ts +0 -2
  82. package/dist/packages/core/src/extensions/focusEvents.d.ts +0 -2
  83. package/dist/packages/core/src/extensions/index.d.ts +0 -6
  84. package/dist/packages/core/src/extensions/keymap.d.ts +0 -2
  85. package/dist/packages/core/src/extensions/tabindex.d.ts +0 -2
  86. package/dist/packages/core/src/helpers/combineTransactionSteps.d.ts +0 -10
  87. package/dist/packages/core/src/helpers/createChainableState.d.ts +0 -10
  88. package/dist/packages/core/src/helpers/createDocument.d.ts +0 -12
  89. package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +0 -15
  90. package/dist/packages/core/src/helpers/defaultBlockAt.d.ts +0 -7
  91. package/dist/packages/core/src/helpers/findChildren.d.ts +0 -9
  92. package/dist/packages/core/src/helpers/findChildrenInRange.d.ts +0 -10
  93. package/dist/packages/core/src/helpers/findParentNode.d.ts +0 -16
  94. package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +0 -17
  95. package/dist/packages/core/src/helpers/generateHTML.d.ts +0 -8
  96. package/dist/packages/core/src/helpers/generateJSON.d.ts +0 -8
  97. package/dist/packages/core/src/helpers/generateText.d.ts +0 -12
  98. package/dist/packages/core/src/helpers/getAttributes.d.ts +0 -9
  99. package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +0 -6
  100. package/dist/packages/core/src/helpers/getChangedRanges.d.ts +0 -11
  101. package/dist/packages/core/src/helpers/getDebugJSON.d.ts +0 -8
  102. package/dist/packages/core/src/helpers/getExtensionField.d.ts +0 -9
  103. package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +0 -2
  104. package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +0 -3
  105. package/dist/packages/core/src/helpers/getMarkRange.d.ts +0 -3
  106. package/dist/packages/core/src/helpers/getMarkType.d.ts +0 -2
  107. package/dist/packages/core/src/helpers/getMarksBetween.d.ts +0 -3
  108. package/dist/packages/core/src/helpers/getNodeAtPosition.d.ts +0 -11
  109. package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +0 -3
  110. package/dist/packages/core/src/helpers/getNodeType.d.ts +0 -2
  111. package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +0 -3
  112. package/dist/packages/core/src/helpers/getSchema.d.ts +0 -4
  113. package/dist/packages/core/src/helpers/getSchemaByResolvedExtensions.d.ts +0 -10
  114. package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +0 -8
  115. package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +0 -8
  116. package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +0 -9
  117. package/dist/packages/core/src/helpers/getText.d.ts +0 -15
  118. package/dist/packages/core/src/helpers/getTextBetween.d.ts +0 -14
  119. package/dist/packages/core/src/helpers/getTextContentFromNodes.d.ts +0 -8
  120. package/dist/packages/core/src/helpers/getTextSerializersFromSchema.d.ts +0 -8
  121. package/dist/packages/core/src/helpers/index.d.ts +0 -50
  122. package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +0 -9
  123. package/dist/packages/core/src/helpers/isActive.d.ts +0 -2
  124. package/dist/packages/core/src/helpers/isAtEndOfNode.d.ts +0 -2
  125. package/dist/packages/core/src/helpers/isAtStartOfNode.d.ts +0 -2
  126. package/dist/packages/core/src/helpers/isExtensionRulesEnabled.d.ts +0 -2
  127. package/dist/packages/core/src/helpers/isList.d.ts +0 -2
  128. package/dist/packages/core/src/helpers/isMarkActive.d.ts +0 -3
  129. package/dist/packages/core/src/helpers/isNodeActive.d.ts +0 -3
  130. package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +0 -8
  131. package/dist/packages/core/src/helpers/isNodeSelection.d.ts +0 -2
  132. package/dist/packages/core/src/helpers/isTextSelection.d.ts +0 -2
  133. package/dist/packages/core/src/helpers/posToDOMRect.d.ts +0 -2
  134. package/dist/packages/core/src/helpers/resolveFocusPosition.d.ts +0 -4
  135. package/dist/packages/core/src/helpers/selectionToInsertionEnd.d.ts +0 -2
  136. package/dist/packages/core/src/helpers/splitExtensions.d.ts +0 -9
  137. package/dist/packages/core/src/index.d.ts +0 -24
  138. package/dist/packages/core/src/inputRules/index.d.ts +0 -5
  139. package/dist/packages/core/src/inputRules/markInputRule.d.ts +0 -13
  140. package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +0 -23
  141. package/dist/packages/core/src/inputRules/textInputRule.d.ts +0 -10
  142. package/dist/packages/core/src/inputRules/textblockTypeInputRule.d.ts +0 -15
  143. package/dist/packages/core/src/inputRules/wrappingInputRule.d.ts +0 -28
  144. package/dist/packages/core/src/pasteRules/index.d.ts +0 -3
  145. package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +0 -13
  146. package/dist/packages/core/src/pasteRules/nodePasteRule.d.ts +0 -13
  147. package/dist/packages/core/src/pasteRules/textPasteRule.d.ts +0 -10
  148. package/dist/packages/core/src/style.d.ts +0 -1
  149. package/dist/packages/core/src/types.d.ts +0 -253
  150. package/dist/packages/core/src/utilities/callOrReturn.d.ts +0 -9
  151. package/dist/packages/core/src/utilities/createStyleTag.d.ts +0 -1
  152. package/dist/packages/core/src/utilities/deleteProps.d.ts +0 -6
  153. package/dist/packages/core/src/utilities/elementFromString.d.ts +0 -1
  154. package/dist/packages/core/src/utilities/escapeForRegEx.d.ts +0 -1
  155. package/dist/packages/core/src/utilities/findDuplicates.d.ts +0 -1
  156. package/dist/packages/core/src/utilities/fromString.d.ts +0 -1
  157. package/dist/packages/core/src/utilities/index.d.ts +0 -20
  158. package/dist/packages/core/src/utilities/isAndroid.d.ts +0 -1
  159. package/dist/packages/core/src/utilities/isEmptyObject.d.ts +0 -1
  160. package/dist/packages/core/src/utilities/isFunction.d.ts +0 -1
  161. package/dist/packages/core/src/utilities/isMacOS.d.ts +0 -1
  162. package/dist/packages/core/src/utilities/isNumber.d.ts +0 -1
  163. package/dist/packages/core/src/utilities/isPlainObject.d.ts +0 -1
  164. package/dist/packages/core/src/utilities/isRegExp.d.ts +0 -1
  165. package/dist/packages/core/src/utilities/isString.d.ts +0 -1
  166. package/dist/packages/core/src/utilities/isiOS.d.ts +0 -1
  167. package/dist/packages/core/src/utilities/mergeAttributes.d.ts +0 -1
  168. package/dist/packages/core/src/utilities/mergeDeep.d.ts +0 -1
  169. package/dist/packages/core/src/utilities/minMax.d.ts +0 -1
  170. package/dist/packages/core/src/utilities/objectIncludes.d.ts +0 -8
  171. package/dist/packages/core/src/utilities/removeDuplicates.d.ts +0 -8
  172. package/dist/packages/extension-link/src/helpers/autolink.d.ts +0 -14
  173. package/dist/packages/extension-link/src/helpers/clickHandler.d.ts +0 -7
  174. package/dist/packages/extension-link/src/helpers/pasteHandler.d.ts +0 -10
  175. package/dist/packages/extension-link/src/index.d.ts +0 -3
package/dist/index.js CHANGED
@@ -1,339 +1,342 @@
1
- import { combineTransactionSteps, getChangedRanges, findChildrenInRange, getMarksBetween, getAttributes, Mark, mergeAttributes, markPasteRule } from '@tiptap/core';
2
- import { tokenize, find, registerCustomProtocol, reset } from 'linkifyjs';
3
- import { Plugin, PluginKey } from '@tiptap/pm/state';
1
+ // src/link.ts
2
+ import {
3
+ Mark,
4
+ markPasteRule,
5
+ mergeAttributes
6
+ } from "@tiptap/core";
7
+ import { find as find2, registerCustomProtocol, reset } from "linkifyjs";
4
8
 
5
- /**
6
- * Check if the provided tokens form a valid link structure, which can either be a single link token
7
- * or a link token surrounded by parentheses or square brackets.
8
- *
9
- * This ensures that only complete and valid text is hyperlinked, preventing cases where a valid
10
- * top-level domain (TLD) is immediately followed by an invalid character, like a number. For
11
- * example, with the `find` method from Linkify, entering `example.com1` would result in
12
- * `example.com` being linked and the trailing `1` left as plain text. By using the `tokenize`
13
- * method, we can perform more comprehensive validation on the input text.
14
- */
9
+ // src/helpers/autolink.ts
10
+ import {
11
+ combineTransactionSteps,
12
+ findChildrenInRange,
13
+ getChangedRanges,
14
+ getMarksBetween
15
+ } from "@tiptap/core";
16
+ import { Plugin, PluginKey } from "@tiptap/pm/state";
17
+ import { tokenize } from "linkifyjs";
15
18
  function isValidLinkStructure(tokens) {
16
- if (tokens.length === 1) {
17
- return tokens[0].isLink;
18
- }
19
- if (tokens.length === 3 && tokens[1].isLink) {
20
- return ['()', '[]'].includes(tokens[0].value + tokens[2].value);
21
- }
22
- return false;
19
+ if (tokens.length === 1) {
20
+ return tokens[0].isLink;
21
+ }
22
+ if (tokens.length === 3 && tokens[1].isLink) {
23
+ return ["()", "[]"].includes(tokens[0].value + tokens[2].value);
24
+ }
25
+ return false;
23
26
  }
24
- /**
25
- * This plugin allows you to automatically add links to your editor.
26
- * @param options The plugin options
27
- * @returns The plugin instance
28
- */
29
27
  function autolink(options) {
30
- return new Plugin({
31
- key: new PluginKey('autolink'),
32
- appendTransaction: (transactions, oldState, newState) => {
33
- /**
34
- * Does the transaction change the document?
35
- */
36
- const docChanges = transactions.some(transaction => transaction.docChanged) && !oldState.doc.eq(newState.doc);
37
- /**
38
- * Prevent autolink if the transaction is not a document change or if the transaction has the meta `preventAutolink`.
39
- */
40
- const preventAutolink = transactions.some(transaction => transaction.getMeta('preventAutolink'));
41
- /**
42
- * Prevent autolink if the transaction is not a document change
43
- * or if the transaction has the meta `preventAutolink`.
44
- */
45
- if (!docChanges || preventAutolink) {
46
- return;
28
+ return new Plugin({
29
+ key: new PluginKey("autolink"),
30
+ appendTransaction: (transactions, oldState, newState) => {
31
+ const docChanges = transactions.some((transaction) => transaction.docChanged) && !oldState.doc.eq(newState.doc);
32
+ const preventAutolink = transactions.some((transaction) => transaction.getMeta("preventAutolink"));
33
+ if (!docChanges || preventAutolink) {
34
+ return;
35
+ }
36
+ const { tr } = newState;
37
+ const transform = combineTransactionSteps(oldState.doc, [...transactions]);
38
+ const changes = getChangedRanges(transform);
39
+ changes.forEach(({ newRange }) => {
40
+ const nodesInChangedRanges = findChildrenInRange(
41
+ newState.doc,
42
+ newRange,
43
+ (node) => node.isTextblock
44
+ );
45
+ let textBlock;
46
+ let textBeforeWhitespace;
47
+ if (nodesInChangedRanges.length > 1) {
48
+ textBlock = nodesInChangedRanges[0];
49
+ textBeforeWhitespace = newState.doc.textBetween(
50
+ textBlock.pos,
51
+ textBlock.pos + textBlock.node.nodeSize,
52
+ void 0,
53
+ " "
54
+ );
55
+ } else if (nodesInChangedRanges.length && newState.doc.textBetween(newRange.from, newRange.to, " ", " ").endsWith(" ")) {
56
+ textBlock = nodesInChangedRanges[0];
57
+ textBeforeWhitespace = newState.doc.textBetween(
58
+ textBlock.pos,
59
+ newRange.to,
60
+ void 0,
61
+ " "
62
+ );
63
+ }
64
+ if (textBlock && textBeforeWhitespace) {
65
+ const wordsBeforeWhitespace = textBeforeWhitespace.split(" ").filter((s) => s !== "");
66
+ if (wordsBeforeWhitespace.length <= 0) {
67
+ return false;
68
+ }
69
+ const lastWordBeforeSpace = wordsBeforeWhitespace[wordsBeforeWhitespace.length - 1];
70
+ const lastWordAndBlockOffset = textBlock.pos + textBeforeWhitespace.lastIndexOf(lastWordBeforeSpace);
71
+ if (!lastWordBeforeSpace) {
72
+ return false;
73
+ }
74
+ const linksBeforeSpace = tokenize(lastWordBeforeSpace).map((t) => t.toObject(options.defaultProtocol));
75
+ if (!isValidLinkStructure(linksBeforeSpace)) {
76
+ return false;
77
+ }
78
+ linksBeforeSpace.filter((link) => link.isLink).map((link) => ({
79
+ ...link,
80
+ from: lastWordAndBlockOffset + link.start + 1,
81
+ to: lastWordAndBlockOffset + link.end + 1
82
+ })).filter((link) => {
83
+ if (!newState.schema.marks.code) {
84
+ return true;
47
85
  }
48
- const { tr } = newState;
49
- const transform = combineTransactionSteps(oldState.doc, [...transactions]);
50
- const changes = getChangedRanges(transform);
51
- changes.forEach(({ newRange }) => {
52
- // Now let’s see if we can add new links.
53
- const nodesInChangedRanges = findChildrenInRange(newState.doc, newRange, node => node.isTextblock);
54
- let textBlock;
55
- let textBeforeWhitespace;
56
- if (nodesInChangedRanges.length > 1) {
57
- // Grab the first node within the changed ranges (ex. the first of two paragraphs when hitting enter).
58
- textBlock = nodesInChangedRanges[0];
59
- textBeforeWhitespace = newState.doc.textBetween(textBlock.pos, textBlock.pos + textBlock.node.nodeSize, undefined, ' ');
60
- }
61
- else if (nodesInChangedRanges.length
62
- // We want to make sure to include the block seperator argument to treat hard breaks like spaces.
63
- && newState.doc.textBetween(newRange.from, newRange.to, ' ', ' ').endsWith(' ')) {
64
- textBlock = nodesInChangedRanges[0];
65
- textBeforeWhitespace = newState.doc.textBetween(textBlock.pos, newRange.to, undefined, ' ');
66
- }
67
- if (textBlock && textBeforeWhitespace) {
68
- const wordsBeforeWhitespace = textBeforeWhitespace.split(' ').filter(s => s !== '');
69
- if (wordsBeforeWhitespace.length <= 0) {
70
- return false;
71
- }
72
- const lastWordBeforeSpace = wordsBeforeWhitespace[wordsBeforeWhitespace.length - 1];
73
- const lastWordAndBlockOffset = textBlock.pos + textBeforeWhitespace.lastIndexOf(lastWordBeforeSpace);
74
- if (!lastWordBeforeSpace) {
75
- return false;
76
- }
77
- const linksBeforeSpace = tokenize(lastWordBeforeSpace).map(t => t.toObject(options.defaultProtocol));
78
- if (!isValidLinkStructure(linksBeforeSpace)) {
79
- return false;
80
- }
81
- linksBeforeSpace
82
- .filter(link => link.isLink)
83
- // Calculate link position.
84
- .map(link => ({
85
- ...link,
86
- from: lastWordAndBlockOffset + link.start + 1,
87
- to: lastWordAndBlockOffset + link.end + 1,
88
- }))
89
- // ignore link inside code mark
90
- .filter(link => {
91
- if (!newState.schema.marks.code) {
92
- return true;
93
- }
94
- return !newState.doc.rangeHasMark(link.from, link.to, newState.schema.marks.code);
95
- })
96
- // validate link
97
- .filter(link => options.validate(link.value))
98
- // Add link mark.
99
- .forEach(link => {
100
- if (getMarksBetween(link.from, link.to, newState.doc).some(item => item.mark.type === options.type)) {
101
- return;
102
- }
103
- tr.addMark(link.from, link.to, options.type.create({
104
- href: link.href,
105
- }));
106
- });
107
- }
108
- });
109
- if (!tr.steps.length) {
110
- return;
86
+ return !newState.doc.rangeHasMark(
87
+ link.from,
88
+ link.to,
89
+ newState.schema.marks.code
90
+ );
91
+ }).filter((link) => options.validate(link.value)).forEach((link) => {
92
+ if (getMarksBetween(link.from, link.to, newState.doc).some((item) => item.mark.type === options.type)) {
93
+ return;
111
94
  }
112
- return tr;
113
- },
114
- });
95
+ tr.addMark(
96
+ link.from,
97
+ link.to,
98
+ options.type.create({
99
+ href: link.href
100
+ })
101
+ );
102
+ });
103
+ }
104
+ });
105
+ if (!tr.steps.length) {
106
+ return;
107
+ }
108
+ return tr;
109
+ }
110
+ });
115
111
  }
116
112
 
113
+ // src/helpers/clickHandler.ts
114
+ import { getAttributes } from "@tiptap/core";
115
+ import { Plugin as Plugin2, PluginKey as PluginKey2 } from "@tiptap/pm/state";
117
116
  function clickHandler(options) {
118
- return new Plugin({
119
- key: new PluginKey('handleClickLink'),
120
- props: {
121
- handleClick: (view, pos, event) => {
122
- var _a, _b;
123
- if (event.button !== 0) {
124
- return false;
125
- }
126
- if (!view.editable) {
127
- return false;
128
- }
129
- let a = event.target;
130
- const els = [];
131
- while (a.nodeName !== 'DIV') {
132
- els.push(a);
133
- a = a.parentNode;
134
- }
135
- if (!els.find(value => value.nodeName === 'A')) {
136
- return false;
137
- }
138
- const attrs = getAttributes(view.state, options.type.name);
139
- const link = event.target;
140
- const href = (_a = link === null || link === void 0 ? void 0 : link.href) !== null && _a !== void 0 ? _a : attrs.href;
141
- const target = (_b = link === null || link === void 0 ? void 0 : link.target) !== null && _b !== void 0 ? _b : attrs.target;
142
- if (link && href) {
143
- window.open(href, target);
144
- return true;
145
- }
146
- return false;
147
- },
148
- },
149
- });
117
+ return new Plugin2({
118
+ key: new PluginKey2("handleClickLink"),
119
+ props: {
120
+ handleClick: (view, pos, event) => {
121
+ var _a, _b;
122
+ if (event.button !== 0) {
123
+ return false;
124
+ }
125
+ if (!view.editable) {
126
+ return false;
127
+ }
128
+ let a = event.target;
129
+ const els = [];
130
+ while (a.nodeName !== "DIV") {
131
+ els.push(a);
132
+ a = a.parentNode;
133
+ }
134
+ if (!els.find((value) => value.nodeName === "A")) {
135
+ return false;
136
+ }
137
+ const attrs = getAttributes(view.state, options.type.name);
138
+ const link = event.target;
139
+ const href = (_a = link == null ? void 0 : link.href) != null ? _a : attrs.href;
140
+ const target = (_b = link == null ? void 0 : link.target) != null ? _b : attrs.target;
141
+ if (link && href) {
142
+ window.open(href, target);
143
+ return true;
144
+ }
145
+ return false;
146
+ }
147
+ }
148
+ });
150
149
  }
151
150
 
151
+ // src/helpers/pasteHandler.ts
152
+ import { Plugin as Plugin3, PluginKey as PluginKey3 } from "@tiptap/pm/state";
153
+ import { find } from "linkifyjs";
152
154
  function pasteHandler(options) {
153
- return new Plugin({
154
- key: new PluginKey('handlePasteLink'),
155
- props: {
156
- handlePaste: (view, event, slice) => {
157
- const { state } = view;
158
- const { selection } = state;
159
- const { empty } = selection;
160
- if (empty) {
161
- return false;
162
- }
163
- let textContent = '';
164
- slice.content.forEach(node => {
165
- textContent += node.textContent;
166
- });
167
- const link = find(textContent, { defaultProtocol: options.defaultProtocol }).find(item => item.isLink && item.value === textContent);
168
- if (!textContent || !link) {
169
- return false;
170
- }
171
- options.editor.commands.setMark(options.type, {
172
- href: link.href,
173
- });
174
- return true;
175
- },
176
- },
177
- });
155
+ return new Plugin3({
156
+ key: new PluginKey3("handlePasteLink"),
157
+ props: {
158
+ handlePaste: (view, event, slice) => {
159
+ const { state } = view;
160
+ const { selection } = state;
161
+ const { empty } = selection;
162
+ if (empty) {
163
+ return false;
164
+ }
165
+ let textContent = "";
166
+ slice.content.forEach((node) => {
167
+ textContent += node.textContent;
168
+ });
169
+ const link = find(textContent, { defaultProtocol: options.defaultProtocol }).find((item) => item.isLink && item.value === textContent);
170
+ if (!textContent || !link) {
171
+ return false;
172
+ }
173
+ options.editor.commands.setMark(options.type, {
174
+ href: link.href
175
+ });
176
+ return true;
177
+ }
178
+ }
179
+ });
178
180
  }
179
181
 
180
- const pasteRegex = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)/gi;
181
- // From DOMPurify
182
- // https://github.com/cure53/DOMPurify/blob/main/src/regexp.js
183
- const ATTR_WHITESPACE = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g; // eslint-disable-line no-control-regex
184
- const IS_ALLOWED_URI = /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i; // eslint-disable-line no-useless-escape
182
+ // src/link.ts
183
+ var pasteRegex = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,}\b(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)(?:[-a-zA-Z0-9@:%._+~#=?!&/]*)/gi;
184
+ var ATTR_WHITESPACE = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g;
185
+ var IS_ALLOWED_URI = /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i;
185
186
  function isAllowedUri(uri) {
186
- return !uri || uri.replace(ATTR_WHITESPACE, '').match(IS_ALLOWED_URI);
187
+ return !uri || uri.replace(ATTR_WHITESPACE, "").match(IS_ALLOWED_URI);
187
188
  }
188
- /**
189
- * This extension allows you to create links.
190
- * @see https://www.tiptap.dev/api/marks/link
191
- */
192
- const Link = Mark.create({
193
- name: 'link',
194
- priority: 1000,
195
- keepOnSplit: false,
196
- exitable: true,
197
- onCreate() {
198
- this.options.protocols.forEach(protocol => {
199
- if (typeof protocol === 'string') {
200
- registerCustomProtocol(protocol);
201
- return;
202
- }
203
- registerCustomProtocol(protocol.scheme, protocol.optionalSlashes);
204
- });
205
- },
206
- onDestroy() {
207
- reset();
208
- },
209
- inclusive() {
210
- return this.options.autolink;
211
- },
212
- addOptions() {
213
- return {
214
- openOnClick: true,
215
- linkOnPaste: true,
216
- autolink: true,
217
- protocols: [],
218
- defaultProtocol: 'http',
219
- HTMLAttributes: {
220
- target: '_blank',
221
- rel: 'noopener noreferrer nofollow',
222
- class: null,
223
- },
224
- validate: url => !!url,
225
- };
226
- },
227
- addAttributes() {
228
- return {
229
- href: {
230
- default: null,
231
- },
232
- target: {
233
- default: this.options.HTMLAttributes.target,
234
- },
235
- rel: {
236
- default: this.options.HTMLAttributes.rel,
237
- },
238
- class: {
239
- default: this.options.HTMLAttributes.class,
240
- },
241
- };
242
- },
243
- parseHTML() {
244
- return [{
245
- tag: 'a[href]',
246
- getAttrs: dom => {
247
- const href = dom.getAttribute('href');
248
- // prevent XSS attacks
249
- if (!href || !isAllowedUri(href)) {
250
- return false;
251
- }
252
- return { href };
253
- },
254
- }];
255
- },
256
- renderHTML({ HTMLAttributes }) {
257
- // prevent XSS attacks
258
- if (!isAllowedUri(HTMLAttributes.href)) {
259
- // strip out the href
260
- return ['a', mergeAttributes(this.options.HTMLAttributes, { ...HTMLAttributes, href: '' }), 0];
261
- }
262
- return ['a', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
263
- },
264
- addCommands() {
265
- return {
266
- setLink: attributes => ({ chain }) => {
267
- return chain().setMark(this.name, attributes).setMeta('preventAutolink', true).run();
268
- },
269
- toggleLink: attributes => ({ chain }) => {
270
- return chain()
271
- .toggleMark(this.name, attributes, { extendEmptyMarkRange: true })
272
- .setMeta('preventAutolink', true)
273
- .run();
274
- },
275
- unsetLink: () => ({ chain }) => {
276
- return chain()
277
- .unsetMark(this.name, { extendEmptyMarkRange: true })
278
- .setMeta('preventAutolink', true)
279
- .run();
280
- },
281
- };
282
- },
283
- addPasteRules() {
284
- return [
285
- markPasteRule({
286
- find: text => {
287
- const foundLinks = [];
288
- if (text) {
289
- const { validate } = this.options;
290
- const links = find(text).filter(item => item.isLink && validate(item.value));
291
- if (links.length) {
292
- links.forEach(link => (foundLinks.push({
293
- text: link.value,
294
- data: {
295
- href: link.href,
296
- },
297
- index: link.start,
298
- })));
299
- }
300
- }
301
- return foundLinks;
302
- },
303
- type: this.type,
304
- getAttributes: match => {
305
- var _a;
306
- return {
307
- href: (_a = match.data) === null || _a === void 0 ? void 0 : _a.href,
308
- };
309
- },
310
- }),
311
- ];
312
- },
313
- addProseMirrorPlugins() {
314
- const plugins = [];
315
- if (this.options.autolink) {
316
- plugins.push(autolink({
317
- type: this.type,
318
- defaultProtocol: this.options.defaultProtocol,
319
- validate: this.options.validate,
320
- }));
189
+ var Link = Mark.create({
190
+ name: "link",
191
+ priority: 1e3,
192
+ keepOnSplit: false,
193
+ exitable: true,
194
+ onCreate() {
195
+ this.options.protocols.forEach((protocol) => {
196
+ if (typeof protocol === "string") {
197
+ registerCustomProtocol(protocol);
198
+ return;
199
+ }
200
+ registerCustomProtocol(protocol.scheme, protocol.optionalSlashes);
201
+ });
202
+ },
203
+ onDestroy() {
204
+ reset();
205
+ },
206
+ inclusive() {
207
+ return this.options.autolink;
208
+ },
209
+ addOptions() {
210
+ return {
211
+ openOnClick: true,
212
+ linkOnPaste: true,
213
+ autolink: true,
214
+ protocols: [],
215
+ defaultProtocol: "http",
216
+ HTMLAttributes: {
217
+ target: "_blank",
218
+ rel: "noopener noreferrer nofollow",
219
+ class: null
220
+ },
221
+ validate: (url) => !!url
222
+ };
223
+ },
224
+ addAttributes() {
225
+ return {
226
+ href: {
227
+ default: null,
228
+ parseHTML(element) {
229
+ return element.getAttribute("href");
321
230
  }
322
- if (this.options.openOnClick === true) {
323
- plugins.push(clickHandler({
324
- type: this.type,
325
- }));
231
+ },
232
+ target: {
233
+ default: this.options.HTMLAttributes.target
234
+ },
235
+ rel: {
236
+ default: this.options.HTMLAttributes.rel
237
+ },
238
+ class: {
239
+ default: this.options.HTMLAttributes.class
240
+ }
241
+ };
242
+ },
243
+ parseHTML() {
244
+ return [{
245
+ tag: "a[href]",
246
+ getAttrs: (dom) => {
247
+ const href = dom.getAttribute("href");
248
+ if (!href || !isAllowedUri(href)) {
249
+ return false;
326
250
  }
327
- if (this.options.linkOnPaste) {
328
- plugins.push(pasteHandler({
329
- editor: this.editor,
330
- defaultProtocol: this.options.defaultProtocol,
331
- type: this.type,
332
- }));
251
+ return null;
252
+ }
253
+ }];
254
+ },
255
+ renderHTML({ HTMLAttributes }) {
256
+ if (!isAllowedUri(HTMLAttributes.href)) {
257
+ return ["a", mergeAttributes(this.options.HTMLAttributes, { ...HTMLAttributes, href: "" }), 0];
258
+ }
259
+ return ["a", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
260
+ },
261
+ addCommands() {
262
+ return {
263
+ setLink: (attributes) => ({ chain }) => {
264
+ return chain().setMark(this.name, attributes).setMeta("preventAutolink", true).run();
265
+ },
266
+ toggleLink: (attributes) => ({ chain }) => {
267
+ return chain().toggleMark(this.name, attributes, { extendEmptyMarkRange: true }).setMeta("preventAutolink", true).run();
268
+ },
269
+ unsetLink: () => ({ chain }) => {
270
+ return chain().unsetMark(this.name, { extendEmptyMarkRange: true }).setMeta("preventAutolink", true).run();
271
+ }
272
+ };
273
+ },
274
+ addPasteRules() {
275
+ return [
276
+ markPasteRule({
277
+ find: (text) => {
278
+ const foundLinks = [];
279
+ if (text) {
280
+ const { validate } = this.options;
281
+ const links = find2(text).filter((item) => item.isLink && validate(item.value));
282
+ if (links.length) {
283
+ links.forEach((link) => foundLinks.push({
284
+ text: link.value,
285
+ data: {
286
+ href: link.href
287
+ },
288
+ index: link.start
289
+ }));
290
+ }
291
+ }
292
+ return foundLinks;
293
+ },
294
+ type: this.type,
295
+ getAttributes: (match) => {
296
+ var _a;
297
+ return {
298
+ href: (_a = match.data) == null ? void 0 : _a.href
299
+ };
333
300
  }
334
- return plugins;
335
- },
301
+ })
302
+ ];
303
+ },
304
+ addProseMirrorPlugins() {
305
+ const plugins = [];
306
+ if (this.options.autolink) {
307
+ plugins.push(
308
+ autolink({
309
+ type: this.type,
310
+ defaultProtocol: this.options.defaultProtocol,
311
+ validate: this.options.validate
312
+ })
313
+ );
314
+ }
315
+ if (this.options.openOnClick === true) {
316
+ plugins.push(
317
+ clickHandler({
318
+ type: this.type
319
+ })
320
+ );
321
+ }
322
+ if (this.options.linkOnPaste) {
323
+ plugins.push(
324
+ pasteHandler({
325
+ editor: this.editor,
326
+ defaultProtocol: this.options.defaultProtocol,
327
+ type: this.type
328
+ })
329
+ );
330
+ }
331
+ return plugins;
332
+ }
336
333
  });
337
334
 
338
- export { Link, Link as default, pasteRegex };
339
- //# sourceMappingURL=index.js.map
335
+ // src/index.ts
336
+ var src_default = Link;
337
+ export {
338
+ Link,
339
+ src_default as default,
340
+ pasteRegex
341
+ };
342
+ //# sourceMappingURL=index.js.map