|
1 | 1 | (function () {
|
2 | 2 | const isAutomated = navigator.webdriver;
|
3 | 3 |
|
4 |
| - /* |
5 |
| - Describe the app state. |
6 |
| - */ |
7 |
| - |
8 |
| - const state = { |
9 |
| - pageIndex: guessPageIndex(), |
10 |
| - }; |
11 |
| - |
12 | 4 | /*
|
13 | 5 | Get configuration parameters and initialize the web page.
|
14 | 6 | */
|
|
24 | 16 | */
|
25 | 17 |
|
26 | 18 | function init(config) {
|
| 19 | + addSlideId(); |
| 20 | + setHash(); |
27 | 21 | addEventListeners(config);
|
28 | 22 | setBorders(config.width, config.height);
|
29 | 23 | addProgress(config);
|
|
34 | 28 | }
|
35 | 29 | }
|
36 | 30 |
|
| 31 | + /* |
| 32 | + Set location hash |
| 33 | + */ |
| 34 | + |
| 35 | + function setHash() { |
| 36 | + window.location.hash = window.location.hash || getSlides()[0].id; |
| 37 | + } |
| 38 | + |
37 | 39 | /*
|
38 | 40 | Add event listeners.
|
39 | 41 | */
|
|
51 | 53 | setCopyToClipBoardButtonPosition(config);
|
52 | 54 | }
|
53 | 55 |
|
54 |
| - updateScroll(); |
| 56 | + updateLocationHash(getSlideId()); |
55 | 57 | },
|
56 | 58 | );
|
57 | 59 | }
|
|
82 | 84 | switch (keyCode) {
|
83 | 85 | case RIGHT:
|
84 | 86 | case DOWN:
|
85 |
| - state.pageIndex = Math.min(state.pageIndex + 1, numberOfSlides()); |
86 |
| - updateScroll(); |
| 87 | + goToNextPage(); |
87 | 88 | break;
|
88 | 89 | case UP:
|
89 | 90 | case LEFT:
|
90 |
| - state.pageIndex = Math.max(state.pageIndex - 1, 0); |
91 |
| - updateScroll(); |
| 91 | + goToPreviousPage(); |
92 | 92 | break;
|
93 | 93 | // No default
|
94 | 94 | }
|
95 | 95 | }
|
96 | 96 |
|
| 97 | + function goToNextPage() { |
| 98 | + updateUrl(+1); |
| 99 | + } |
| 100 | + |
| 101 | + function goToPreviousPage() { |
| 102 | + updateUrl(-1); |
| 103 | + } |
| 104 | + |
97 | 105 | /*
|
98 | 106 | Add progress to each slide based on config.
|
99 | 107 | */
|
|
127 | 135 | }
|
128 | 136 |
|
129 | 137 | /*
|
130 |
| - Return the correct scroll position. |
| 138 | + Update location hash. |
131 | 139 | */
|
132 | 140 |
|
133 |
| - function getScroll() { |
134 |
| - return state.pageIndex * window.innerHeight; |
| 141 | + function updateLocationHash(pageId) { |
| 142 | + window.location.hash = `#${pageId}`; |
135 | 143 | }
|
136 | 144 |
|
137 | 145 | /*
|
138 |
| - Update scroll position on resize events. |
| 146 | + Get the id of the current slide |
139 | 147 | */
|
140 | 148 |
|
141 |
| - function updateScroll() { |
142 |
| - window.scroll(0, getScroll()); |
| 149 | + function getSlideId() { |
| 150 | + return window.location.hash.slice(1); |
| 151 | + } |
| 152 | + |
| 153 | + /* |
| 154 | + Update url. |
| 155 | + */ |
| 156 | + |
| 157 | + function updateUrl(increment) { |
| 158 | + const index = getPageIndex(); |
| 159 | + if ( |
| 160 | + (increment === +1 && index < numberOfSlides() - 1) || |
| 161 | + (increment === -1 && index > 0) |
| 162 | + ) { |
| 163 | + const slideId = getSlides()[index + increment].id; |
| 164 | + updateLocationHash(slideId); |
| 165 | + } |
143 | 166 | }
|
144 | 167 |
|
145 | 168 | /*
|
|
151 | 174 | }
|
152 | 175 |
|
153 | 176 | /*
|
154 |
| - Guess page index from the scroll position. |
| 177 | + Get page index location hash. |
155 | 178 | */
|
156 | 179 |
|
157 |
| - function guessPageIndex() { |
158 |
| - const index = Math.round(document.documentElement.scrollTop / window.innerHeight); |
159 |
| - return Math.max(0, Math.min(index, numberOfSlides())); |
| 180 | + function getPageIndex() { |
| 181 | + return getSlides() |
| 182 | + .map(page => page.id) |
| 183 | + .findIndex(id => id === getSlideId()); |
160 | 184 | }
|
161 | 185 |
|
162 | 186 | /*
|
|
203 | 227 | });
|
204 | 228 | }
|
205 | 229 |
|
| 230 | + /* |
| 231 | + Add slide id |
| 232 | + */ |
| 233 | + |
| 234 | + function addSlideId() { |
| 235 | + const slides = getSlides(); |
| 236 | + slides.forEach((slide, index) => { |
| 237 | + slide.id = slide.id || `${index + 1}`; |
| 238 | + }); |
| 239 | + } |
| 240 | + |
206 | 241 | /*
|
207 | 242 | Add copy-to-clipboard button
|
208 | 243 | */
|
|
218 | 253 | const element = document.createElement('html');
|
219 | 254 | element.innerHTML = text;
|
220 | 255 | const articles = element.querySelectorAll('main > article');
|
221 |
| - const htmlSnippet = articles[state.pageIndex].outerHTML; |
| 256 | + const htmlSnippet = articles[getPageIndex()].outerHTML; |
222 | 257 | navigator.clipboard.writeText(htmlSnippet).then(() => {
|
223 | 258 | // Console.log('Copied to clipboard.');
|
224 | 259 | }, () => {
|
|
0 commit comments