Skip to content

Commit

Permalink
refactor(link): use isMarkActive in insertOrSetLink
Browse files Browse the repository at this point in the history
This way we do not hand in the active state
and avoid inconsistencies.

Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud authored and grnd-alt committed Feb 4, 2025
1 parent 98322e6 commit 965bdab
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cypress/e2e/marks/Link.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Link marks', { retries: 0 }, () => {

it('will insert a link in a normal paragraph', () => {
prepareEditor('hello\n', 3)
editor.commands.insertOrSetLink(false, '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')
})

Expand Down
2 changes: 1 addition & 1 deletion src/components/Menu/ActionInsertLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export default {
// Avoid issues when parsing urls later on in markdown that might be entered in an invalid format (e.g. "mailto: example@example.com")
const href = url.replaceAll(' ', '%20')
const chain = this.$editor.chain()
chain.insertOrSetLink(this.state.active, text, { href })
chain.insertOrSetLink(text, { href })
chain.focus().run()
},
/**
Expand Down
5 changes: 3 additions & 2 deletions src/marks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +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'

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

Expand Down Expand Up @@ -93,11 +94,11 @@ const Link = TipTapLink.extend({
* Insert a link if there currently is none.
*
*/
insertOrSetLink: (active, text, attrs) => ({ state, chain, commands }) => {
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 (active) {
if (isMarkActive(state, this.name)) {
commands.deleteNode('paragraph')
}
return chain().insertContent({
Expand Down

0 comments on commit 965bdab

Please sign in to comment.