Skip to content

Commit

Permalink
fix: find link href based on prosemirror node
Browse files Browse the repository at this point in the history
* Resolve the position given to the `handleClick` function.
* Get the `link` mark from the resolved position.
* Use the attrs of this mark.

`editor.getAttributes` gets the attributes at the current selection.
In Firefox the selection will not move to the clicked position in read only mode.
So instead of relying on the selection we now use the `pos` of the click.

Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Apr 20, 2022
1 parent 5f9e026 commit 5b5da24
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nextcloud/text",
"description": "Collaborative document editing",
"version": "0.1.0-beta.2",
"version": "0.1.0-beta.3",
"authors": [
{
"name": "Julius Härtl",
Expand Down
17 changes: 11 additions & 6 deletions package/marks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ const Link = TipTapLink.extend({
new Plugin({
props: {
key: new PluginKey('textLink'),
handleClick: (_view, _pos, event) => {
const attrs = this.editor.getAttributes('link')
const link = event.target.closest('a')
if (link && attrs.href && this.options.onClick) {
return this.options.onClick(event, attrs)
handleClick: (view, pos, event) => {
const $clicked = view.state.doc.resolve(pos)
const link = $clicked.marks().find(m => m.type.name === this.name)
if (!link) {
return false
}
return false
if (!link.attrs.href) {
console.warn('Could not determine href of link.')
console.debug(link)
return false
}
return this.options.onClick?.(event, link.attrs)
},
},
}),
Expand Down

0 comments on commit 5b5da24

Please sign in to comment.