From d8c8450d84d630a51b52d35831a96e026da2221d Mon Sep 17 00:00:00 2001 From: grnd-alt Date: Tue, 11 Feb 2025 11:38:39 +0100 Subject: [PATCH] fix: replace link correctly Signed-off-by: grnd-alt --- cypress/e2e/marks/Link.spec.js | 1 - src/marks/Link.js | 17 +++++------- src/tests/marks/Link.spec.js | 47 ++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 src/tests/marks/Link.spec.js diff --git a/cypress/e2e/marks/Link.spec.js b/cypress/e2e/marks/Link.spec.js index f2ccb7dd2d7..76df2282594 100644 --- a/cypress/e2e/marks/Link.spec.js +++ b/cypress/e2e/marks/Link.spec.js @@ -53,7 +53,6 @@ describe('Link marks', { retries: 0 }, () => { editor.commands.insertOrSetLink('https://nextcloud.com', { href: 'https://nextcloud.com' }) expectMarkdown(editor, 'he\n\n\n\nllo') }) - }) /** diff --git a/src/marks/Link.js b/src/marks/Link.js index 8a261845024..3ee822b7b72 100644 --- a/src/marks/Link.js +++ b/src/marks/Link.js @@ -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({ @@ -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) diff --git a/src/tests/marks/Link.spec.js b/src/tests/marks/Link.spec.js new file mode 100644 index 00000000000..2aa08694d6d --- /dev/null +++ b/src/tests/marks/Link.spec.js @@ -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: '

Test HELLO WORLD

', + extensions: [Link,Underline], + }) + expect(editor.commands).toHaveProperty('insertOrSetLink') + }) + + it('should update link if anchor has mark', () => { + const editor = createCustomEditor({ + content: '

Test HELLO WORLD

', + 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: '

TestTest

', + 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: '

Test HELLO WORLD

', + 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"}) + }) + + +})