@@ -30,13 +30,28 @@ function postMessage(action) {
3030
3131function runQuery ( rootNode , query ) {
3232 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+
3436 state . queriedNodes =
35- result . elements . length > 0
36- ? Array . from ( rootNode . querySelectorAll ( selector ) )
37- : [ ] ;
37+ elements . length > 0 ? Array . from ( rootNode . querySelectorAll ( selector ) ) : [ ] ;
38+
3839 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+ } ;
4055}
4156
4257function setInnerHTML ( node , html ) {
@@ -132,24 +147,21 @@ function onSelectNode(node, { origin }) {
132147 }
133148
134149 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+ }
135158}
136159
137160function updateSandbox ( rootNode , markup , query ) {
138161 postMessage ( { type : 'SANDBOX_BUSY' } ) ;
139162 setInnerHTML ( rootNode , markup ) ;
140163
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 ) ;
153165 postMessage ( { type : 'SANDBOX_READY' , result } ) ;
154166}
155167
0 commit comments