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

Function restoreSelection

code/one-direction-data-flow/public/app.js:6397–6427  ·  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

6395 * nodes and place them back in, resulting in focus being lost.
6396 */
6397function restoreSelection(priorSelectionInformation) {
6398 var curFocusedElem = getActiveElement();
6399 var priorFocusedElem = priorSelectionInformation.focusedElem;
6400 var priorSelectionRange = priorSelectionInformation.selectionRange;
6401 if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) {
6402 if (hasSelectionCapabilities(priorFocusedElem)) {
6403 setSelection(priorFocusedElem, priorSelectionRange);
6404 }
6405
6406 // Focusing a node can change the scroll position, which is undesirable
6407 var ancestors = [];
6408 var ancestor = priorFocusedElem;
6409 while (ancestor = ancestor.parentNode) {
6410 if (ancestor.nodeType === ELEMENT_NODE) {
6411 ancestors.push({
6412 element: ancestor,
6413 left: ancestor.scrollLeft,
6414 top: ancestor.scrollTop
6415 });
6416 }
6417 }
6418
6419 priorFocusedElem.focus();
6420
6421 for (var i = 0; i < ancestors.length; i++) {
6422 var info = ancestors[i];
6423 info.element.scrollLeft = info.left;
6424 info.element.scrollTop = info.top;
6425 }
6426 }
6427}
6428
6429/**
6430 * @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