From 0e2df9d1f530bc7980a476afa269da29159d5892 Mon Sep 17 00:00:00 2001 From: svenadlung Date: Wed, 24 May 2023 19:47:03 +0200 Subject: [PATCH] do not dispatch transaction without any links getting pasted --- .../src/helpers/pasteHandler.ts | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/extension-link/src/helpers/pasteHandler.ts b/packages/extension-link/src/helpers/pasteHandler.ts index 0d4c834032c..eaf0fda2faa 100644 --- a/packages/extension-link/src/helpers/pasteHandler.ts +++ b/packages/extension-link/src/helpers/pasteHandler.ts @@ -18,8 +18,11 @@ export function pasteHandler(options: PasteHandlerOptions): Plugin { const { selection } = state const pastedLinkMarks: Mark[] = [] + let textContent = '' slice.content.forEach(node => { + textContent += node.textContent + node.marks.forEach(mark => { if (mark.type.name === options.type.name) { pastedLinkMarks.push(mark) @@ -28,32 +31,28 @@ export function pasteHandler(options: PasteHandlerOptions): Plugin { }) const hasPastedLink = pastedLinkMarks.length > 0 - - let textContent = '' - - slice.content.forEach(node => { - textContent += node.textContent - }) - const link = find(textContent).find(item => item.isLink && item.value === textContent) if (!selection.empty && options.linkOnPaste) { const pastedLink = hasPastedLink ? pastedLinkMarks[0].attrs.href : link?.href || null if (pastedLink) { - options.editor.commands.setMark(options.type, { - href: pastedLink, - }) + options.editor.commands.setMark(options.type, { href: pastedLink }) + return true } } - if (slice.content.firstChild?.type.name === 'text' && slice.content.firstChild?.marks.some(mark => mark.type.name === options.type.name)) { + const firstChildIsText = slice.content.firstChild?.type.name === 'text' + const firstChildContainsLinkMark = slice.content.firstChild?.marks.some(mark => mark.type.name === options.type.name) + + if (firstChildIsText && firstChildContainsLinkMark) { return false } if (link && selection.empty) { options.editor.commands.insertContent(`${link.href}`) + return true } @@ -62,13 +61,15 @@ export function pasteHandler(options: PasteHandlerOptions): Plugin { if (!selection.empty) { deleteOnly = true + tr.delete(selection.from, selection.to) } let currentPos = selection.from + let fragmentLinks = [] slice.content.forEach(node => { - const fragmentLinks = find(node.textContent) + fragmentLinks = find(node.textContent) tr.insert(currentPos - 1, node) @@ -78,7 +79,6 @@ export function pasteHandler(options: PasteHandlerOptions): Plugin { fragmentLinks.forEach(fragmentLink => { const linkStart = currentPos + fragmentLink.start const linkEnd = currentPos + fragmentLink.end - const hasMark = tr.doc.rangeHasMark(linkStart, linkEnd, options.type) if (!hasMark) { @@ -90,8 +90,11 @@ export function pasteHandler(options: PasteHandlerOptions): Plugin { currentPos += node.nodeSize }) - if (tr.docChanged && !deleteOnly) { + const hasFragmentLinks = fragmentLinks.length > 0 + + if (tr.docChanged && !deleteOnly && hasFragmentLinks) { options.editor.view.dispatch(tr) + return true }