From ebb4c977db3e17abb27272898ae361347aaa4a05 Mon Sep 17 00:00:00 2001 From: Pascal Jufer Date: Fri, 30 May 2025 16:01:44 +0200 Subject: [PATCH] fix: opening formatted links --- docs/CHANGELOG.md | 1 + src/components/dom.ts | 10 ------- src/components/modules/ui.ts | 57 ++++++++++++++++++++++++++++-------- src/components/utils.ts | 9 ------ 4 files changed, 45 insertions(+), 32 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 5770a1e74..735b6adfa 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -16,6 +16,7 @@ - `DX` - Tools submodules removed from the repository - `Improvement` - Shift + Down/Up will allow to select next/previous line instead of Inline Toolbar flipping - `Improvement` - The API `caret.setToBlock()` offset now works across the entire block content, not just the first or last node. +- `Fix` - Opening links (via ctrl/cmd key + click) that are additionally formatted (e.g. bold) ### 2.30.7 diff --git a/src/components/dom.ts b/src/components/dom.ts index 675735555..960a31a6a 100644 --- a/src/components/dom.ts +++ b/src/components/dom.ts @@ -556,16 +556,6 @@ export default class Dom { return element; } - /** - * Returns true if element is anchor (is A tag) - * - * @param {Element} element - element to check - * @returns {boolean} - */ - public static isAnchor(element: Element): element is HTMLAnchorElement { - return element.tagName.toLowerCase() === 'a'; - } - /** * Return element's offset related to the document * diff --git a/src/components/modules/ui.ts b/src/components/modules/ui.ts index a4d3baad3..44f5b216d 100644 --- a/src/components/modules/ui.ts +++ b/src/components/modules/ui.ts @@ -767,26 +767,57 @@ export default class UI extends Module { return; } - /** - * case when user clicks on anchor element - * if it is clicked via ctrl key, then we open new window with url - */ - const element = event.target as Element; + if (this.processLinkClick(event)) { + return; + } + + this.processBottomZoneClick(event); + } + + /** + * Check if user clicks on a link while holding down the ctrl/cmd key. + * In that case, open it in a new tab/window. + * + * @param event - click event + * @returns true if a link has been opened + */ + private processLinkClick(event: MouseEvent): boolean { const ctrlKey = event.metaKey || event.ctrlKey; - if ($.isAnchor(element) && ctrlKey) { - event.stopImmediatePropagation(); - event.stopPropagation(); + if (ctrlKey && event.target instanceof Element) { + let currentElement: Element | null = event.target; + let anchor = null; - const href = element.getAttribute('href'); - const validUrl = _.getValidUrl(href); + while (currentElement) { + if (currentElement === this.nodes.redactor) { + return false; + } - _.openTab(validUrl); + if (currentElement.tagName === 'A') { + anchor = currentElement; + break; + } - return; + currentElement = currentElement.parentElement; + } + + if (anchor) { + event.stopImmediatePropagation(); + event.stopPropagation(); + + const href = anchor.getAttribute('href'); + + if (href !== null) { + const validUrl = _.getValidUrl(href); + + window.open(validUrl, '_blank'); + } + + return true; + } } - this.processBottomZoneClick(event); + return false; } /** diff --git a/src/components/utils.ts b/src/components/utils.ts index a6418a990..0f0ad3d79 100644 --- a/src/components/utils.ts +++ b/src/components/utils.ts @@ -665,15 +665,6 @@ export function generateBlockId(): string { return nanoid(idLen); } -/** - * Opens new Tab with passed URL - * - * @param {string} url - URL address to redirect - */ -export function openTab(url: string): void { - window.open(url, '_blank'); -} - /** * Returns random generated identifier *