Skip to content

Commit

Permalink
fix link on paste breaking links / link behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbch committed Aug 7, 2023
1 parent 4bfa96e commit 9b7ef50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 44 deletions.
37 changes: 2 additions & 35 deletions packages/extension-link/src/helpers/autolink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@tiptap/core'
import { MarkType } from '@tiptap/pm/model'
import { Plugin, PluginKey } from '@tiptap/pm/state'
import { find, test } from 'linkifyjs'
import { find } from 'linkifyjs'

type AutolinkOptions = {
type: MarkType
Expand All @@ -27,42 +27,9 @@ export function autolink(options: AutolinkOptions): Plugin {

const { tr } = newState
const transform = combineTransactionSteps(oldState.doc, [...transactions])
const { mapping } = transform
const changes = getChangedRanges(transform)
let needsAutolink = true

changes.forEach(({ oldRange, newRange }) => {
// 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 => {
const newFrom = mapping.map(oldMark.from)
const newTo = mapping.map(oldMark.to)
const newMarks = getMarksBetween(newFrom, newTo, newState.doc).filter(
item => item.mark.type === options.type,
)

if (!newMarks.length) {
return
}

const newMark = newMarks[0]
const oldLinkText = oldState.doc.textBetween(oldMark.from, oldMark.to, undefined, ' ')
const newLinkText = newState.doc.textBetween(newMark.from, newMark.to, undefined, ' ')
const wasLink = test(oldLinkText)
const isLink = test(newLinkText)

if (wasLink) {
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.
if (wasLink && !isLink) {
tr.removeMark(needsAutolink ? newMark.from : newMark.to - 1, newMark.to, options.type)
}
})

changes.forEach(({ newRange }) => {
// Now let’s see if we can add new links.
const nodesInChangedRanges = findChildrenInRange(
newState.doc,
Expand Down
22 changes: 13 additions & 9 deletions packages/extension-link/src/helpers/pasteHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Editor } from '@tiptap/core'
import { Mark, MarkType } from '@tiptap/pm/model'
import { MarkType } from '@tiptap/pm/model'
import { Plugin, PluginKey } from '@tiptap/pm/state'
import { find } from 'linkifyjs'

Expand All @@ -22,24 +22,28 @@ export function pasteHandler(options: PasteHandlerOptions): Plugin {
return false
}

const pastedLinkMarks: Mark[] = []
let textContent = ''

slice.content.forEach(node => {
textContent += node.textContent
})

let isAlreadyLink = false

node.marks.forEach(mark => {
if (mark.type.name === options.type.name) {
pastedLinkMarks.push(mark)
}
})
slice.content.descendants(node => {
if (node.marks.some(mark => mark.type.name === options.type.name)) {
isAlreadyLink = true
}
})

const hasPastedLink = pastedLinkMarks.length > 0
if (isAlreadyLink) {
return
}

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
const pastedLink = link?.href || null

if (pastedLink) {
options.editor.commands.setMark(options.type, { href: pastedLink })
Expand Down

0 comments on commit 9b7ef50

Please sign in to comment.