Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Open links when shift-click on link or url #203

Merged
merged 8 commits into from
Oct 16, 2017
7 changes: 7 additions & 0 deletions app/less/editor.less
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
.editor-searching();
}

.shift-pressed {
.cm-link:not(.cm-formatting),
.cm-url:not(.cm-formatting) {
cursor: pointer
}
}

// Generic editor classes
// ======================

Expand Down
53 changes: 53 additions & 0 deletions app/renderer/abr-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ function AbrDocument () {
}
});

that.cm.on("mousedown", function(cm, event) {
var shouldOpenLink = document.body.classList.contains("shift-pressed") &&
!event.target.classList.contains("cm-formatting") &&
(event.target.classList.contains("cm-link") ||
event.target.classList.contains("cm-url"));
if (shouldOpenLink) {
event.preventDefault();
OpenLinkHandler(event);
}
});

// Handle local keybindings that arent linked to a specific menu
document.onkeydown = function(evt) {
evt = evt || window.event;
Expand All @@ -207,6 +218,48 @@ function AbrDocument () {
}
};

// Handle ALT modifier key
var ShiftKeyHandler = function(e) {
var linkIsClickable = !that.cm.somethingSelected() &&
e.type === "keydown" &&
e.shiftKey;
document.body.classList.toggle("shift-pressed", linkIsClickable);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is smart, but I had to read it twice to understand why you did it this way. Could you rename the class name in something less confusing (maybe "link-clickable")?

}

window.addEventListener("keydown", ShiftKeyHandler, false);
window.addEventListener("keyup", ShiftKeyHandler, false);
// when leaving Abricotine to go the browser, the keyup event doesn't trigger
window.addEventListener("blur", ShiftKeyHandler, false);

var OpenLinkHandler = function(e) {
if (!document.body.classList.contains("shift-pressed")) return;

var open = null;
var url = null;
if (e.target.classList.contains("cm-url")) {
open = $(e.target).prevUntil(":not(.cm-url)").andSelf().first();
url = open.nextUntil(".cm-formatting-link-string").text();
} else if (e.target.classList.contains("cm-link")) {
open = $(e.target).prevUntil(":not(.cm-link)");
if (open.first().is(".cm-formatting-link")) {
// link is in standard MD format
open = $(e.target).nextAll(".cm-formatting-link-string").first();
url = open.nextUntil(".cm-formatting-link-string").text();
} else {
// link is a raw url
open = open.andSelf().first();
url = open.nextUntil(":not(.cm-link)").andSelf().text();
}
}

if (url === "") return;
var hasProtocol = /^[a-z]+:\/\//.test(url);
if (!hasProtocol) {
url = "http://" + url;
}
shell.openExternal(url);
}

// Handle CTRL+MouseWheel events
var MouseWheelHandler = function (e) {
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
Expand Down