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
|
|
|
|
2021-06-20 16:58:36 +03:00
|
|
|
// Define element handles
|
|
|
|
const versionElement = document.getElementById("version");
|
|
|
|
const lineNOsElement = document.getElementById("linenos");
|
|
|
|
const codeElement = document.getElementById("code");
|
|
|
|
const inputElement = document.getElementById("input");
|
|
|
|
|
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();
|
2021-06-20 16:58:36 +03:00
|
|
|
versionElement.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;
|
2021-06-20 16:58:36 +03:00
|
|
|
let CODE;
|
2020-08-24 19:00:04 +03:00
|
|
|
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];
|
2021-06-20 16:58:36 +03:00
|
|
|
|
2020-08-24 19:00:04 +03:00
|
|
|
// 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
|
|
|
}
|
2021-06-20 16:58:36 +03:00
|
|
|
CODE = (await response.json()).content;
|
|
|
|
|
2020-08-24 19:00:04 +03:00
|
|
|
// 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-09-16 21:06:01 +03:00
|
|
|
|
2021-06-20 16:58:36 +03:00
|
|
|
// Set the paste content to the DOM
|
|
|
|
codeElement.innerHTML = language
|
|
|
|
? hljs.highlight(language, CODE).value
|
|
|
|
: hljs.highlightAuto(CODE).value;
|
2020-09-16 21:06:01 +03:00
|
|
|
|
2021-06-20 16:58:36 +03:00
|
|
|
// Display the line numbers
|
|
|
|
renderLineNumbers();
|
|
|
|
window.addEventListener("resize", renderLineNumbers);
|
2020-09-16 21:06:01 +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 {
|
2021-06-20 16:58:36 +03:00
|
|
|
inputElement.classList.remove("hidden");
|
|
|
|
inputElement.focus();
|
|
|
|
window.addEventListener("keydown", function (event) {
|
2020-08-24 20:30:33 +03:00
|
|
|
if (event.keyCode != 9) return;
|
|
|
|
event.preventDefault();
|
2021-06-20 16:58:36 +03:00
|
|
|
|
|
|
|
insertTextAtCursor(inputElement, " ");
|
2020-08-24 20:30:33 +03:00
|
|
|
});
|
2020-08-24 19:00:04 +03:00
|
|
|
}
|
2020-08-23 21:02:51 +03:00
|
|
|
}
|
2021-06-20 16:58:36 +03:00
|
|
|
spinner.surround(loadPaste);
|
|
|
|
|
|
|
|
function renderLineNumbers() {
|
|
|
|
lineNOsElement.innerHTML = CODE.split(/\n/).map((line, index) => {
|
|
|
|
let lineWidth = getTextWidth(line, "16px Source Code Pro");
|
|
|
|
let linesSpace = Math.ceil(lineWidth / codeElement.offsetWidth);
|
|
|
|
|
|
|
|
let result = `<span>${index+1}</span>`;
|
|
|
|
if (linesSpace > 1) {
|
|
|
|
result += "<span></span>".repeat(linesSpace - 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}).join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1:1 skid from https://stackoverflow.com/questions/7404366/how-do-i-insert-some-text-where-the-cursor-is
|
|
|
|
function insertTextAtCursor(element, text) {
|
|
|
|
let value = element.value, endIndex, range, doc = element.ownerDocument;
|
|
|
|
if (typeof element.selectionStart == "number"
|
|
|
|
&& typeof element.selectionEnd == "number") {
|
|
|
|
endIndex = element.selectionEnd;
|
|
|
|
element.value = value.slice(0, endIndex) + text + value.slice(endIndex);
|
|
|
|
element.selectionStart = element.selectionEnd = endIndex + text.length;
|
|
|
|
} else if (doc.selection != "undefined" && doc.selection.createRange) {
|
|
|
|
element.focus();
|
|
|
|
range = doc.selection.createRange();
|
|
|
|
range.collapse(false);
|
|
|
|
range.text = text;
|
|
|
|
range.select();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also a kind of skid
|
|
|
|
function getTextWidth(text, font) {
|
|
|
|
let canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
|
|
|
|
let context = canvas.getContext("2d");
|
|
|
|
context.font = font;
|
|
|
|
return context.measureText(text).width;
|
|
|
|
}
|