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 5636ec1 commit 1288fd9
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 @@ -7,7 +7,7 @@ import { markInputRule } from '@tiptap/core'
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'

Check failure on line 10 in src/marks/Link.js

View workflow job for this annotation

GitHub Actions / NPM lint

'/home/runner/actions-runner/_work/text/text/node_modules/@tiptap/core/dist/index.js' imported multiple times

const PROTOCOLS_TO_LINK_TO = ['http:', 'https:', 'mailto:', 'tel:']

Expand Down Expand Up @@ -100,18 +100,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';

Check warning on line 1 in src/tests/marks/Link.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing file extension "js" for "./../../marks/Link"

Check failure on line 1 in src/tests/marks/Link.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

"./../../marks/Link" is not found

Check failure on line 1 in src/tests/marks/Link.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
import { Underline } from '../../marks';

Check warning on line 2 in src/tests/marks/Link.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing file extension "js" for "../../marks"

Check failure on line 2 in src/tests/marks/Link.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

"../../marks" is not found

Check failure on line 2 in src/tests/marks/Link.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
import { getExtensionField, getMarkRange } from '@tiptap/core'

Check failure on line 3 in src/tests/marks/Link.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

'getExtensionField' is defined but never used
import { createCustomEditor } from '../helpers'

Check warning on line 4 in src/tests/marks/Link.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Unable to resolve path to module '../helpers'

Check warning on line 4 in src/tests/marks/Link.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing file extension for "../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"})

Check warning on line 34 in src/tests/marks/Link.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

Unexpected trailing comma
})

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 1288fd9

Please sign in to comment.