UNPKG

respond-framework

Version:
77 lines (76 loc) 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.linkOut = exports.forward = exports.back = void 0; var _reactNative = require("react-native"); var _sessionStorage = require("../../utils/sessionStorage.js"); var _browserState = require("../browserState.js"); var buttons = require("./buttons.js"); var _constants = require("../../helpers/constants.js"); const back = async () => { _browserState.default.prevIndex = -1; const { linkedOut, maxIndex } = _browserState.default; _sessionStorage.default.setItem('browserState', JSON.stringify({ linkedOut, maxIndex })); await buttons.go(-getIndex()); history.back(); }; exports.back = back; const forward = async () => { if (!_browserState.default.linkedOut) { // go to tail, but hasn't linked out yet, so won't leave site const delta = _browserState.default.maxIndex - getIndex(); await buttons.go(delta); _browserState.default.prevIndex = _browserState.default.maxIndex; history.replaceState(history.state, '', window.state.respond.prevUrl); // ensure the same/previous url appears on our placeholder tail entry return; } _browserState.default.prevIndex = _browserState.default.maxIndex + 1; _browserState.default.out = true; const { linkedOut, maxIndex } = _browserState.default; _sessionStorage.default.setItem('browserState', JSON.stringify({ linkedOut, maxIndex })); const delta = _browserState.default.maxIndex - getIndex(); await buttons.go(delta); history.forward(); }; exports.forward = forward; const linkOut = (url, e) => { e = typeof url === 'object' ? url : e; // convenience: <a href={url} onClick={state.respond.history.linkOut} url = typeof url === 'string' ? url : e?.currentTarget.href; e?.preventDefault(); if (_constants.isTest) return url; if (_constants.isNative) { _reactNative.Linking.openURL(url); return; } if (location.host === new URL(url).host) { window.open(url, '_blank'); // a host of problems will occur if you open your site twice in the same tab, as they'll share the same sessionStorage -- apps should be designed to not need reloads, which is especially easy to resolve given Respond keeps pretty much everything in state, including things such as basenames and navigationCache; if you really need this -- which is a non-ideal workaround in today's reactive landscape -- feel free to work on this file and submit a PR; basically you will have to differentiate between the below sessionStorage items between multiple tabs somehow; the juice most likely isn't worth the squeeze return; } _browserState.default.prevIndex = _browserState.default.maxIndex + 1; _browserState.default.linkedOut = true; _browserState.default.out = true; const { linkedOut, maxIndex } = _browserState.default; _sessionStorage.default.setItem('browserState', JSON.stringify({ linkedOut, maxIndex })); window.location = url; }; exports.linkOut = linkOut; const getIndex = () => history.state?.index ?? 0; // should never be 0 if everything is working correctly