From e861b24a2b84385f70f4f621d77f607c3222554d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kol=C4=8D=C3=A1=C5=99?= Date: Thu, 7 Sep 2023 13:25:27 +0200 Subject: [PATCH] link click handler fixed --- demos/src/Marks/Link/React/index.jsx | 6 ++++-- .../extension-link/src/helpers/clickHandler.ts | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/demos/src/Marks/Link/React/index.jsx b/demos/src/Marks/Link/React/index.jsx index 53b3134a93f..4c48e311c8f 100644 --- a/demos/src/Marks/Link/React/index.jsx +++ b/demos/src/Marks/Link/React/index.jsx @@ -1,5 +1,6 @@ import './styles.scss' +import Bold from '@tiptap/extension-bold' import Code from '@tiptap/extension-code' import Document from '@tiptap/extension-document' import Link from '@tiptap/extension-link' @@ -15,13 +16,14 @@ export default () => { Paragraph, Text, Code, + Bold, Link.configure({ - openOnClick: false, + openOnClick: true, }), ], content: `

- Wow, this editor has support for links to the whole world wide web. We tested a lot of URLs and I think you can add *every URL* you want. Isn’t that cool? Let’s try another one! Yep, seems to work. + Wow, this editor has support for links to the whole world wide web>. We tested a lot of URLs and I think you can add *every URL* you want. Isn’t that cool? Let’s try another one! Yep, seems to work.

By default every link will get a rel="noopener noreferrer nofollow" attribute. It’s configurable though. diff --git a/packages/extension-link/src/helpers/clickHandler.ts b/packages/extension-link/src/helpers/clickHandler.ts index 5312881ca4a..b2c4ce5fb76 100644 --- a/packages/extension-link/src/helpers/clickHandler.ts +++ b/packages/extension-link/src/helpers/clickHandler.ts @@ -15,6 +15,18 @@ export function clickHandler(options: ClickHandlerOptions): Plugin { return false } + let a = event.target as HTMLElement + const els = [] + + while (a.nodeName !== 'DIV') { + els.push(a) + a = a.parentNode as HTMLElement + } + + if (!els.find(value => value.nodeName === 'A')) { + return false + } + const attrs = getAttributes(view.state, options.type.name) const link = (event.target as HTMLLinkElement) @@ -22,9 +34,7 @@ export function clickHandler(options: ClickHandlerOptions): Plugin { const target = link?.target ?? attrs.target if (link && href) { - if (view.editable) { - window.open(href, target) - } + window.open(href, target) return true }