Skip to content

Commit 3a4d468

Browse files
authored
fix: update suggestion pane when clicking in preview pane (#269)
1 parent 3e98635 commit 3a4d468

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/sandbox.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,28 @@ function postMessage(action) {
3030

3131
function 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

4257
function 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

137160
function 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

Comments
 (0)