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

Function restoreSelection

code/new-context-api/public/app.js:5885–5915  ·  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

5883 * nodes and place them back in, resulting in focus being lost.
5884 */
5885function restoreSelection(priorSelectionInformation) {
5886 var curFocusedElem = getActiveElement();
5887 var priorFocusedElem = priorSelectionInformation.focusedElem;
5888 var priorSelectionRange = priorSelectionInformation.selectionRange;
5889 if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) {
5890 if (hasSelectionCapabilities(priorFocusedElem)) {
5891 setSelection(priorFocusedElem, priorSelectionRange);
5892 }
5893
5894 // Focusing a node can change the scroll position, which is undesirable
5895 var ancestors = [];
5896 var ancestor = priorFocusedElem;
5897 while (ancestor = ancestor.parentNode) {
5898 if (ancestor.nodeType === ELEMENT_NODE) {
5899 ancestors.push({
5900 element: ancestor,
5901 left: ancestor.scrollLeft,
5902 top: ancestor.scrollTop
5903 });
5904 }
5905 }
5906
5907 priorFocusedElem.focus();
5908
5909 for (var i = 0; i < ancestors.length; i++) {
5910 var info = ancestors[i];
5911 info.element.scrollLeft = info.left;
5912 info.element.scrollTop = info.top;
5913 }
5914 }
5915}
5916
5917/**
5918 * @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