-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathOverlay.vue
251 lines (236 loc) · 6.04 KB
/
Overlay.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
<template>
<div>
<!-- toggle button -->
<div
v-if="controllerVisible"
class="vue-inspector__controller"
:class="{disabled: !enabled}"
:style="controllerStyle"
@click="toggle"
>
<img
ref="target"
src="https://github.com/webfansplz/vite-plugin-vue-inspector/blob/main/docs/images/logo.png?raw=true"
alt="logo"
draggable="false"
/>
</div>
<!-- overlay -->
<ul
v-if="overlayVisible"
ref="overlayTarget"
class="vue-inspector__overlay"
:style="overlayStyle"
>
<li>
file: <em>{{ `<${linkQuery.title}>` }}</em>
</li>
<li>
line: <em>{{ linkQuery.line }}</em>
</li>
<li>
column: <em>{{ linkQuery.column }}</em>
</li>
</ul>
</div>
</template>
<script>
import inspectorOptions from "virtual:vue-inspector-options"
const isClient = typeof window !== "undefined"
const importMetaUrl = isClient ? new URL(import.meta.url) : {}
const protocol = inspectorOptions.serverOptions?.https ? "https:" : importMetaUrl?.protocol
const host = inspectorOptions.serverOptions?.host ?? importMetaUrl?.hostname
const port = inspectorOptions.serverOptions?.port ?? importMetaUrl?.port
const baseUrl = isClient ? `${protocol}//${host}:${port}` : ""
export default {
data() {
return {
target: null,
overlayTarget: null,
enabled: inspectorOptions.enabled,
toggleCombo: inspectorOptions.toggleComboKey?.toLowerCase().split("-"),
overlayVisible: false,
linkQuery: {
file: "",
line: 0,
column: 0,
},
position: {
x: 0,
y: 0,
},
// drag control
controlPosition: {
x: 0,
y: 0,
},
pressedDelta: undefined,
}
},
computed: {
controllerVisible() {
const { toggleButtonVisibility } = inspectorOptions
return toggleButtonVisibility === "always" || (toggleButtonVisibility === "active" && this.enabled)
},
controllerStyle() {
return inspectorOptions.toggleButtonPos
.split("-")
.map(p => `${p}: 15px;`)
.join("")
},
overlayStyle() {
return {
left: `${
this.position.x - this.$refs.overlayTarget?.clientWidth / 2 || 0
}px`,
top: `${this.position.y + 20}px`,
}
},
},
mounted() {
this.toggleCombo && document.body.addEventListener("keydown", this.onKeydown)
this.enabled && this.enable()
},
methods: {
toggleEvent() {
const listener = this.enabled ? document.body.addEventListener : document.body.removeEventListener
listener?.("mousemove", this.onMouseMove)
listener?.("click", this.onFetch, true)
},
enable() {
this.enabled = true
this.toggleEvent()
},
disable() {
this.enabled = false
this.overlayVisible = false
this.toggleEvent()
},
toggle() {
this.enabled ? this.disable() : this.enable()
},
isKeyActive(key, event) {
switch (key) {
case "shift":
case "control":
case "alt":
case "meta":
return event.getModifierState(key.charAt(0).toUpperCase() + key.slice(1))
default:
return key === event.key.toLowerCase()
}
},
onKeydown(event) {
if (event.repeat || event.key === undefined)
return
if (["Escape", "Esc"].includes(event.key) && this.enabled) {
this.disable()
return
}
const isCombo = this.toggleCombo?.every(key => this.isKeyActive(key, event))
if (isCombo)
this.toggle()
},
getTargetNode(e) {
const path = e.path ?? e.composedPath()
const targetNode = path?.find(node => node?.hasAttribute?.("data-v-inspector-file"))
if (targetNode === this.$refs.target) {
return {
targetNode: null,
params: null,
}
}
return {
targetNode,
params: targetNode
? {
file: targetNode?.getAttribute?.("data-v-inspector-file"),
line: targetNode?.getAttribute?.("data-v-inspector-line"),
column: targetNode?.getAttribute?.("data-v-inspector-column"),
title: targetNode?.getAttribute?.("data-v-inspector-title"),
}
: null,
}
},
onFetch(e) {
if (e.target === this.$refs.target) return
e.preventDefault()
e.stopPropagation()
e.stopImmediatePropagation()
const { targetNode, params } = this.getTargetNode(e)
if (!targetNode) return
const { file, line, column } = params
this.overlayVisible = false
fetch(
`${baseUrl}/__open-stack-frame-in-editor?file=${file}&line=${line}&column=${column}`,
{
mode: "no-cors",
},
)
},
onMouseMove(e) {
const { targetNode, params } = this.getTargetNode(e)
if (targetNode) {
this.position.x = e.clientX
this.position.y = e.clientY
this.overlayVisible = true
this.linkQuery = params
}
else {
this.overlayVisible = false
this.linkQuery = {
file: "",
line: 0,
column: 0,
}
}
},
},
}
</script>
<style scoped>
.vue-inspector__controller {
cursor: pointer;
position: fixed;
text-align: center;
z-index: 100000;
width: 60px;
height: 60px;
background-color: #fff;
border: 2px solid #29a8f2;
border-radius: 50%;
}
.vue-inspector__controller img {
width: 50px;
margin: 0 auto;
}
.vue-inspector__controller:hover {
background-color: #29a8f2;
}
.vue-inspector__controller.disabled {
border: 2px dashed #ccc;
}
.vue-inspector__controller.disabled:hover {
background-color: #ccc;
}
.vue-inspector__controller.disabled img {
opacity: 0.5;
}
.vue-inspector__overlay {
z-index: 100000;
position: fixed;
border: 2px dashed #666;
background-color: rgba(0,0,0,0.8);
color: #fff;
padding: 10px;
border-radius: 5px;
text-align: left;
}
.vue-inspector__overlay li {
list-style-type: none;
}
.vue-inspector__overlay em {
font-style: normal;
font-weight: 500;
}
</style>