Skip to content

Commit e10da45

Browse files
authored
[Issue-1057]: open new window by clicking anchor with ctrl (#1062)
* open new window when anchor clicked with ctrl * delete source maps * update function name * update * update changelog
1 parent 3a83a1d commit e10da45

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

dist/editor.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- `Fix` - Some mistakes are fixed in [installation.md](installation.md)
77
- `Fix` - Fixed multiple paste callback triggering in a case when several editors are instantiated [#1011](https://github.com/codex-team/editor.js/issues/1011)
88
- `Fix` - Fixed inline toolbar flipper activation on closing conversion toolbar [#995](https://github.com/codex-team/editor.js/issues/995)
9+
- `Improvements` - New window tab is opened by clicking on anchor with ctrl [#1057](https://github.com/codex-team/editor.js/issues/1057)
910

1011
### 2.16.1
1112

src/components/dom.ts

+9
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,13 @@ export default class Dom {
547547

548548
return node && extensions.includes(node.nodeName);
549549
}
550+
551+
/**
552+
* Returns true if element is anchor (is A tag)
553+
*
554+
* @param element
555+
*/
556+
public static isAnchor(element: Element): boolean {
557+
return element.tagName.toLowerCase() === 'a';
558+
}
550559
}

src/components/modules/ui.ts

+15
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,21 @@ export default class UI extends Module {
584584
event.stopImmediatePropagation();
585585
event.stopPropagation();
586586

587+
/**
588+
* case when user clicks on anchor element
589+
* if it is clicked via ctrl key, then we open new window with url
590+
*/
591+
const element = event.target as Element;
592+
const ctrlKey = event.metaKey || event.ctrlKey;
593+
594+
if ($.isAnchor(element) && ctrlKey) {
595+
const href = element.getAttribute('href');
596+
const validUrl = _.getValidUrl(href);
597+
598+
_.openTab(validUrl);
599+
return;
600+
}
601+
587602
if (!this.Editor.BlockManager.currentBlock) {
588603
this.Editor.BlockManager.insert();
589604
}

src/components/utils.ts

+32
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,35 @@ export function beautifyShortcut(shortcut: string): string {
487487

488488
return shortcut;
489489
}
490+
491+
/**
492+
* Returns valid URL. If it is going outside and valid, it returns itself
493+
* If url has `one slash`, then it concatenates with window location origin
494+
* or when url has `two lack` it appends only protocol
495+
*
496+
* @param {String} url
497+
*/
498+
export function getValidUrl(url: string): string {
499+
try {
500+
const urlObject = new URL(url);
501+
502+
return urlObject.href;
503+
} catch (e) {
504+
// do nothing but handle below
505+
}
506+
507+
if (url.substring(0, 2) === '//') {
508+
return window.location.protocol + url;
509+
} else {
510+
return window.location.origin + url;
511+
}
512+
}
513+
514+
/**
515+
* Opens new Tab with passed URL
516+
*
517+
* @param {String} url - URL address to redirect
518+
*/
519+
export function openTab(url: string): void {
520+
window.open(url, '_blank');
521+
}

0 commit comments

Comments
 (0)