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

Function restoreSelection

code/communication/public/app.js:6559–6589  ·  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

6557 * nodes and place them back in, resulting in focus being lost.
6558 */
6559function restoreSelection(priorSelectionInformation) {
6560 var curFocusedElem = getActiveElement();
6561 var priorFocusedElem = priorSelectionInformation.focusedElem;
6562 var priorSelectionRange = priorSelectionInformation.selectionRange;
6563 if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) {
6564 if (hasSelectionCapabilities(priorFocusedElem)) {
6565 setSelection(priorFocusedElem, priorSelectionRange);
6566 }
6567
6568 // Focusing a node can change the scroll position, which is undesirable
6569 var ancestors = [];
6570 var ancestor = priorFocusedElem;
6571 while (ancestor = ancestor.parentNode) {
6572 if (ancestor.nodeType === ELEMENT_NODE) {
6573 ancestors.push({
6574 element: ancestor,
6575 left: ancestor.scrollLeft,
6576 top: ancestor.scrollTop
6577 });
6578 }
6579 }
6580
6581 priorFocusedElem.focus();
6582
6583 for (var i = 0; i < ancestors.length; i++) {
6584 var info = ancestors[i];
6585 info.element.scrollLeft = info.left;
6586 info.element.scrollTop = info.top;
6587 }
6588 }
6589}
6590
6591/**
6592 * @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