Skip to content

Commit 2695f62

Browse files
AWolf81Rokt33r
authored andcommitted
add special link handling
1 parent ccd0355 commit 2695f62

File tree

2 files changed

+116
-19
lines changed

2 files changed

+116
-19
lines changed

browser/main/Main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class Main extends React.Component {
181181
'menubar:togglemenubar',
182182
this.toggleMenuBarVisible.bind(this)
183183
)
184+
eventEmitter.on('dispatch:push', this.changeRoutePush.bind(this))
184185
}
185186

186187
componentWillUnmount() {
@@ -189,6 +190,12 @@ class Main extends React.Component {
189190
'menubar:togglemenubar',
190191
this.toggleMenuBarVisible.bind(this)
191192
)
193+
eventEmitter.off('dispatch:push', this.changeRoutePush.bind(this))
194+
}
195+
196+
changeRoutePush(event, destination) {
197+
const { dispatch } = this.props
198+
dispatch(push(destination))
192199
}
193200

194201
toggleMenuBarVisible() {
Lines changed: 109 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
1-
(function (mod) {
2-
if (typeof exports === 'object' && typeof module === 'object') { // Common JS
1+
;(function(mod) {
2+
if (typeof exports === 'object' && typeof module === 'object') {
3+
// Common JS
34
mod(require('../codemirror/lib/codemirror'))
4-
} else if (typeof define === 'function' && define.amd) { // AMD
5+
} else if (typeof define === 'function' && define.amd) {
6+
// AMD
57
define(['../codemirror/lib/codemirror'], mod)
6-
} else { // Plain browser env
8+
} else {
9+
// Plain browser env
710
mod(CodeMirror)
811
}
9-
})(function (CodeMirror) {
12+
})(function(CodeMirror) {
1013
'use strict'
1114

1215
const shell = require('electron').shell
16+
const remote = require('electron').remote
17+
const eventEmitter = {
18+
emit: function() {
19+
remote.getCurrentWindow().webContents.send.apply(null, arguments)
20+
}
21+
}
1322
const yOffset = 2
1423

1524
const macOS = global.process.platform === 'darwin'
1625
const modifier = macOS ? 'metaKey' : 'ctrlKey'
1726

1827
class HyperLink {
19-
constructor (cm) {
28+
constructor(cm) {
2029
this.cm = cm
2130
this.lineDiv = cm.display.lineDiv
2231

@@ -28,11 +37,16 @@
2837
this.tooltip = document.createElement('div')
2938
this.tooltipContent = document.createElement('div')
3039
this.tooltipIndicator = document.createElement('div')
31-
this.tooltip.setAttribute('class', 'CodeMirror-hover CodeMirror-matchingbracket CodeMirror-selected')
40+
this.tooltip.setAttribute(
41+
'class',
42+
'CodeMirror-hover CodeMirror-matchingbracket CodeMirror-selected'
43+
)
3244
this.tooltip.setAttribute('cm-ignore-events', 'true')
3345
this.tooltip.appendChild(this.tooltipContent)
3446
this.tooltip.appendChild(this.tooltipIndicator)
35-
this.tooltipContent.textContent = `${macOS ? 'Cmd(⌘)' : 'Ctrl(^)'} + click to follow link`
47+
this.tooltipContent.textContent = `${
48+
macOS ? 'Cmd(⌘)' : 'Ctrl(^)'
49+
} + click to follow link`
3650

3751
this.lineDiv.addEventListener('mousedown', this.onMouseDown)
3852
this.lineDiv.addEventListener('mouseenter', this.onMouseEnter, {
@@ -47,7 +61,7 @@
4761
passive: true
4862
})
4963
}
50-
getUrl (el) {
64+
getUrl(el) {
5165
const className = el.className.split(' ')
5266

5367
if (className.indexOf('cm-url') !== -1) {
@@ -60,41 +74,117 @@
6074

6175
return null
6276
}
63-
onMouseDown (e) {
77+
specialLinkHandler(e, rawHref, linkHash) {
78+
const isStartWithHash = rawHref[0] === '#'
79+
80+
const extractIdRegex = /file:\/\/.*main.?\w*.html#/ // file://path/to/main(.development.)html
81+
const regexNoteInternalLink = new RegExp(`${extractIdRegex.source}(.+)`)
82+
if (isStartWithHash || regexNoteInternalLink.test(rawHref)) {
83+
const posOfHash = linkHash.indexOf('#')
84+
if (posOfHash > -1) {
85+
const extractedId = linkHash.slice(posOfHash + 1)
86+
const targetId = mdurl.encode(extractedId)
87+
const targetElement = document.getElementById(targetId) // this.getWindow().document.getElementById(targetId)
88+
89+
if (targetElement != null) {
90+
this.scrollTo(0, targetElement.offsetTop)
91+
}
92+
return
93+
}
94+
}
95+
96+
// this will match the new uuid v4 hash and the old hash
97+
// e.g.
98+
// :note:1c211eb7dcb463de6490 and
99+
// :note:7dd23275-f2b4-49cb-9e93-3454daf1af9c
100+
const regexIsNoteLink = /^:note:([a-zA-Z0-9-]{20,36})$/
101+
if (regexIsNoteLink.test(linkHash)) {
102+
eventEmitter.emit('list:jump', linkHash.replace(':note:', ''))
103+
return
104+
}
105+
106+
const regexIsLine = /^:line:[0-9]/
107+
if (regexIsLine.test(linkHash)) {
108+
const numberPattern = /\d+/g
109+
110+
const lineNumber = parseInt(linkHash.match(numberPattern)[0])
111+
eventEmitter.emit('line:jump', lineNumber)
112+
return
113+
}
114+
115+
// this will match the old link format storage.key-note.key
116+
// e.g.
117+
// 877f99c3268608328037-1c211eb7dcb463de6490
118+
const regexIsLegacyNoteLink = /^(.{20})-(.{20})$/
119+
if (regexIsLegacyNoteLink.test(linkHash)) {
120+
eventEmitter.emit('list:jump', linkHash.split('-')[1])
121+
return
122+
}
123+
124+
const regexIsTagLink = /^:tag:#([\w]+)$/
125+
if (regexIsTagLink.test(rawHref)) {
126+
const tag = rawHref.match(regexIsTagLink)[1]
127+
eventEmitter.emit('dispatch:push', `/tags/${encodeURIComponent(tag)}`)
128+
return
129+
}
130+
}
131+
onMouseDown(e) {
64132
const { target } = e
65133
if (!e[modifier]) {
66134
return
67135
}
68136

137+
const rawHref = e.target.innerText.trim().slice(1, -1) // get link text from markdown text
138+
139+
if (!rawHref) return // not checked href because parser will create file://... string for [empty link]()
140+
141+
const parser = document.createElement('a')
142+
parser.href = rawHref
143+
const { href, hash } = parser
144+
145+
if (!rawHref) return // not checked href because parser will create file://... string for [empty link]()
146+
147+
const linkHash = hash === '' ? rawHref : hash // needed because we're having special link formats that are removed by parser e.g. :line:10
148+
149+
this.specialLinkHandler(target, rawHref, linkHash)
150+
69151
const url = this.getUrl(target)
152+
153+
// all special cases handled --> other case
70154
if (url) {
71155
e.preventDefault()
72156

73157
shell.openExternal(url)
74158
}
75159
}
76-
onMouseEnter (e) {
160+
onMouseEnter(e) {
77161
const { target } = e
78162

79163
const url = this.getUrl(target)
80164
if (url) {
81165
if (e[modifier]) {
82-
target.classList.add('CodeMirror-activeline-background', 'CodeMirror-hyperlink')
166+
target.classList.add(
167+
'CodeMirror-activeline-background',
168+
'CodeMirror-hyperlink'
169+
)
83170
} else {
84171
target.classList.add('CodeMirror-activeline-background')
85172
}
86173

87174
this.showInfo(target)
88175
}
89176
}
90-
onMouseLeave (e) {
177+
onMouseLeave(e) {
91178
if (this.tooltip.parentElement === this.lineDiv) {
92-
e.target.classList.remove('CodeMirror-activeline-background', 'CodeMirror-hyperlink')
179+
e.target.classList.remove(
180+
'CodeMirror-activeline-background',
181+
'CodeMirror-hyperlink'
182+
)
93183

94184
this.lineDiv.removeChild(this.tooltip)
95185
}
96186
}
97-
onMouseMove (e) {
187+
onMouseMove(e) {
98188
if (this.tooltip.parentElement === this.lineDiv) {
99189
if (e[modifier]) {
100190
e.target.classList.add('CodeMirror-hyperlink')
@@ -103,25 +193,25 @@
103193
}
104194
}
105195
}
106-
showInfo (relatedTo) {
196+
showInfo(relatedTo) {
107197
const b1 = relatedTo.getBoundingClientRect()
108198
const b2 = this.lineDiv.getBoundingClientRect()
109199
const tdiv = this.tooltip
110200

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

114204
const b3 = tdiv.getBoundingClientRect()
115205
const top = b1.top - b2.top - b3.height - yOffset
116206
if (top < 0) {
117-
tdiv.style.top = (b1.top - b2.top + b1.height + yOffset) + 'px'
207+
tdiv.style.top = b1.top - b2.top + b1.height + yOffset + 'px'
118208
} else {
119209
tdiv.style.top = top + 'px'
120210
}
121211
}
122212
}
123213

124-
CodeMirror.defineOption('hyperlink', true, (cm) => {
214+
CodeMirror.defineOption('hyperlink', true, cm => {
125215
const addon = new HyperLink(cm)
126216
})
127217
})

0 commit comments

Comments
 (0)