@@ -15,66 +15,81 @@ window.__svelte_devtools_select_element = function (element) {
15
15
if ( node ) send ( 'inspect' , { node : serialize ( node ) } ) ;
16
16
} ;
17
17
18
+ const previous = {
19
+ /** @type {HTMLElement | null } */
20
+ target : null ,
21
+ style : {
22
+ cursor : '' ,
23
+ background : '' ,
24
+ outline : '' ,
25
+ } ,
26
+
27
+ clear ( ) {
28
+ if ( this . target ) {
29
+ this . target . style . cursor = this . style . cursor ;
30
+ this . target . style . background = this . style . background ;
31
+ this . target . style . outline = this . style . outline ;
32
+ }
33
+ this . target = null ;
34
+ } ,
35
+ } ;
36
+
37
+ const inspect = {
38
+ /** @param {MouseEvent } event */
39
+ handle ( { target } ) {
40
+ const same = previous . target && previous . target === target ;
41
+ const html = target instanceof HTMLElement ;
42
+ if ( same || ! html ) return ;
43
+
44
+ if ( previous . target ) previous . clear ( ) ;
45
+ previous . target = target ;
46
+ previous . style = {
47
+ cursor : target . style . cursor ,
48
+ background : target . style . background ,
49
+ outline : target . style . outline ,
50
+ } ;
51
+ target . style . cursor = 'pointer' ;
52
+ target . style . background = 'rgba(0, 136, 204, 0.2)' ;
53
+ target . style . outline = '1px dashed rgb(0, 136, 204)' ;
54
+ } ,
55
+ /** @param {MouseEvent } event */
56
+ click ( event ) {
57
+ event . preventDefault ( ) ;
58
+ document . removeEventListener ( 'mousemove' , inspect . handle , true ) ;
59
+ const node = getNode ( /** @type {Node } */ ( event . target ) ) ;
60
+ if ( node ) send ( 'bridge::ext/inspect' , { node : serialize ( node ) } ) ;
61
+ previous . clear ( ) ;
62
+ } ,
63
+ } ;
64
+
18
65
window . addEventListener ( 'message' , ( { data, source } ) => {
19
66
// only accept messages from our application or script
20
67
if ( source !== window || data ?. source !== 'svelte-devtools' ) return ;
21
68
22
69
if ( data . type === 'bridge::ext/select' ) {
23
70
const node = getNode ( data . payload ) ;
24
- // @ts -expect-error - saved for `inspect()`
71
+ // @ts -expect-error - saved for `devtools. inspect()`
25
72
if ( node ) window . $n = node . detail ;
26
73
} else if ( data . type === 'bridge::ext/highlight' ) {
27
74
const node = getNode ( data . payload ) ;
28
75
return highlight ( node ) ;
76
+ } else if ( data . type === 'bridge::ext/inspect' ) {
77
+ switch ( data . payload ) {
78
+ case 'start' : {
79
+ document . addEventListener ( 'mousemove' , inspect . handle , true ) ;
80
+ document . addEventListener ( 'click' , inspect . click , {
81
+ capture : true ,
82
+ once : true ,
83
+ } ) ;
84
+ break ;
85
+ }
86
+ default : {
87
+ document . removeEventListener ( 'mousemove' , inspect . handle , true ) ;
88
+ document . removeEventListener ( 'click' , inspect . click , true ) ;
89
+ previous . clear ( ) ;
90
+ }
91
+ }
29
92
}
30
-
31
- // --- TODO: cleanup/implement below ---
32
-
33
- // case 'bridge::ext/inspect': {
34
- // console.log(data.payload, data.payload instanceof HTMLElement);
35
- // /** @param {MouseEvent } event */
36
- // const move = ({ target }) => highlight({ type: 'element', detail: target });
37
- // if (data.payload instanceof HTMLElement) {
38
- // const node = getNode(data.payload);
39
- // if (node) window.postMessage({ type: 'inspect', node: serialize(node) });
40
- // } else if (data.payload === 'start') {
41
- // document.addEventListener('mousemove', move, true);
42
- // document.addEventListener(
43
- // 'click',
44
- // ({ target }) => {
45
- // document.removeEventListener('mousemove', move, true);
46
- // const node = getNode(/** @type {Node } */ (target));
47
- // if (node) window.postMessage({ type: 'inspect', node: serialize(node) });
48
- // },
49
- // { capture: true, once: true },
50
- // );
51
- // return;
52
- // }
53
- // document.removeEventListener('mousemove', move, true);
54
- // return highlight(undefined);
55
- // }
56
-
57
- // case 'bridge::ext/profiler': {
58
- // return data.payload ? startProfiler() : stopProfiler();
59
- // }
60
-
61
- // switch (data.type) {
62
-
63
- // case 'startPicker':
64
- // startPicker();
65
- // break;
66
-
67
- // case 'stopPicker':
68
- // stopPicker();
69
- // break;
70
-
71
- // case 'startProfiler':
72
- // profiler.start();
73
- // break;
74
-
75
- // case 'stopProfiler':
76
- // profiler.stop();
77
- // break;
78
93
} ) ;
79
94
80
95
/**
0 commit comments