|
1 | | -import { dispatch, isStandalone, listen } from 'codesandbox-api'; |
| 1 | +import { dispatch, listen } from 'codesandbox-api'; |
2 | 2 |
|
3 | 3 | const origHistoryProto = window.history.__proto__; // eslint-disable-line no-proto |
4 | 4 | const historyList = []; |
@@ -37,107 +37,105 @@ export default function setupHistoryListeners() { |
37 | 37 | } |
38 | 38 | } |
39 | 39 | } |
40 | | - if (!isStandalone) { |
41 | | - Object.assign(window.history, { |
42 | | - go(delta) { |
43 | | - const newPos = historyPosition + delta; |
44 | | - if (newPos >= 0 && newPos <= historyList.length - 1) { |
45 | | - historyPosition = newPos; |
46 | | - const { url, state } = historyList[historyPosition]; |
47 | | - const oldURL = document.location.href; |
48 | | - origHistoryProto.replaceState.call(window.history, state, '', url); |
49 | | - const newURL = document.location.href; |
50 | | - sendUrlChange(newURL); |
51 | | - if (newURL.indexOf('#') === -1) { |
52 | | - window.dispatchEvent(new PopStateEvent('popstate', { state })); |
53 | | - } else { |
54 | | - disableNextHashChange = true; |
55 | | - window.dispatchEvent( |
56 | | - new HashChangeEvent('hashchange', { oldURL, newURL }) |
57 | | - ); |
58 | | - } |
| 40 | + Object.assign(window.history, { |
| 41 | + go(delta) { |
| 42 | + const newPos = historyPosition + delta; |
| 43 | + if (newPos >= 0 && newPos <= historyList.length - 1) { |
| 44 | + historyPosition = newPos; |
| 45 | + const { url, state } = historyList[historyPosition]; |
| 46 | + const oldURL = document.location.href; |
| 47 | + origHistoryProto.replaceState.call(window.history, state, '', url); |
| 48 | + const newURL = document.location.href; |
| 49 | + sendUrlChange(newURL); |
| 50 | + if (newURL.indexOf('#') === -1) { |
| 51 | + window.dispatchEvent(new PopStateEvent('popstate', { state })); |
| 52 | + } else { |
| 53 | + disableNextHashChange = true; |
| 54 | + window.dispatchEvent( |
| 55 | + new HashChangeEvent('hashchange', { oldURL, newURL }) |
| 56 | + ); |
59 | 57 | } |
60 | | - }, |
| 58 | + } |
| 59 | + }, |
61 | 60 |
|
62 | | - back() { |
63 | | - window.history.go(-1); |
64 | | - }, |
| 61 | + back() { |
| 62 | + window.history.go(-1); |
| 63 | + }, |
65 | 64 |
|
66 | | - forward() { |
67 | | - window.history.go(1); |
68 | | - }, |
| 65 | + forward() { |
| 66 | + window.history.go(1); |
| 67 | + }, |
69 | 68 |
|
70 | | - pushState(state, title, url) { |
71 | | - origHistoryProto.replaceState.call(window.history, state, title, url); |
72 | | - pushHistory(url, state); |
73 | | - sendUrlChange(document.location.href); |
74 | | - }, |
| 69 | + pushState(state, title, url) { |
| 70 | + origHistoryProto.replaceState.call(window.history, state, title, url); |
| 71 | + pushHistory(url, state); |
| 72 | + sendUrlChange(document.location.href); |
| 73 | + }, |
75 | 74 |
|
76 | | - replaceState(state, title, url) { |
77 | | - origHistoryProto.replaceState.call(window.history, state, title, url); |
78 | | - historyList[historyPosition] = { state, url }; |
79 | | - sendUrlChange(document.location.href); |
80 | | - }, |
81 | | - }); |
| 75 | + replaceState(state, title, url) { |
| 76 | + origHistoryProto.replaceState.call(window.history, state, title, url); |
| 77 | + historyList[historyPosition] = { state, url }; |
| 78 | + sendUrlChange(document.location.href); |
| 79 | + }, |
| 80 | + }); |
82 | 81 |
|
83 | | - Object.defineProperties(window.history, { |
84 | | - length: { |
85 | | - get() { |
86 | | - return historyList.length; |
87 | | - }, |
| 82 | + Object.defineProperties(window.history, { |
| 83 | + length: { |
| 84 | + get() { |
| 85 | + return historyList.length; |
88 | 86 | }, |
| 87 | + }, |
89 | 88 |
|
90 | | - state: { |
91 | | - get() { |
92 | | - return historyList[historyPosition].state; |
93 | | - }, |
| 89 | + state: { |
| 90 | + get() { |
| 91 | + return historyList[historyPosition].state; |
94 | 92 | }, |
95 | | - }); |
| 93 | + }, |
| 94 | + }); |
96 | 95 |
|
97 | | - window.addEventListener('hashchange', () => { |
98 | | - if (!disableNextHashChange) { |
99 | | - const url = pathWithHash(document.location); |
100 | | - pushHistory(url, null); |
101 | | - sendUrlChange(document.location.href); |
102 | | - } else { |
103 | | - disableNextHashChange = false; |
104 | | - } |
105 | | - }); |
| 96 | + window.addEventListener('hashchange', () => { |
| 97 | + if (!disableNextHashChange) { |
| 98 | + const url = pathWithHash(document.location); |
| 99 | + pushHistory(url, null); |
| 100 | + sendUrlChange(document.location.href); |
| 101 | + } else { |
| 102 | + disableNextHashChange = false; |
| 103 | + } |
| 104 | + }); |
106 | 105 |
|
107 | | - document.addEventListener( |
108 | | - 'click', |
109 | | - ev => { |
110 | | - const el = ev.target; |
111 | | - if ( |
112 | | - el.nodeName === 'A' && |
113 | | - !el.__vue__ && // workaround for vue-router <router-link> |
114 | | - el.href.indexOf('#') !== -1 && |
115 | | - el.href.substr(-1) !== '#' |
116 | | - ) { |
117 | | - const url = el.href; |
118 | | - const oldURL = document.location.href; |
119 | | - origHistoryProto.replaceState.call(window.history, null, '', url); |
120 | | - const newURL = document.location.href; |
121 | | - if (oldURL !== newURL) { |
122 | | - disableNextHashChange = true; |
123 | | - window.dispatchEvent( |
124 | | - new HashChangeEvent('hashchange', { oldURL, newURL }) |
125 | | - ); |
126 | | - pushHistory(pathWithHash(document.location), null); |
127 | | - sendUrlChange(document.location.href); |
128 | | - } |
129 | | - ev.preventDefault(); |
130 | | - ev.stopPropagation(); |
| 106 | + document.addEventListener( |
| 107 | + 'click', |
| 108 | + ev => { |
| 109 | + const el = ev.target; |
| 110 | + if ( |
| 111 | + el.nodeName === 'A' && |
| 112 | + !el.__vue__ && // workaround for vue-router <router-link> |
| 113 | + el.href.indexOf('#') !== -1 && |
| 114 | + el.href.substr(-1) !== '#' |
| 115 | + ) { |
| 116 | + const url = el.href; |
| 117 | + const oldURL = document.location.href; |
| 118 | + origHistoryProto.replaceState.call(window.history, null, '', url); |
| 119 | + const newURL = document.location.href; |
| 120 | + if (oldURL !== newURL) { |
| 121 | + disableNextHashChange = true; |
| 122 | + window.dispatchEvent( |
| 123 | + new HashChangeEvent('hashchange', { oldURL, newURL }) |
| 124 | + ); |
| 125 | + pushHistory(pathWithHash(document.location), null); |
| 126 | + sendUrlChange(document.location.href); |
131 | 127 | } |
132 | | - }, |
133 | | - true |
134 | | - ); |
| 128 | + ev.preventDefault(); |
| 129 | + ev.stopPropagation(); |
| 130 | + } |
| 131 | + }, |
| 132 | + true |
| 133 | + ); |
135 | 134 |
|
136 | | - pushHistory(pathWithHash(document.location), null); |
| 135 | + pushHistory(pathWithHash(document.location), null); |
137 | 136 |
|
138 | | - setTimeout(() => { |
139 | | - sendUrlChange(document.location.href); |
140 | | - }); |
141 | | - } |
| 137 | + setTimeout(() => { |
| 138 | + sendUrlChange(document.location.href); |
| 139 | + }); |
142 | 140 | return listen(handleMessage); |
143 | 141 | } |
0 commit comments