Skip to content

Commit 4c6e1e7

Browse files
Add copy-to-clipboard button
1 parent 6b5fe32 commit 4c6e1e7

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

template/src/index.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ html, body {
55
overflow: hidden;
66
}
77

8+
body, button {
9+
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
10+
-webkit-font-smoothing: antialiased;
11+
-moz-osx-font-smoothing: grayscale;
12+
-webkit-text-size-adjust: 100%;
13+
}
14+
815
main > article {
916
margin: 0;
1017
padding: 10px;
@@ -16,3 +23,10 @@ main > article {
1623
position: relative;
1724
box-sizing: border-box;
1825
}
26+
27+
button.copy-to-clipboard {
28+
position: fixed;
29+
font-size: 0.9vw;
30+
padding: 0.2vw;
31+
cursor: pointer;
32+
}

template/src/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
(function () {
2+
const isAutomated = navigator.webdriver;
3+
24
/*
35
Describe the app state.
46
*/
@@ -27,6 +29,9 @@
2729
addProgress(config);
2830
addSlideNumber(config);
2931
addDate(config);
32+
if (!isAutomated) {
33+
addCopyToClipBoardButton(config);
34+
}
3035
}
3136

3237
/*
@@ -42,6 +47,10 @@
4247
'resize',
4348
() => {
4449
setBorders(config.width, config.height);
50+
if (!isAutomated) {
51+
setCopyToClipBoardButtonPosition(config);
52+
}
53+
4554
updateScroll();
4655
},
4756
);
@@ -193,4 +202,44 @@
193202
}
194203
});
195204
}
205+
206+
/*
207+
Add copy-to-clipboard button
208+
*/
209+
function addCopyToClipBoardButton(config) {
210+
appendCopyToClipBoardButton();
211+
setCopyToClipBoardButtonPosition(config);
212+
}
213+
214+
function copyToClipBoard() {
215+
fetch(window.location.href)
216+
.then(response => response.text())
217+
.then(text => {
218+
const element = document.createElement('html');
219+
element.innerHTML = text;
220+
const articles = element.querySelectorAll('main > article');
221+
const htmlSnippet = articles[state.pageIndex].outerHTML;
222+
navigator.clipboard.writeText(htmlSnippet).then(() => {
223+
// Console.log('Copied to clipboard.');
224+
}, () => {
225+
// Console.log('Error, not copied.');
226+
});
227+
});
228+
}
229+
230+
function appendCopyToClipBoardButton() {
231+
const button = document.createElement('button');
232+
button.className = 'copy-to-clipboard';
233+
button.textContent = 'copy to clipboard';
234+
button.addEventListener('click', copyToClipBoard);
235+
document.body.append(button);
236+
}
237+
238+
function setCopyToClipBoardButtonPosition(config) {
239+
const button = document.querySelectorAll('.copy-to-clipboard')[0];
240+
const bottom = Math.max(0, (window.innerHeight - config.height) / 2);
241+
const right = Math.max(0, (window.innerWidth - config.width) / 2);
242+
button.style.bottom = `calc(${bottom}px - 2.5vw)`;
243+
button.style.right = `${right}px`;
244+
}
196245
})();

0 commit comments

Comments
 (0)