Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(link): Fix autolinking and pasting #4292

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 14 additions & 10 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 All @@ -51,7 +55,7 @@ export function pasteHandler(options: PasteHandlerOptions): Plugin {
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) {
if ((firstChildIsText && firstChildContainsLinkMark) || !options.linkOnPaste) {
return false
}

Expand Down