Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(links): add insertOrSetLink command
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud authored and grnd-alt committed Mar 19, 2025
commit 2b1ce9a633d1bac5b34ff58b4feb9bb01f0bc787
22 changes: 1 addition & 21 deletions src/components/Menu/ActionInsertLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -189,27 +189,7 @@ export default {
// Avoid issues when parsing urls later on in markdown that might be entered in an invalid format (e.g. "mailto: [email protected]")
const href = url.replaceAll(' ', '%20')
const chain = this.$editor.chain()
// Check if any text is selected, if not insert the link using the given text property
if (this.$editor.view.state?.selection.empty) {
if (this.state.active) {
chain.deleteNode('paragraph')
}
chain.insertContent({
type: 'paragraph',
content: [{
type: 'text',
marks: [{
type: 'link',
attrs: {
href,
},
}],
text,
}],
})
} else {
chain.setLink({ href })
}
chain.insertOrSetLink(this.state.active, text, { href })
chain.focus().run()
},
/**
Expand Down
31 changes: 31 additions & 0 deletions src/marks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,37 @@ const Link = TipTapLink.extend({
}),
]
},
addCommands() {
return {
/**
* Update the target of existing links.
* Insert a link if there currently is none.
*
*/
insertOrSetLink: (active, 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) {
commands.deleteNode('paragraph')
}
return chain().insertContent({
type: 'paragraph',
content: [{
type: 'text',
marks: [{
type: 'link',
attrs,
}],
text,
}],
})
} else {
return commands.setLink(attrs)
}
},
}
},

addProseMirrorPlugins() {
const plugins = this.parent()
Expand Down