mirror of
https://github.com/codex-team/editor.js
synced 2024-06-26 09:20:07 +02:00
[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
This commit is contained in:
parent
3a83a1d12e
commit
e10da45113
2
dist/editor.js
vendored
2
dist/editor.js
vendored
File diff suppressed because one or more lines are too long
|
@ -6,6 +6,7 @@
|
|||
- `Fix` - Some mistakes are fixed in [installation.md](installation.md)
|
||||
- `Fix` - Fixed multiple paste callback triggering in a case when several editors are instantiated [#1011](https://github.com/codex-team/editor.js/issues/1011)
|
||||
- `Fix` - Fixed inline toolbar flipper activation on closing conversion toolbar [#995](https://github.com/codex-team/editor.js/issues/995)
|
||||
- `Improvements` - New window tab is opened by clicking on anchor with ctrl [#1057](https://github.com/codex-team/editor.js/issues/1057)
|
||||
|
||||
### 2.16.1
|
||||
|
||||
|
|
|
@ -547,4 +547,13 @@ export default class Dom {
|
|||
|
||||
return node && extensions.includes(node.nodeName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if element is anchor (is A tag)
|
||||
*
|
||||
* @param element
|
||||
*/
|
||||
public static isAnchor(element: Element): boolean {
|
||||
return element.tagName.toLowerCase() === 'a';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -584,6 +584,21 @@ export default class UI extends Module {
|
|||
event.stopImmediatePropagation();
|
||||
event.stopPropagation();
|
||||
|
||||
/**
|
||||
* 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;
|
||||
const ctrlKey = event.metaKey || event.ctrlKey;
|
||||
|
||||
if ($.isAnchor(element) && ctrlKey) {
|
||||
const href = element.getAttribute('href');
|
||||
const validUrl = _.getValidUrl(href);
|
||||
|
||||
_.openTab(validUrl);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.Editor.BlockManager.currentBlock) {
|
||||
this.Editor.BlockManager.insert();
|
||||
}
|
||||
|
|
|
@ -487,3 +487,35 @@ export function beautifyShortcut(shortcut: string): string {
|
|||
|
||||
return shortcut;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns valid URL. If it is going outside and valid, it returns itself
|
||||
* If url has `one slash`, then it concatenates with window location origin
|
||||
* or when url has `two lack` it appends only protocol
|
||||
*
|
||||
* @param {String} url
|
||||
*/
|
||||
export function getValidUrl(url: string): string {
|
||||
try {
|
||||
const urlObject = new URL(url);
|
||||
|
||||
return urlObject.href;
|
||||
} catch (e) {
|
||||
// do nothing but handle below
|
||||
}
|
||||
|
||||
if (url.substring(0, 2) === '//') {
|
||||
return window.location.protocol + url;
|
||||
} else {
|
||||
return window.location.origin + url;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens new Tab with passed URL
|
||||
*
|
||||
* @param {String} url - URL address to redirect
|
||||
*/
|
||||
export function openTab(url: string): void {
|
||||
window.open(url, '_blank');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue