Skip to content
This repository has been 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
Merged

Conversation

ayaankazerouni
Copy link
Contributor

@ayaankazerouni ayaankazerouni commented Oct 13, 2017

Before submitting your PR please make sure:

  • You worked on the develop branch (or any other branch which was forked from develop),
  • Your PR is not targeting the master branch,
  • You tested your code and it works.

Related to: #198

Caveats:

The links only open when they're in the form https://github.com. Simple github.com or www.github.com won't do anything. I'm unsure if this is something electron does on purpose. @brrd Any ideas?

@brrd
Copy link
Owner

brrd commented Oct 14, 2017

Any ideas?

According to the documentation about shell.openExternal():

Open the given external protocol URL in the desktop’s default manner. (For example, mailto: URLs in the user’s default mail agent).

So I think if the URL doesn't start with the protocol then the shell module can not guess how to open it.

Why not adding it when it misses?

}

var url = open.nextUntil(".cm-formatting-link-string").text();
if (url !== "") {
Copy link
Owner

Choose a reason for hiding this comment

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

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

@brrd
Copy link
Owner

brrd commented Oct 14, 2017

Few comments about this PR:

Raw URL

It obviously doesn't work with raw URL:

This is working: [Test](http://google.com)

But clicking this does nothing: http://google.com

Pointer

I don't see the pointer when Alt key is pressed on a link, but a crosshair (I tried it on Windows 10). Probably related to https://github.com/codemirror/CodeMirror/blob/dda5d7220f0e3cbf8c203223ee4e7dc2d5c04bec/src/edit/key_events.js#L119

Alt key

I would not use Alt key because 1) on Windows and some Linux DE the Alt key also triggers the menu if it's hidden, making the editor contents move with possible confusion about what is clicked, 2) Alt is already used by CodeMirror for column selection.

Control/command is already used for multiple cursors and Shift is used for extending the current selection. My suggestion would be to:

  1. use the Shift key
  2. trigger the function on "mouseup" (instead of "mousedown")
  3. stop execution if something is selected in the editor (cm.doc.somethingSelected())

This would also solve the pointer issue.

What do you think?

Also become robust against clicks on any part
of the link for when CodeMirror breaks it up.
@ayaankazerouni
Copy link
Contributor Author

Raw URL

Ok, this is dealt with. The following cases work:

http://google.com (raw url)
http://github.com ("github" is seen as a spelling error, so it breaks the link into `spans`)
[Facebook](facebook.com) (standard MD link without protocol)
[Facebook](https://facebook.com) (standard MD link)

Pointer, Alt Key

Ok, using Shift key now, when nothing is selected. CodeMirror doesn't seem to emit a mouseup event. What are the downsides of using mousedown?

@brrd
Copy link
Owner

brrd commented Oct 15, 2017

What are the downsides of using mousedown?

Selection is not updated yet when mousedown event is fired, so cm.somethingSelected() will refer to the previous selection.

CodeMirror doesn't seem to emit a mouseup event.

You don't need to use CodeMirror events, you can simply do:

document.addEventListener("mouseup", function () {
// Do stuff
});

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")?

@ayaankazerouni
Copy link
Contributor Author

ayaankazerouni commented Oct 15, 2017

I now open the link on mouseup as requested. But I have to listen to mousedownas well to preventDefault the standard shift-click behaviour (it selects text between the original cursor position and the clicked position). I check for the same set of conditions as in mouseup to avoid to preventing default behaviour any more than necessary.

@brrd brrd changed the title Open links when alt-click on link or url Open links when shift-click on link or url Oct 16, 2017
@brrd brrd merged commit 27d9b7d into brrd:develop Oct 16, 2017
@brrd
Copy link
Owner

brrd commented Oct 16, 2017

Thanks!

@ayaankazerouni
Copy link
Contributor Author

No problem! This is a great app! Do you have an ETA on the next release?

@brrd
Copy link
Owner

brrd commented Oct 16, 2017

As soon as possible! (I'm too busy right now 😕)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants