MCPcopy Create free account
hub / github.com/krasimir/react-in-patterns / restoreSelection

Function restoreSelection

code/styling/public/app.js:6524–6554  ·  view source on GitHub ↗

* @restoreSelection: If any selection information was potentially lost, * restore it. This is useful when performing operations that could remove dom * nodes and place them back in, resulting in focus being lost.

(priorSelectionInformation)

Source from the content-addressed store, hash-verified

6522 * nodes and place them back in, resulting in focus being lost.
6523 */
6524function restoreSelection(priorSelectionInformation) {
6525 var curFocusedElem = getActiveElement();
6526 var priorFocusedElem = priorSelectionInformation.focusedElem;
6527 var priorSelectionRange = priorSelectionInformation.selectionRange;
6528 if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) {
6529 if (hasSelectionCapabilities(priorFocusedElem)) {
6530 setSelection(priorFocusedElem, priorSelectionRange);
6531 }
6532
6533 // Focusing a node can change the scroll position, which is undesirable
6534 var ancestors = [];
6535 var ancestor = priorFocusedElem;
6536 while (ancestor = ancestor.parentNode) {
6537 if (ancestor.nodeType === ELEMENT_NODE) {
6538 ancestors.push({
6539 element: ancestor,
6540 left: ancestor.scrollLeft,
6541 top: ancestor.scrollTop
6542 });
6543 }
6544 }
6545
6546 priorFocusedElem.focus();
6547
6548 for (var i = 0; i < ancestors.length; i++) {
6549 var info = ancestors[i];
6550 info.element.scrollLeft = info.left;
6551 info.element.scrollTop = info.top;
6552 }
6553 }
6554}
6555
6556/**
6557 * @getSelection: Gets the selection bounds of a focused textarea, input or

Callers 1

app.jsFile · 0.70

Calls 4

getActiveElementFunction · 0.70
isInDocumentFunction · 0.70
hasSelectionCapabilitiesFunction · 0.70
setSelectionFunction · 0.70

Tested by

no test coverage detected