2020-08-24 19:00:04 +03:00
|
|
|
// Import the used modules
|
|
|
|
import * as api from "./api.js";
|
|
|
|
import * as buttons from "./buttons.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
|
|
|
|
|
|
|
// Set up the buttons
|
|
|
|
buttons.setupButtons();
|
|
|
|
buttons.setupKeybinds();
|
2020-08-23 21:02:51 +03:00
|
|
|
|
2020-08-24 19:00:04 +03:00
|
|
|
// Load the API information
|
|
|
|
async function loadAPIInformation() {
|
|
|
|
const response = await api.getAPIInformation();
|
2020-08-26 21:34:43 +03:00
|
|
|
if (!response.ok) {
|
|
|
|
const data = await response.text();
|
|
|
|
notifications.error("Failed fetching the API information: <b>" + data + "</b>");
|
|
|
|
return;
|
2020-08-24 19:00:04 +03:00
|
|
|
}
|
2020-08-26 21:34:43 +03:00
|
|
|
const data = await response.json();
|
|
|
|
document.getElementById("version").innerText = data.version;
|
2020-08-24 19:00:04 +03:00
|
|
|
}
|
|
|
|
loadAPIInformation();
|
2020-08-23 21:02:51 +03:00
|
|
|
|
|
|
|
// Try to load a paste if one exists
|
2020-08-24 19:00:04 +03:00
|
|
|
export let PASTE_ID;
|
|
|
|
async function loadPaste() {
|
2020-08-24 22:53:23 +03:00
|
|
|
if (location.pathname !== "/") {
|
2020-08-24 19:00:04 +03:00
|
|
|
// Define the paste ID and language
|
|
|
|
const split = location.pathname.replace("/", "").split(".");
|
|
|
|
const pasteID = split[0];
|
|
|
|
const language = split[1];
|
|
|
|
|
|
|
|
// Retrieve the paste from the API and redirect the user to the main page if it could not be found
|
|
|
|
const response = await api.getPaste(pasteID);
|
|
|
|
if (!response.ok) {
|
2020-08-23 21:02:51 +03:00
|
|
|
location.replace(location.protocol + "//" + location.host);
|
|
|
|
return;
|
2020-08-24 19:00:04 +03:00
|
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
// Adjust the button states
|
2020-08-23 21:02:51 +03:00
|
|
|
document.getElementById("btn_save").setAttribute("disabled", true);
|
|
|
|
document.getElementById("btn_delete").removeAttribute("disabled");
|
|
|
|
document.getElementById("btn_copy").removeAttribute("disabled");
|
2020-08-24 19:00:04 +03:00
|
|
|
|
|
|
|
// Set the paste content to the DOM
|
2020-08-23 21:02:51 +03:00
|
|
|
document.getElementById("code").innerHTML = language
|
2020-08-24 00:44:47 +03:00
|
|
|
? hljs.highlight(language, data.content).value
|
|
|
|
: hljs.highlightAuto(data.content).value;
|
2020-08-24 19:00:04 +03:00
|
|
|
|
|
|
|
// Display the line numbers
|
2020-08-24 22:53:23 +03:00
|
|
|
document.getElementById("linenos").innerHTML = data.content.split(/\n/).map((_, index) => `<span>${index + 1}</span>`).join('');
|
2020-08-24 19:00:04 +03:00
|
|
|
|
2020-08-23 21:02:51 +03:00
|
|
|
// Set the PASTE_ID variable
|
|
|
|
PASTE_ID = pasteID;
|
2020-08-24 19:00:04 +03:00
|
|
|
} else {
|
|
|
|
const input = document.getElementById("input");
|
|
|
|
input.classList.remove("hidden");
|
|
|
|
input.focus();
|
2020-08-24 20:30:33 +03:00
|
|
|
window.addEventListener("keydown", function(event) {
|
|
|
|
if (event.keyCode != 9) return;
|
|
|
|
event.preventDefault();
|
|
|
|
input.value += " ";
|
|
|
|
});
|
2020-08-24 19:00:04 +03:00
|
|
|
}
|
2020-08-23 21:02:51 +03:00
|
|
|
}
|
2020-08-24 19:23:52 +03:00
|
|
|
spinner.surround(loadPaste);
|