Skip to content

Commit d2ba3c6

Browse files
committed
feat: icon draggleable
1 parent 1d468e1 commit d2ba3c6

4 files changed

Lines changed: 67 additions & 40 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ npm install vite-plugin-dev-inspector -D
4040

4141
## Usage
4242

43+
44+
### Keyboard shortcut
45+
46+
- Mac: <kbd>⌘</kbd> + <kbd>⇧</kbd>
47+
- Windows: <kbd>Ctrl</kbd> + <kbd>⇧</kbd>
48+
49+
4350
### Configuration Vite
4451

4552
```ts

packages/core/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ npm install vite-plugin-dev-inspector -D
4141

4242
## Usage
4343

44+
### Keyboard shortcut
45+
46+
- Mac: <kbd>⌘</kbd> + <kbd>⇧</kbd>
47+
- Windows: <kbd>Ctrl</kbd> + <kbd>⇧</kbd>
48+
49+
4450
### Configuration Vite
4551

4652
```ts

packages/core/src/overlay.js

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ template.innerHTML = `<style>
2727
font-family: Arial, Helvetica, sans-serif;
2828
top: 15px;
2929
right: 15px;
30+
width: 40px;
31+
height: 40px;
3032
}
3133
3234
.dev-inspector-card {
@@ -104,7 +106,7 @@ class DevInspector extends HTMLElement {
104106
super()
105107
this.enabled = false
106108
this.containerVisible = false
107-
109+
this.isDragging = false
108110
this.toggleCombo = inspectorOptions.toggleComboKey?.toLowerCase?.()?.split?.('-') ?? false
109111
this.disableInspectorOnEditorOpen = inspectorOptions.disableInspectorOnEditorOpen ?? true
110112

@@ -322,6 +324,50 @@ class DevInspector extends HTMLElement {
322324
return promise
323325
}
324326

327+
// button position
328+
containerPosition = () => {
329+
// draggable element 选中可拖动的元素
330+
const draggableElement = this.root.querySelector('#toggle-inspector-container')
331+
332+
// toggle button visibility
333+
const { toggleButtonVisibility } = inspectorOptions
334+
if (toggleButtonVisibility === 'always' || (toggleButtonVisibility === 'active' && this.enabled)) {
335+
draggableElement.style.display = 'block'
336+
this.switchBtnVisible()
337+
}
338+
else {
339+
draggableElement.style.display = 'none'
340+
}
341+
342+
// draggle element position
343+
if (window.localStorage.getItem('inspectorX') && window.localStorage.getItem('inspectorY')) {
344+
draggableElement.style.left = window.localStorage.getItem('inspectorX')
345+
draggableElement.style.top = window.localStorage.getItem('inspectorY')
346+
}
347+
348+
let offsetX, offsetY
349+
350+
draggableElement.addEventListener('mousedown', (e) => {
351+
this.isDragging = true
352+
offsetX = e.clientX - draggableElement.getBoundingClientRect().left
353+
offsetY = e.clientY - draggableElement.getBoundingClientRect().top
354+
})
355+
356+
document.addEventListener('mousemove', (e) => {
357+
if (this.isDragging) {
358+
draggableElement.style.left = `${e.clientX - offsetX}px`
359+
draggableElement.style.top = `${e.clientY - offsetY}px`
360+
window.localStorage.setItem('inspectorX', `${e.clientX - offsetX}px`)
361+
window.localStorage.setItem('inspectorY', `${e.clientY - offsetY}px`)
362+
}
363+
})
364+
365+
document.addEventListener('mouseup', () => {
366+
if (this.isDragging)
367+
this.isDragging = false
368+
})
369+
}
370+
325371
connectedCallback() {
326372
this.root = this.attachShadow({ mode: 'closed' })
327373
this.root.appendChild(template.content.cloneNode(true))
@@ -337,19 +383,11 @@ class DevInspector extends HTMLElement {
337383
// toggle overlay visibility
338384
this.toggleOverlayVisibility()
339385

340-
// toggle button visibility
341-
const toggleInspectorContainer = this.root.querySelector('#toggle-inspector-container')
342-
const { toggleButtonVisibility } = inspectorOptions
343-
if (toggleButtonVisibility === 'always' || (toggleButtonVisibility === 'active' && this.enabled)) {
344-
toggleInspectorContainer.style.display = 'block'
345-
this.switchBtnVisible()
346-
}
347-
else {
348-
toggleInspectorContainer.style.display = 'none'
349-
}
386+
// button position(draggable)
387+
this.containerPosition()
350388

351389
// Expose control to global
352-
window.__VUE_INSPECTOR__ = this
390+
window.__DEV_INSPECTOR__ = this
353391
}
354392
}
355393

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from 'vite'
22
import { createVuePlugin } from 'vite-plugin-vue2'
3-
import Inspector from 'vite-plugin-dev-inspector'
3+
import inspector from 'vite-plugin-dev-inspector'
44

55
export default defineConfig({
66
plugins: [
@@ -10,32 +10,8 @@ export default defineConfig({
1010
compositionAPI: true,
1111
},
1212
}),
13-
14-
// viteExternalsPlugin({
15-
// 'vue': 'Vue',
16-
// }),
17-
18-
Inspector({
19-
toggleButtonVisibility: 'always',
20-
// enabled: true,
21-
// disableInspectorOnEditorOpen: true,
22-
}),
13+
inspector({
14+
toggleButtonVisibility: 'always', // always:一直展示, never:隐藏, active
15+
}) as any,
2316
],
24-
25-
// optimizeDeps: {
26-
// include: optimizedInclude(),
27-
// exclude: [],
28-
// },
2917
})
30-
31-
// function optimizedInclude() {
32-
// return [
33-
// 'qs',
34-
// 'vue-clipboard2',
35-
// 'mockjs',
36-
// 'nprogress',
37-
// '@hb/vue-bd-header-nav',
38-
// 'resize-observer-polyfill',
39-
// 'throttle-debounce',
40-
// ]
41-
// }

0 commit comments

Comments
 (0)