UNPKG

3.44 kBJavaScriptView Raw
1import { addLeadingSlash } from '@tarojs/runtime';
2import { createBrowserHistory, createHashHistory, Action } from 'history';
3export { createBrowserHistory, createHashHistory } from 'history';
4import { RouterConfig } from './router/index.js';
5
6let history;
7let basename = '/';
8class MpaHistory {
9 constructor() {
10 this.back = window.history.back;
11 this.forward = window.history.forward;
12 this.pushState = this.eventState('pushState');
13 this.replaceState = this.eventState('replaceState');
14 }
15 get location() {
16 return {
17 pathname: window.location.pathname,
18 search: window.location.search,
19 hash: window.location.hash,
20 key: `${window.history.length}`,
21 state: window.history.state
22 };
23 }
24 createHref(_to) {
25 throw new Error('Method not implemented.');
26 }
27 parseUrl(to) {
28 let url = to.pathname || '';
29 if (RouterConfig.isPage(addLeadingSlash(url))) {
30 url += '.html';
31 }
32 if (to.search) {
33 url += `?${to.search}`;
34 }
35 if (to.hash) {
36 url += to.hash.startsWith('#') ? to.hash : `#${to.hash}`;
37 }
38 return url;
39 }
40 push(to, _state = {}) {
41 window.location.assign(this.parseUrl(to));
42 // this.pushState(_state, '', this.parseUrl(to))
43 }
44 replace(to, _state = {}) {
45 window.location.replace(this.parseUrl(to));
46 // this.replaceState(_state, '', this.parseUrl(to))
47 }
48 go(delta) {
49 window.history.go(delta);
50 }
51 listen(listener) {
52 function callback(e) {
53 if (e.action === 'pushState') {
54 listener({ action: Action.Push, location: this.location });
55 }
56 else if (e.action === 'replaceState') {
57 listener({ action: Action.Replace, location: this.location });
58 }
59 else {
60 // NOTE: 这里包括 back、forward、go 三种可能,并非是 POP 事件
61 listener({ action: Action.Pop, location: this.location });
62 }
63 }
64 window.addEventListener('popstate', callback);
65 return () => {
66 window.removeEventListener('popstate', callback);
67 };
68 }
69 block(_blocker) {
70 throw new Error('Method not implemented.');
71 }
72 eventState(action) {
73 return (data, unused, url) => {
74 const wrapper = window.history[action](data, unused, url);
75 const evt = new Event(action);
76 evt.action = action;
77 evt.state = data;
78 evt.unused = unused;
79 evt.url = url;
80 window.dispatchEvent(evt);
81 return wrapper;
82 };
83 }
84}
85function setHistory(h, base = '/') {
86 history = h;
87 basename = base;
88}
89function createMpaHistory(_) {
90 return new MpaHistory();
91}
92function setHistoryMode(mode, base = '/') {
93 const options = {
94 window
95 };
96 basename = base;
97 if (mode === 'browser') {
98 history = createBrowserHistory(options);
99 }
100 else if (mode === 'multi') {
101 history = createMpaHistory();
102 }
103 else {
104 // default is hash
105 history = createHashHistory(options);
106 }
107}
108function prependBasename(url = '') {
109 return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
110}
111
112export { createMpaHistory, history, prependBasename, setHistory, setHistoryMode };