-
-
Notifications
You must be signed in to change notification settings - Fork 589
Expand file tree
/
Copy pathclient.ts
More file actions
110 lines (101 loc) · 3.71 KB
/
client.ts
File metadata and controls
110 lines (101 loc) · 3.71 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import type { App } from 'vue'
import FloatingVue, { recomputeAllPoppers } from 'floating-vue'
const isMobile = typeof navigator !== 'undefined' && /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
export type FloatingVueConfig = Parameters<(typeof FloatingVue)['install']>[1]
/**
* Vue plugin to install FloatingVue with styles.
*
* Import this function in `.vitepress/theme/index.ts` and use `app.use(TwoslashFloatingVue)` inside the `enhanceApp` hook.
*/
const TwoslashFloatingVue = {
install: (app: App, options: FloatingVueConfig = {}) => {
if (typeof window !== 'undefined') {
// Recompute poppers when clicking on a tab
window.addEventListener('vitepress:codeGroupTabActivate', recomputeAllPoppers, { passive: true })
window.addEventListener('click', (e) => {
const path = e.composedPath()
if (path.some((el: any) => el?.classList?.contains?.('vp-code-group') || el?.classList?.contains?.('tabs')))
recomputeAllPoppers()
}, { passive: true })
// On desktop where the poppers are shown on hover, make sure that they do
// not show or hide while we're dragging (selecting text) so they don't
// interfere with the selection.
if (!isMobile) {
let isDragging = false
window.addEventListener('mousedown', () => {
isDragging = true
}, { passive: true })
window.addEventListener('mouseup', () => {
isDragging = false
}, { passive: true })
const _component = app.component
app.component = function (this: typeof app, ...rest: any[]) {
// @ts-expect-error type mismatch for `rest`
const comp = _component.apply(this, rest)
if (rest.length >= 2 && rest[0] === 'VMenu') {
try {
const PopperVue = rest[1].components.Popper
const PopperTs = PopperVue.extends
const _show = PopperTs.methods.show
PopperTs.methods.show = function (...args: any[]) {
if (!isDragging)
return _show.apply(this, args)
}
const _hide = PopperTs.methods.hide
PopperTs.methods.hide = function (...args: any[]) {
if (!isDragging)
return _hide.apply(this, args)
}
}
catch (e) {
console.error('Failed to patch FloatingVue', e)
}
}
return comp
}
}
}
app.use(FloatingVue, {
strategy: 'fixed',
...options,
themes: {
...options.themes,
'twoslash': {
$extend: 'dropdown',
triggers: isMobile ? ['touch'] : ['hover', 'touch'],
popperTriggers: isMobile ? ['touch'] : ['hover', 'touch'],
placement: 'bottom-start',
overflowPadding: 10,
delay: 0,
handleResize: false,
autoHide: true,
noAutoFocus: true,
instantMove: true,
flip: false,
arrowPadding: 8,
autoBoundaryMaxSize: true,
...options.themes?.twoslash ?? {},
},
'twoslash-query': {
$extend: 'twoslash',
triggers: ['click'],
popperTriggers: ['click'],
autoHide: false,
noAutoFocus: true,
...options.themes?.['twoslash-query'] ?? {},
},
'twoslash-completion': {
$extend: 'twoslash-query',
triggers: ['click'],
popperTriggers: ['click'],
autoHide: false,
noAutoFocus: true,
distance: 0,
arrowOverflow: true,
...options.themes?.['twoslash-completion'] ?? {},
},
},
})
},
}
export default TwoslashFloatingVue