Skip to content

Commit e25220b

Browse files
Add the global attribute id to each page
1 parent eb0fb54 commit e25220b

File tree

1 file changed

+59
-24
lines changed

1 file changed

+59
-24
lines changed

template/src/index.js

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
(function () {
22
const isAutomated = navigator.webdriver;
33

4-
/*
5-
Describe the app state.
6-
*/
7-
8-
const state = {
9-
pageIndex: guessPageIndex(),
10-
};
11-
124
/*
135
Get configuration parameters and initialize the web page.
146
*/
@@ -24,6 +16,8 @@
2416
*/
2517

2618
function init(config) {
19+
addSlideId();
20+
setHash();
2721
addEventListeners(config);
2822
setBorders(config.width, config.height);
2923
addProgress(config);
@@ -34,6 +28,14 @@
3428
}
3529
}
3630

31+
/*
32+
Set location hash
33+
*/
34+
35+
function setHash() {
36+
window.location.hash = window.location.hash || getSlides()[0].id;
37+
}
38+
3739
/*
3840
Add event listeners.
3941
*/
@@ -51,7 +53,7 @@
5153
setCopyToClipBoardButtonPosition(config);
5254
}
5355

54-
updateScroll();
56+
updateLocationHash(getSlideId());
5557
},
5658
);
5759
}
@@ -82,18 +84,24 @@
8284
switch (keyCode) {
8385
case RIGHT:
8486
case DOWN:
85-
state.pageIndex = Math.min(state.pageIndex + 1, numberOfSlides());
86-
updateScroll();
87+
goToNextPage();
8788
break;
8889
case UP:
8990
case LEFT:
90-
state.pageIndex = Math.max(state.pageIndex - 1, 0);
91-
updateScroll();
91+
goToPreviousPage();
9292
break;
9393
// No default
9494
}
9595
}
9696

97+
function goToNextPage() {
98+
updateUrl(+1);
99+
}
100+
101+
function goToPreviousPage() {
102+
updateUrl(-1);
103+
}
104+
97105
/*
98106
Add progress to each slide based on config.
99107
*/
@@ -127,19 +135,34 @@
127135
}
128136

129137
/*
130-
Return the correct scroll position.
138+
Update location hash.
131139
*/
132140

133-
function getScroll() {
134-
return state.pageIndex * window.innerHeight;
141+
function updateLocationHash(pageId) {
142+
window.location.hash = `#${pageId}`;
135143
}
136144

137145
/*
138-
Update scroll position on resize events.
146+
Get the id of the current slide
139147
*/
140148

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+
}
143166
}
144167

145168
/*
@@ -151,12 +174,13 @@
151174
}
152175

153176
/*
154-
Guess page index from the scroll position.
177+
Get page index location hash.
155178
*/
156179

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());
160184
}
161185

162186
/*
@@ -203,6 +227,17 @@
203227
});
204228
}
205229

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+
206241
/*
207242
Add copy-to-clipboard button
208243
*/
@@ -218,7 +253,7 @@
218253
const element = document.createElement('html');
219254
element.innerHTML = text;
220255
const articles = element.querySelectorAll('main > article');
221-
const htmlSnippet = articles[state.pageIndex].outerHTML;
256+
const htmlSnippet = articles[getPageIndex()].outerHTML;
222257
navigator.clipboard.writeText(htmlSnippet).then(() => {
223258
// Console.log('Copied to clipboard.');
224259
}, () => {

0 commit comments

Comments
 (0)