|
| 1 | +/* eslint-env browser */ |
| 2 | +import ScrollBehavior, { |
| 3 | + TransitionHook, |
| 4 | + ScrollPosition, |
| 5 | + ScrollTarget, |
| 6 | +} from 'scroll-behavior' |
| 7 | +import { |
| 8 | + Api, |
| 9 | + Middleware, |
| 10 | + LocationEntry, |
| 11 | + FluxStandardRoutingAction, |
| 12 | + Request, |
| 13 | + ScrollRestorer, |
| 14 | + ScrollRestorerCreator, |
| 15 | +} from '@respond-framework/types' |
| 16 | + |
| 17 | +export { ScrollPosition } from 'scroll-behavior' |
| 18 | + |
| 19 | +export type ShouldUpdateScroll<Action extends FluxStandardRoutingAction> = ( |
| 20 | + request: Request<Action>, |
| 21 | +) => ScrollTarget |
| 22 | + |
| 23 | +export type RestoreScrollOptions<Action extends FluxStandardRoutingAction> = { |
| 24 | + shouldUpdateScroll?: ShouldUpdateScroll<Action> |
| 25 | +} |
| 26 | + |
| 27 | +type Location<Action extends FluxStandardRoutingAction> = LocationEntry< |
| 28 | + Action |
| 29 | +> & { |
| 30 | + action: 'unknown' |
| 31 | +} |
| 32 | + |
| 33 | +export class RudyScrollRestorer<Action extends FluxStandardRoutingAction> |
| 34 | + implements ScrollRestorer<Action> { |
| 35 | + private options: RestoreScrollOptions<Action> |
| 36 | + |
| 37 | + private behavior: ScrollBehavior<Location<Action>, Request<Action>> |
| 38 | + |
| 39 | + private lastRequest?: Request<Action> |
| 40 | + |
| 41 | + private api: Api<Action> |
| 42 | + |
| 43 | + private transitionHooks: { [index: string]: TransitionHook } = {} |
| 44 | + |
| 45 | + private nextHookIndex = 0 |
| 46 | + |
| 47 | + private makeStorageKey = ( |
| 48 | + entry: LocationEntry<Action> | null, |
| 49 | + scrollBehaviorKey: string | null, |
| 50 | + ): string => |
| 51 | + `@@rudy-restore-scroll/${ |
| 52 | + entry ? `${entry.location.key}/` : `` |
| 53 | + }${JSON.stringify(scrollBehaviorKey)}` |
| 54 | + |
| 55 | + private saveScrollPosition = ( |
| 56 | + entry: LocationEntry<Action>, |
| 57 | + key: string | null, |
| 58 | + value: ScrollPosition, |
| 59 | + ): void => { |
| 60 | + window.sessionStorage.setItem( |
| 61 | + this.makeStorageKey(entry, key), |
| 62 | + JSON.stringify(value), |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + readScrollPosition = ( |
| 67 | + entry: LocationEntry<Action>, |
| 68 | + key: string | null, |
| 69 | + ): ScrollPosition | null => { |
| 70 | + const savedItem = window.sessionStorage.getItem( |
| 71 | + this.makeStorageKey(entry, key), |
| 72 | + ) |
| 73 | + if (savedItem === null) { |
| 74 | + return null |
| 75 | + } |
| 76 | + try { |
| 77 | + return JSON.parse(savedItem) |
| 78 | + } catch { |
| 79 | + return null |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + constructor(api: Api<Action>, options: RestoreScrollOptions<Action> = {}) { |
| 84 | + this.api = api |
| 85 | + this.options = options |
| 86 | + this.behavior = new ScrollBehavior<Location<Action>, Request<Action>>({ |
| 87 | + addTransitionHook: (hook: TransitionHook) => { |
| 88 | + const hookIndex = this.nextHookIndex |
| 89 | + this.nextHookIndex += 1 |
| 90 | + this.transitionHooks[hookIndex] = hook |
| 91 | + return () => { |
| 92 | + delete this.transitionHooks[hookIndex] |
| 93 | + } |
| 94 | + }, |
| 95 | + stateStorage: { |
| 96 | + save: this.saveScrollPosition, |
| 97 | + read: this.readScrollPosition, |
| 98 | + }, |
| 99 | + getCurrentLocation: () => this.getCurrentLocation(), |
| 100 | + shouldUpdateScroll: (_, request) => { |
| 101 | + if (!this.options.shouldUpdateScroll) { |
| 102 | + return true // default behaviour |
| 103 | + } |
| 104 | + return this.options.shouldUpdateScroll(request) |
| 105 | + }, |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + private getCurrentLocation = (): Location<Action> => { |
| 110 | + const location = this.api.getLocation() |
| 111 | + return { |
| 112 | + ...location.entries[location.index], |
| 113 | + action: 'unknown', |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + saveScroll: Middleware<Action> = () => { |
| 118 | + return (request, next) => { |
| 119 | + this.lastRequest = request |
| 120 | + const { action } = request |
| 121 | + if (!('location' in action && action.location.prev)) { |
| 122 | + // If there is no previous location, there is no position to save |
| 123 | + return next() |
| 124 | + } |
| 125 | + Object.keys(this.transitionHooks).forEach((hookIndex) => { |
| 126 | + this.transitionHooks[hookIndex]() |
| 127 | + }) |
| 128 | + return next() |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + restoreScroll: Middleware<Action> = () => { |
| 133 | + return (request, next) => { |
| 134 | + this.behavior.updateScroll(null, request) |
| 135 | + return next() |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + updateScroll = (): void => { |
| 140 | + if (this.lastRequest) { |
| 141 | + this.behavior.updateScroll(null, this.lastRequest) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +export default <Action extends FluxStandardRoutingAction>( |
| 147 | + options?: RestoreScrollOptions<Action>, |
| 148 | +): ScrollRestorerCreator<Action> => (api) => |
| 149 | + new RudyScrollRestorer(api, options) |
0 commit comments