added ability to copy words to the clipboard

This commit is contained in:
Mariusz Gumienny 2021-03-28 07:52:05 +02:00
parent 533f995baa
commit f6a21d5e18
2 changed files with 48 additions and 0 deletions

View file

@ -1174,6 +1174,33 @@ export function updateThemeCommands() {
}
}
let commandsCopyWordsToClipboard = {
title: "Are you sure...",
list: [
{
id: "copyNo",
display: "Nevermind",
exec: () => {},
},
{
id: "copyYes",
display: "Yes, I am sure",
exec: () => {
const words = Misc.getWords();
navigator.clipboard.writeText(words).then(
() => {
Notifications.add("Copied to clipboard", 1);
},
() => {
Notifications.add("Failed to copy!", -1);
}
);
},
},
],
};
export let defaultCommands = {
title: "",
list: [
@ -1826,6 +1853,15 @@ export let defaultCommands = {
UpdateConfig.toggleMonkey();
},
},
{
id: "copyWordsToClipboard",
display: "Copy words to clipboard",
subgroup: true,
exec: () => {
current.push(commandsCopyWordsToClipboard);
Commandline.show();
},
},
],
};

View file

@ -622,6 +622,18 @@ export function toggleFullscreen(elem) {
}
}
export function getWords() {
const words = [...document.querySelectorAll("#words .word")]
.map((word) => {
return [...word.querySelectorAll("letter")]
.map((letter) => letter.innerText)
.join("");
})
.join(" ");
return words;
}
//credit: https://www.w3resource.com/javascript-exercises/javascript-string-exercise-32.php
export function remove_non_ascii(str) {
if (str === null || str === "") return false;