Skip to content

Commit eed7038

Browse files
committed
bind alt+click shortcut for copying page ID with fragment to section title anchors
1 parent efcf1b5 commit eed7038

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/js/03-fragment-jumper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
function jumpToAnchor (e) {
2020
if (e) {
21+
if (e.altKey) return
2122
window.location.hash = '#' + this.id
2223
e.preventDefault()
2324
}

src/js/06-copy-page-spec.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
;(function () {
22
'use strict'
33

4+
var pageSpec = document.querySelector('head meta[name=page-spec]').content
45
var editPageLink = document.querySelector('.toolbar .edit-this-page a')
5-
if (!editPageLink) return
6-
editPageLink.addEventListener('click', function (e) {
7-
if (e.altKey) navigator.clipboard.writeText(document.querySelector('head meta[name=page-spec]').content)
6+
if (editPageLink) editPageLink.addEventListener('click', onEditPageLinkClick)
7+
;[].slice.call(document.querySelectorAll('.doc a.anchor')).forEach(function (anchor) {
8+
if (/H[2-6]/.test(anchor.parentNode.tagName)) anchor.addEventListener('click', onSectionAnchorClick.bind(anchor))
89
})
10+
11+
function onEditPageLinkClick (e) {
12+
if (e.altKey) navigator.clipboard.writeText(pageSpec)
13+
}
14+
15+
function onSectionAnchorClick (e) {
16+
if (e.altKey) {
17+
e.preventDefault()
18+
navigator.clipboard.writeText(pageSpec + decodeFragment(this.hash))
19+
}
20+
}
21+
22+
function decodeFragment (hash) {
23+
return ~hash.indexOf('%') ? decodeURIComponent(hash) : hash
24+
}
925
})()

0 commit comments

Comments
 (0)