-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathDock.vue
More file actions
340 lines (301 loc) · 10.5 KB
/
Dock.vue
File metadata and controls
340 lines (301 loc) · 10.5 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<script setup lang="ts">
import type { DocksContext } from '@vitejs/devtools-kit/client'
import { useEventListener, useScreenSafeArea } from '@vueuse/core'
import { computed, onMounted, reactive, ref, useTemplateRef, watchEffect } from 'vue'
import { BUILTIN_ENTRY_CLIENT_AUTH_NOTICE } from '../constants'
import { docksSplitGroupsWithCapacity } from '../state/dock-settings'
import DockEntriesWithCategories from './DockEntriesWithCategories.vue'
import DockOverflowButton from './DockOverflowButton.vue'
import BracketLeft from './icons/BracketLeft.vue'
import BracketRight from './icons/BracketRight.vue'
import VitePlusCore from './icons/VitePlusCore.vue'
const props = defineProps<{
context: DocksContext
}>()
// Here we directly destructure is as we don't expect context to be changed
const context = props.context
const isSafari = navigator.userAgent.includes('Safari') && !navigator.userAgent.includes('Chrome')
const PANEL_MARGIN = 2
const panelMargins = reactive({
left: PANEL_MARGIN,
top: PANEL_MARGIN,
right: PANEL_MARGIN,
bottom: PANEL_MARGIN,
})
const safeArea = useScreenSafeArea()
function toNumber(value: string) {
const num = +value
if (Number.isNaN(num))
return 0
return num
}
watchEffect(() => {
panelMargins.left = toNumber(safeArea.left.value) + PANEL_MARGIN
panelMargins.top = toNumber(safeArea.top.value) + PANEL_MARGIN
panelMargins.right = toNumber(safeArea.right.value) + PANEL_MARGIN
panelMargins.bottom = toNumber(safeArea.bottom.value) + PANEL_MARGIN
})
const SNAP_THRESHOLD = 2
const dockEl = useTemplateRef<HTMLDivElement>('dockEl')
const anchorEl = useTemplateRef<HTMLDivElement>('anchorEl')
const recalculateCounter = ref(0)
const isHovering = ref(false)
const windowSize = reactive({
width: window.innerWidth,
height: window.innerHeight,
})
const draggingOffset = reactive({ x: 0, y: 0 })
const mousePosition = reactive({ x: 0, y: 0 })
function onPointerDown(e: PointerEvent) {
if (!dockEl.value)
return
context.panel.isDragging = true
const { left, top, width, height } = dockEl.value!.getBoundingClientRect()
draggingOffset.x = e.clientX - left - width / 2
draggingOffset.y = e.clientY - top - height / 2
}
const isRpcTrusted = ref(context.rpc.isTrusted)
context.rpc.events.on('rpc:is-trusted:updated', (isTrusted) => {
isRpcTrusted.value = isTrusted
if (isTrusted && context.docks.selected?.id === BUILTIN_ENTRY_CLIENT_AUTH_NOTICE.id)
context.docks.switchEntry(null)
})
const splitedEntries = computed(() => {
return docksSplitGroupsWithCapacity(context.docks.groupedEntries, 5)
})
onMounted(async () => {
windowSize.width = window.innerWidth
windowSize.height = window.innerHeight
useEventListener(window, 'resize', () => {
windowSize.width = window.innerWidth
windowSize.height = window.innerHeight
})
useEventListener(window, 'pointermove', (e: PointerEvent) => {
if (!context.panel.isDragging)
return
const store = context.panel.store
const centerX = window.innerWidth / 2
const centerY = window.innerHeight / 2
const x = e.clientX - draggingOffset.x
const y = e.clientY - draggingOffset.y
if (Number.isNaN(x) || Number.isNaN(y))
return
mousePosition.x = x
mousePosition.y = y
// Get position
const deg = Math.atan2(y - centerY, x - centerX)
const HORIZONTAL_MARGIN = 70
const TL = Math.atan2(0 - centerY + HORIZONTAL_MARGIN, 0 - centerX)
const TR = Math.atan2(0 - centerY + HORIZONTAL_MARGIN, window.innerWidth - centerX)
const BL = Math.atan2(window.innerHeight - HORIZONTAL_MARGIN - centerY, 0 - centerX)
const BR = Math.atan2(window.innerHeight - HORIZONTAL_MARGIN - centerY, window.innerWidth - centerX)
store.position = deg >= TL && deg <= TR
? 'top'
: deg >= TR && deg <= BR
? 'right'
: deg >= BR && deg <= BL
? 'bottom'
: 'left'
store.left = snapToPoints(x / window.innerWidth * 100)
store.top = snapToPoints(y / window.innerHeight * 100)
})
useEventListener(window, 'pointerup', () => {
context.panel.isDragging = false
})
useEventListener(window, 'pointerleave', () => {
context.panel.isDragging = false
})
})
function snapToPoints(value: number) {
if (value < 5)
return 0
if (value > 95)
return 100
if (Math.abs(value - 50) < SNAP_THRESHOLD)
return 50
return value
}
function clamp(value: number, min: number, max: number) {
return Math.min(Math.max(value, min), max)
}
const anchorPos = computed(() => {
// eslint-disable-next-line ts/no-unused-expressions
recalculateCounter.value
const store = context.panel.store
const halfWidth = (dockEl.value?.clientWidth || 0) / 2
const halfHeight = (dockEl.value?.clientHeight || 0) / 2
const left = store.left * windowSize.width / 100
const top = store.top * windowSize.height / 100
switch (store.position) {
case 'top':
return {
left: clamp(left, halfWidth + panelMargins.left, windowSize.width - halfWidth - panelMargins.right),
top: panelMargins.top + halfHeight,
}
case 'right':
return {
left: windowSize.width - panelMargins.right - halfHeight,
top: clamp(top, halfWidth + panelMargins.top, windowSize.height - halfWidth - panelMargins.bottom),
}
case 'left':
return {
left: panelMargins.left + halfHeight,
top: clamp(top, halfWidth + panelMargins.top, windowSize.height - halfWidth - panelMargins.bottom),
}
case 'bottom':
default:
return {
left: clamp(left, halfWidth + panelMargins.left, windowSize.width - halfWidth - panelMargins.right),
top: windowSize.height - panelMargins.bottom - halfHeight,
}
}
})
let _timer: ReturnType<typeof setTimeout> | null = null
function bringUp() {
isHovering.value = true
if (context.panel.store.inactiveTimeout < 0)
return
if (_timer)
clearTimeout(_timer)
_timer = setTimeout(() => {
isHovering.value = false
}, +context.panel.store.inactiveTimeout || 0)
}
const isHidden = computed(() => false)
const isMinimized = computed(() => {
if (context.panel.store.inactiveTimeout < 0)
return false
if (context.panel.store.inactiveTimeout === 0)
return true
// @ts-expect-error compatibility
const isTouchDevice = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0
return !context.panel.isDragging
&& !context.panel.store.open
&& !isHovering.value
&& !isTouchDevice
&& context.panel.store.inactiveTimeout
})
const anchorStyle = computed(() => {
return {
left: `${anchorPos.value.left}px`,
top: `${anchorPos.value.top}px`,
pointerEvents: isHidden.value ? 'none' : 'auto',
} as const
})
const panelStyle = computed(() => {
const style: any = {
transform: context.panel.isVertical
? `translate(-50%, -50%) rotate(90deg)`
: `translate(-50%, -50%)`,
}
if (isHidden.value) {
style.opacity = 0
style.pointerEvents = 'none'
}
if (context.panel.isDragging)
style.transition = 'none !important'
return style
})
onMounted(() => {
if (context.panel.store.open && !isRpcTrusted.value)
context.panel.store.open = false
if (isRpcTrusted.value)
bringUp()
recalculateCounter.value++
})
</script>
<template>
<div
id="vite-devtools-anchor"
ref="anchorEl"
:style="[anchorStyle]"
:class="{
'vite-devtools-horizontal': !context.panel.isVertical,
'vite-devtools-vertical': context.panel.isVertical,
'vite-devtools-minimized': isMinimized,
}"
@mousemove="bringUp"
>
<div
v-if="!isSafari"
id="vite-devtools-glowing"
:class="context.panel.isDragging ? 'op60!' : ''"
/>
<slot
:context="context"
:dock-el="dockEl"
:selected="context.docks.selected"
:panel-margins="panelMargins"
/>
<div
id="vite-devtools-dock-container"
ref="dockEl"
:style="panelStyle"
>
<div
id="vite-devtools-dock"
@pointerdown="onPointerDown"
>
<BracketLeft
class="vite-devtools-dock-bracket absolute left--1 top-1/2 translate-y--1/2 bottom-0 w-2.5 op75 transition-opacity duration-300"
/>
<BracketRight
class="vite-devtools-dock-bracket absolute right--1 top-1/2 translate-y--1/2 bottom-0 w-2.5 op75 transition-opacity duration-300"
:class="context.panel.isVertical ? 'scale-y--100' : ''"
/>
<div
class="w-3 h-3 absolute left-1/2 top-1/2 translate-x--1/2 translate-y--1/2 transition-opacity duration-300"
:class="[
isMinimized ? 'op100' : 'op0',
context.panel.isVertical ? 'rotate-270' : 'rotate-0',
]"
>
<VitePlusCore />
<div v-if="!isRpcTrusted" class="i-fluent-emoji-flat-warning absolute bottom-0 right--1px w-1.5 h-1.5" />
</div>
<div
v-if="!isRpcTrusted"
class="transition duration-300 delay-200"
:class="isMinimized ? 'opacity-0 pointer-events-none ws-nowrap text-sm text-orange of-hidden' : 'opacity-100'"
>
<button
class="p2 transition hover:bg-active rounded-full px4"
@click="context.docks.toggleEntry(BUILTIN_ENTRY_CLIENT_AUTH_NOTICE.id)"
>
<div class="flex items-center gap-1">
<div
class="i-fluent-emoji-flat-warning flex-none"
:class="context.panel.isVertical ? 'rotate-270' : 'rotate-0'"
/>
<div class="ws-nowrap text-amber">
Unauthorized
</div>
</div>
</button>
</div>
<div
:class="isMinimized ? 'opacity-0 pointer-events-none' : 'opacity-100'"
class="transition duration-200 flex items-center w-full h-full justify-center px3"
>
<DockEntriesWithCategories
:context="context"
:groups="splitedEntries.visible"
:is-vertical="context.panel.isVertical"
:selected="context.docks.selected"
@select="(e) => context.docks.switchEntry(e?.id)"
/>
<template v-if="splitedEntries.overflow.length > 0">
<div class="border-base m1 h-20px w-px border-r-1.5" />
<DockOverflowButton
:context="context"
:is-vertical="context.panel.isVertical"
:groups="splitedEntries.overflow"
:selected="context.docks.selected"
@select="(e) => context.docks.switchEntry(e?.id)"
/>
</template>
</div>
</div>
</div>
</div>
</template>