2020-08-24 19:00:04 +03:00
|
|
|
// Import the used modules
|
|
|
|
import * as api from "./api.js";
|
|
|
|
import * as autoload from "./autoload.js";
|
2020-08-24 19:23:52 +03:00
|
|
|
import * as spinner from "./spinner.js";
|
2020-08-26 21:34:43 +03:00
|
|
|
import * as notifications from "./notifications.js";
|
2020-08-24 19:00:04 +03:00
|
|
|
|
2020-08-23 21:02:51 +03:00
|
|
|
// setupKeybinds initializes the keybinds for the buttons
|
2020-08-24 19:00:04 +03:00
|
|
|
export function setupKeybinds() {
|
2020-08-24 20:30:33 +03:00
|
|
|
window.addEventListener("keydown", function(event) {
|
2020-08-24 19:00:04 +03:00
|
|
|
// Return if the CTRL key was not pressed
|
2020-08-23 21:02:51 +03:00
|
|
|
if (!event.ctrlKey) return;
|
2020-08-24 19:00:04 +03:00
|
|
|
|
|
|
|
// Define the DOM element of the pressed button
|
2020-08-23 21:02:51 +03:00
|
|
|
let element = null;
|
|
|
|
switch (event.keyCode) {
|
|
|
|
case 81: {
|
|
|
|
element = document.getElementById("btn_new");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 83: {
|
|
|
|
element = document.getElementById("btn_save");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 88: {
|
|
|
|
element = document.getElementById("btn_delete");
|
|
|
|
break;
|
|
|
|
}
|
2020-09-16 21:17:40 +03:00
|
|
|
case 66: {
|
2020-08-23 21:02:51 +03:00
|
|
|
element = document.getElementById("btn_copy");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-08-24 19:00:04 +03:00
|
|
|
|
|
|
|
// Call the onClick function of the button
|
2020-08-23 21:02:51 +03:00
|
|
|
if (element) {
|
|
|
|
if (element.hasAttribute("disabled")) return;
|
|
|
|
event.preventDefault();
|
2020-08-24 19:00:04 +03:00
|
|
|
element.click();
|
2020-08-23 21:02:51 +03:00
|
|
|
}
|
2020-08-24 20:30:33 +03:00
|
|
|
});
|
2020-08-23 21:02:51 +03:00
|
|
|
}
|
|
|
|
|
2020-08-24 19:00:04 +03:00
|
|
|
// setupButtons configures the click listeners of the buttons
|
|
|
|
export function setupButtons() {
|
|
|
|
// Define the behavior of the 'new' button
|
|
|
|
document.getElementById("btn_new").addEventListener("click", function() {
|
|
|
|
location.replace(location.protocol + "//" + location.host);
|
|
|
|
});
|
2020-08-23 21:02:51 +03:00
|
|
|
|
2020-08-24 19:00:04 +03:00
|
|
|
// Define the behavior of the 'save' button
|
2020-08-24 19:23:52 +03:00
|
|
|
document.getElementById("btn_save").addEventListener("click", function() {
|
|
|
|
spinner.surround(async function() {
|
|
|
|
// Return if the text area is empty
|
|
|
|
const input = document.getElementById("input");
|
|
|
|
if (!input.value) return;
|
2020-08-23 21:02:51 +03:00
|
|
|
|
2020-08-24 19:23:52 +03:00
|
|
|
// Create the paste
|
|
|
|
const response = await api.createPaste(input.value);
|
|
|
|
if (!response.ok) {
|
2020-08-26 21:34:43 +03:00
|
|
|
notifications.error("Failed creating the paste: <b>" + data + "</b>");
|
2020-08-24 19:23:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const data = await response.json();
|
2020-08-23 21:02:51 +03:00
|
|
|
|
2020-08-26 21:47:15 +03:00
|
|
|
// Give the user the chance to copy the deletion token
|
2021-04-20 17:38:00 +03:00
|
|
|
if (data.deletionToken) {
|
|
|
|
prompt("The deletion token for your paste is:", data.deletionToken);
|
|
|
|
}
|
2020-08-26 21:47:15 +03:00
|
|
|
|
2020-08-24 19:23:52 +03:00
|
|
|
// Redirect the user to the paste page
|
|
|
|
let address = location.protocol + "//" + location.host + "/" + data.id;
|
|
|
|
if (data.suggestedSyntaxType) address += "." + data.suggestedSyntaxType;
|
|
|
|
location.replace(address);
|
|
|
|
});
|
2020-08-23 21:02:51 +03:00
|
|
|
});
|
|
|
|
|
2020-08-24 19:00:04 +03:00
|
|
|
// Define the behavior of the 'delete' button
|
2020-08-24 19:23:52 +03:00
|
|
|
document.getElementById("btn_delete").addEventListener("click", function() {
|
|
|
|
spinner.surround(async function() {
|
|
|
|
// Ask the user for the deletion token
|
2020-08-26 21:47:15 +03:00
|
|
|
const deletionToken = prompt("Deletion Token:");
|
2020-08-24 19:23:52 +03:00
|
|
|
if (!deletionToken) return;
|
2020-08-23 21:02:51 +03:00
|
|
|
|
2020-08-24 19:23:52 +03:00
|
|
|
// Delete the paste
|
|
|
|
const response = await api.deletePaste(autoload.PASTE_ID, deletionToken);
|
|
|
|
const data = await response.text();
|
|
|
|
if (!response.ok) {
|
2020-08-26 21:34:43 +03:00
|
|
|
notifications.error("Failed deleting the paste: <b>" + data + "</b>");
|
2020-08-24 19:23:52 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-08-23 21:02:51 +03:00
|
|
|
|
2020-08-24 19:23:52 +03:00
|
|
|
// Redirect the user to the main page
|
|
|
|
location.replace(location.protocol + "//" + location.host);
|
|
|
|
});
|
2020-08-23 21:02:51 +03:00
|
|
|
});
|
2020-08-24 19:00:04 +03:00
|
|
|
|
|
|
|
// Define the behavior of the 'copy' button
|
2020-08-24 19:23:52 +03:00
|
|
|
document.getElementById("btn_copy").addEventListener("click", function() {
|
|
|
|
spinner.surround(async function() {
|
|
|
|
// Ask for the clipboard permissions
|
|
|
|
askClipboardPermissions();
|
|
|
|
|
|
|
|
// Copy the code
|
|
|
|
await navigator.clipboard.writeText(document.getElementById("code").innerText);
|
2020-08-26 21:34:43 +03:00
|
|
|
notifications.success("Copied the code!");
|
2020-08-24 19:23:52 +03:00
|
|
|
});
|
2020-08-24 19:00:04 +03:00
|
|
|
});
|
2020-08-23 21:02:51 +03:00
|
|
|
}
|
|
|
|
|
2020-08-24 19:00:04 +03:00
|
|
|
// askClipboardPermissions asks the user for the clipboard permissions
|
|
|
|
async function askClipboardPermissions() {
|
|
|
|
try {
|
|
|
|
const state = await navigator.permissions.query({
|
2020-08-26 21:34:43 +03:00
|
|
|
name: "clipboard-write"
|
2020-08-24 19:00:04 +03:00
|
|
|
});
|
|
|
|
return state === "granted";
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-23 21:02:51 +03:00
|
|
|
}
|