Skip to content
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

[Issue-1057]: open new window by clicking anchor with ctrl #1062

Merged
merged 6 commits into from
Mar 14, 2020
Merged
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
2 changes: 1 addition & 1 deletion dist/editor.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions src/components/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
15 changes: 15 additions & 0 deletions src/components/modules/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
32 changes: 32 additions & 0 deletions src/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}