-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
root.ts
67 lines (58 loc) · 1.94 KB
/
root.ts
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
65
66
67
/* __imports__ */
import { watch } from 'vue'
import { useHead, useHtmlAttrs } from '@vueuse/head'
import { nanoid } from 'nanoid'
import { configs } from '../env'
import { initSharedState, onPatch, patch } from '../state/shared'
import { initDrawingState } from '../state/drawings'
import { clicks, currentPage, getPath, isNotesViewer, isPresenter } from '../logic/nav'
import { router } from '../routes'
import { TRUST_ORIGINS } from '../constants'
export default function setupRoot() {
// @ts-expect-error injected in runtime
const injection_arg = undefined
/* __injections__ */
const title = configs.titleTemplate.replace('%s', configs.title || 'Slidev')
useHead({ title })
useHtmlAttrs(configs.htmlAttrs)
initSharedState(`${title} - shared`)
initDrawingState(`${title} - drawings`)
const id = `${location.origin}_${nanoid()}`
// update shared state
function updateSharedState() {
if (isNotesViewer.value)
return
// we allow Presenter mode, or Viewer mode from trusted origins to update the shared state
if (!isPresenter.value && !TRUST_ORIGINS.includes(location.host.split(':')[0]))
return
if (isPresenter.value) {
patch('page', +currentPage.value)
patch('clicks', clicks.value)
}
else {
patch('viewerPage', +currentPage.value)
patch('viewerClicks', clicks.value)
}
patch('lastUpdate', {
id,
type: isPresenter.value ? 'presenter' : 'viewer',
time: new Date().getTime(),
})
}
router.afterEach(updateSharedState)
watch(clicks, updateSharedState)
onPatch((state) => {
const routePath = router.currentRoute.value.path
if (!routePath.match(/^\/(\d+|presenter)\/?/))
return
if (+state.page !== +currentPage.value || clicks.value !== state.clicks) {
router.replace({
path: getPath(state.page),
query: {
...router.currentRoute.value.query,
clicks: state.clicks || 0,
},
})
}
})
}