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

Function restoreSelection

code/third-party/public/app.js:5757–5787  ·  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

5755 * nodes and place them back in, resulting in focus being lost.
5756 */
5757function restoreSelection(priorSelectionInformation) {
5758 var curFocusedElem = getActiveElement();
5759 var priorFocusedElem = priorSelectionInformation.focusedElem;
5760 var priorSelectionRange = priorSelectionInformation.selectionRange;
5761 if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) {
5762 if (hasSelectionCapabilities(priorFocusedElem)) {
5763 setSelection(priorFocusedElem, priorSelectionRange);
5764 }
5765
5766 // Focusing a node can change the scroll position, which is undesirable
5767 var ancestors = [];
5768 var ancestor = priorFocusedElem;
5769 while (ancestor = ancestor.parentNode) {
5770 if (ancestor.nodeType === ELEMENT_NODE) {
5771 ancestors.push({
5772 element: ancestor,
5773 left: ancestor.scrollLeft,
5774 top: ancestor.scrollTop
5775 });
5776 }
5777 }
5778
5779 priorFocusedElem.focus();
5780
5781 for (var i = 0; i < ancestors.length; i++) {
5782 var info = ancestors[i];
5783 info.element.scrollLeft = info.left;
5784 info.element.scrollTop = info.top;
5785 }
5786 }
5787}
5788
5789/**
5790 * @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