Files
30-seconds-of-code/node_modules/gatsby-react-router-scroll/StateStorage.js
2019-08-20 15:52:05 +02:00

63 lines
1.9 KiB
JavaScript

"use strict";
exports.__esModule = true;
exports.default = void 0;
var STATE_KEY_PREFIX = "@@scroll|";
var GATSBY_ROUTER_SCROLL_STATE = "___GATSBY_REACT_ROUTER_SCROLL";
var SessionStorage =
/*#__PURE__*/
function () {
function SessionStorage() {}
var _proto = SessionStorage.prototype;
_proto.read = function read(location, key) {
var stateKey = this.getStateKey(location, key);
try {
var value = window.sessionStorage.getItem(stateKey);
return JSON.parse(value);
} catch (e) {
if (process.env.NODE_ENV !== "production") {
console.warn("[gatsby-react-router-scroll] Unable to access sessionStorage; sessionStorage is not available.");
}
if (window && window[GATSBY_ROUTER_SCROLL_STATE] && window[GATSBY_ROUTER_SCROLL_STATE][stateKey]) {
return window[GATSBY_ROUTER_SCROLL_STATE][stateKey];
}
return {};
}
};
_proto.save = function save(location, key, value) {
var stateKey = this.getStateKey(location, key);
var storedValue = JSON.stringify(value);
try {
window.sessionStorage.setItem(stateKey, storedValue);
} catch (e) {
if (window && window[GATSBY_ROUTER_SCROLL_STATE]) {
window[GATSBY_ROUTER_SCROLL_STATE][stateKey] = JSON.parse(storedValue);
} else {
window[GATSBY_ROUTER_SCROLL_STATE] = {};
window[GATSBY_ROUTER_SCROLL_STATE][stateKey] = JSON.parse(storedValue);
}
if (process.env.NODE_ENV !== "production") {
console.warn("[gatsby-react-router-scroll] Unable to save state in sessionStorage; sessionStorage is not available.");
}
}
};
_proto.getStateKey = function getStateKey(location, key) {
var locationKey = location.key || location.pathname;
var stateKeyBase = "" + STATE_KEY_PREFIX + locationKey;
return key === null || typeof key === "undefined" ? stateKeyBase : stateKeyBase + "|" + key;
};
return SessionStorage;
}();
exports.default = SessionStorage;