@tiptap/extension-link 2.0.0-beta.21 → 2.0.0-beta.211
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +277 -0
- package/dist/{packages/extension-link/src/link.d.ts → index.d.ts} +57 -40
- package/dist/index.js +277 -0
- package/package.json +33 -9
- package/src/helpers/autolink.ts +142 -0
- package/src/helpers/clickHandler.ts +27 -0
- package/src/helpers/pasteHandler.ts +44 -0
- package/src/link.ts +99 -82
- package/LICENSE.md +0 -21
- package/dist/packages/extension-link/src/index.d.ts +0 -3
- package/dist/tiptap-extension-link.cjs.js +0 -125
- package/dist/tiptap-extension-link.cjs.js.map +0 -1
- package/dist/tiptap-extension-link.esm.js +0 -120
- package/dist/tiptap-extension-link.esm.js.map +0 -1
- package/dist/tiptap-extension-link.umd.js +0 -127
- package/dist/tiptap-extension-link.umd.js.map +0 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/link.ts
|
|
2
|
+
var _core = require('@tiptap/core');
|
|
3
|
+
var _linkifyjs = require('linkifyjs');
|
|
4
|
+
|
|
5
|
+
// src/helpers/autolink.ts
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
var _state = require('@tiptap/pm/state');
|
|
13
|
+
|
|
14
|
+
function autolink(options) {
|
|
15
|
+
return new (0, _state.Plugin)({
|
|
16
|
+
key: new (0, _state.PluginKey)("autolink"),
|
|
17
|
+
appendTransaction: (transactions, oldState, newState) => {
|
|
18
|
+
const docChanges = transactions.some((transaction) => transaction.docChanged) && !oldState.doc.eq(newState.doc);
|
|
19
|
+
const preventAutolink = transactions.some((transaction) => transaction.getMeta("preventAutolink"));
|
|
20
|
+
if (!docChanges || preventAutolink) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const { tr } = newState;
|
|
24
|
+
const transform = _core.combineTransactionSteps.call(void 0, oldState.doc, [...transactions]);
|
|
25
|
+
const { mapping } = transform;
|
|
26
|
+
const changes = _core.getChangedRanges.call(void 0, transform);
|
|
27
|
+
changes.forEach(({ oldRange, newRange }) => {
|
|
28
|
+
_core.getMarksBetween.call(void 0, oldRange.from, oldRange.to, oldState.doc).filter((item) => item.mark.type === options.type).forEach((oldMark) => {
|
|
29
|
+
const newFrom = mapping.map(oldMark.from);
|
|
30
|
+
const newTo = mapping.map(oldMark.to);
|
|
31
|
+
const newMarks = _core.getMarksBetween.call(void 0, newFrom, newTo, newState.doc).filter(
|
|
32
|
+
(item) => item.mark.type === options.type
|
|
33
|
+
);
|
|
34
|
+
if (!newMarks.length) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const newMark = newMarks[0];
|
|
38
|
+
const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, void 0, " ");
|
|
39
|
+
const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, void 0, " ");
|
|
40
|
+
const wasLink = _linkifyjs.test.call(void 0, oldLinkText);
|
|
41
|
+
const isLink = _linkifyjs.test.call(void 0, newLinkText);
|
|
42
|
+
if (wasLink && !isLink) {
|
|
43
|
+
tr.removeMark(newMark.from, newMark.to, options.type);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const nodesInChangedRanges = _core.findChildrenInRange.call(void 0,
|
|
47
|
+
newState.doc,
|
|
48
|
+
newRange,
|
|
49
|
+
(node) => node.isTextblock
|
|
50
|
+
);
|
|
51
|
+
let textBlock;
|
|
52
|
+
let textBeforeWhitespace;
|
|
53
|
+
if (nodesInChangedRanges.length > 1) {
|
|
54
|
+
textBlock = nodesInChangedRanges[0];
|
|
55
|
+
textBeforeWhitespace = newState.doc.textBetween(
|
|
56
|
+
textBlock.pos,
|
|
57
|
+
textBlock.pos + textBlock.node.nodeSize,
|
|
58
|
+
void 0,
|
|
59
|
+
" "
|
|
60
|
+
);
|
|
61
|
+
} else if (nodesInChangedRanges.length && newState.doc.textBetween(newRange.from, newRange.to, " ", " ").endsWith(" ")) {
|
|
62
|
+
textBlock = nodesInChangedRanges[0];
|
|
63
|
+
textBeforeWhitespace = newState.doc.textBetween(
|
|
64
|
+
textBlock.pos,
|
|
65
|
+
newRange.to,
|
|
66
|
+
void 0,
|
|
67
|
+
" "
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
if (textBlock && textBeforeWhitespace) {
|
|
71
|
+
const wordsBeforeWhitespace = textBeforeWhitespace.split(" ").filter((s) => s !== "");
|
|
72
|
+
if (wordsBeforeWhitespace.length <= 0) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const lastWordBeforeSpace = wordsBeforeWhitespace[wordsBeforeWhitespace.length - 1];
|
|
76
|
+
const lastWordAndBlockOffset = textBlock.pos + textBeforeWhitespace.lastIndexOf(lastWordBeforeSpace);
|
|
77
|
+
if (!lastWordBeforeSpace) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
_linkifyjs.find.call(void 0, lastWordBeforeSpace).filter((link) => link.isLink).filter((link) => {
|
|
81
|
+
if (options.validate) {
|
|
82
|
+
return options.validate(link.value);
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
}).map((link) => ({
|
|
86
|
+
...link,
|
|
87
|
+
from: lastWordAndBlockOffset + link.start + 1,
|
|
88
|
+
to: lastWordAndBlockOffset + link.end + 1
|
|
89
|
+
})).forEach((link) => {
|
|
90
|
+
tr.addMark(
|
|
91
|
+
link.from,
|
|
92
|
+
link.to,
|
|
93
|
+
options.type.create({
|
|
94
|
+
href: link.href
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
if (!tr.steps.length) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
return tr;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/helpers/clickHandler.ts
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
function clickHandler(options) {
|
|
112
|
+
return new (0, _state.Plugin)({
|
|
113
|
+
key: new (0, _state.PluginKey)("handleClickLink"),
|
|
114
|
+
props: {
|
|
115
|
+
handleClick: (view, pos, event) => {
|
|
116
|
+
var _a;
|
|
117
|
+
const attrs = _core.getAttributes.call(void 0, view.state, options.type.name);
|
|
118
|
+
const link = (_a = event.target) == null ? void 0 : _a.closest("a");
|
|
119
|
+
if (link && attrs.href) {
|
|
120
|
+
window.open(attrs.href, attrs.target);
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/helpers/pasteHandler.ts
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
function pasteHandler(options) {
|
|
133
|
+
return new (0, _state.Plugin)({
|
|
134
|
+
key: new (0, _state.PluginKey)("handlePasteLink"),
|
|
135
|
+
props: {
|
|
136
|
+
handlePaste: (view, event, slice) => {
|
|
137
|
+
const { state } = view;
|
|
138
|
+
const { selection } = state;
|
|
139
|
+
const { empty } = selection;
|
|
140
|
+
if (empty) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
let textContent = "";
|
|
144
|
+
slice.content.forEach((node) => {
|
|
145
|
+
textContent += node.textContent;
|
|
146
|
+
});
|
|
147
|
+
const link = _linkifyjs.find.call(void 0, textContent).find((item) => item.isLink && item.value === textContent);
|
|
148
|
+
if (!textContent || !link) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
options.editor.commands.setMark(options.type, {
|
|
152
|
+
href: link.href
|
|
153
|
+
});
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// src/link.ts
|
|
161
|
+
var Link = _core.Mark.create({
|
|
162
|
+
name: "link",
|
|
163
|
+
priority: 1e3,
|
|
164
|
+
keepOnSplit: false,
|
|
165
|
+
onCreate() {
|
|
166
|
+
this.options.protocols.forEach(_linkifyjs.registerCustomProtocol);
|
|
167
|
+
},
|
|
168
|
+
onDestroy() {
|
|
169
|
+
_linkifyjs.reset.call(void 0, );
|
|
170
|
+
},
|
|
171
|
+
inclusive() {
|
|
172
|
+
return this.options.autolink;
|
|
173
|
+
},
|
|
174
|
+
addOptions() {
|
|
175
|
+
return {
|
|
176
|
+
openOnClick: true,
|
|
177
|
+
linkOnPaste: true,
|
|
178
|
+
autolink: true,
|
|
179
|
+
protocols: [],
|
|
180
|
+
HTMLAttributes: {
|
|
181
|
+
target: "_blank",
|
|
182
|
+
rel: "noopener noreferrer nofollow",
|
|
183
|
+
class: null
|
|
184
|
+
},
|
|
185
|
+
validate: void 0
|
|
186
|
+
};
|
|
187
|
+
},
|
|
188
|
+
addAttributes() {
|
|
189
|
+
return {
|
|
190
|
+
href: {
|
|
191
|
+
default: null
|
|
192
|
+
},
|
|
193
|
+
target: {
|
|
194
|
+
default: this.options.HTMLAttributes.target
|
|
195
|
+
},
|
|
196
|
+
class: {
|
|
197
|
+
default: this.options.HTMLAttributes.class
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
},
|
|
201
|
+
parseHTML() {
|
|
202
|
+
return [{ tag: 'a[href]:not([href *= "javascript:" i])' }];
|
|
203
|
+
},
|
|
204
|
+
renderHTML({ HTMLAttributes }) {
|
|
205
|
+
return ["a", _core.mergeAttributes.call(void 0, this.options.HTMLAttributes, HTMLAttributes), 0];
|
|
206
|
+
},
|
|
207
|
+
addCommands() {
|
|
208
|
+
return {
|
|
209
|
+
setLink: (attributes) => ({ chain }) => {
|
|
210
|
+
return chain().setMark(this.name, attributes).setMeta("preventAutolink", true).run();
|
|
211
|
+
},
|
|
212
|
+
toggleLink: (attributes) => ({ chain }) => {
|
|
213
|
+
return chain().toggleMark(this.name, attributes, { extendEmptyMarkRange: true }).setMeta("preventAutolink", true).run();
|
|
214
|
+
},
|
|
215
|
+
unsetLink: () => ({ chain }) => {
|
|
216
|
+
return chain().unsetMark(this.name, { extendEmptyMarkRange: true }).setMeta("preventAutolink", true).run();
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
addPasteRules() {
|
|
221
|
+
return [
|
|
222
|
+
_core.markPasteRule.call(void 0, {
|
|
223
|
+
find: (text) => _linkifyjs.find.call(void 0, text).filter((link) => {
|
|
224
|
+
if (this.options.validate) {
|
|
225
|
+
return this.options.validate(link.value);
|
|
226
|
+
}
|
|
227
|
+
return true;
|
|
228
|
+
}).filter((link) => link.isLink).map((link) => ({
|
|
229
|
+
text: link.value,
|
|
230
|
+
index: link.start,
|
|
231
|
+
data: link
|
|
232
|
+
})),
|
|
233
|
+
type: this.type,
|
|
234
|
+
getAttributes: (match) => {
|
|
235
|
+
var _a;
|
|
236
|
+
return {
|
|
237
|
+
href: (_a = match.data) == null ? void 0 : _a.href
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
})
|
|
241
|
+
];
|
|
242
|
+
},
|
|
243
|
+
addProseMirrorPlugins() {
|
|
244
|
+
const plugins = [];
|
|
245
|
+
if (this.options.autolink) {
|
|
246
|
+
plugins.push(
|
|
247
|
+
autolink({
|
|
248
|
+
type: this.type,
|
|
249
|
+
validate: this.options.validate
|
|
250
|
+
})
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
if (this.options.openOnClick) {
|
|
254
|
+
plugins.push(
|
|
255
|
+
clickHandler({
|
|
256
|
+
type: this.type
|
|
257
|
+
})
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
if (this.options.linkOnPaste) {
|
|
261
|
+
plugins.push(
|
|
262
|
+
pasteHandler({
|
|
263
|
+
editor: this.editor,
|
|
264
|
+
type: this.type
|
|
265
|
+
})
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
return plugins;
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
// src/index.ts
|
|
273
|
+
var src_default = Link;
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
exports.Link = Link; exports.default = src_default;
|
|
@@ -1,40 +1,57 @@
|
|
|
1
|
-
import { Mark } from '@tiptap/core';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
1
|
+
import { Mark } from '@tiptap/core';
|
|
2
|
+
|
|
3
|
+
interface LinkOptions {
|
|
4
|
+
/**
|
|
5
|
+
* If enabled, it adds links as you type.
|
|
6
|
+
*/
|
|
7
|
+
autolink: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* An array of custom protocols to be registered with linkifyjs.
|
|
10
|
+
*/
|
|
11
|
+
protocols: Array<string>;
|
|
12
|
+
/**
|
|
13
|
+
* If enabled, links will be opened on click.
|
|
14
|
+
*/
|
|
15
|
+
openOnClick: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Adds a link to the current selection if the pasted content only contains an url.
|
|
18
|
+
*/
|
|
19
|
+
linkOnPaste: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* A list of HTML attributes to be rendered.
|
|
22
|
+
*/
|
|
23
|
+
HTMLAttributes: Record<string, any>;
|
|
24
|
+
/**
|
|
25
|
+
* A validation function that modifies link verification for the auto linker.
|
|
26
|
+
* @param url - The url to be validated.
|
|
27
|
+
* @returns - True if the url is valid, false otherwise.
|
|
28
|
+
*/
|
|
29
|
+
validate?: (url: string) => boolean;
|
|
30
|
+
}
|
|
31
|
+
declare module '@tiptap/core' {
|
|
32
|
+
interface Commands<ReturnType> {
|
|
33
|
+
link: {
|
|
34
|
+
/**
|
|
35
|
+
* Set a link mark
|
|
36
|
+
*/
|
|
37
|
+
setLink: (attributes: {
|
|
38
|
+
href: string;
|
|
39
|
+
target?: string | null;
|
|
40
|
+
}) => ReturnType;
|
|
41
|
+
/**
|
|
42
|
+
* Toggle a link mark
|
|
43
|
+
*/
|
|
44
|
+
toggleLink: (attributes: {
|
|
45
|
+
href: string;
|
|
46
|
+
target?: string | null;
|
|
47
|
+
}) => ReturnType;
|
|
48
|
+
/**
|
|
49
|
+
* Unset a link mark
|
|
50
|
+
*/
|
|
51
|
+
unsetLink: () => ReturnType;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
declare const Link: Mark<LinkOptions, any>;
|
|
56
|
+
|
|
57
|
+
export { Link, LinkOptions, Link as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// src/link.ts
|
|
2
|
+
import { Mark, markPasteRule, mergeAttributes } from "@tiptap/core";
|
|
3
|
+
import { find as find3, registerCustomProtocol, reset } from "linkifyjs";
|
|
4
|
+
|
|
5
|
+
// src/helpers/autolink.ts
|
|
6
|
+
import {
|
|
7
|
+
combineTransactionSteps,
|
|
8
|
+
findChildrenInRange,
|
|
9
|
+
getChangedRanges,
|
|
10
|
+
getMarksBetween
|
|
11
|
+
} from "@tiptap/core";
|
|
12
|
+
import { Plugin, PluginKey } from "@tiptap/pm/state";
|
|
13
|
+
import { find, test } from "linkifyjs";
|
|
14
|
+
function autolink(options) {
|
|
15
|
+
return new Plugin({
|
|
16
|
+
key: new PluginKey("autolink"),
|
|
17
|
+
appendTransaction: (transactions, oldState, newState) => {
|
|
18
|
+
const docChanges = transactions.some((transaction) => transaction.docChanged) && !oldState.doc.eq(newState.doc);
|
|
19
|
+
const preventAutolink = transactions.some((transaction) => transaction.getMeta("preventAutolink"));
|
|
20
|
+
if (!docChanges || preventAutolink) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const { tr } = newState;
|
|
24
|
+
const transform = combineTransactionSteps(oldState.doc, [...transactions]);
|
|
25
|
+
const { mapping } = transform;
|
|
26
|
+
const changes = getChangedRanges(transform);
|
|
27
|
+
changes.forEach(({ oldRange, newRange }) => {
|
|
28
|
+
getMarksBetween(oldRange.from, oldRange.to, oldState.doc).filter((item) => item.mark.type === options.type).forEach((oldMark) => {
|
|
29
|
+
const newFrom = mapping.map(oldMark.from);
|
|
30
|
+
const newTo = mapping.map(oldMark.to);
|
|
31
|
+
const newMarks = getMarksBetween(newFrom, newTo, newState.doc).filter(
|
|
32
|
+
(item) => item.mark.type === options.type
|
|
33
|
+
);
|
|
34
|
+
if (!newMarks.length) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const newMark = newMarks[0];
|
|
38
|
+
const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, void 0, " ");
|
|
39
|
+
const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, void 0, " ");
|
|
40
|
+
const wasLink = test(oldLinkText);
|
|
41
|
+
const isLink = test(newLinkText);
|
|
42
|
+
if (wasLink && !isLink) {
|
|
43
|
+
tr.removeMark(newMark.from, newMark.to, options.type);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const nodesInChangedRanges = findChildrenInRange(
|
|
47
|
+
newState.doc,
|
|
48
|
+
newRange,
|
|
49
|
+
(node) => node.isTextblock
|
|
50
|
+
);
|
|
51
|
+
let textBlock;
|
|
52
|
+
let textBeforeWhitespace;
|
|
53
|
+
if (nodesInChangedRanges.length > 1) {
|
|
54
|
+
textBlock = nodesInChangedRanges[0];
|
|
55
|
+
textBeforeWhitespace = newState.doc.textBetween(
|
|
56
|
+
textBlock.pos,
|
|
57
|
+
textBlock.pos + textBlock.node.nodeSize,
|
|
58
|
+
void 0,
|
|
59
|
+
" "
|
|
60
|
+
);
|
|
61
|
+
} else if (nodesInChangedRanges.length && newState.doc.textBetween(newRange.from, newRange.to, " ", " ").endsWith(" ")) {
|
|
62
|
+
textBlock = nodesInChangedRanges[0];
|
|
63
|
+
textBeforeWhitespace = newState.doc.textBetween(
|
|
64
|
+
textBlock.pos,
|
|
65
|
+
newRange.to,
|
|
66
|
+
void 0,
|
|
67
|
+
" "
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
if (textBlock && textBeforeWhitespace) {
|
|
71
|
+
const wordsBeforeWhitespace = textBeforeWhitespace.split(" ").filter((s) => s !== "");
|
|
72
|
+
if (wordsBeforeWhitespace.length <= 0) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const lastWordBeforeSpace = wordsBeforeWhitespace[wordsBeforeWhitespace.length - 1];
|
|
76
|
+
const lastWordAndBlockOffset = textBlock.pos + textBeforeWhitespace.lastIndexOf(lastWordBeforeSpace);
|
|
77
|
+
if (!lastWordBeforeSpace) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
find(lastWordBeforeSpace).filter((link) => link.isLink).filter((link) => {
|
|
81
|
+
if (options.validate) {
|
|
82
|
+
return options.validate(link.value);
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
}).map((link) => ({
|
|
86
|
+
...link,
|
|
87
|
+
from: lastWordAndBlockOffset + link.start + 1,
|
|
88
|
+
to: lastWordAndBlockOffset + link.end + 1
|
|
89
|
+
})).forEach((link) => {
|
|
90
|
+
tr.addMark(
|
|
91
|
+
link.from,
|
|
92
|
+
link.to,
|
|
93
|
+
options.type.create({
|
|
94
|
+
href: link.href
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
if (!tr.steps.length) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
return tr;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/helpers/clickHandler.ts
|
|
109
|
+
import { getAttributes } from "@tiptap/core";
|
|
110
|
+
import { Plugin as Plugin2, PluginKey as PluginKey2 } from "@tiptap/pm/state";
|
|
111
|
+
function clickHandler(options) {
|
|
112
|
+
return new Plugin2({
|
|
113
|
+
key: new PluginKey2("handleClickLink"),
|
|
114
|
+
props: {
|
|
115
|
+
handleClick: (view, pos, event) => {
|
|
116
|
+
var _a;
|
|
117
|
+
const attrs = getAttributes(view.state, options.type.name);
|
|
118
|
+
const link = (_a = event.target) == null ? void 0 : _a.closest("a");
|
|
119
|
+
if (link && attrs.href) {
|
|
120
|
+
window.open(attrs.href, attrs.target);
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/helpers/pasteHandler.ts
|
|
130
|
+
import { Plugin as Plugin3, PluginKey as PluginKey3 } from "@tiptap/pm/state";
|
|
131
|
+
import { find as find2 } from "linkifyjs";
|
|
132
|
+
function pasteHandler(options) {
|
|
133
|
+
return new Plugin3({
|
|
134
|
+
key: new PluginKey3("handlePasteLink"),
|
|
135
|
+
props: {
|
|
136
|
+
handlePaste: (view, event, slice) => {
|
|
137
|
+
const { state } = view;
|
|
138
|
+
const { selection } = state;
|
|
139
|
+
const { empty } = selection;
|
|
140
|
+
if (empty) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
let textContent = "";
|
|
144
|
+
slice.content.forEach((node) => {
|
|
145
|
+
textContent += node.textContent;
|
|
146
|
+
});
|
|
147
|
+
const link = find2(textContent).find((item) => item.isLink && item.value === textContent);
|
|
148
|
+
if (!textContent || !link) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
options.editor.commands.setMark(options.type, {
|
|
152
|
+
href: link.href
|
|
153
|
+
});
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// src/link.ts
|
|
161
|
+
var Link = Mark.create({
|
|
162
|
+
name: "link",
|
|
163
|
+
priority: 1e3,
|
|
164
|
+
keepOnSplit: false,
|
|
165
|
+
onCreate() {
|
|
166
|
+
this.options.protocols.forEach(registerCustomProtocol);
|
|
167
|
+
},
|
|
168
|
+
onDestroy() {
|
|
169
|
+
reset();
|
|
170
|
+
},
|
|
171
|
+
inclusive() {
|
|
172
|
+
return this.options.autolink;
|
|
173
|
+
},
|
|
174
|
+
addOptions() {
|
|
175
|
+
return {
|
|
176
|
+
openOnClick: true,
|
|
177
|
+
linkOnPaste: true,
|
|
178
|
+
autolink: true,
|
|
179
|
+
protocols: [],
|
|
180
|
+
HTMLAttributes: {
|
|
181
|
+
target: "_blank",
|
|
182
|
+
rel: "noopener noreferrer nofollow",
|
|
183
|
+
class: null
|
|
184
|
+
},
|
|
185
|
+
validate: void 0
|
|
186
|
+
};
|
|
187
|
+
},
|
|
188
|
+
addAttributes() {
|
|
189
|
+
return {
|
|
190
|
+
href: {
|
|
191
|
+
default: null
|
|
192
|
+
},
|
|
193
|
+
target: {
|
|
194
|
+
default: this.options.HTMLAttributes.target
|
|
195
|
+
},
|
|
196
|
+
class: {
|
|
197
|
+
default: this.options.HTMLAttributes.class
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
},
|
|
201
|
+
parseHTML() {
|
|
202
|
+
return [{ tag: 'a[href]:not([href *= "javascript:" i])' }];
|
|
203
|
+
},
|
|
204
|
+
renderHTML({ HTMLAttributes }) {
|
|
205
|
+
return ["a", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
|
|
206
|
+
},
|
|
207
|
+
addCommands() {
|
|
208
|
+
return {
|
|
209
|
+
setLink: (attributes) => ({ chain }) => {
|
|
210
|
+
return chain().setMark(this.name, attributes).setMeta("preventAutolink", true).run();
|
|
211
|
+
},
|
|
212
|
+
toggleLink: (attributes) => ({ chain }) => {
|
|
213
|
+
return chain().toggleMark(this.name, attributes, { extendEmptyMarkRange: true }).setMeta("preventAutolink", true).run();
|
|
214
|
+
},
|
|
215
|
+
unsetLink: () => ({ chain }) => {
|
|
216
|
+
return chain().unsetMark(this.name, { extendEmptyMarkRange: true }).setMeta("preventAutolink", true).run();
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
addPasteRules() {
|
|
221
|
+
return [
|
|
222
|
+
markPasteRule({
|
|
223
|
+
find: (text) => find3(text).filter((link) => {
|
|
224
|
+
if (this.options.validate) {
|
|
225
|
+
return this.options.validate(link.value);
|
|
226
|
+
}
|
|
227
|
+
return true;
|
|
228
|
+
}).filter((link) => link.isLink).map((link) => ({
|
|
229
|
+
text: link.value,
|
|
230
|
+
index: link.start,
|
|
231
|
+
data: link
|
|
232
|
+
})),
|
|
233
|
+
type: this.type,
|
|
234
|
+
getAttributes: (match) => {
|
|
235
|
+
var _a;
|
|
236
|
+
return {
|
|
237
|
+
href: (_a = match.data) == null ? void 0 : _a.href
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
})
|
|
241
|
+
];
|
|
242
|
+
},
|
|
243
|
+
addProseMirrorPlugins() {
|
|
244
|
+
const plugins = [];
|
|
245
|
+
if (this.options.autolink) {
|
|
246
|
+
plugins.push(
|
|
247
|
+
autolink({
|
|
248
|
+
type: this.type,
|
|
249
|
+
validate: this.options.validate
|
|
250
|
+
})
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
if (this.options.openOnClick) {
|
|
254
|
+
plugins.push(
|
|
255
|
+
clickHandler({
|
|
256
|
+
type: this.type
|
|
257
|
+
})
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
if (this.options.linkOnPaste) {
|
|
261
|
+
plugins.push(
|
|
262
|
+
pasteHandler({
|
|
263
|
+
editor: this.editor,
|
|
264
|
+
type: this.type
|
|
265
|
+
})
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
return plugins;
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
// src/index.ts
|
|
273
|
+
var src_default = Link;
|
|
274
|
+
export {
|
|
275
|
+
Link,
|
|
276
|
+
src_default as default
|
|
277
|
+
};
|