forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-toc.js
More file actions
64 lines (54 loc) · 1.3 KB
/
use-toc.js
File metadata and controls
64 lines (54 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { ref, computed } from 'vue'
import { useDocStore } from 'assets/doc-store'
function updateActiveToc (position, tocList, activeToc) {
if (position === void 0) {
position = document.documentElement.scrollTop || document.body.scrollTop
}
let last
for (const i in tocList.value) {
const section = tocList.value[ i ]
const item = document.getElementById(section.id)
if (item === null) {
continue
}
if (item.offsetTop >= position + 155) {
if (last === void 0) {
last = section.id
}
break
}
else {
last = section.id
}
}
if (last !== void 0) {
activeToc.value = last
const tocEl = document.getElementById('toc--' + last)
if (tocEl) {
tocEl.scrollIntoView({ block: 'nearest' })
}
}
}
export default function useToc (scope, $route) {
const $store = useDocStore()
const activeToc = ref($route.hash.length > 1
? $route.hash.substring(1)
: null
)
const tocList = computed(() => {
return $store.toc.length > 0
? [
{ id: 'introduction', title: 'Introduction' },
...$store.toc
]
: $store.toc
})
function setActiveToc (pos) {
updateActiveToc(pos, tocList, activeToc)
}
Object.assign(scope, {
tocList,
activeToc,
setActiveToc
})
}