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

Function restoreSelection

code/higher-order-components/public/app.js:6518–6548  ·  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

6516 * nodes and place them back in, resulting in focus being lost.
6517 */
6518function restoreSelection(priorSelectionInformation) {
6519 var curFocusedElem = getActiveElement();
6520 var priorFocusedElem = priorSelectionInformation.focusedElem;
6521 var priorSelectionRange = priorSelectionInformation.selectionRange;
6522 if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) {
6523 if (hasSelectionCapabilities(priorFocusedElem)) {
6524 setSelection(priorFocusedElem, priorSelectionRange);
6525 }
6526
6527 // Focusing a node can change the scroll position, which is undesirable
6528 var ancestors = [];
6529 var ancestor = priorFocusedElem;
6530 while (ancestor = ancestor.parentNode) {
6531 if (ancestor.nodeType === ELEMENT_NODE) {
6532 ancestors.push({
6533 element: ancestor,
6534 left: ancestor.scrollLeft,
6535 top: ancestor.scrollTop
6536 });
6537 }
6538 }
6539
6540 priorFocusedElem.focus();
6541
6542 for (var i = 0; i < ancestors.length; i++) {
6543 var info = ancestors[i];
6544 info.element.scrollLeft = info.left;
6545 info.element.scrollTop = info.top;
6546 }
6547 }
6548}
6549
6550/**
6551 * @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