/
Frame.vue
297 lines (259 loc) · 9.47 KB
/
Frame.vue
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
<script setup lang="ts">
import type { VueInspectorClient } from 'vite-plugin-vue-inspector'
import type { PropType } from 'vue'
import { computed, ref, watch, watchEffect } from 'vue'
import type { NuxtDevtoolsHostClient, NuxtDevtoolsIframeClient, NuxtDevtoolsGlobal as NuxtDevtoolsViewGlobal } from '../../../types'
import { PANEL_MAX, PANEL_MIN, PANEL_PADDING, closePanel, settings, state, viewMode } from './state'
import { useEventListener } from './utils'
// Can't use reactivity transform here because this file is shipped as-is,
// where we can't guarantee that the user has the reactivity transform enabled.
// Same for not using auto imports.
const props = defineProps({
client: Object as PropType<NuxtDevtoolsHostClient>,
})
const CLIENT_PATH = '/__nuxt_devtools__/client'
const initialUrl = CLIENT_PATH + state.value.route
const iframe = ref<HTMLIFrameElement>()
const isDragging = ref<false | 'vertical' | 'horizontal' | 'both'>(false)
const frameStyle = computed(() => {
if (viewMode.value === 'component-inspector') {
return {
bottom: `${PANEL_PADDING}px`,
left: '50%',
transform: 'translateX(-50%)',
height: '80px',
width: '400px',
}
}
const height = `calc(${state.value.height}vh - ${PANEL_PADDING * 2}px)`
const width = `calc(${state.value.width}vw - ${PANEL_PADDING * 2}px)`
if (state.value.position === 'left' || state.value.position === 'right') {
return {
[state.value.position]: `${PANEL_PADDING}px`,
borderRadius: '0.5rem',
top: `calc(${(100 - state.value.height) / 2}vh + ${PANEL_PADDING}px)`,
height,
width,
}
}
else {
return {
[state.value.position || 'bottom']: `${PANEL_PADDING}px`,
borderRadius: '0.5rem',
left: `calc(${(100 - state.value.width) / 2}vw + ${PANEL_PADDING}px)`,
height,
width,
}
}
})
async function onLoad() {
await waitForClientInjection()
setupClient()
}
function waitForClientInjection(retry = 10, timeout = 200) {
const test = () => !!iframe.value?.contentWindow?.__NUXT_DEVTOOLS_VIEW__
if (test())
return
return new Promise<void>((resolve, reject) => {
const interval = setInterval(() => {
if (test()) {
clearInterval(interval)
resolve()
}
else if (retry-- <= 0) {
clearInterval(interval)
// eslint-disable-next-line prefer-promise-reject-errors
reject('Nuxt Devtools client injection failed')
}
}, timeout)
})
}
function refreshReactivity() {
props.client?.hooks.callHook('host:update:reactivity')
}
function setupClient() {
// trigger update for payload change
watch(() => props.client?.nuxt.payload, () => {
refreshReactivity()
}, { deep: true })
// trigger update for route change
props.client?.nuxt.vueApp.config.globalProperties?.$router?.afterEach(() => {
refreshReactivity()
})
// trigger update for app mounted
props.client?.nuxt.hook('app:mounted', () => {
refreshReactivity()
})
updateClient()
}
function updateClient() {
const injection = iframe.value?.contentWindow?.__NUXT_DEVTOOLS_VIEW__ as NuxtDevtoolsViewGlobal
const componentInspector = window.__VUE_INSPECTOR__ as VueInspectorClient
if (componentInspector) {
componentInspector.openInEditor = async (baseUrl, file, line, column) => {
disableComponentInspector()
await props.client!.hooks.callHook('host:inspector:click', baseUrl, file, line, column)
}
componentInspector.onUpdated = () => {
props.client!.hooks.callHook('host:inspector:update', {
...componentInspector.linkParams,
...componentInspector.position,
})
}
}
injection?.setClient({
...props.client as any,
inspector: {
enable: enableComponentInspector,
disable: disableComponentInspector,
instance: componentInspector,
},
})
props.client?.hooks.hook('devtools:navigate', (path) => {
state.value.route = path
})
}
function enableComponentInspector() {
window.__VUE_INSPECTOR__?.enable()
viewMode.value = 'component-inspector'
}
function disableComponentInspector() {
window.__VUE_INSPECTOR__?.disable()
props.client?.hooks.callHook('host:inspector:close')
if (viewMode.value === 'component-inspector')
viewMode.value = 'default'
}
useEventListener(window, 'mousemove', (e: MouseEvent) => {
if (!isDragging.value)
return
const alignSide = state.value.position === 'left' || state.value.position === 'right'
if (isDragging.value === 'horizontal' || isDragging.value === 'both') {
const y = state.value.position === 'top'
? window.innerHeight - e.clientY
: e.clientY
const boxHeight = window.innerHeight - PANEL_PADDING * 2
const value = alignSide
? (Math.abs(y - (window.innerHeight / 2)) - PANEL_PADDING) / boxHeight * 100 * 2
: (window.innerHeight - y - PANEL_PADDING) / boxHeight * 100
state.value.height = Math.min(PANEL_MAX, Math.max(PANEL_MIN, value))
}
if (isDragging.value === 'vertical' || isDragging.value === 'both') {
const x = state.value.position === 'left'
? window.innerWidth - e.clientX
: e.clientX
const boxWidth = window.innerWidth - PANEL_PADDING * 2
const value = alignSide
? (window.innerWidth - x - PANEL_PADDING) / boxWidth * 100
: (Math.abs(x - (window.innerWidth / 2)) - PANEL_PADDING) / boxWidth * 100 * 2
state.value.width = Math.min(PANEL_MAX, Math.max(PANEL_MIN, value))
}
})
useEventListener(window, 'mouseup', () => {
isDragging.value = false
})
useEventListener(window, 'mouseleave', () => {
isDragging.value = false
})
useEventListener(window, 'keydown', (e: KeyboardEvent) => {
if (viewMode.value === 'component-inspector' && e.key === 'Escape')
closePanel()
})
// Close panel on outside click (when enabled)
useEventListener(window, 'mousedown', (e: MouseEvent) => {
if (!settings.value.interactionCloseOnOutsideClick)
return
if (!state.value.open || isDragging.value || viewMode.value !== 'default')
return
const matched = e.composedPath().find((_el) => {
const el = _el as HTMLElement
return Array.from(el.classList || []).some(c => c.startsWith('nuxt-devtools-'))
|| el.tagName?.toLowerCase() === 'iframe'
})
if (!matched)
state.value.open = false
})
watch(viewMode, (mode) => {
if (mode === 'component-inspector')
enableComponentInspector()
else
disableComponentInspector()
}, { immediate: true })
watchEffect(() => {
if (!state.value.open)
iframe.value?.blur()
else
iframe.value?.focus()
})
</script>
<script lang="ts">
declare global {
interface Window {
__NUXT_DEVTOOLS_VIEW__?: NuxtDevtoolsViewGlobal
__NUXT_DEVTOOLS__?: NuxtDevtoolsIframeClient
__VUE_INSPECTOR__?: VueInspectorClient
}
}
</script>
<template>
<div v-show="state.open" class="nuxt-devtools-frame" :style="frameStyle">
<iframe
ref="iframe"
:src="initialUrl"
:style="{
'pointer-events': isDragging ? 'none' : 'auto',
}"
@load="onLoad"
/>
<template v-if="viewMode === 'default'">
<div v-if="state.position !== 'top'" class="nuxt-devtools-resize-handle nuxt-devtools-resize-handle-horizontal" :style="{ top: 0 }" @mousedown.prevent="() => isDragging = 'horizontal'" />
<div v-if="state.position !== 'bottom'" class="nuxt-devtools-resize-handle nuxt-devtools-resize-handle-horizontal" :style="{ bottom: 0 }" @mousedown.prevent="() => isDragging = 'horizontal'" />
<div v-if="state.position !== 'left'" class="nuxt-devtools-resize-handle nuxt-devtools-resize-handle-vertical" :style="{ left: 0 }" @mousedown.prevent="() => isDragging = 'vertical'" />
<div v-if="state.position !== 'right'" class="nuxt-devtools-resize-handle nuxt-devtools-resize-handle-vertical" :style="{ right: 0 }" @mousedown.prevent="() => isDragging = 'vertical'" />
<div v-if="state.position !== 'top' && state.position !== 'left'" class="nuxt-devtools-resize-handle nuxt-devtools-resize-handle-corner" :style="{ top: 0, left: 0, cursor: 'nwse-resize' }" @mousedown.prevent="() => isDragging = 'both'" />
<div v-if="state.position !== 'top' && state.position !== 'right'" class="nuxt-devtools-resize-handle nuxt-devtools-resize-handle-corner" :style="{ top: 0, right: 0, cursor: 'nesw-resize' }" @mousedown.prevent="() => isDragging = 'both'" />
<div v-if="state.position !== 'bottom' && state.position !== 'right'" class="nuxt-devtools-resize-handle nuxt-devtools-resize-handle-corner" :style="{ bottom: 0, right: 0, cursor: 'nwse-resize' }" @mousedown.prevent="() => isDragging = 'both'" />
<div v-if="state.position !== 'bottom' && state.position !== 'left'" class="nuxt-devtools-resize-handle nuxt-devtools-resize-handle-corner" :style="{ bottom: 0, left: 0, cursor: 'nesw-resize' }" @mousedown.prevent="() => isDragging = 'both'" />
</template>
</div>
</template>
<style scoped>
.nuxt-devtools-frame {
position: fixed;
z-index: 2147483646;
}
.nuxt-devtools-frame iframe {
width: 100%;
height: 100%;
outline: none;
border: 1px solid rgba(125,125,125,0.2);
border-radius: 10px;
}
.nuxt-devtools-resize-handle-horizontal {
position: absolute;
left: 6px;
right: 6px;
height: 10px;
margin: -5px 0;
cursor: ns-resize;
border-radius: 5px;
}
.nuxt-devtools-resize-handle-vertical {
position: absolute;
top: 6px;
bottom: 0;
width: 10px;
margin: 0 -5px;
cursor: ew-resize;
border-radius: 5px;
}
.nuxt-devtools-resize-handle-corner {
position: absolute;
width: 14px;
height: 14px;
margin: -6px;
border-radius: 6px;
}
.nuxt-devtools-resize-handle:hover {
background: rgba(125,125,125,0.1);
}
</style>