|
1 | | -import { createQueue } from 'iterable-observer'; |
2 | | -import { getVisibleText, scrollTo, formToJSON , buildURLData } from 'web-utility'; |
3 | | - |
4 | | -export type LinkElement = HTMLAnchorElement | HTMLAreaElement | HTMLFormElement; |
5 | | - |
6 | | -export enum PathPrefix { |
7 | | - hash = '#', |
8 | | - path = '/' |
9 | | -} |
10 | | - |
11 | | -export type PathMode = keyof typeof PathPrefix; |
| 1 | +import { URLPattern } from 'urlpattern-polyfill'; |
| 2 | +import { |
| 3 | + getVisibleText, |
| 4 | + scrollTo, |
| 5 | + formToJSON, |
| 6 | + buildURLData, |
| 7 | + parseURLData, |
| 8 | + delegate, |
| 9 | + isXDomain |
| 10 | +} from 'web-utility'; |
| 11 | +import { observable, action } from 'mobx'; |
12 | 12 |
|
13 | 13 | const { location, history } = window; |
| 14 | +const baseURL = document.querySelector('base')?.href || location.origin, |
| 15 | + originalTitle = document.querySelector('title')?.textContent.trim(); |
14 | 16 |
|
15 | 17 | export class History { |
16 | | - stream = createQueue<string>(); |
17 | | - paths: string[] = []; |
18 | | - prefix: PathPrefix; |
19 | | - |
20 | | - get path() { |
21 | | - return location[ |
22 | | - this.prefix === PathPrefix.hash ? 'hash' : 'pathname' |
23 | | - ].slice(1); |
| 18 | + @observable |
| 19 | + path = ''; |
| 20 | + |
| 21 | + @observable |
| 22 | + oldPath = ''; |
| 23 | + |
| 24 | + constructor() { |
| 25 | + this.restore(); |
| 26 | + |
| 27 | + window.addEventListener('hashchange', this.restore); |
| 28 | + window.addEventListener('popstate', this.restore); |
| 29 | + |
| 30 | + document.addEventListener( |
| 31 | + 'click', |
| 32 | + delegate('a[href], area[href]', this.handleLink.bind(this)) |
| 33 | + ); |
| 34 | + document.addEventListener( |
| 35 | + 'submit', |
| 36 | + delegate('form[action]', this.handleForm) |
| 37 | + ); |
24 | 38 | } |
25 | 39 |
|
26 | | - constructor(mode: PathMode = 'hash') { |
27 | | - this.prefix = PathPrefix[mode]; |
28 | | - } |
29 | | - |
30 | | - [Symbol.asyncIterator]() { |
31 | | - return this.stream.observable[Symbol.asyncIterator](); |
32 | | - } |
| 40 | + protected restore = () => { |
| 41 | + const { state } = history; |
33 | 42 |
|
34 | | - async set(path: string, title = document.title) { |
35 | | - if (!this.paths.includes(path)) this.paths.push(path); |
| 43 | + this.push(); |
36 | 44 |
|
37 | | - await this.stream.process(path); |
38 | | - |
39 | | - document.title = title; |
40 | | - } |
| 45 | + document.title = |
| 46 | + state?.title || this.titleOf() || originalTitle || location.href; |
| 47 | + }; |
41 | 48 |
|
42 | | - push(path: string, title = document.title) { |
43 | | - history.pushState({ path, title }, title, this.prefix + path); |
| 49 | + @action |
| 50 | + push(path = location.href) { |
| 51 | + path = path.replace(baseURL, ''); |
44 | 52 |
|
45 | | - return this.set(path, title); |
46 | | - } |
| 53 | + if (path === this.path) return path; |
47 | 54 |
|
48 | | - replace(path: string, title = document.title) { |
49 | | - history.replaceState({ path, title }, title, this.prefix + path); |
| 55 | + this.oldPath = this.path; |
50 | 56 |
|
51 | | - return this.set(path, title); |
| 57 | + return (this.path = path); |
52 | 58 | } |
53 | 59 |
|
54 | | - compare(last: string, next: string) { |
55 | | - for (const path of this.paths) |
56 | | - if (last === path) return -1; |
57 | | - else if (next === path) return 1; |
| 60 | + static dataOf(path: string) { |
| 61 | + const [before, after] = path.split('#'); |
58 | 62 |
|
59 | | - return 0; |
| 63 | + return parseURLData(after || before); |
60 | 64 | } |
61 | 65 |
|
62 | | - static getInnerPath(link: LinkElement) { |
63 | | - const path = link.getAttribute('href') || link.getAttribute('action'); |
| 66 | + static match(pattern: string, path: string) { |
| 67 | + const { pathname, hash } = |
| 68 | + new URLPattern(pattern, baseURL).exec( |
| 69 | + new URL(path.split('?')[0], baseURL) |
| 70 | + ) || {}; |
64 | 71 |
|
65 | | - if ( |
66 | | - (link.target || '_self') === '_self' && |
67 | | - !path.match(/^\w+:/) && |
68 | | - (!(link instanceof HTMLFormElement) || |
69 | | - (link.getAttribute('method') || 'get').toLowerCase() === 'get') |
70 | | - ) |
71 | | - return path; |
| 72 | + return (hash || pathname)?.groups; |
72 | 73 | } |
73 | 74 |
|
74 | 75 | static getTitle(root: HTMLElement) { |
75 | 76 | return root.title || getVisibleText(root); |
76 | 77 | } |
77 | 78 |
|
78 | | - handleClick = (event: MouseEvent) => { |
79 | | - const link = (event.target as HTMLElement).closest< |
80 | | - HTMLAnchorElement | HTMLAreaElement |
81 | | - >('a[href], area[href]'); |
82 | | - |
83 | | - if (!link) return; |
84 | | - |
85 | | - const path = History.getInnerPath(link); |
| 79 | + titleOf(path = this.path) { |
| 80 | + path = path.replace(/^\//, ''); |
86 | 81 |
|
87 | | - if (!path) return; |
| 82 | + if (path) |
| 83 | + for (const node of document.querySelectorAll<HTMLAnchorElement>( |
| 84 | + `a[href="${path}"], area[href="${path}"]` |
| 85 | + )) { |
| 86 | + const title = History.getTitle(node); |
88 | 87 |
|
89 | | - event.preventDefault(); |
90 | | - |
91 | | - if (/^#.+/.test(path)) |
92 | | - return scrollTo(path, event.currentTarget as Element); |
93 | | - |
94 | | - this.push(path, History.getTitle(link)); |
95 | | - }; |
| 88 | + if (title) return title; |
| 89 | + } |
| 90 | + } |
96 | 91 |
|
97 | | - handleForm = (event: Event) => { |
98 | | - const form = event.target as HTMLFormElement; |
99 | | - const path = History.getInnerPath(form); |
| 92 | + handleLink(event: Event, link: HTMLAnchorElement) { |
| 93 | + const path = link.getAttribute('href'); |
100 | 94 |
|
101 | | - if (!path) return; |
| 95 | + if ((link.target || '_self') !== '_self' || isXDomain(path)) return; |
102 | 96 |
|
103 | 97 | event.preventDefault(); |
104 | 98 |
|
105 | | - this.push(path + '?' + buildURLData(formToJSON(form)), form.title); |
106 | | - }; |
107 | | - |
108 | | - private popping = false; |
| 99 | + if (path.startsWith('#')) |
| 100 | + try { |
| 101 | + if (document.querySelector(path)) |
| 102 | + return scrollTo(path, event.currentTarget as Element); |
| 103 | + } catch {} |
109 | 104 |
|
110 | | - listen(root: Element) { |
111 | | - root.addEventListener('click', this.handleClick); |
112 | | - root.addEventListener('submit', this.handleForm); |
| 105 | + const title = History.getTitle(link); |
113 | 106 |
|
114 | | - if (this.prefix === PathPrefix.hash) |
115 | | - window.addEventListener( |
116 | | - 'hashchange', |
117 | | - () => this.popping || this.set(this.path) |
118 | | - ); |
| 107 | + history.pushState({ title }, (document.title = title), path); |
119 | 108 |
|
120 | | - window.addEventListener('popstate', async ({ state }) => { |
121 | | - const { path = this.path, title } = state || {}; |
| 109 | + this.push(path); |
| 110 | + } |
122 | 111 |
|
123 | | - this.popping = true; |
| 112 | + handleForm = (event: Event, form: HTMLFormElement) => { |
| 113 | + const { method, target } = form; |
124 | 114 |
|
125 | | - await this.set(path, title); |
| 115 | + if (method !== 'get' || (target || '_self') !== '_self') return; |
126 | 116 |
|
127 | | - this.popping = false; |
128 | | - }); |
| 117 | + event.preventDefault(); |
129 | 118 |
|
130 | | - setTimeout(() => this.replace(this.path, (history.state || {}).title)); |
| 119 | + const path = form.getAttribute('action'), |
| 120 | + data = buildURLData(formToJSON(form)); |
131 | 121 |
|
132 | | - return this; |
133 | | - } |
| 122 | + this.push(`${path}?${data}`); |
| 123 | + }; |
134 | 124 | } |
| 125 | + |
| 126 | +export default new History(); |
0 commit comments