Skip to content

Commit

Permalink
fix: replace link correctly
Browse files Browse the repository at this point in the history
Signed-off-by: grnd-alt <[email protected]>
  • Loading branch information
grnd-alt committed Feb 12, 2025
1 parent 6dc849a commit d8c8450
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
1 change: 0 additions & 1 deletion cypress/e2e/marks/Link.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ describe('Link marks', { retries: 0 }, () => {
editor.commands.insertOrSetLink('https://nextcloud.com', { href: 'https://nextcloud.com' })
expectMarkdown(editor, 'he\n\n<https://nextcloud.com>\n\nllo')
})

})

/**
Expand Down
17 changes: 7 additions & 10 deletions src/marks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import TipTapLink from '@tiptap/extension-link'
import { domHref, parseHref } from './../helpers/links.js'
import { linkClicking } from '../plugins/links.js'
import { isMarkActive } from '@tiptap/core'
import { getMarkRange, isMarkActive } from '@tiptap/core'

const Link = TipTapLink.extend({

Expand Down Expand Up @@ -79,18 +79,15 @@ const Link = TipTapLink.extend({
// if not insert the link using the given text property
if (state.selection.empty) {
if (isMarkActive(state, this.name)) {
commands.deleteNode('paragraph')
commands.deleteRange(getMarkRange(state.selection.$anchor, state.schema.marks.link))
}
return chain().insertContent({
type: 'paragraph',
content: [{
type: 'text',
marks: [{
type: 'link',
attrs,
}],
text,
type: 'text',
marks: [{
type: 'link',
attrs,
}],
text,
})
} else {
return commands.setLink(attrs)
Expand Down
47 changes: 47 additions & 0 deletions src/tests/marks/Link.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Link from './../../marks/Link';
import { Underline } from '../../marks';
import { getExtensionField, getMarkRange } from '@tiptap/core'
import { createCustomEditor } from '../helpers'
import { Editor } from '@tiptap/vue-2';

describe('Link extension integrated in the editor', () => {

it('should have link available in commands', () => {
const editor = createCustomEditor({
content: '<p><a href="nextcloud.com">Te<u>s</u>t</a> HELLO WORLD</p>',
extensions: [Link,Underline],
})
expect(editor.commands).toHaveProperty('insertOrSetLink')
})

it('should update link if anchor has mark', () => {
const editor = createCustomEditor({
content: '<p><a href="nextcloud.com">Te<u>s</u>t</a> HELLO WORLD</p>',
extensions: [Link,Underline],
})
editor.commands.setTextSelection(3)
editor.commands.insertOrSetLink('updated.de', {href: 'updated.de'})
expect(editor.getJSON()).toEqual({"content": [{"content": [{"marks": [{"attrs": {"href": "updated.de", "title": null}, "type": "link"}], "text": "updated.de", "type": "text"}, {"text": " HELLO WORLD", "type": "text"}], "type": "paragraph"}], "type": "doc"})
})

it('Should only update the anchor is on', () => {
const editor = createCustomEditor({
content: '<p><a href="nextcloud.com">Test</a><a href="nextcloud.com">Test</a></p>',
extensions: [Link],
})
editor.commands.setTextSelection(3)
editor.commands.insertOrSetLink('updated.de', {href: 'updated.de'})
expect(editor.getJSON()).toEqual({"content": [{"content": [{"marks": [{"attrs": {"href": "updated.de", "title": null}, "type": "link"}], "text": "updated.de", "type": "text"}, ], "type": "paragraph"}], "type": "doc"})
})

it('should insert new link if none at anchor', () => {
const editor = createCustomEditor({
content: '<p><a href="nextcloud.com">Test</a> HELLO WORLD</p>',
extensions: [Link],
})
editor.commands.setTextSelection(10)
expect(editor.getJSON()).toEqual({"content": [{"content": [{"marks": [{"attrs": {"href": "nextcloud.com", "title": null}, "type": "link"}], "text": "Test", "type": "text"}, {"text": " HELLO WORLD", "type": "text"}], "type": "paragraph"}], "type": "doc"})
})


})

0 comments on commit d8c8450

Please sign in to comment.