question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Links not working when editor is not editable

See original GitHub issue

What’s the bug you are facing?

We have links in editor which work fine while the editor is editable. For scenarios where the editor is read only, we render the editor with the links but it always redirects to the last link on the page.

How can we reproduce the bug on our side?

<p><a target="_blank" rel="noopener noreferrer nofollow" href="http://google.com">google.com</a></p><p><a target="_blank" rel="noopener noreferrer nofollow" href="http://www.google.com">www.google.com</a></p><p><a target="_blank" rel="noopener noreferrer nofollow" href="https://test.com">http://test.com</a></p>

Use this html in tiptap editor and set the editor to read only. There are three links but when you click on any of them, it always redirects to last link.

Can you provide a CodeSandbox?

No response

What did you expect to happen?

I expect it to work the same way as when the editor is editable.

Anything to add? (optional)

No response

Did you update your dependencies?

  • Yes, I’ve updated my dependencies to use the latest version of all packages.

Are you sponsoring us?

  • Yes, I’m a sponsor. 💖

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
scottsidwellcommented, Apr 11, 2022

@bdbch I can reproduce it consistently by:

  1. Opening that link
  2. Toggling “editable” to be false immediately
  3. Clicking on “test.com”

This is actually a pretty interesting bug.

The underlying cause is that “selectionchange” events aren’t triggered when clicking a link in a non-editable div, and that our clickHandler::handleClick still fires when isEditable is false (instead of letting the browser do it). As a result, since selectionchange events don’t fire, the editor retains its previous selection when our clickHandler then calls to getAttributes for the mark attrs getting the wrong link href and target https://github.com/ueberdosis/tiptap/blob/e07a5b625d90c77fc5820e5eefa093d3cfb96512/packages/extension-link/src/helpers/clickHandler.ts#L13-L15

The simplest fix is to just not handle clicks when the editor is disabled:

handleClick: (view, pos, event) => {
  // Rely on default browser link handling when editing is disabled
  if (!view.editable) {
    return false;
  }
  ...
}

But it might be a better idea to rely on the onClick pos rather than deferring to the selection for link clicks, so we’d end up with:

handleClick: (view, pos, event) => {
  // Rely on default browser link handling when editing is disabled
  if (!view.editable) {
      return false;
  }
  
  // If we clicked at a position with a link, follow it
  const $pos = view.state.doc.resolve(pos)
  const link = options.type.isInSet($pos.marks());
  if (link && link.attrs.href) {
      window.open(link.attrs.href, link.attrs.target);
      return true;
  }
  return false;
}
1reaction
bdbchcommented, Apr 12, 2022

Thanks @scottsidwell! This looks promising. I’ll work on a fix for this. Maybe this also resolves issue #2669 - it seems like the clickHandler is doing something fishy here too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Edit hyperlink option grayed out - Microsoft Community
I've been unable to edit hyperlinks in Word. I am copying the full hyperlink into the document, right clicking on it and choosing...
Read more >
Can't edit link - WordPress.org
Hi, Adding a link works fine, but as soon as I want to edit it, ... I'm using Easy Recipe plugin for my...
Read more >
Links can't be edited or clicked on after saving the page - Jira
Click on "Edit" to edit the page again. You can see that the link is no longer editable because nothing appears when you...
Read more >
Topic: insert/edit link not working? | WordPress.com Forums
insert/edit link not working? ... I am adding a new post and wish to insert a url. In the Visual Editor the insert/edit...
Read more >
Can't edit a link in a PDF - Adobe Support Community
It's not a form field and it's linking to another location within the document, so it's not automatically generated based on a URL....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found