@@ -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
0 commit comments