Skip to content

Commit

Permalink
chore(example): avoid re-highlighting the existing selection
Browse files Browse the repository at this point in the history
  • Loading branch information
alienzhou committed May 20, 2020
1 parent c9ca81a commit 53c1909
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,42 @@ highlighter
});

/**
* attach hooks
* avoid re-highlighting the existing selection
*/
highlighter.hooks.Render.SelectedNodes.tap(
(id, selectedNodes) => selectedNodes.filter(n => n.$node.textContent)
);
function getIds(selected) {
if (!selected || !selected.$node || !selected.$node.parentNode) {
return [];
}
return [
highlighter.getIdByDom(selected.$node.parentNode),
...highlighter.getExtraIdByDom(selected.$node.parentNode)
].filter(i => i)
}
function getIntersection(arrA, arrB) {
const record = {};
const intersection = [];
arrA.forEach(i => record[i] = true);
arrB.forEach(i => record[i] && intersection.push(i) && (record[i] = false));
return intersection;
}
highlighter.hooks.Render.SelectedNodes.tap((id, selectedNodes) => {
selectedNodes = selectedNodes.filter(n => n.$node.textContent);
if (selectedNodes.length === 0) {
return [];
}

const candidates = selectedNodes.slice(1).reduce(
(left, selected) => getIntersection(left, getIds(selected)),
getIds(selectedNodes[0])
);
for (let i = 0; i < candidates.length; i++) {
if (highlighter.getDoms(candidates[i]).length === selectedNodes.length) {
return [];
}
}

return selectedNodes;
});

highlighter.hooks.Serialize.Restore.tap(
source => log('Serialize.Restore hook -', source)
Expand Down

0 comments on commit 53c1909

Please sign in to comment.