Skip to content

fix: update suggestion pane when clicking in preview pane #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions src/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,28 @@ function postMessage(action) {

function runQuery(rootNode, query) {
const result = parser.parse({ rootNode, query });
const selector = result.elements.map((x) => x.cssPath).join(', ');
const { elements, expression, formatted } = result;
const selector = elements.map((x) => x.cssPath).join(', ');

state.queriedNodes =
result.elements.length > 0
? Array.from(rootNode.querySelectorAll(selector))
: [];
elements.length > 0 ? Array.from(rootNode.querySelectorAll(selector)) : [];

state.highlighter.highlight({ nodes: state.queriedNodes });
return result;

const accessibleRoles = Object.keys(result.accessibleRoles).reduce(
(acc, key) => {
acc[key] = true;
return acc;
},
{},
);

return {
elements,
expression,
formatted,
accessibleRoles,
};
}

function setInnerHTML(node, html) {
Expand Down Expand Up @@ -132,24 +147,21 @@ function onSelectNode(node, { origin }) {
}

postMessage(action);

if (action.type === 'SELECT_NODE') {
// we patched the highlight optimistically, but we also
// need to inform the state manager. Complete the update
state.query = action.suggestion.snippet;
const result = runQuery(state.rootNode, state.query);
postMessage({ type: 'SANDBOX_READY', result });
}
}

function updateSandbox(rootNode, markup, query) {
postMessage({ type: 'SANDBOX_BUSY' });
setInnerHTML(rootNode, markup);

// get and clean result
// eslint-disable-next-line no-unused-vars
const { markup: m, query: q, ...data } = runQuery(rootNode, query);

const result = {
...data,
accessibleRoles: Object.keys(data.accessibleRoles).reduce((acc, key) => {
acc[key] = true;
return acc;
}, {}),
};

const result = runQuery(rootNode, query);
postMessage({ type: 'SANDBOX_READY', result });
}

Expand Down