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 <github@belakkaf.net>
  • Loading branch information
grnd-alt committed Feb 12, 2025
1 parent 5636ec1 commit d52b7f4
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 74 deletions.
67 changes: 9 additions & 58 deletions cypress/e2e/marks/Link.spec.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
/* eslint-disable no-unused-expressions */
/**
* @copyright Copyright (c) 2024 Max <max@nextcloud.com>
*
* @author Max <max@nextcloud.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import Markdown from './../../../src/extensions/Markdown.js'
Expand All @@ -27,18 +9,12 @@ import { createCustomEditor } from './../../support/components.js'
import { loadMarkdown, expectMarkdown } from '../nodes/helpers.js'

describe('Link marks', { retries: 0 }, () => {

const editor = createCustomEditor({
content: '',
extensions: [
Markdown,
Link,
Italic,
],
extensions: [Markdown, Link, Italic],
})

describe('insertOrSetLink command', { retries: 0 }, () => {

it('is available in commands', () => {
expect(editor.commands).to.have.property('insertOrSetLink')
})
Expand All @@ -50,45 +26,20 @@ describe('Link marks', { retries: 0 }, () => {

it('will insert a link in a normal paragraph', () => {
prepareEditor('hello\n', 3)
editor.commands.insertOrSetLink('https://nextcloud.com', { href: 'https://nextcloud.com' })
editor.commands.insertOrSetLink('https://nextcloud.com', {
href: 'https://nextcloud.com',
})
expectMarkdown(editor, 'he\n\n<https://nextcloud.com>\n\nllo')
})

})

/**
* Expect a link in the editor.
*/
function expectLink() {
expect(getParentNode().type.name).to.equal('paragraph')
expect(getParentNode().attrs.href).to.equal('https://nextcloud.com')
expect(getMark().attrs.href).to.equal('https://nextcloud.com')
}

/**
*
*/
function getParentNode() {
const { state: { selection } } = editor
return selection.$head.parent
}

/**
*
*/
function getMark() {
const { state: { selection } } = editor
console.info(selection.$head)
return selection.$head.nodeAfter.marks[0]
}

/**
*
* @param input
* @param {*} input markdown content
* @param {*} position cursor pos
*/
function prepareEditor(input, position = 1) {
loadMarkdown(editor, input)
editor.commands.setTextSelection( position )
editor.commands.setTextSelection(position)
}

})
23 changes: 7 additions & 16 deletions src/marks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

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 { markInputRule, getMarkRange, isMarkActive } from '@tiptap/core'

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

Expand Down Expand Up @@ -90,28 +89,20 @@ const Link = TipTapLink.extend({
},
addCommands() {
return {
/**
* Update the target of existing links.
* Insert a link if there currently is none.
*
*/
insertOrSetLink: (text, attrs) => ({ state, chain, commands }) => {
// Check if any text is selected,
// 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
93 changes: 93 additions & 0 deletions src/tests/marks/Link.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import Link from './../../marks/Link.js'
import Underline from '../../marks/Underline.js'
import createCustomEditor from '../testHelpers/createCustomEditor.ts'

describe('Link extension integrated in the editor', () => {
it('should have link available in commands', () => {
const editor = createCustomEditor('<p><a href="nextcloud.com">Test</a> HELLO WORLD</p>', [Link])
expect(editor.commands).toHaveProperty('insertOrSetLink')
})

it('should update link if anchor has mark', () => {
const editor = createCustomEditor(
'<p><a href="nextcloud.com">Te<u>s</u>t</a> HELLO WORLD</p>',
[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(
'<p><a href="nextcloud.com">Test</a><a href="nextcloud.com">Test</a></p>',
[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(
'<p><a href="nextcloud.com">Test</a> HELLO WORLD</p>',
[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 d52b7f4

Please sign in to comment.