Skip to content

Commit b536329

Browse files
committed
fix: opening formatted links
1 parent 7da61e9 commit b536329

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `DX` - Tools submodules removed from the repository
1717
- `Improvement` - Shift + Down/Up will allow to select next/previous line instead of Inline Toolbar flipping
1818
- `Improvement` - The API `caret.setToBlock()` offset now works across the entire block content, not just the first or last node.
19+
- `Fix` - Opening links (via ctrl/cmd key + click) that are additionally formatted (e.g. bold)
1920

2021
### 2.30.7
2122

src/components/dom.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -556,16 +556,6 @@ export default class Dom {
556556
return element;
557557
}
558558

559-
/**
560-
* Returns true if element is anchor (is A tag)
561-
*
562-
* @param {Element} element - element to check
563-
* @returns {boolean}
564-
*/
565-
public static isAnchor(element: Element): element is HTMLAnchorElement {
566-
return element.tagName.toLowerCase() === 'a';
567-
}
568-
569559
/**
570560
* Return element's offset related to the document
571561
*

src/components/modules/ui.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -767,23 +767,41 @@ export default class UI extends Module<UINodes> {
767767
return;
768768
}
769769

770-
/**
771-
* case when user clicks on anchor element
772-
* if it is clicked via ctrl key, then we open new window with url
773-
*/
774-
const element = event.target as Element;
770+
// Check if user clicks on a link while holding down the ctrl/cmd key.
771+
// In that case, open it in a new tab/window.
775772
const ctrlKey = event.metaKey || event.ctrlKey;
776773

777-
if ($.isAnchor(element) && ctrlKey) {
778-
event.stopImmediatePropagation();
779-
event.stopPropagation();
774+
if (ctrlKey && event.target instanceof Element) {
775+
let currentElement: Element | null = event.target;
776+
let anchor = null;
780777

781-
const href = element.getAttribute('href');
782-
const validUrl = _.getValidUrl(href);
778+
while (currentElement) {
779+
if (currentElement === this.nodes.redactor) {
780+
break;
781+
}
783782

784-
_.openTab(validUrl);
783+
if (currentElement.tagName === 'A') {
784+
anchor = currentElement;
785+
break;
786+
}
785787

786-
return;
788+
currentElement = currentElement.parentElement;
789+
}
790+
791+
if (anchor) {
792+
event.stopImmediatePropagation();
793+
event.stopPropagation();
794+
795+
const href = anchor.getAttribute('href');
796+
797+
if (href !== null) {
798+
const validUrl = _.getValidUrl(href);
799+
800+
window.open(validUrl, '_blank');
801+
}
802+
803+
return;
804+
}
787805
}
788806

789807
this.processBottomZoneClick(event);

src/components/utils.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -665,15 +665,6 @@ export function generateBlockId(): string {
665665
return nanoid(idLen);
666666
}
667667

668-
/**
669-
* Opens new Tab with passed URL
670-
*
671-
* @param {string} url - URL address to redirect
672-
*/
673-
export function openTab(url: string): void {
674-
window.open(url, '_blank');
675-
}
676-
677668
/**
678669
* Returns random generated identifier
679670
*

0 commit comments

Comments
 (0)