Skip to content

fix: opening formatted links #2937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 0 additions & 10 deletions src/components/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
57 changes: 44 additions & 13 deletions src/components/modules/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,26 +767,57 @@ export default class UI extends Module<UINodes> {
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;
}

/**
Expand Down
9 changes: 0 additions & 9 deletions src/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down