|
| 1 | +import { writable } from "svelte/store"; |
| 2 | +import { parse, stringify } from "./url"; |
| 3 | + |
| 4 | +/** |
| 5 | + * stack, get path and routes by store subscribe calling. |
| 6 | + */ |
| 7 | +export let historyStack; |
| 8 | + |
| 9 | +let beforeHookHandler = () => true; |
| 10 | + |
| 11 | +/** |
| 12 | + * store, pass reactive data to Route component. |
| 13 | + */ |
| 14 | +export const historyStore = writable({ |
| 15 | + currentRoute: parse("/"), |
| 16 | + routes: [parse("/")] |
| 17 | +}) |
| 18 | + |
| 19 | +historyStore.subscribe((val) => { |
| 20 | + historyStack = val; |
| 21 | +}); |
| 22 | + |
| 23 | +/** |
| 24 | + * router hook before changing |
| 25 | + * |
| 26 | + * @param {function} hook |
| 27 | + */ |
| 28 | +export const onBeforeEnter = (hook) => { |
| 29 | + beforeHookHandler = hook; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * handle route jumping logic by calling hook, if step into next route or jump to redirect route, change promise to resolve. else if intercept router, change promise to reject. |
| 34 | + * |
| 35 | + * @param {object} to |
| 36 | + * @param {object} from |
| 37 | + * @param {string} action |
| 38 | + */ |
| 39 | +const callHook = (to, from, action) => { |
| 40 | + return new Promise((resolve, reject) => { |
| 41 | + let nextStatus = undefined; |
| 42 | + const next = (res = true) => nextStatus = res; |
| 43 | + beforeHookHandler(to, from, next, action); |
| 44 | + if (nextStatus === true) { |
| 45 | + resolve(); |
| 46 | + } else if (nextStatus === false) { |
| 47 | + reject(); |
| 48 | + } else if ( |
| 49 | + typeof nextStatus === "string" || |
| 50 | + typeof nextStatus === "object" |
| 51 | + ) { |
| 52 | + resolve(nextStatus); |
| 53 | + } |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * push forward a history route |
| 59 | + * |
| 60 | + * @param {string | object} route |
| 61 | + */ |
| 62 | +export const push = async (route) => { |
| 63 | + try { |
| 64 | + const url = stringify(route); |
| 65 | + const routeObj = parse(route); |
| 66 | + if (historyStack.currentRoute.url !== url) { |
| 67 | + const status = await callHook(routeObj, historyStack.currentRoute, "push"); |
| 68 | + if (status !== undefined) { |
| 69 | + push(status); |
| 70 | + } else { |
| 71 | + window.history.pushState(null, "", url); |
| 72 | + historyStore.update((store) => { |
| 73 | + store.currentRoute = routeObj; |
| 74 | + store.routes.push(routeObj); |
| 75 | + return store; |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + } catch (error) { |
| 80 | + if (error) { |
| 81 | + console.log("[router push error:]", error); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * replace current history route |
| 88 | + * |
| 89 | + * @param {string | object} route |
| 90 | + */ |
| 91 | +export const replace = async (route) => { |
| 92 | + try { |
| 93 | + const url = stringify(route); |
| 94 | + const routeObj = parse(route); |
| 95 | + if (historyStack.currentRoute.url !== url) { |
| 96 | + const status = await callHook(routeObj, historyStack.currentRoute, "replace"); |
| 97 | + if (status !== undefined) { |
| 98 | + push(status); |
| 99 | + } else { |
| 100 | + window.history.replaceState(null, "", url); |
| 101 | + historyStore.update((store) => { |
| 102 | + store.currentRoute = routeObj; |
| 103 | + store.routes[0] = routeObj; |
| 104 | + return store; |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | + } catch (error) { |
| 109 | + if (error) { |
| 110 | + console.log("[router replace error:]", error); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * jump route |
| 117 | + * |
| 118 | + * @param {number} step |
| 119 | + */ |
| 120 | +export const go = async (step) => { |
| 121 | + try { |
| 122 | + const curretIndex = historyStack.routes.findIndex(item => item.url === historyStack.currentRoute.url); |
| 123 | + const stepRouteObj = historyStack.routes[curretIndex + step]; |
| 124 | + if (step !== 0) { |
| 125 | + const status = await callHook(stepRouteObj, historyStack.currentRoute, "go"); |
| 126 | + if (status !== undefined) { |
| 127 | + push(status); |
| 128 | + } else { |
| 129 | + window.history.go(step); |
| 130 | + historyStore.update((store) => { |
| 131 | + store.currentRoute = stepRouteObj; |
| 132 | + return store; |
| 133 | + }); |
| 134 | + } |
| 135 | + } |
| 136 | + } catch (error) { |
| 137 | + if (error) { |
| 138 | + console.log("[router go error:]", error); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * step forward a route |
| 145 | + */ |
| 146 | +export const forward = async () => { |
| 147 | + try { |
| 148 | + const curretIndex = historyStack.routes.findIndex(item => item.url === historyStack.currentRoute.url); |
| 149 | + const forwardRouteObj = historyStack.routes[curretIndex + 1]; |
| 150 | + const status = await callHook(forwardRouteObj, historyStack.currentRoute, "forward"); |
| 151 | + if (status !== undefined) { |
| 152 | + push(status); |
| 153 | + } else { |
| 154 | + window.history.forward(); |
| 155 | + historyStore.update((store) => { |
| 156 | + store.currentRoute = forwardRouteObj; |
| 157 | + return store; |
| 158 | + }); |
| 159 | + } |
| 160 | + } catch (error) { |
| 161 | + if (error) { |
| 162 | + console.log("[router forward error:]", error); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * step back a route |
| 169 | + */ |
| 170 | +export const back = async () => { |
| 171 | + try { |
| 172 | + const curretIndex = historyStack.routes.findIndex(item => item.url === historyStack.currentRoute.url); |
| 173 | + const backRouteObj = historyStack.routes[curretIndex - 1]; |
| 174 | + const status = await callHook(backRouteObj, historyStack.currentRoute, "back"); |
| 175 | + if (status !== undefined) { |
| 176 | + push(status); |
| 177 | + } else { |
| 178 | + window.history.back(); |
| 179 | + historyStore.update((store) => { |
| 180 | + store.currentRoute = backRouteObj; |
| 181 | + return store; |
| 182 | + }); |
| 183 | + } |
| 184 | + } catch (error) { |
| 185 | + if (error) { |
| 186 | + console.log("[router go error:]", error); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +window.onpopstate = async (e) => { |
| 192 | + try { |
| 193 | + const url = window.location.href.split(window.location.origin)[1]; |
| 194 | + const routeObj = parse(url); |
| 195 | + const status = await callHook(routeObj, historyStack.currentRoute, "popstate"); |
| 196 | + if (status !== undefined) { |
| 197 | + push(status); |
| 198 | + } else { |
| 199 | + historyStore.update((store) => { |
| 200 | + store.currentRoute = routeObj; |
| 201 | + return store; |
| 202 | + }); |
| 203 | + } |
| 204 | + } catch (error) { |
| 205 | + if (error) { |
| 206 | + console.log("[router change error:]", error); |
| 207 | + } else { |
| 208 | + /** |
| 209 | + * avoid browser history stepping back or forward |
| 210 | + */ |
| 211 | + const url = window.location.href.split(window.location.origin)[1]; |
| 212 | + const routeObj = parse(url); |
| 213 | + const curretIndex = historyStack.routes.findIndex(item => item.url === historyStack.currentRoute.url); |
| 214 | + const routeIndex = historyStack.routes.findIndex(item => item.url === routeObj.url); |
| 215 | + const isBackForward = routeIndex < curretIndex; |
| 216 | + const isNextForward = routeIndex > curretIndex; |
| 217 | + if (isBackForward) { |
| 218 | + window.history.forward(); |
| 219 | + } else if (isNextForward) { |
| 220 | + window.history.back(); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments