one
Version:
One is a new React Framework that makes Vite serve both native and web.
177 lines (176 loc) • 5.28 kB
JavaScript
import { useEffect } from "react";
import { setLastAction } from "../router/lastAction.native.js";
import { subscribeToLoadingState, subscribeToRootState } from "../router/router.native.js";
var KEY = "one-sr";
var GROUP_KEY = "one-sr-groups";
var getState = function () {
return JSON.parse(sessionStorage.getItem(KEY) || "{}");
};
var getGroupState = function () {
return JSON.parse(sessionStorage.getItem(GROUP_KEY) || "{}");
};
var isFirstLoad = true;
var activeGroups = /* @__PURE__ */new Set();
function registerScrollGroup(groupId) {
activeGroups.add(groupId);
return function () {
activeGroups.delete(groupId);
};
}
function getGroupKey(pathname) {
var longestMatch = null;
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = void 0;
try {
for (var _iterator = activeGroups[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var group = _step.value;
if (pathname.startsWith(group) && (!longestMatch || group.length > longestMatch.length)) {
longestMatch = group;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return longestMatch;
}
function restorePosition() {
try {
var positions = getState();
var saved = positions[window.location.pathname];
if (typeof saved === "number") {
setTimeout(function () {
window.scrollTo(0, saved);
});
}
} catch (error) {
console.error(`Error restoring scroll position`, error);
sessionStorage.removeItem(KEY);
}
}
function restoreGroupPosition(groupId) {
try {
var positions = getGroupState();
var saved = positions[groupId];
if (typeof saved === "number") {
setTimeout(function () {
window.scrollTo(0, saved);
});
}
} catch (error) {
console.error(`Error restoring scroll position for group ${groupId}`, error);
sessionStorage.removeItem(GROUP_KEY);
}
}
var didPop = false;
var previousPathname = null;
function rememberScrollPosition() {
didPop = false;
var pathname = window.location.pathname;
var state = getState();
state[pathname] = window.scrollY;
sessionStorage.setItem(KEY, JSON.stringify(state));
var groupKey = getGroupKey(pathname);
if (groupKey) {
var groupState = getGroupState();
groupState[groupKey] = window.scrollY;
sessionStorage.setItem(GROUP_KEY, JSON.stringify(groupState));
}
previousPathname = pathname;
}
var disable = null;
function configure(props) {
if (typeof window === "undefined" || !window.addEventListener) {
return;
}
disable === null || disable === void 0 ? void 0 : disable();
var popStateController = new AbortController();
window.addEventListener("popstate", function () {
didPop = true;
setLastAction();
}, {
signal: popStateController.signal
});
var disposeOnLoadState = subscribeToLoadingState(function (state) {
if (state === "loading") {
rememberScrollPosition();
}
});
var disposeOnRootState = subscribeToRootState(function (state) {
var _state_linkOptions;
if (isFirstLoad) {
isFirstLoad = false;
previousPathname = window.location.pathname;
return;
}
if (((_state_linkOptions = state.linkOptions) === null || _state_linkOptions === void 0 ? void 0 : _state_linkOptions.scroll) === false) {
return;
}
var {
hash
} = state;
var currentPathname = window.location.pathname;
if (hash) {
setTimeout(function () {
scrollToHash(hash);
});
} else if (didPop) {
if (props.disable !== "restore") {
restorePosition();
}
} else {
var _state_linkOptions1;
var prevGroup = previousPathname ? getGroupKey(previousPathname) : null;
var currentGroup = getGroupKey(currentPathname);
if (prevGroup && currentGroup && prevGroup === currentGroup) {
restoreGroupPosition(currentGroup);
} else if ((_state_linkOptions1 = state.linkOptions) === null || _state_linkOptions1 === void 0 ? void 0 : _state_linkOptions1.scrollGroup) {
restoreGroupPosition(state.linkOptions.scrollGroup);
} else {
window.scrollTo(0, 0);
}
}
previousPathname = currentPathname;
});
disable = function () {
popStateController.abort();
disposeOnLoadState();
disposeOnRootState();
};
return disable;
}
function scrollToHash(hash) {
if (!hash || !hash.startsWith("#")) return;
var id = hash.slice(1);
var el = document.getElementById(id);
if (!el) return;
el.scrollIntoView({
behavior: "instant"
});
}
function ScrollBehavior(props) {
if (process.env.VITE_ENVIRONMENT === "client") {
useEffect(function () {
if (window.location.hash) {
scrollToHash(window.location.hash);
}
}, []);
useEffect(function () {
return configure(props);
}, [props.disable]);
}
return null;
}
export { ScrollBehavior, registerScrollGroup };
//# sourceMappingURL=ScrollBehavior.native.js.map