@@ -30,13 +30,28 @@ function postMessage(action) {
30
30
31
31
function runQuery ( rootNode , query ) {
32
32
const result = parser . parse ( { rootNode, query } ) ;
33
- const selector = result . elements . map ( ( x ) => x . cssPath ) . join ( ', ' ) ;
33
+ const { elements, expression, formatted } = result ;
34
+ const selector = elements . map ( ( x ) => x . cssPath ) . join ( ', ' ) ;
35
+
34
36
state . queriedNodes =
35
- result . elements . length > 0
36
- ? Array . from ( rootNode . querySelectorAll ( selector ) )
37
- : [ ] ;
37
+ elements . length > 0 ? Array . from ( rootNode . querySelectorAll ( selector ) ) : [ ] ;
38
+
38
39
state . highlighter . highlight ( { nodes : state . queriedNodes } ) ;
39
- return result ;
40
+
41
+ const accessibleRoles = Object . keys ( result . accessibleRoles ) . reduce (
42
+ ( acc , key ) => {
43
+ acc [ key ] = true ;
44
+ return acc ;
45
+ } ,
46
+ { } ,
47
+ ) ;
48
+
49
+ return {
50
+ elements,
51
+ expression,
52
+ formatted,
53
+ accessibleRoles,
54
+ } ;
40
55
}
41
56
42
57
function setInnerHTML ( node , html ) {
@@ -132,24 +147,21 @@ function onSelectNode(node, { origin }) {
132
147
}
133
148
134
149
postMessage ( action ) ;
150
+
151
+ if ( action . type === 'SELECT_NODE' ) {
152
+ // we patched the highlight optimistically, but we also
153
+ // need to inform the state manager. Complete the update
154
+ state . query = action . suggestion . snippet ;
155
+ const result = runQuery ( state . rootNode , state . query ) ;
156
+ postMessage ( { type : 'SANDBOX_READY' , result } ) ;
157
+ }
135
158
}
136
159
137
160
function updateSandbox ( rootNode , markup , query ) {
138
161
postMessage ( { type : 'SANDBOX_BUSY' } ) ;
139
162
setInnerHTML ( rootNode , markup ) ;
140
163
141
- // get and clean result
142
- // eslint-disable-next-line no-unused-vars
143
- const { markup : m , query : q , ...data } = runQuery ( rootNode , query ) ;
144
-
145
- const result = {
146
- ...data ,
147
- accessibleRoles : Object . keys ( data . accessibleRoles ) . reduce ( ( acc , key ) => {
148
- acc [ key ] = true ;
149
- return acc ;
150
- } , { } ) ,
151
- } ;
152
-
164
+ const result = runQuery ( rootNode , query ) ;
153
165
postMessage ( { type : 'SANDBOX_READY' , result } ) ;
154
166
}
155
167
0 commit comments