Skip to content

Commit

Permalink
fix(extension-link): fix paste handling
Browse files Browse the repository at this point in the history
* do not dispatch transaction without any links getting pasted
* prevent onPaste handling in code blocks
  • Loading branch information
svenadlung authored May 25, 2023
1 parent 614fc80 commit d19267e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
16 changes: 8 additions & 8 deletions packages/extension-link/src/helpers/autolink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function autolink(options: AutolinkOptions): Plugin {
let needsAutolink = true

changes.forEach(({ oldRange, newRange }) => {
// at first we check if we have to remove links
// At first we check if we have to remove links.
getMarksBetween(oldRange.from, oldRange.to, oldState.doc)
.filter(item => item.mark.type === options.type)
.forEach(oldMark => {
Expand All @@ -56,14 +56,14 @@ export function autolink(options: AutolinkOptions): Plugin {
needsAutolink = false
}

// remove only the link, if it was a link before too
// because we don’t want to remove links that were set manually
// Remove only the link, if it was a link before too.
// Because we don’t want to remove links that were set manually.
if (wasLink && !isLink) {
tr.removeMark(needsAutolink ? newMark.from : newMark.to - 1, newMark.to, options.type)
}
})

// now let’s see if we can add new links
// Now let’s see if we can add new links.
const nodesInChangedRanges = findChildrenInRange(
newState.doc,
newRange,
Expand All @@ -74,7 +74,7 @@ export function autolink(options: AutolinkOptions): Plugin {
let textBeforeWhitespace: string | undefined

if (nodesInChangedRanges.length > 1) {
// Grab the first node within the changed ranges (ex. the first of two paragraphs when hitting enter)
// Grab the first node within the changed ranges (ex. the first of two paragraphs when hitting enter).
textBlock = nodesInChangedRanges[0]
textBeforeWhitespace = newState.doc.textBetween(
textBlock.pos,
Expand All @@ -84,7 +84,7 @@ export function autolink(options: AutolinkOptions): Plugin {
)
} else if (
nodesInChangedRanges.length
// We want to make sure to include the block seperator argument to treat hard breaks like spaces
// We want to make sure to include the block seperator argument to treat hard breaks like spaces.
&& newState.doc.textBetween(newRange.from, newRange.to, ' ', ' ').endsWith(' ')
) {
textBlock = nodesInChangedRanges[0]
Expand Down Expand Up @@ -118,13 +118,13 @@ export function autolink(options: AutolinkOptions): Plugin {
}
return true
})
// calculate link position
// Calculate link position.
.map(link => ({
...link,
from: lastWordAndBlockOffset + link.start + 1,
to: lastWordAndBlockOffset + link.end + 1,
}))
// add link mark
// Add link mark.
.forEach(link => {
if (getMarksBetween(link.from, link.to, newState.doc).some(item => item.mark.type === options.type)) {
return
Expand Down
36 changes: 22 additions & 14 deletions packages/extension-link/src/helpers/pasteHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ export function pasteHandler(options: PasteHandlerOptions): Plugin {
const { state } = view
const { selection } = state

// Do not proceed if in code block.
if (state.doc.resolve(selection.from).parent.type.spec.code) {
return false
}

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 +36,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 +66,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 +84,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 +95,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 d19267e

Please sign in to comment.