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

Links are clickable #2382

Merged
merged 5 commits into from
Sep 15, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 9 additions & 0 deletions browser/main/global.styl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ body[data-theme="dark"]
.CodeMirror-foldgutter-folded:after
content: "\25B8"

.CodeMirror-hover
padding 2px 4px 0 4px
position absolute
z-index 99

.CodeMirror-hyperlink
cursor pointer


.sortableItemHelper
z-index modalZIndex + 5

Expand Down
124 changes: 124 additions & 0 deletions extra_scripts/codemirror/addon/hyperlink/hyperlink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
(function (mod) {
if (typeof exports === 'object' && typeof module === 'object') { // Common JS
mod(require('../codemirror/lib/codemirror'))
} else if (typeof define === 'function' && define.amd) { // AMD
define(['../codemirror/lib/codemirror'], mod)
} else { // Plain browser env
mod(CodeMirror)
}
})(function (CodeMirror) {
'use strict'

const shell = require('electron').shell
const yOffset = 2

class HyperLink {
constructor(cm) {
this.cm = cm
this.lineDiv = cm.display.lineDiv

this.onMouseDown = this.onMouseDown.bind(this)
this.onMouseEnter = this.onMouseEnter.bind(this)
this.onMouseLeave = this.onMouseLeave.bind(this)
this.onMouseMove = this.onMouseMove.bind(this)

this.tooltip = document.createElement('div')
this.tooltipContent = document.createElement('div')
this.tooltipIndicator = document.createElement('div')
this.tooltip.setAttribute('class', 'CodeMirror-hover CodeMirror-matchingbracket CodeMirror-selected')
this.tooltip.setAttribute('cm-ignore-events', 'true')
this.tooltip.appendChild(this.tooltipContent)
this.tooltip.appendChild(this.tooltipIndicator)
this.tooltipContent.textContent = 'Cmd + click to follow link'

this.lineDiv.addEventListener('mousedown', this.onMouseDown)
this.lineDiv.addEventListener('mouseenter', this.onMouseEnter, {
capture: true,
passive: true
})
this.lineDiv.addEventListener('mouseleave', this.onMouseLeave, {
capture: true,
passive: true
})
this.lineDiv.addEventListener('mousemove', this.onMouseMove, {
passive: true
})
}
getUrl(el) {
const className = el.className.split(' ')

if (className.indexOf('cm-url') !== -1) {
const match = /^\((.*)\)|\[(.*)\]|(.*)$/.exec(el.textContent)
return match[1] || match[2] || match[3]
}

return null
}
onMouseDown(e) {
const { target, metaKey } = e
if (!metaKey) {
Copy link
Member

Choose a reason for hiding this comment

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

On Macintosh keyboards, this is the ⌘ Command key. On Windows keyboards, this is the Windows key (⊞ Windows).

I don't know how it looks on MacOS but on Windows and Linux, the meta key is used to open the main menu therefore when you hit meta key + click link, the link does open in the browser but the menu is also displayed which is quite annoying.

I think you will have to change this key to something else. Also you can't set it to ctrl on windows and linux as it's the key for multi-cursor in codemirror. Shift perhaps?

return
}

const url = this.getUrl(target)
if (url) {
e.preventDefault()

shell.openExternal(url)
}
}
onMouseEnter(e) {
const { target, metaKey } = e

const url = this.getUrl(target)
if (url) {
if (metaKey) {
target.classList.add('CodeMirror-activeline-background', 'CodeMirror-hyperlink')
}
else {
target.classList.add('CodeMirror-activeline-background')
}

this.showInfo(target)
}
}
onMouseLeave(e) {
if (this.tooltip.parentElement === this.lineDiv) {
e.target.classList.remove('CodeMirror-activeline-background', 'CodeMirror-hyperlink')

this.lineDiv.removeChild(this.tooltip)
}
}
onMouseMove(e) {
if (this.tooltip.parentElement === this.lineDiv) {
if (e.metaKey) {
e.target.classList.add('CodeMirror-hyperlink')
}
else {
e.target.classList.remove('CodeMirror-hyperlink')
}
}
}
showInfo(relatedTo) {
const b1 = relatedTo.getBoundingClientRect()
const b2 = this.lineDiv.getBoundingClientRect()
const tdiv = this.tooltip

tdiv.style.left = (b1.left - b2.left) + 'px'
this.lineDiv.appendChild(tdiv)

const b3 = tdiv.getBoundingClientRect()
const top = b1.top - b2.top - b3.height - yOffset
if (top < 0) {
tdiv.style.top = (b1.top - b2.top + b1.height + yOffset) + 'px'
}
else {
tdiv.style.top = top + 'px'
}
}
}

CodeMirror.defineOption('hyperlink', true, (cm) => {
const addon = new HyperLink(cm)
})
})
2 changes: 2 additions & 0 deletions lib/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<link rel="stylesheet" href="../node_modules/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="../node_modules/katex/dist/katex.min.css">
<link rel="stylesheet" href="../node_modules/codemirror/addon/dialog/dialog.css">

<title>Boostnote</title>

<style>
Expand Down Expand Up @@ -95,6 +96,7 @@
<script src="../node_modules/codemirror/addon/runmode/runmode.js"></script>

<script src="../extra_scripts/boost/boostNewLineIndentContinueMarkdownList.js"></script>
<script src="../extra_scripts/codemirror/addon/hyperlink/hyperlink.js"></script>

<script src="../node_modules/codemirror/addon/edit/closebrackets.js"></script>
<script src="../node_modules/codemirror/addon/edit/matchbrackets.js"></script>
Expand Down