one
Version:
One is a new React Framework that makes Vite serve both native and web.
161 lines (159 loc) • 6 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: !0
});
},
__copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
return to;
};
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: !0
}), mod);
var ScrollBehavior_exports = {};
__export(ScrollBehavior_exports, {
ScrollBehavior: () => ScrollBehavior,
registerScrollGroup: () => registerScrollGroup
});
module.exports = __toCommonJS(ScrollBehavior_exports);
var import_react = require("react"),
import_lastAction = require("../router/lastAction.native.js"),
import_router = require("../router/router.native.js"),
KEY = "one-sr",
GROUP_KEY = "one-sr-groups",
getState = function () {
return JSON.parse(sessionStorage.getItem(KEY) || "{}");
},
getGroupState = function () {
return JSON.parse(sessionStorage.getItem(GROUP_KEY) || "{}");
},
isFirstLoad = !0,
activeGroups = /* @__PURE__ */new Set();
function registerScrollGroup(groupId) {
return activeGroups.add(groupId), function () {
activeGroups.delete(groupId);
};
}
function getGroupKey(pathname) {
var longestMatch = null,
_iteratorNormalCompletion = !0,
_didIteratorError = !1,
_iteratorError = void 0;
try {
for (var _iterator = activeGroups[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
var group = _step.value;
pathname.startsWith(group) && (!longestMatch || group.length > longestMatch.length) && (longestMatch = group);
}
} catch (err) {
_didIteratorError = !0, _iteratorError = err;
} finally {
try {
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
} finally {
if (_didIteratorError) throw _iteratorError;
}
}
return longestMatch;
}
function restorePosition() {
try {
var positions = getState(),
saved = positions[window.location.pathname];
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(),
saved = positions[groupId];
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 = !1,
previousPathname = null;
function rememberScrollPosition() {
didPop = !1;
var pathname = window.location.pathname,
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 > "u" || !window.addEventListener)) {
disable?.();
var popStateController = new AbortController();
window.addEventListener("popstate", function () {
didPop = !0, (0, import_lastAction.setLastAction)();
}, {
signal: popStateController.signal
});
var disposeOnLoadState = (0, import_router.subscribeToLoadingState)(function (state) {
state === "loading" && rememberScrollPosition();
}),
disposeOnRootState = (0, import_router.subscribeToRootState)(function (state) {
var _state_linkOptions;
if (isFirstLoad) {
isFirstLoad = !1, previousPathname = window.location.pathname;
return;
}
if (((_state_linkOptions = state.linkOptions) === null || _state_linkOptions === void 0 ? void 0 : _state_linkOptions.scroll) !== !1) {
var {
hash
} = state,
currentPathname = window.location.pathname;
if (hash) setTimeout(function () {
scrollToHash(hash);
});else if (didPop) props.disable !== "restore" && restorePosition();else {
var _state_linkOptions1,
prevGroup = previousPathname ? getGroupKey(previousPathname) : null,
currentGroup = getGroupKey(currentPathname);
prevGroup && currentGroup && prevGroup === currentGroup ? restoreGroupPosition(currentGroup) : !((_state_linkOptions1 = state.linkOptions) === null || _state_linkOptions1 === void 0) && _state_linkOptions1.scrollGroup ? restoreGroupPosition(state.linkOptions.scrollGroup) : window.scrollTo(0, 0);
}
previousPathname = currentPathname;
}
});
return disable = function () {
popStateController.abort(), disposeOnLoadState(), disposeOnRootState();
}, disable;
}
}
function scrollToHash(hash) {
if (!(!hash || !hash.startsWith("#"))) {
var id = hash.slice(1),
el = document.getElementById(id);
el && el.scrollIntoView({
behavior: "instant"
});
}
}
function ScrollBehavior(props) {
return process.env.VITE_ENVIRONMENT === "client" && ((0, import_react.useEffect)(function () {
window.location.hash && scrollToHash(window.location.hash);
}, []), (0, import_react.useEffect)(function () {
return configure(props);
}, [props.disable])), null;
}
//# sourceMappingURL=ScrollBehavior.native.js.map