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

Function restoreSelection

code/dependency-injection/public/app.js:6617–6647  ·  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

6615 * nodes and place them back in, resulting in focus being lost.
6616 */
6617function restoreSelection(priorSelectionInformation) {
6618 var curFocusedElem = getActiveElement();
6619 var priorFocusedElem = priorSelectionInformation.focusedElem;
6620 var priorSelectionRange = priorSelectionInformation.selectionRange;
6621 if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) {
6622 if (hasSelectionCapabilities(priorFocusedElem)) {
6623 setSelection(priorFocusedElem, priorSelectionRange);
6624 }
6625
6626 // Focusing a node can change the scroll position, which is undesirable
6627 var ancestors = [];
6628 var ancestor = priorFocusedElem;
6629 while (ancestor = ancestor.parentNode) {
6630 if (ancestor.nodeType === ELEMENT_NODE) {
6631 ancestors.push({
6632 element: ancestor,
6633 left: ancestor.scrollLeft,
6634 top: ancestor.scrollTop
6635 });
6636 }
6637 }
6638
6639 priorFocusedElem.focus();
6640
6641 for (var i = 0; i < ancestors.length; i++) {
6642 var info = ancestors[i];
6643 info.element.scrollLeft = info.left;
6644 info.element.scrollTop = info.top;
6645 }
6646 }
6647}
6648
6649/**
6650 * @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