Skip to content

Commit

Permalink
do not dispatch transaction without any links getting pasted
Browse files Browse the repository at this point in the history
  • Loading branch information
svenadlung committed May 24, 2023
1 parent d61a621 commit 0e2df9d
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions packages/extension-link/src/helpers/pasteHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(`<a href="${link.href}">${link.href}</a>`)

return true
}

Expand All @@ -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)

Expand All @@ -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) {
Expand All @@ -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
}

Expand Down

0 comments on commit 0e2df9d

Please sign in to comment.