@tiptap/extension-link 2.0.0-beta.31 → 2.0.0-beta.35
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/packages/extension-link/src/helpers/autolink.d.ts +1 -1
- package/dist/packages/extension-link/src/helpers/clickHandler.d.ts +1 -1
- package/dist/packages/extension-link/src/helpers/pasteHandler.d.ts +1 -1
- package/dist/tiptap-extension-link.cjs.js +23 -10
- package/dist/tiptap-extension-link.cjs.js.map +1 -1
- package/dist/tiptap-extension-link.esm.js +23 -10
- package/dist/tiptap-extension-link.esm.js.map +1 -1
- package/dist/tiptap-extension-link.umd.js +23 -10
- package/dist/tiptap-extension-link.umd.js.map +1 -1
- package/package.json +4 -4
- package/src/helpers/autolink.ts +15 -5
- package/src/helpers/clickHandler.ts +1 -1
- package/src/helpers/pasteHandler.ts +1 -1
- package/src/link.ts +18 -9
|
@@ -3,5 +3,5 @@ import { MarkType } from 'prosemirror-model';
|
|
|
3
3
|
declare type ClickHandlerOptions = {
|
|
4
4
|
type: MarkType;
|
|
5
5
|
};
|
|
6
|
-
export
|
|
6
|
+
export declare function clickHandler(options: ClickHandlerOptions): Plugin;
|
|
7
7
|
export {};
|
|
@@ -12,7 +12,8 @@ function autolink(options) {
|
|
|
12
12
|
appendTransaction: (transactions, oldState, newState) => {
|
|
13
13
|
const docChanges = transactions.some(transaction => transaction.docChanged)
|
|
14
14
|
&& !oldState.doc.eq(newState.doc);
|
|
15
|
-
|
|
15
|
+
const preventAutolink = transactions.some(transaction => transaction.getMeta('preventAutolink'));
|
|
16
|
+
if (!docChanges || preventAutolink) {
|
|
16
17
|
return;
|
|
17
18
|
}
|
|
18
19
|
const { tr } = newState;
|
|
@@ -32,8 +33,8 @@ function autolink(options) {
|
|
|
32
33
|
return;
|
|
33
34
|
}
|
|
34
35
|
const newMark = newMarks[0];
|
|
35
|
-
const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to);
|
|
36
|
-
const newLinkText = newState.doc.textBetween(newMark.from, newMark.to);
|
|
36
|
+
const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, undefined, ' ');
|
|
37
|
+
const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, undefined, ' ');
|
|
37
38
|
const wasLink = linkifyjs.test(oldLinkText);
|
|
38
39
|
const isLink = linkifyjs.test(newLinkText);
|
|
39
40
|
// remove only the link, if it was a link before too
|
|
@@ -45,7 +46,10 @@ function autolink(options) {
|
|
|
45
46
|
// now let’s see if we can add new links
|
|
46
47
|
core.findChildrenInRange(newState.doc, newRange, node => node.isTextblock)
|
|
47
48
|
.forEach(textBlock => {
|
|
48
|
-
|
|
49
|
+
// we need to define a placeholder for leaf nodes
|
|
50
|
+
// so that the link position can be calculated correctly
|
|
51
|
+
const text = newState.doc.textBetween(textBlock.pos, textBlock.pos + textBlock.node.nodeSize, undefined, ' ');
|
|
52
|
+
linkifyjs.find(text)
|
|
49
53
|
.filter(link => link.isLink)
|
|
50
54
|
// calculate link position
|
|
51
55
|
.map(link => ({
|
|
@@ -163,14 +167,23 @@ const Link = core.Mark.create({
|
|
|
163
167
|
},
|
|
164
168
|
addCommands() {
|
|
165
169
|
return {
|
|
166
|
-
setLink: attributes => ({
|
|
167
|
-
return
|
|
170
|
+
setLink: attributes => ({ chain }) => {
|
|
171
|
+
return chain()
|
|
172
|
+
.setMark(this.name, attributes)
|
|
173
|
+
.setMeta('preventAutolink', true)
|
|
174
|
+
.run();
|
|
168
175
|
},
|
|
169
|
-
toggleLink: attributes => ({
|
|
170
|
-
return
|
|
176
|
+
toggleLink: attributes => ({ chain }) => {
|
|
177
|
+
return chain()
|
|
178
|
+
.toggleMark(this.name, attributes, { extendEmptyMarkRange: true })
|
|
179
|
+
.setMeta('preventAutolink', true)
|
|
180
|
+
.run();
|
|
171
181
|
},
|
|
172
|
-
unsetLink: () => ({
|
|
173
|
-
return
|
|
182
|
+
unsetLink: () => ({ chain }) => {
|
|
183
|
+
return chain()
|
|
184
|
+
.unsetMark(this.name, { extendEmptyMarkRange: true })
|
|
185
|
+
.setMeta('preventAutolink', true)
|
|
186
|
+
.run();
|
|
174
187
|
},
|
|
175
188
|
};
|
|
176
189
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tiptap-extension-link.cjs.js","sources":["../src/helpers/autolink.ts","../src/helpers/clickHandler.ts","../src/helpers/pasteHandler.ts","../src/link.ts"],"sourcesContent":["import {\n getMarksBetween,\n findChildrenInRange,\n combineTransactionSteps,\n getChangedRanges,\n} from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find, test } from 'linkifyjs'\n\ntype AutolinkOptions = {\n type: MarkType,\n}\n\nexport default function autolink(options: AutolinkOptions): Plugin {\n return new Plugin({\n key: new PluginKey('autolink'),\n appendTransaction: (transactions, oldState, newState) => {\n const docChanges = transactions.some(transaction => transaction.docChanged)\n && !oldState.doc.eq(newState.doc)\n\n if (!docChanges) {\n return\n }\n\n const { tr } = newState\n const transform = combineTransactionSteps(oldState.doc, transactions)\n const { mapping } = transform\n const changes = getChangedRanges(transform)\n\n changes.forEach(({ oldRange, newRange }) => {\n // at first we check if we have to remove links\n getMarksBetween(oldRange.from, oldRange.to, oldState.doc)\n .filter(item => item.mark.type === options.type)\n .forEach(oldMark => {\n const newFrom = mapping.map(oldMark.from)\n const newTo = mapping.map(oldMark.to)\n const newMarks = getMarksBetween(newFrom, newTo, newState.doc)\n .filter(item => item.mark.type === options.type)\n\n if (!newMarks.length) {\n return\n }\n\n const newMark = newMarks[0]\n const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to)\n const newLinkText = newState.doc.textBetween(newMark.from, newMark.to)\n const wasLink = test(oldLinkText)\n const isLink = test(newLinkText)\n\n // remove only the link, if it was a link before too\n // because we don’t want to remove links that were set manually\n if (wasLink && !isLink) {\n tr.removeMark(newMark.from, newMark.to, options.type)\n }\n })\n\n // now let’s see if we can add new links\n findChildrenInRange(newState.doc, newRange, node => node.isTextblock)\n .forEach(textBlock => {\n find(textBlock.node.textContent)\n .filter(link => link.isLink)\n // calculate link position\n .map(link => ({\n ...link,\n from: textBlock.pos + link.start + 1,\n to: textBlock.pos + link.end + 1,\n }))\n // check if link is within the changed range\n .filter(link => {\n const fromIsInRange = newRange.from >= link.from && newRange.from <= link.to\n const toIsInRange = newRange.to >= link.from && newRange.to <= link.to\n\n return fromIsInRange || toIsInRange\n })\n // add link mark\n .forEach(link => {\n tr.addMark(link.from, link.to, options.type.create({\n href: link.href,\n }))\n })\n })\n })\n\n if (!tr.steps.length) {\n return\n }\n\n return tr\n },\n })\n}\n","import { getAttributes } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\n\ntype ClickHandlerOptions = {\n type: MarkType,\n}\n\nexport default function clickHandler(options: ClickHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handleClickLink'),\n props: {\n handleClick: (view, pos, event) => {\n const attrs = getAttributes(view.state, options.type.name)\n const link = (event.target as HTMLElement)?.closest('a')\n\n if (link && attrs.href) {\n window.open(attrs.href, attrs.target)\n\n return true\n }\n\n return false\n },\n },\n })\n}\n","import { Editor } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find } from 'linkifyjs'\n\ntype PasteHandlerOptions = {\n editor: Editor,\n type: MarkType,\n}\n\nexport default function pasteHandler(options: PasteHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handlePasteLink'),\n props: {\n handlePaste: (view, event, slice) => {\n const { state } = view\n const { selection } = state\n const { empty } = selection\n\n if (empty) {\n return false\n }\n\n let textContent = ''\n\n slice.content.forEach(node => {\n textContent += node.textContent\n })\n\n const link = find(textContent).find(item => item.isLink && item.value === textContent)\n\n if (!textContent || !link) {\n return false\n }\n\n options.editor.commands.setMark(options.type, {\n href: link.href,\n })\n\n return true\n },\n },\n })\n}\n","import { Mark, markPasteRule, mergeAttributes } from '@tiptap/core'\nimport { find } from 'linkifyjs'\nimport autolink from './helpers/autolink'\nimport clickHandler from './helpers/clickHandler'\nimport pasteHandler from './helpers/pasteHandler'\n\nexport interface LinkOptions {\n /**\n * If enabled, it adds links as you type.\n */\n autolink: boolean,\n /**\n * If enabled, links will be opened on click.\n */\n openOnClick: boolean,\n /**\n * Adds a link to the current selection if the pasted content only contains an url.\n */\n linkOnPaste: boolean,\n /**\n * A list of HTML attributes to be rendered.\n */\n HTMLAttributes: Record<string, any>,\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n link: {\n /**\n * Set a link mark\n */\n setLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Toggle a link mark\n */\n toggleLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Unset a link mark\n */\n unsetLink: () => ReturnType,\n }\n }\n}\n\nexport const Link = Mark.create<LinkOptions>({\n name: 'link',\n\n priority: 1000,\n\n keepOnSplit: false,\n\n inclusive() {\n return this.options.autolink\n },\n\n addOptions() {\n return {\n openOnClick: true,\n linkOnPaste: true,\n autolink: true,\n HTMLAttributes: {\n target: '_blank',\n rel: 'noopener noreferrer nofollow',\n },\n }\n },\n\n addAttributes() {\n return {\n href: {\n default: null,\n },\n target: {\n default: this.options.HTMLAttributes.target,\n },\n }\n },\n\n parseHTML() {\n return [\n { tag: 'a[href]' },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return [\n 'a',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n 0,\n ]\n },\n\n addCommands() {\n return {\n setLink: attributes => ({ commands }) => {\n return commands.setMark(this.name, attributes)\n },\n\n toggleLink: attributes => ({ commands }) => {\n return commands.toggleMark(this.name, attributes, { extendEmptyMarkRange: true })\n },\n\n unsetLink: () => ({ commands }) => {\n return commands.unsetMark(this.name, { extendEmptyMarkRange: true })\n },\n }\n },\n\n addPasteRules() {\n return [\n markPasteRule({\n find: text => find(text)\n .filter(link => link.isLink)\n .map(link => ({\n text: link.value,\n index: link.start,\n data: link,\n })),\n type: this.type,\n getAttributes: match => ({\n href: match.data?.href,\n }),\n }),\n ]\n },\n\n addProseMirrorPlugins() {\n const plugins = []\n\n if (this.options.autolink) {\n plugins.push(autolink({\n type: this.type,\n }))\n }\n\n if (this.options.openOnClick) {\n plugins.push(clickHandler({\n type: this.type,\n }))\n }\n\n if (this.options.linkOnPaste) {\n plugins.push(pasteHandler({\n editor: this.editor,\n type: this.type,\n }))\n }\n\n return plugins\n },\n})\n"],"names":["Plugin","PluginKey","combineTransactionSteps","getChangedRanges","getMarksBetween","test","findChildrenInRange","find","getAttributes","Mark","mergeAttributes","markPasteRule"],"mappings":";;;;;;;;SAcwB,QAAQ,CAAC,OAAwB;IACvD,OAAO,IAAIA,uBAAM,CAAC;QAChB,GAAG,EAAE,IAAIC,0BAAS,CAAC,UAAU,CAAC;QAC9B,iBAAiB,EAAE,CAAC,YAAY,EAAE,QAAQ,EAAE,QAAQ;YAClD,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,UAAU,CAAC;mBACtE,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;YAEnC,IAAI,CAAC,UAAU,EAAE;gBACf,OAAM;aACP;YAED,MAAM,EAAE,EAAE,EAAE,GAAG,QAAQ,CAAA;YACvB,MAAM,SAAS,GAAGC,4BAAuB,CAAC,QAAQ,CAAC,GAAG,EAAE,YAAY,CAAC,CAAA;YACrE,MAAM,EAAE,OAAO,EAAE,GAAG,SAAS,CAAA;YAC7B,MAAM,OAAO,GAAGC,qBAAgB,CAAC,SAAS,CAAC,CAAA;YAE3C,OAAO,CAAC,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE;;gBAErCC,oBAAe,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,EAAE,QAAQ,CAA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IAAI;aAChB,CAAC,CAAC,CAAA;SACJ;QAED,OAAO,OAAO,CAAA;KACf;CACF;;;;;"}
|
|
1
|
+
{"version":3,"file":"tiptap-extension-link.cjs.js","sources":["../src/helpers/autolink.ts","../src/helpers/clickHandler.ts","../src/helpers/pasteHandler.ts","../src/link.ts"],"sourcesContent":["import {\n getMarksBetween,\n findChildrenInRange,\n combineTransactionSteps,\n getChangedRanges,\n} from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find, test } from 'linkifyjs'\n\ntype AutolinkOptions = {\n type: MarkType,\n}\n\nexport function autolink(options: AutolinkOptions): Plugin {\n return new Plugin({\n key: new PluginKey('autolink'),\n appendTransaction: (transactions, oldState, newState) => {\n const docChanges = transactions.some(transaction => transaction.docChanged)\n && !oldState.doc.eq(newState.doc)\n const preventAutolink = transactions.some(transaction => transaction.getMeta('preventAutolink'))\n\n if (!docChanges || preventAutolink) {\n return\n }\n\n const { tr } = newState\n const transform = combineTransactionSteps(oldState.doc, transactions)\n const { mapping } = transform\n const changes = getChangedRanges(transform)\n\n changes.forEach(({ oldRange, newRange }) => {\n // at first we check if we have to remove links\n getMarksBetween(oldRange.from, oldRange.to, oldState.doc)\n .filter(item => item.mark.type === options.type)\n .forEach(oldMark => {\n const newFrom = mapping.map(oldMark.from)\n const newTo = mapping.map(oldMark.to)\n const newMarks = getMarksBetween(newFrom, newTo, newState.doc)\n .filter(item => item.mark.type === options.type)\n\n if (!newMarks.length) {\n return\n }\n\n const newMark = newMarks[0]\n const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, undefined, ' ')\n const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, undefined, ' ')\n const wasLink = test(oldLinkText)\n const isLink = test(newLinkText)\n\n // remove only the link, if it was a link before too\n // because we don’t want to remove links that were set manually\n if (wasLink && !isLink) {\n tr.removeMark(newMark.from, newMark.to, options.type)\n }\n })\n\n // now let’s see if we can add new links\n findChildrenInRange(newState.doc, newRange, node => node.isTextblock)\n .forEach(textBlock => {\n // we need to define a placeholder for leaf nodes\n // so that the link position can be calculated correctly\n const text = newState.doc.textBetween(\n textBlock.pos,\n textBlock.pos + textBlock.node.nodeSize,\n undefined,\n ' ',\n )\n\n find(text)\n .filter(link => link.isLink)\n // calculate link position\n .map(link => ({\n ...link,\n from: textBlock.pos + link.start + 1,\n to: textBlock.pos + link.end + 1,\n }))\n // check if link is within the changed range\n .filter(link => {\n const fromIsInRange = newRange.from >= link.from && newRange.from <= link.to\n const toIsInRange = newRange.to >= link.from && newRange.to <= link.to\n\n return fromIsInRange || toIsInRange\n })\n // add link mark\n .forEach(link => {\n tr.addMark(link.from, link.to, options.type.create({\n href: link.href,\n }))\n })\n })\n })\n\n if (!tr.steps.length) {\n return\n }\n\n return tr\n },\n })\n}\n","import { getAttributes } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\n\ntype ClickHandlerOptions = {\n type: MarkType,\n}\n\nexport function clickHandler(options: ClickHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handleClickLink'),\n props: {\n handleClick: (view, pos, event) => {\n const attrs = getAttributes(view.state, options.type.name)\n const link = (event.target as HTMLElement)?.closest('a')\n\n if (link && attrs.href) {\n window.open(attrs.href, attrs.target)\n\n return true\n }\n\n return false\n },\n },\n })\n}\n","import { Editor } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find } from 'linkifyjs'\n\ntype PasteHandlerOptions = {\n editor: Editor,\n type: MarkType,\n}\n\nexport function pasteHandler(options: PasteHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handlePasteLink'),\n props: {\n handlePaste: (view, event, slice) => {\n const { state } = view\n const { selection } = state\n const { empty } = selection\n\n if (empty) {\n return false\n }\n\n let textContent = ''\n\n slice.content.forEach(node => {\n textContent += node.textContent\n })\n\n const link = find(textContent).find(item => item.isLink && item.value === textContent)\n\n if (!textContent || !link) {\n return false\n }\n\n options.editor.commands.setMark(options.type, {\n href: link.href,\n })\n\n return true\n },\n },\n })\n}\n","import { Mark, markPasteRule, mergeAttributes } from '@tiptap/core'\nimport { find } from 'linkifyjs'\nimport { autolink } from './helpers/autolink'\nimport { clickHandler } from './helpers/clickHandler'\nimport { pasteHandler } from './helpers/pasteHandler'\n\nexport interface LinkOptions {\n /**\n * If enabled, it adds links as you type.\n */\n autolink: boolean,\n /**\n * If enabled, links will be opened on click.\n */\n openOnClick: boolean,\n /**\n * Adds a link to the current selection if the pasted content only contains an url.\n */\n linkOnPaste: boolean,\n /**\n * A list of HTML attributes to be rendered.\n */\n HTMLAttributes: Record<string, any>,\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n link: {\n /**\n * Set a link mark\n */\n setLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Toggle a link mark\n */\n toggleLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Unset a link mark\n */\n unsetLink: () => ReturnType,\n }\n }\n}\n\nexport const Link = Mark.create<LinkOptions>({\n name: 'link',\n\n priority: 1000,\n\n keepOnSplit: false,\n\n inclusive() {\n return this.options.autolink\n },\n\n addOptions() {\n return {\n openOnClick: true,\n linkOnPaste: true,\n autolink: true,\n HTMLAttributes: {\n target: '_blank',\n rel: 'noopener noreferrer nofollow',\n },\n }\n },\n\n addAttributes() {\n return {\n href: {\n default: null,\n },\n target: {\n default: this.options.HTMLAttributes.target,\n },\n }\n },\n\n parseHTML() {\n return [\n { tag: 'a[href]' },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return [\n 'a',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n 0,\n ]\n },\n\n addCommands() {\n return {\n setLink: attributes => ({ chain }) => {\n return chain()\n .setMark(this.name, attributes)\n .setMeta('preventAutolink', true)\n .run()\n },\n\n toggleLink: attributes => ({ chain }) => {\n return chain()\n .toggleMark(this.name, attributes, { extendEmptyMarkRange: true })\n .setMeta('preventAutolink', true)\n .run()\n },\n\n unsetLink: () => ({ chain }) => {\n return chain()\n .unsetMark(this.name, { extendEmptyMarkRange: true })\n .setMeta('preventAutolink', true)\n .run()\n },\n }\n },\n\n addPasteRules() {\n return [\n markPasteRule({\n find: text => find(text)\n .filter(link => link.isLink)\n .map(link => ({\n text: link.value,\n index: link.start,\n data: link,\n })),\n type: this.type,\n getAttributes: match => ({\n href: match.data?.href,\n }),\n }),\n ]\n },\n\n addProseMirrorPlugins() {\n const plugins = []\n\n if (this.options.autolink) {\n plugins.push(autolink({\n type: this.type,\n }))\n }\n\n if (this.options.openOnClick) {\n plugins.push(clickHandler({\n type: this.type,\n }))\n }\n\n if (this.options.linkOnPaste) {\n plugins.push(pasteHandler({\n editor: this.editor,\n type: this.type,\n }))\n }\n\n return plugins\n },\n})\n"],"names":["Plugin","PluginKey","combineTransactionSteps","getChangedRanges","getMarksBetween","test","findChildrenInRange","find","getAttributes","Mark","mergeAttributes","markPasteRule"],"mappings":";;;;;;;;SAcgB,QAAQ,CAAC,OAAwB;IAC/C,OAAO,IAAIA,uBAAM,CAAC;QAChB,GAAG,EAAE,IAAIC,0BAAS,CAAC,UAAU,CAAC;QAC9B,iBAAiB,EAAE,CAAC,YAAY,EAAE,QAAQ,EAAE,QAAQ;YAClD,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,UAAU,CAAC;mBACtE,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;YACnC,MAAM,eAAe,GAAG,YAAY,CAAC,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAA;YAEhG,IAAI,CAAC,UAAU,IAAI,eAAe,EAAE;gBAClC,OAAM;aACP;YAED,MAAM,EAAE,EAAE,EAAE,GAAG,QAAQ,CAAA;YACvB,MAAM,SAAS,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AAI;iBACX,CAAC,CAAC;gBACL,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,aAAa,EAAE,KAAK;;oBAAI,QAAC;wBACvB,IAAI,EAAE,MAAA,KAAK,CAAC,IAAI,0CAAE,IAAI;qBACvB,EAAC;iBAAA;aACH,CAAC;SACH,CAAA;KACF;IAED,qBAAqB;QACnB,MAAM,OAAO,GAAG,EAAE,CAAA;QAElB,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;YACzB,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC;gBACpB,IAAI,EAAE,IAAI,CAAC,IAAI;aAChB,CAAC,CAAC,CAAA;SACJ;QAED,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE;YAC5B,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC;gBACxB,IAAI,EAAE,IAAI,CAAC,IAAI;aAChB,CAAC,CAAC,CAAA;SACJ;QAED,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE;YAC5B,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC;gBACxB,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,IAAI,EAAE,IAAI,CAAC,IAAI;aAChB,CAAC,CAAC,CAAA;SACJ;QAED,OAAO,OAAO,CAAA;KACf;CACF;;;;;"}
|
|
@@ -8,7 +8,8 @@ function autolink(options) {
|
|
|
8
8
|
appendTransaction: (transactions, oldState, newState) => {
|
|
9
9
|
const docChanges = transactions.some(transaction => transaction.docChanged)
|
|
10
10
|
&& !oldState.doc.eq(newState.doc);
|
|
11
|
-
|
|
11
|
+
const preventAutolink = transactions.some(transaction => transaction.getMeta('preventAutolink'));
|
|
12
|
+
if (!docChanges || preventAutolink) {
|
|
12
13
|
return;
|
|
13
14
|
}
|
|
14
15
|
const { tr } = newState;
|
|
@@ -28,8 +29,8 @@ function autolink(options) {
|
|
|
28
29
|
return;
|
|
29
30
|
}
|
|
30
31
|
const newMark = newMarks[0];
|
|
31
|
-
const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to);
|
|
32
|
-
const newLinkText = newState.doc.textBetween(newMark.from, newMark.to);
|
|
32
|
+
const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, undefined, ' ');
|
|
33
|
+
const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, undefined, ' ');
|
|
33
34
|
const wasLink = test(oldLinkText);
|
|
34
35
|
const isLink = test(newLinkText);
|
|
35
36
|
// remove only the link, if it was a link before too
|
|
@@ -41,7 +42,10 @@ function autolink(options) {
|
|
|
41
42
|
// now let’s see if we can add new links
|
|
42
43
|
findChildrenInRange(newState.doc, newRange, node => node.isTextblock)
|
|
43
44
|
.forEach(textBlock => {
|
|
44
|
-
|
|
45
|
+
// we need to define a placeholder for leaf nodes
|
|
46
|
+
// so that the link position can be calculated correctly
|
|
47
|
+
const text = newState.doc.textBetween(textBlock.pos, textBlock.pos + textBlock.node.nodeSize, undefined, ' ');
|
|
48
|
+
find(text)
|
|
45
49
|
.filter(link => link.isLink)
|
|
46
50
|
// calculate link position
|
|
47
51
|
.map(link => ({
|
|
@@ -159,14 +163,23 @@ const Link = Mark.create({
|
|
|
159
163
|
},
|
|
160
164
|
addCommands() {
|
|
161
165
|
return {
|
|
162
|
-
setLink: attributes => ({
|
|
163
|
-
return
|
|
166
|
+
setLink: attributes => ({ chain }) => {
|
|
167
|
+
return chain()
|
|
168
|
+
.setMark(this.name, attributes)
|
|
169
|
+
.setMeta('preventAutolink', true)
|
|
170
|
+
.run();
|
|
164
171
|
},
|
|
165
|
-
toggleLink: attributes => ({
|
|
166
|
-
return
|
|
172
|
+
toggleLink: attributes => ({ chain }) => {
|
|
173
|
+
return chain()
|
|
174
|
+
.toggleMark(this.name, attributes, { extendEmptyMarkRange: true })
|
|
175
|
+
.setMeta('preventAutolink', true)
|
|
176
|
+
.run();
|
|
167
177
|
},
|
|
168
|
-
unsetLink: () => ({
|
|
169
|
-
return
|
|
178
|
+
unsetLink: () => ({ chain }) => {
|
|
179
|
+
return chain()
|
|
180
|
+
.unsetMark(this.name, { extendEmptyMarkRange: true })
|
|
181
|
+
.setMeta('preventAutolink', true)
|
|
182
|
+
.run();
|
|
170
183
|
},
|
|
171
184
|
};
|
|
172
185
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tiptap-extension-link.esm.js","sources":["../src/helpers/autolink.ts","../src/helpers/clickHandler.ts","../src/helpers/pasteHandler.ts","../src/link.ts"],"sourcesContent":["import {\n getMarksBetween,\n findChildrenInRange,\n combineTransactionSteps,\n getChangedRanges,\n} from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find, test } from 'linkifyjs'\n\ntype AutolinkOptions = {\n type: MarkType,\n}\n\nexport default function autolink(options: AutolinkOptions): Plugin {\n return new Plugin({\n key: new PluginKey('autolink'),\n appendTransaction: (transactions, oldState, newState) => {\n const docChanges = transactions.some(transaction => transaction.docChanged)\n && !oldState.doc.eq(newState.doc)\n\n if (!docChanges) {\n return\n }\n\n const { tr } = newState\n const transform = combineTransactionSteps(oldState.doc, transactions)\n const { mapping } = transform\n const changes = getChangedRanges(transform)\n\n changes.forEach(({ oldRange, newRange }) => {\n // at first we check if we have to remove links\n getMarksBetween(oldRange.from, oldRange.to, oldState.doc)\n .filter(item => item.mark.type === options.type)\n .forEach(oldMark => {\n const newFrom = mapping.map(oldMark.from)\n const newTo = mapping.map(oldMark.to)\n const newMarks = getMarksBetween(newFrom, newTo, newState.doc)\n .filter(item => item.mark.type === options.type)\n\n if (!newMarks.length) {\n return\n }\n\n const newMark = newMarks[0]\n const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to)\n const newLinkText = newState.doc.textBetween(newMark.from, newMark.to)\n const wasLink = test(oldLinkText)\n const isLink = test(newLinkText)\n\n // remove only the link, if it was a link before too\n // because we don’t want to remove links that were set manually\n if (wasLink && !isLink) {\n tr.removeMark(newMark.from, newMark.to, options.type)\n }\n })\n\n // now let’s see if we can add new links\n findChildrenInRange(newState.doc, newRange, node => node.isTextblock)\n .forEach(textBlock => {\n find(textBlock.node.textContent)\n .filter(link => link.isLink)\n // calculate link position\n .map(link => ({\n ...link,\n from: textBlock.pos + link.start + 1,\n to: textBlock.pos + link.end + 1,\n }))\n // check if link is within the changed range\n .filter(link => {\n const fromIsInRange = newRange.from >= link.from && newRange.from <= link.to\n const toIsInRange = newRange.to >= link.from && newRange.to <= link.to\n\n return fromIsInRange || toIsInRange\n })\n // add link mark\n .forEach(link => {\n tr.addMark(link.from, link.to, options.type.create({\n href: link.href,\n }))\n })\n })\n })\n\n if (!tr.steps.length) {\n return\n }\n\n return tr\n },\n })\n}\n","import { getAttributes } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\n\ntype ClickHandlerOptions = {\n type: MarkType,\n}\n\nexport default function clickHandler(options: ClickHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handleClickLink'),\n props: {\n handleClick: (view, pos, event) => {\n const attrs = getAttributes(view.state, options.type.name)\n const link = (event.target as HTMLElement)?.closest('a')\n\n if (link && attrs.href) {\n window.open(attrs.href, attrs.target)\n\n return true\n }\n\n return false\n },\n },\n })\n}\n","import { Editor } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find } from 'linkifyjs'\n\ntype PasteHandlerOptions = {\n editor: Editor,\n type: MarkType,\n}\n\nexport default function pasteHandler(options: PasteHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handlePasteLink'),\n props: {\n handlePaste: (view, event, slice) => {\n const { state } = view\n const { selection } = state\n const { empty } = selection\n\n if (empty) {\n return false\n }\n\n let textContent = ''\n\n slice.content.forEach(node => {\n textContent += node.textContent\n })\n\n const link = find(textContent).find(item => item.isLink && item.value === textContent)\n\n if (!textContent || !link) {\n return false\n }\n\n options.editor.commands.setMark(options.type, {\n href: link.href,\n })\n\n return true\n },\n },\n })\n}\n","import { Mark, markPasteRule, mergeAttributes } from '@tiptap/core'\nimport { find } from 'linkifyjs'\nimport autolink from './helpers/autolink'\nimport clickHandler from './helpers/clickHandler'\nimport pasteHandler from './helpers/pasteHandler'\n\nexport interface LinkOptions {\n /**\n * If enabled, it adds links as you type.\n */\n autolink: boolean,\n /**\n * If enabled, links will be opened on click.\n */\n openOnClick: boolean,\n /**\n * Adds a link to the current selection if the pasted content only contains an url.\n */\n linkOnPaste: boolean,\n /**\n * A list of HTML attributes to be rendered.\n */\n HTMLAttributes: Record<string, any>,\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n link: {\n /**\n * Set a link mark\n */\n setLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Toggle a link mark\n */\n toggleLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Unset a link mark\n */\n unsetLink: () => ReturnType,\n }\n }\n}\n\nexport const Link = Mark.create<LinkOptions>({\n name: 'link',\n\n priority: 1000,\n\n keepOnSplit: false,\n\n inclusive() {\n return this.options.autolink\n },\n\n addOptions() {\n return {\n openOnClick: true,\n linkOnPaste: true,\n autolink: true,\n HTMLAttributes: {\n target: '_blank',\n rel: 'noopener noreferrer nofollow',\n },\n }\n },\n\n addAttributes() {\n return {\n href: {\n default: null,\n },\n target: {\n default: this.options.HTMLAttributes.target,\n },\n }\n },\n\n parseHTML() {\n return [\n { tag: 'a[href]' },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return [\n 'a',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n 0,\n ]\n },\n\n addCommands() {\n return {\n setLink: attributes => ({ commands }) => {\n return commands.setMark(this.name, attributes)\n },\n\n toggleLink: attributes => ({ commands }) => {\n return commands.toggleMark(this.name, attributes, { extendEmptyMarkRange: true })\n },\n\n unsetLink: () => ({ commands }) => {\n return commands.unsetMark(this.name, { extendEmptyMarkRange: true })\n },\n }\n },\n\n addPasteRules() {\n return [\n markPasteRule({\n find: text => find(text)\n .filter(link => link.isLink)\n .map(link => ({\n text: link.value,\n index: link.start,\n data: link,\n })),\n type: this.type,\n getAttributes: match => ({\n href: match.data?.href,\n }),\n }),\n ]\n },\n\n addProseMirrorPlugins() {\n const plugins = []\n\n if (this.options.autolink) {\n plugins.push(autolink({\n type: this.type,\n }))\n }\n\n if (this.options.openOnClick) {\n plugins.push(clickHandler({\n type: this.type,\n }))\n }\n\n if (this.options.linkOnPaste) {\n plugins.push(pasteHandler({\n editor: this.editor,\n type: this.type,\n }))\n }\n\n return plugins\n },\n})\n"],"names":[],"mappings":";;;;SAcwB,QAAQ,CAAC,OAAwB;IACvD,OAAO,IAAI,MAAM,CAAC;QAChB,GAAG,EAAE,IAAI,SAAS,CAAC,UAAU,CAAC;QAC9B,iBAAiB,EAAE,CAAC,YAAY,EAAE,QAAQ,EAAE,QAAQ;YAClD,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,UAAU,CAAC;mBACtE,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;YAEnC,IAAI,CAAC,UAAU,EAAE;gBACf,OAAM;aACP;YAED,MAAM,EAAE,EAAE,EAAE,GAAG,QAAQ,CAAA;YACvB,MAAM,SAAS,GAAG,uBAAuB,CAAC,QAAQ,CAAC,GAAG,EAAE,YAAY,CAAC,CAAA;YACrE,MAAM,EAAE,OAAO,EAAE,GAAG,SAAS,CAAA;YAC7B,MAAM,OAAO,GAAG,gBAAgB,CAAC,SAAS,CAAC,CAAA;YAE3C,OAAO,CAAC,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE;;gBAErC,eAAe,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,EAAE,QAAQ,CAAC,GAAG,CAAC;qBACtD,MAAM,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,OAAO,CA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sources":["../src/helpers/autolink.ts","../src/helpers/clickHandler.ts","../src/helpers/pasteHandler.ts","../src/link.ts"],"sourcesContent":["import {\n getMarksBetween,\n findChildrenInRange,\n combineTransactionSteps,\n getChangedRanges,\n} from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find, test } from 'linkifyjs'\n\ntype AutolinkOptions = {\n type: MarkType,\n}\n\nexport function autolink(options: AutolinkOptions): Plugin {\n return new Plugin({\n key: new PluginKey('autolink'),\n appendTransaction: (transactions, oldState, newState) => {\n const docChanges = transactions.some(transaction => transaction.docChanged)\n && !oldState.doc.eq(newState.doc)\n const preventAutolink = transactions.some(transaction => transaction.getMeta('preventAutolink'))\n\n if (!docChanges || preventAutolink) {\n return\n }\n\n const { tr } = newState\n const transform = combineTransactionSteps(oldState.doc, transactions)\n const { mapping } = transform\n const changes = getChangedRanges(transform)\n\n changes.forEach(({ oldRange, newRange }) => {\n // at first we check if we have to remove links\n getMarksBetween(oldRange.from, oldRange.to, oldState.doc)\n .filter(item => item.mark.type === options.type)\n .forEach(oldMark => {\n const newFrom = mapping.map(oldMark.from)\n const newTo = mapping.map(oldMark.to)\n const newMarks = getMarksBetween(newFrom, newTo, newState.doc)\n .filter(item => item.mark.type === options.type)\n\n if (!newMarks.length) {\n return\n }\n\n const newMark = newMarks[0]\n const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, undefined, ' ')\n const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, undefined, ' ')\n const wasLink = test(oldLinkText)\n const isLink = test(newLinkText)\n\n // remove only the link, if it was a link before too\n // because we don’t want to remove links that were set manually\n if (wasLink && !isLink) {\n tr.removeMark(newMark.from, newMark.to, options.type)\n }\n })\n\n // now let’s see if we can add new links\n findChildrenInRange(newState.doc, newRange, node => node.isTextblock)\n .forEach(textBlock => {\n // we need to define a placeholder for leaf nodes\n // so that the link position can be calculated correctly\n const text = newState.doc.textBetween(\n textBlock.pos,\n textBlock.pos + textBlock.node.nodeSize,\n undefined,\n ' ',\n )\n\n find(text)\n .filter(link => link.isLink)\n // calculate link position\n .map(link => ({\n ...link,\n from: textBlock.pos + link.start + 1,\n to: textBlock.pos + link.end + 1,\n }))\n // check if link is within the changed range\n .filter(link => {\n const fromIsInRange = newRange.from >= link.from && newRange.from <= link.to\n const toIsInRange = newRange.to >= link.from && newRange.to <= link.to\n\n return fromIsInRange || toIsInRange\n })\n // add link mark\n .forEach(link => {\n tr.addMark(link.from, link.to, options.type.create({\n href: link.href,\n }))\n })\n })\n })\n\n if (!tr.steps.length) {\n return\n }\n\n return tr\n },\n })\n}\n","import { getAttributes } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\n\ntype ClickHandlerOptions = {\n type: MarkType,\n}\n\nexport function clickHandler(options: ClickHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handleClickLink'),\n props: {\n handleClick: (view, pos, event) => {\n const attrs = getAttributes(view.state, options.type.name)\n const link = (event.target as HTMLElement)?.closest('a')\n\n if (link && attrs.href) {\n window.open(attrs.href, attrs.target)\n\n return true\n }\n\n return false\n },\n },\n })\n}\n","import { Editor } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find } from 'linkifyjs'\n\ntype PasteHandlerOptions = {\n editor: Editor,\n type: MarkType,\n}\n\nexport function pasteHandler(options: PasteHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handlePasteLink'),\n props: {\n handlePaste: (view, event, slice) => {\n const { state } = view\n const { selection } = state\n const { empty } = selection\n\n if (empty) {\n return false\n }\n\n let textContent = ''\n\n slice.content.forEach(node => {\n textContent += node.textContent\n })\n\n const link = find(textContent).find(item => item.isLink && item.value === textContent)\n\n if (!textContent || !link) {\n return false\n }\n\n options.editor.commands.setMark(options.type, {\n href: link.href,\n })\n\n return true\n },\n },\n })\n}\n","import { Mark, markPasteRule, mergeAttributes } from '@tiptap/core'\nimport { find } from 'linkifyjs'\nimport { autolink } from './helpers/autolink'\nimport { clickHandler } from './helpers/clickHandler'\nimport { pasteHandler } from './helpers/pasteHandler'\n\nexport interface LinkOptions {\n /**\n * If enabled, it adds links as you type.\n */\n autolink: boolean,\n /**\n * If enabled, links will be opened on click.\n */\n openOnClick: boolean,\n /**\n * Adds a link to the current selection if the pasted content only contains an url.\n */\n linkOnPaste: boolean,\n /**\n * A list of HTML attributes to be rendered.\n */\n HTMLAttributes: Record<string, any>,\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n link: {\n /**\n * Set a link mark\n */\n setLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Toggle a link mark\n */\n toggleLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Unset a link mark\n */\n unsetLink: () => ReturnType,\n }\n }\n}\n\nexport const Link = Mark.create<LinkOptions>({\n name: 'link',\n\n priority: 1000,\n\n keepOnSplit: false,\n\n inclusive() {\n return this.options.autolink\n },\n\n addOptions() {\n return {\n openOnClick: true,\n linkOnPaste: true,\n autolink: true,\n HTMLAttributes: {\n target: '_blank',\n rel: 'noopener noreferrer nofollow',\n },\n }\n },\n\n addAttributes() {\n return {\n href: {\n default: null,\n },\n target: {\n default: this.options.HTMLAttributes.target,\n },\n }\n },\n\n parseHTML() {\n return [\n { tag: 'a[href]' },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return [\n 'a',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n 0,\n ]\n },\n\n addCommands() {\n return {\n setLink: attributes => ({ chain }) => {\n return chain()\n .setMark(this.name, attributes)\n .setMeta('preventAutolink', true)\n .run()\n },\n\n toggleLink: attributes => ({ chain }) => {\n return chain()\n .toggleMark(this.name, attributes, { extendEmptyMarkRange: true })\n .setMeta('preventAutolink', true)\n .run()\n },\n\n unsetLink: () => ({ chain }) => {\n return chain()\n .unsetMark(this.name, { extendEmptyMarkRange: true })\n .setMeta('preventAutolink', true)\n .run()\n },\n }\n },\n\n addPasteRules() {\n return [\n markPasteRule({\n find: text => find(text)\n .filter(link => link.isLink)\n .map(link => ({\n text: link.value,\n index: link.start,\n data: link,\n })),\n type: this.type,\n getAttributes: match => ({\n href: match.data?.href,\n }),\n }),\n ]\n },\n\n addProseMirrorPlugins() {\n const plugins = []\n\n if (this.options.autolink) {\n plugins.push(autolink({\n type: this.type,\n }))\n }\n\n if (this.options.openOnClick) {\n plugins.push(clickHandler({\n type: this.type,\n }))\n }\n\n if (this.options.linkOnPaste) {\n plugins.push(pasteHandler({\n editor: this.editor,\n type: this.type,\n }))\n }\n\n return plugins\n },\n})\n"],"names":[],"mappings":";;;;SAcgB,QAAQ,CAAC,OAAwB;IAC/C,OAAO,IAAI,MAAM,CAAC;QAChB,GAAG,EAAE,IAAI,SAAS,CAAC,UAAU,CAAC;QAC9B,iBAAiB,EAAE,CAAC,YAAY,EAAE,QAAQ,EAAE,QAAQ;YAClD,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,UAAU,CAAC;mBACtE,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;YACnC,MAAM,eAAe,GAAG,YAAY,CAAC,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAA;YAEhG,IAAI,CAAC,UAAU,IAAI,eAAe,EAAE;gBAClC,OAAM;aACP;YAED,MAAM,EAAE,EAAE,EAAE,GAAG,QAAQ,CAAA;YACvB,MAAM,SAAS,GAAG,uBAAuB,CAAC,QAAQ,CAAC,GAAG,EAAE,YAAY,CAAC,CAAA;YACrE,MAAM,EAAE,OAAO,EAAE,GAAG,SAAS,CAAA;YAC7B,MAAM,OAAO,GAAG,gBAAgB,CAAC,SAAS,CAAC,CAAA;YAE3C,OAAO,CAAC,OAAO,CAAC,CAAC,EAAE,QAAQ,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B,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;YACzB,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC;gBACpB,IAAI,EAAE,IAAI,CAAC,IAAI;aAChB,CAAC,CAAC,CAAA;SACJ;QAED,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE;YAC5B,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC;gBACxB,IAAI,EAAE,IAAI,CAAC,IAAI;aAChB,CAAC,CAAC,CAAA;SACJ;QAED,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE;YAC5B,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC;gBACxB,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,IAAI,EAAE,IAAI,CAAC,IAAI;aAChB,CAAC,CAAC,CAAA;SACJ;QAED,OAAO,OAAO,CAAA;KACf;CACF;;;;"}
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
appendTransaction: (transactions, oldState, newState) => {
|
|
11
11
|
const docChanges = transactions.some(transaction => transaction.docChanged)
|
|
12
12
|
&& !oldState.doc.eq(newState.doc);
|
|
13
|
-
|
|
13
|
+
const preventAutolink = transactions.some(transaction => transaction.getMeta('preventAutolink'));
|
|
14
|
+
if (!docChanges || preventAutolink) {
|
|
14
15
|
return;
|
|
15
16
|
}
|
|
16
17
|
const { tr } = newState;
|
|
@@ -30,8 +31,8 @@
|
|
|
30
31
|
return;
|
|
31
32
|
}
|
|
32
33
|
const newMark = newMarks[0];
|
|
33
|
-
const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to);
|
|
34
|
-
const newLinkText = newState.doc.textBetween(newMark.from, newMark.to);
|
|
34
|
+
const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, undefined, ' ');
|
|
35
|
+
const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, undefined, ' ');
|
|
35
36
|
const wasLink = linkifyjs.test(oldLinkText);
|
|
36
37
|
const isLink = linkifyjs.test(newLinkText);
|
|
37
38
|
// remove only the link, if it was a link before too
|
|
@@ -43,7 +44,10 @@
|
|
|
43
44
|
// now let’s see if we can add new links
|
|
44
45
|
core.findChildrenInRange(newState.doc, newRange, node => node.isTextblock)
|
|
45
46
|
.forEach(textBlock => {
|
|
46
|
-
|
|
47
|
+
// we need to define a placeholder for leaf nodes
|
|
48
|
+
// so that the link position can be calculated correctly
|
|
49
|
+
const text = newState.doc.textBetween(textBlock.pos, textBlock.pos + textBlock.node.nodeSize, undefined, ' ');
|
|
50
|
+
linkifyjs.find(text)
|
|
47
51
|
.filter(link => link.isLink)
|
|
48
52
|
// calculate link position
|
|
49
53
|
.map(link => ({
|
|
@@ -161,14 +165,23 @@
|
|
|
161
165
|
},
|
|
162
166
|
addCommands() {
|
|
163
167
|
return {
|
|
164
|
-
setLink: attributes => ({
|
|
165
|
-
return
|
|
168
|
+
setLink: attributes => ({ chain }) => {
|
|
169
|
+
return chain()
|
|
170
|
+
.setMark(this.name, attributes)
|
|
171
|
+
.setMeta('preventAutolink', true)
|
|
172
|
+
.run();
|
|
166
173
|
},
|
|
167
|
-
toggleLink: attributes => ({
|
|
168
|
-
return
|
|
174
|
+
toggleLink: attributes => ({ chain }) => {
|
|
175
|
+
return chain()
|
|
176
|
+
.toggleMark(this.name, attributes, { extendEmptyMarkRange: true })
|
|
177
|
+
.setMeta('preventAutolink', true)
|
|
178
|
+
.run();
|
|
169
179
|
},
|
|
170
|
-
unsetLink: () => ({
|
|
171
|
-
return
|
|
180
|
+
unsetLink: () => ({ chain }) => {
|
|
181
|
+
return chain()
|
|
182
|
+
.unsetMark(this.name, { extendEmptyMarkRange: true })
|
|
183
|
+
.setMeta('preventAutolink', true)
|
|
184
|
+
.run();
|
|
172
185
|
},
|
|
173
186
|
};
|
|
174
187
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tiptap-extension-link.umd.js","sources":["../src/helpers/autolink.ts","../src/helpers/clickHandler.ts","../src/helpers/pasteHandler.ts","../src/link.ts"],"sourcesContent":["import {\n getMarksBetween,\n findChildrenInRange,\n combineTransactionSteps,\n getChangedRanges,\n} from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find, test } from 'linkifyjs'\n\ntype AutolinkOptions = {\n type: MarkType,\n}\n\nexport default function autolink(options: AutolinkOptions): Plugin {\n return new Plugin({\n key: new PluginKey('autolink'),\n appendTransaction: (transactions, oldState, newState) => {\n const docChanges = transactions.some(transaction => transaction.docChanged)\n && !oldState.doc.eq(newState.doc)\n\n if (!docChanges) {\n return\n }\n\n const { tr } = newState\n const transform = combineTransactionSteps(oldState.doc, transactions)\n const { mapping } = transform\n const changes = getChangedRanges(transform)\n\n changes.forEach(({ oldRange, newRange }) => {\n // at first we check if we have to remove links\n getMarksBetween(oldRange.from, oldRange.to, oldState.doc)\n .filter(item => item.mark.type === options.type)\n .forEach(oldMark => {\n const newFrom = mapping.map(oldMark.from)\n const newTo = mapping.map(oldMark.to)\n const newMarks = getMarksBetween(newFrom, newTo, newState.doc)\n .filter(item => item.mark.type === options.type)\n\n if (!newMarks.length) {\n return\n }\n\n const newMark = newMarks[0]\n const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to)\n const newLinkText = newState.doc.textBetween(newMark.from, newMark.to)\n const wasLink = test(oldLinkText)\n const isLink = test(newLinkText)\n\n // remove only the link, if it was a link before too\n // because we don’t want to remove links that were set manually\n if (wasLink && !isLink) {\n tr.removeMark(newMark.from, newMark.to, options.type)\n }\n })\n\n // now let’s see if we can add new links\n findChildrenInRange(newState.doc, newRange, node => node.isTextblock)\n .forEach(textBlock => {\n find(textBlock.node.textContent)\n .filter(link => link.isLink)\n // calculate link position\n .map(link => ({\n ...link,\n from: textBlock.pos + link.start + 1,\n to: textBlock.pos + link.end + 1,\n }))\n // check if link is within the changed range\n .filter(link => {\n const fromIsInRange = newRange.from >= link.from && newRange.from <= link.to\n const toIsInRange = newRange.to >= link.from && newRange.to <= link.to\n\n return fromIsInRange || toIsInRange\n })\n // add link mark\n .forEach(link => {\n tr.addMark(link.from, link.to, options.type.create({\n href: link.href,\n }))\n })\n })\n })\n\n if (!tr.steps.length) {\n return\n }\n\n return tr\n },\n })\n}\n","import { getAttributes } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\n\ntype ClickHandlerOptions = {\n type: MarkType,\n}\n\nexport default function clickHandler(options: ClickHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handleClickLink'),\n props: {\n handleClick: (view, pos, event) => {\n const attrs = getAttributes(view.state, options.type.name)\n const link = (event.target as HTMLElement)?.closest('a')\n\n if (link && attrs.href) {\n window.open(attrs.href, attrs.target)\n\n return true\n }\n\n return false\n },\n },\n })\n}\n","import { Editor } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find } from 'linkifyjs'\n\ntype PasteHandlerOptions = {\n editor: Editor,\n type: MarkType,\n}\n\nexport default function pasteHandler(options: PasteHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handlePasteLink'),\n props: {\n handlePaste: (view, event, slice) => {\n const { state } = view\n const { selection } = state\n const { empty } = selection\n\n if (empty) {\n return false\n }\n\n let textContent = ''\n\n slice.content.forEach(node => {\n textContent += node.textContent\n })\n\n const link = find(textContent).find(item => item.isLink && item.value === textContent)\n\n if (!textContent || !link) {\n return false\n }\n\n options.editor.commands.setMark(options.type, {\n href: link.href,\n })\n\n return true\n },\n },\n })\n}\n","import { Mark, markPasteRule, mergeAttributes } from '@tiptap/core'\nimport { find } from 'linkifyjs'\nimport autolink from './helpers/autolink'\nimport clickHandler from './helpers/clickHandler'\nimport pasteHandler from './helpers/pasteHandler'\n\nexport interface LinkOptions {\n /**\n * If enabled, it adds links as you type.\n */\n autolink: boolean,\n /**\n * If enabled, links will be opened on click.\n */\n openOnClick: boolean,\n /**\n * Adds a link to the current selection if the pasted content only contains an url.\n */\n linkOnPaste: boolean,\n /**\n * A list of HTML attributes to be rendered.\n */\n HTMLAttributes: Record<string, any>,\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n link: {\n /**\n * Set a link mark\n */\n setLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Toggle a link mark\n */\n toggleLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Unset a link mark\n */\n unsetLink: () => ReturnType,\n }\n }\n}\n\nexport const Link = Mark.create<LinkOptions>({\n name: 'link',\n\n priority: 1000,\n\n keepOnSplit: false,\n\n inclusive() {\n return this.options.autolink\n },\n\n addOptions() {\n return {\n openOnClick: true,\n linkOnPaste: true,\n autolink: true,\n HTMLAttributes: {\n target: '_blank',\n rel: 'noopener noreferrer nofollow',\n },\n }\n },\n\n addAttributes() {\n return {\n href: {\n default: null,\n },\n target: {\n default: this.options.HTMLAttributes.target,\n },\n }\n },\n\n parseHTML() {\n return [\n { tag: 'a[href]' },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return [\n 'a',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n 0,\n ]\n },\n\n addCommands() {\n return {\n setLink: attributes => ({ commands }) => {\n return commands.setMark(this.name, attributes)\n },\n\n toggleLink: attributes => ({ commands }) => {\n return commands.toggleMark(this.name, attributes, { extendEmptyMarkRange: true })\n },\n\n unsetLink: () => ({ commands }) => {\n return commands.unsetMark(this.name, { extendEmptyMarkRange: true })\n },\n }\n },\n\n addPasteRules() {\n return [\n markPasteRule({\n find: text => find(text)\n .filter(link => link.isLink)\n .map(link => ({\n text: link.value,\n index: link.start,\n data: link,\n })),\n type: this.type,\n getAttributes: match => ({\n href: match.data?.href,\n }),\n }),\n ]\n },\n\n addProseMirrorPlugins() {\n const plugins = []\n\n if (this.options.autolink) {\n plugins.push(autolink({\n type: this.type,\n }))\n }\n\n if (this.options.openOnClick) {\n plugins.push(clickHandler({\n type: this.type,\n }))\n }\n\n if (this.options.linkOnPaste) {\n plugins.push(pasteHandler({\n editor: this.editor,\n type: this.type,\n }))\n }\n\n return plugins\n },\n})\n"],"names":["Plugin","PluginKey","combineTransactionSteps","getChangedRanges","getMarksBetween","test","findChildrenInRange","find","getAttributes","Mark","mergeAttributes","markPasteRule"],"mappings":";;;;;;WAcwB,QAAQ,CAAC,OAAwB;MACvD,OAAO,IAAIA,uBAAM,CAAC;UAChB,GAAG,EAAE,IAAIC,0BAAS,CAAC,UAAU,CAAC;UAC9B,iBAAiB,EAAE,CAAC,YAAY,EAAE,QAAQ,EAAE,QAAQ;cAClD,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,UAAU,CAAC;qBACtE,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;cAEnC,IAAI,CAAC,UAAU,EAAE;kBACf,OAAM;eACP;cAED,MAAM,EAAE,EAAE,EAAE,GAAG,QAAQ,CAAA;cACvB,MAAM,SAAS,GAAGC,4BAAuB,CAAC,QAAQ,CAAC,GAAG,EAAE,YAAY,CAAC,CAAA;cACrE,MAAM,EAAE,OAAO,EAAE,GAAG,SAAS,CAAA;cAC7B,MAAM,OAAO,GAAGC,qBAAgB,CAAC,SAAS,CAAC,CAAA;cAE3C,OAAO,CAAC,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE;;kBAErCC,oBAAe,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,EAAE,QAAQ,CAAC,GAAG,CAAC;uBACtD,MAAM,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC;uBAC/C,OAAO,CAAC,OAAO;sBACd,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;sBACzC,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,O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AC;kBACZ,IAAI,EAAE,IAAI,IAAIJ,cAAI,CAAC,IAAI,CAAC;uBACrB,MAAM,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC;uBAC3B,GAAG,CAAC,IAAI,KAAK;sBACZ,IAAI,EAAE,IAAI,CAAC,KAAK;sBAChB,KAAK,EAAE,IAAI,CAAC,KAAK;sBACjB,IAAI,EAAE,IAAI;mBACX,CAAC,CAAC;kBACL,IAAI,EAAE,IAAI,CAAC,IAAI;kBACf,aAAa,EAAE,KAAK;;sBAAI,QAAC;0BACvB,IAAI,EAAE,MAAA,KAAK,CAAC,IAAI,0CAAE,IAAI;uBACvB,EAAC;mBAAA;eACH,CAAC;WACH,CAAA;OACF;MAED,qBAAqB;UACnB,MAAM,OAAO,GAAG,EAAE,CAAA;UAElB,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;cACzB,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC;kBACpB,IAAI,EAAE,IAAI,CAAC,IAAI;eAChB,CAAC,CAAC,CAAA;WACJ;UAED,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE;cAC5B,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC;kBACxB,IAAI,EAAE,IAAI,CAAC,IAAI;eAChB,CAAC,CAAC,CAAA;WACJ;UAED,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE;cAC5B,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC;kBACxB,MAAM,EAAE,IAAI,CAAC,MAAM;kBACnB,IAAI,EAAE,IAAI,CAAC,IAAI;eAChB,CAAC,CAAC,CAAA;WACJ;UAED,OAAO,OAAO,CAAA;OACf;GACF;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"tiptap-extension-link.umd.js","sources":["../src/helpers/autolink.ts","../src/helpers/clickHandler.ts","../src/helpers/pasteHandler.ts","../src/link.ts"],"sourcesContent":["import {\n getMarksBetween,\n findChildrenInRange,\n combineTransactionSteps,\n getChangedRanges,\n} from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find, test } from 'linkifyjs'\n\ntype AutolinkOptions = {\n type: MarkType,\n}\n\nexport function autolink(options: AutolinkOptions): Plugin {\n return new Plugin({\n key: new PluginKey('autolink'),\n appendTransaction: (transactions, oldState, newState) => {\n const docChanges = transactions.some(transaction => transaction.docChanged)\n && !oldState.doc.eq(newState.doc)\n const preventAutolink = transactions.some(transaction => transaction.getMeta('preventAutolink'))\n\n if (!docChanges || preventAutolink) {\n return\n }\n\n const { tr } = newState\n const transform = combineTransactionSteps(oldState.doc, transactions)\n const { mapping } = transform\n const changes = getChangedRanges(transform)\n\n changes.forEach(({ oldRange, newRange }) => {\n // at first we check if we have to remove links\n getMarksBetween(oldRange.from, oldRange.to, oldState.doc)\n .filter(item => item.mark.type === options.type)\n .forEach(oldMark => {\n const newFrom = mapping.map(oldMark.from)\n const newTo = mapping.map(oldMark.to)\n const newMarks = getMarksBetween(newFrom, newTo, newState.doc)\n .filter(item => item.mark.type === options.type)\n\n if (!newMarks.length) {\n return\n }\n\n const newMark = newMarks[0]\n const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, undefined, ' ')\n const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, undefined, ' ')\n const wasLink = test(oldLinkText)\n const isLink = test(newLinkText)\n\n // remove only the link, if it was a link before too\n // because we don’t want to remove links that were set manually\n if (wasLink && !isLink) {\n tr.removeMark(newMark.from, newMark.to, options.type)\n }\n })\n\n // now let’s see if we can add new links\n findChildrenInRange(newState.doc, newRange, node => node.isTextblock)\n .forEach(textBlock => {\n // we need to define a placeholder for leaf nodes\n // so that the link position can be calculated correctly\n const text = newState.doc.textBetween(\n textBlock.pos,\n textBlock.pos + textBlock.node.nodeSize,\n undefined,\n ' ',\n )\n\n find(text)\n .filter(link => link.isLink)\n // calculate link position\n .map(link => ({\n ...link,\n from: textBlock.pos + link.start + 1,\n to: textBlock.pos + link.end + 1,\n }))\n // check if link is within the changed range\n .filter(link => {\n const fromIsInRange = newRange.from >= link.from && newRange.from <= link.to\n const toIsInRange = newRange.to >= link.from && newRange.to <= link.to\n\n return fromIsInRange || toIsInRange\n })\n // add link mark\n .forEach(link => {\n tr.addMark(link.from, link.to, options.type.create({\n href: link.href,\n }))\n })\n })\n })\n\n if (!tr.steps.length) {\n return\n }\n\n return tr\n },\n })\n}\n","import { getAttributes } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\n\ntype ClickHandlerOptions = {\n type: MarkType,\n}\n\nexport function clickHandler(options: ClickHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handleClickLink'),\n props: {\n handleClick: (view, pos, event) => {\n const attrs = getAttributes(view.state, options.type.name)\n const link = (event.target as HTMLElement)?.closest('a')\n\n if (link && attrs.href) {\n window.open(attrs.href, attrs.target)\n\n return true\n }\n\n return false\n },\n },\n })\n}\n","import { Editor } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { MarkType } from 'prosemirror-model'\nimport { find } from 'linkifyjs'\n\ntype PasteHandlerOptions = {\n editor: Editor,\n type: MarkType,\n}\n\nexport function pasteHandler(options: PasteHandlerOptions): Plugin {\n return new Plugin({\n key: new PluginKey('handlePasteLink'),\n props: {\n handlePaste: (view, event, slice) => {\n const { state } = view\n const { selection } = state\n const { empty } = selection\n\n if (empty) {\n return false\n }\n\n let textContent = ''\n\n slice.content.forEach(node => {\n textContent += node.textContent\n })\n\n const link = find(textContent).find(item => item.isLink && item.value === textContent)\n\n if (!textContent || !link) {\n return false\n }\n\n options.editor.commands.setMark(options.type, {\n href: link.href,\n })\n\n return true\n },\n },\n })\n}\n","import { Mark, markPasteRule, mergeAttributes } from '@tiptap/core'\nimport { find } from 'linkifyjs'\nimport { autolink } from './helpers/autolink'\nimport { clickHandler } from './helpers/clickHandler'\nimport { pasteHandler } from './helpers/pasteHandler'\n\nexport interface LinkOptions {\n /**\n * If enabled, it adds links as you type.\n */\n autolink: boolean,\n /**\n * If enabled, links will be opened on click.\n */\n openOnClick: boolean,\n /**\n * Adds a link to the current selection if the pasted content only contains an url.\n */\n linkOnPaste: boolean,\n /**\n * A list of HTML attributes to be rendered.\n */\n HTMLAttributes: Record<string, any>,\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n link: {\n /**\n * Set a link mark\n */\n setLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Toggle a link mark\n */\n toggleLink: (attributes: { href: string, target?: string }) => ReturnType,\n /**\n * Unset a link mark\n */\n unsetLink: () => ReturnType,\n }\n }\n}\n\nexport const Link = Mark.create<LinkOptions>({\n name: 'link',\n\n priority: 1000,\n\n keepOnSplit: false,\n\n inclusive() {\n return this.options.autolink\n },\n\n addOptions() {\n return {\n openOnClick: true,\n linkOnPaste: true,\n autolink: true,\n HTMLAttributes: {\n target: '_blank',\n rel: 'noopener noreferrer nofollow',\n },\n }\n },\n\n addAttributes() {\n return {\n href: {\n default: null,\n },\n target: {\n default: this.options.HTMLAttributes.target,\n },\n }\n },\n\n parseHTML() {\n return [\n { tag: 'a[href]' },\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n return [\n 'a',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n 0,\n ]\n },\n\n addCommands() {\n return {\n setLink: attributes => ({ chain }) => {\n return chain()\n .setMark(this.name, attributes)\n .setMeta('preventAutolink', true)\n .run()\n },\n\n toggleLink: attributes => ({ chain }) => {\n return chain()\n .toggleMark(this.name, attributes, { extendEmptyMarkRange: true })\n .setMeta('preventAutolink', true)\n .run()\n },\n\n unsetLink: () => ({ chain }) => {\n return chain()\n .unsetMark(this.name, { extendEmptyMarkRange: true })\n .setMeta('preventAutolink', true)\n .run()\n },\n }\n },\n\n addPasteRules() {\n return [\n markPasteRule({\n find: text => find(text)\n .filter(link => link.isLink)\n .map(link => ({\n text: link.value,\n index: link.start,\n data: link,\n })),\n type: this.type,\n getAttributes: match => ({\n href: match.data?.href,\n }),\n }),\n ]\n },\n\n addProseMirrorPlugins() {\n const plugins = []\n\n if (this.options.autolink) {\n plugins.push(autolink({\n type: this.type,\n }))\n }\n\n if (this.options.openOnClick) {\n plugins.push(clickHandler({\n type: this.type,\n }))\n }\n\n if (this.options.linkOnPaste) {\n plugins.push(pasteHandler({\n editor: this.editor,\n type: this.type,\n }))\n }\n\n return plugins\n },\n})\n"],"names":["Plugin","PluginKey","combineTransactionSteps","getChangedRanges","getMarksBetween","test","findChildrenInRange","find","getAttributes","Mark","mergeAttributes","markPasteRule"],"mappings":";;;;;;WAcgB,QAAQ,CAAC,OAAwB;MAC/C,OAAO,IAAIA,uBAAM,CAAC;UAChB,GAAG,EAAE,IAAIC,0BAAS,CAAC,UAAU,CAAC;UAC9B,iBAAiB,EAAE,CAAC,YAAY,EAAE,QAAQ,EAAE,QAAQ;cAClD,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,UAAU,CAAC;qBACtE,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;cACnC,MAAM,eAAe,GAAG,YAAY,CAAC,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAA;cAEhG,IAAI,CAAC,UAAU,IAAI,eAAe,EAAE;kBAClC,OAAM;eACP;cAED,MAAM,EAAE,EAAE,EAAE,GAAG,QAAQ,CAAA;cACvB,MAAM,SAAS,GAAGC,4BAAuB,CAAC,QAAQ,CAAC,GAAG,EAAE,YAAY,CAAC,CAAA;cACrE,MAAM,EAAE,OAAO,EAAE,GAAG,SAAS,CAAA;cAC7B,MAAM,OAAO,GAAGC,qBAAgB,CAAC,SAAS,CAAC,CAAA;cAE3C,OAAO,CAAC,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE;;kBAErCC,oBAAe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AAI,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;cACzB,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC;kBACpB,IAAI,EAAE,IAAI,CAAC,IAAI;eAChB,CAAC,CAAC,CAAA;WACJ;UAED,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE;cAC5B,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC;kBACxB,IAAI,EAAE,IAAI,CAAC,IAAI;eAChB,CAAC,CAAC,CAAA;WACJ;UAED,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE;cAC5B,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC;kBACxB,MAAM,EAAE,IAAI,CAAC,MAAM;kBACnB,IAAI,EAAE,IAAI,CAAC,IAAI;eAChB,CAAC,CAAC,CAAA;WACJ;UAED,OAAO,OAAO,CAAA;OACf;GACF;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiptap/extension-link",
|
|
3
3
|
"description": "link extension for tiptap",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.35",
|
|
5
5
|
"homepage": "https://tiptap.dev",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tiptap",
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"@tiptap/core": "^2.0.0-beta.1"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"linkifyjs": "^3.0.
|
|
28
|
-
"prosemirror-model": "^1.
|
|
27
|
+
"linkifyjs": "^3.0.5",
|
|
28
|
+
"prosemirror-model": "^1.16.1",
|
|
29
29
|
"prosemirror-state": "^1.3.4"
|
|
30
30
|
},
|
|
31
31
|
"repository": {
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"url": "https://github.com/ueberdosis/tiptap",
|
|
34
34
|
"directory": "packages/extension-link"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "5144a6cd1b12a161b5879efab22d2dda0c2a20ba"
|
|
37
37
|
}
|
package/src/helpers/autolink.ts
CHANGED
|
@@ -12,14 +12,15 @@ type AutolinkOptions = {
|
|
|
12
12
|
type: MarkType,
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export
|
|
15
|
+
export function autolink(options: AutolinkOptions): Plugin {
|
|
16
16
|
return new Plugin({
|
|
17
17
|
key: new PluginKey('autolink'),
|
|
18
18
|
appendTransaction: (transactions, oldState, newState) => {
|
|
19
19
|
const docChanges = transactions.some(transaction => transaction.docChanged)
|
|
20
20
|
&& !oldState.doc.eq(newState.doc)
|
|
21
|
+
const preventAutolink = transactions.some(transaction => transaction.getMeta('preventAutolink'))
|
|
21
22
|
|
|
22
|
-
if (!docChanges) {
|
|
23
|
+
if (!docChanges || preventAutolink) {
|
|
23
24
|
return
|
|
24
25
|
}
|
|
25
26
|
|
|
@@ -43,8 +44,8 @@ export default function autolink(options: AutolinkOptions): Plugin {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
const newMark = newMarks[0]
|
|
46
|
-
const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to)
|
|
47
|
-
const newLinkText = newState.doc.textBetween(newMark.from, newMark.to)
|
|
47
|
+
const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, undefined, ' ')
|
|
48
|
+
const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, undefined, ' ')
|
|
48
49
|
const wasLink = test(oldLinkText)
|
|
49
50
|
const isLink = test(newLinkText)
|
|
50
51
|
|
|
@@ -58,7 +59,16 @@ export default function autolink(options: AutolinkOptions): Plugin {
|
|
|
58
59
|
// now let’s see if we can add new links
|
|
59
60
|
findChildrenInRange(newState.doc, newRange, node => node.isTextblock)
|
|
60
61
|
.forEach(textBlock => {
|
|
61
|
-
|
|
62
|
+
// we need to define a placeholder for leaf nodes
|
|
63
|
+
// so that the link position can be calculated correctly
|
|
64
|
+
const text = newState.doc.textBetween(
|
|
65
|
+
textBlock.pos,
|
|
66
|
+
textBlock.pos + textBlock.node.nodeSize,
|
|
67
|
+
undefined,
|
|
68
|
+
' ',
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
find(text)
|
|
62
72
|
.filter(link => link.isLink)
|
|
63
73
|
// calculate link position
|
|
64
74
|
.map(link => ({
|
|
@@ -6,7 +6,7 @@ type ClickHandlerOptions = {
|
|
|
6
6
|
type: MarkType,
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export
|
|
9
|
+
export function clickHandler(options: ClickHandlerOptions): Plugin {
|
|
10
10
|
return new Plugin({
|
|
11
11
|
key: new PluginKey('handleClickLink'),
|
|
12
12
|
props: {
|
|
@@ -8,7 +8,7 @@ type PasteHandlerOptions = {
|
|
|
8
8
|
type: MarkType,
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export
|
|
11
|
+
export function pasteHandler(options: PasteHandlerOptions): Plugin {
|
|
12
12
|
return new Plugin({
|
|
13
13
|
key: new PluginKey('handlePasteLink'),
|
|
14
14
|
props: {
|
package/src/link.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Mark, markPasteRule, mergeAttributes } from '@tiptap/core'
|
|
2
2
|
import { find } from 'linkifyjs'
|
|
3
|
-
import autolink from './helpers/autolink'
|
|
4
|
-
import clickHandler from './helpers/clickHandler'
|
|
5
|
-
import pasteHandler from './helpers/pasteHandler'
|
|
3
|
+
import { autolink } from './helpers/autolink'
|
|
4
|
+
import { clickHandler } from './helpers/clickHandler'
|
|
5
|
+
import { pasteHandler } from './helpers/pasteHandler'
|
|
6
6
|
|
|
7
7
|
export interface LinkOptions {
|
|
8
8
|
/**
|
|
@@ -92,16 +92,25 @@ export const Link = Mark.create<LinkOptions>({
|
|
|
92
92
|
|
|
93
93
|
addCommands() {
|
|
94
94
|
return {
|
|
95
|
-
setLink: attributes => ({
|
|
96
|
-
return
|
|
95
|
+
setLink: attributes => ({ chain }) => {
|
|
96
|
+
return chain()
|
|
97
|
+
.setMark(this.name, attributes)
|
|
98
|
+
.setMeta('preventAutolink', true)
|
|
99
|
+
.run()
|
|
97
100
|
},
|
|
98
101
|
|
|
99
|
-
toggleLink: attributes => ({
|
|
100
|
-
return
|
|
102
|
+
toggleLink: attributes => ({ chain }) => {
|
|
103
|
+
return chain()
|
|
104
|
+
.toggleMark(this.name, attributes, { extendEmptyMarkRange: true })
|
|
105
|
+
.setMeta('preventAutolink', true)
|
|
106
|
+
.run()
|
|
101
107
|
},
|
|
102
108
|
|
|
103
|
-
unsetLink: () => ({
|
|
104
|
-
return
|
|
109
|
+
unsetLink: () => ({ chain }) => {
|
|
110
|
+
return chain()
|
|
111
|
+
.unsetMark(this.name, { extendEmptyMarkRange: true })
|
|
112
|
+
.setMeta('preventAutolink', true)
|
|
113
|
+
.run()
|
|
105
114
|
},
|
|
106
115
|
}
|
|
107
116
|
},
|