2022-05-21 16:17:25 +03:00
|
|
|
|
|
|
|
function sendMyData(el) {
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
// Check if the writing watch was sending something already
|
2022-06-01 17:43:05 +03:00
|
|
|
if (!!window.writingWatch) {
|
2022-05-21 16:17:25 +03:00
|
|
|
// Clear old timeout
|
|
|
|
clearTimeout(window.writingWatch);
|
|
|
|
}
|
|
|
|
|
2022-06-01 17:43:05 +03:00
|
|
|
if (el.value.length > maximumPadSize) {
|
2022-05-21 16:17:25 +03:00
|
|
|
let err = new Error(`Your Pad is too big! Please keep it limited to ${maximumPadSize} characters!`);
|
|
|
|
alert(err);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
el.setAttribute(`readonly`, `1`);
|
|
|
|
|
2022-06-01 16:58:09 +03:00
|
|
|
const textareaPreview = document.getElementById(`textarea-preview`)
|
2022-06-01 17:43:05 +03:00
|
|
|
if (!!textareaPreview) {
|
2022-06-01 16:58:09 +03:00
|
|
|
textareaPreview.textContent = el.value;
|
2022-06-01 17:43:05 +03:00
|
|
|
|
|
|
|
hljs.highlightElement(document.getElementById(`textarea-preview`));
|
2022-06-01 16:58:09 +03:00
|
|
|
}
|
|
|
|
|
2022-05-21 16:17:25 +03:00
|
|
|
formData.set("content", el.value);
|
|
|
|
|
|
|
|
updateStatus(`Attempting to save...`, `text-warning`);
|
|
|
|
|
|
|
|
fetch(window.location.href.toString(), {
|
|
|
|
body: formData,
|
|
|
|
method: "post",
|
|
|
|
})
|
2022-06-01 17:43:05 +03:00
|
|
|
.then(resp => {
|
|
|
|
resp.json()
|
|
|
|
.then(e => {
|
|
|
|
document.getElementById(`last_modified_`).value = e.pad.last_modified;
|
|
|
|
updateStatus(`Succesfully Saved`, `text-success`);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
updateStatus(`Failed to Save`, `text-danger`);
|
|
|
|
console.error(err);
|
|
|
|
})
|
2022-05-21 16:17:25 +03:00
|
|
|
})
|
2022-06-01 17:43:05 +03:00
|
|
|
.catch(err => {
|
2022-05-21 16:17:25 +03:00
|
|
|
updateStatus(`Failed to Save`, `text-danger`);
|
|
|
|
console.error(err);
|
|
|
|
})
|
2022-06-01 17:43:05 +03:00
|
|
|
.finally(() => {
|
|
|
|
el.removeAttribute(`readonly`);
|
|
|
|
})
|
2022-05-21 16:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function toggleWritingWatch(el) {
|
|
|
|
|
|
|
|
// Check if the writing watch was sending something already
|
2022-06-01 17:43:05 +03:00
|
|
|
if (!!window.writingWatch) {
|
2022-05-21 16:17:25 +03:00
|
|
|
// Clear old timeout
|
|
|
|
clearTimeout(window.writingWatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a timeout for the action
|
2022-06-01 17:43:05 +03:00
|
|
|
window.writingWatch = setTimeout(() => {
|
2022-05-21 16:17:25 +03:00
|
|
|
// Send out the data
|
|
|
|
sendMyData(el)
|
|
|
|
}, 750)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateStatus(txt, cls) {
|
|
|
|
|
|
|
|
const loading_status = document.getElementById(`loading_status`)
|
|
|
|
|
|
|
|
loading_status.value = txt;
|
|
|
|
loading_status.classList.remove("text-danger", "text-warning", "text-success", "text-white", "text-primary");
|
|
|
|
loading_status.classList.add(cls);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLocalArchives() {
|
|
|
|
|
2022-05-21 16:19:24 +03:00
|
|
|
let a = localStorage.getItem(`${padTitle}_archives`);
|
2022-05-21 16:17:25 +03:00
|
|
|
|
|
|
|
// Check if we had anything in storage for the archives
|
2022-06-01 17:43:05 +03:00
|
|
|
if (a == null) {
|
2022-05-21 16:17:25 +03:00
|
|
|
// There were nothing in storage
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Try and parse the json
|
|
|
|
a = JSON.parse(a);
|
2022-06-01 17:43:05 +03:00
|
|
|
} catch (err) {
|
2022-05-21 16:17:25 +03:00
|
|
|
// Return null of the fail
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
function storeArchives(archives) {
|
|
|
|
|
|
|
|
// Check if the provided list is an array
|
2022-06-01 17:43:05 +03:00
|
|
|
if (!Array.isArray(archives)) return;
|
2022-05-21 16:17:25 +03:00
|
|
|
|
|
|
|
// Set the current archives
|
2022-05-21 16:19:24 +03:00
|
|
|
localStorage.setItem(`${padTitle}_archives`, JSON.stringify(archives));
|
2022-05-21 16:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderArchivesSelection() {
|
|
|
|
|
|
|
|
// Get the archives selection
|
|
|
|
const archivesSelection = document.getElementById(`archives-selection`);
|
|
|
|
const rowTemplate = document.getElementById(`archive-selection-example`);
|
|
|
|
// Clear any old optiosn
|
2022-06-01 17:43:05 +03:00
|
|
|
archivesSelection.querySelectorAll(`.dropdown-item:not(#do-archive-button):not(#archive-selection-example)`).forEach(el => {
|
2022-05-21 16:17:25 +03:00
|
|
|
// Remove the element
|
|
|
|
el.remove();
|
|
|
|
})
|
2022-06-01 17:43:05 +03:00
|
|
|
|
2022-05-21 16:17:25 +03:00
|
|
|
// Get the current list of available archives
|
2022-06-01 17:43:05 +03:00
|
|
|
for (let a of getLocalArchives()) {
|
2022-05-21 16:17:25 +03:00
|
|
|
// Clone the template row
|
|
|
|
const row = rowTemplate.cloneNode(true);
|
|
|
|
|
|
|
|
// Remove the id from the row
|
|
|
|
row.removeAttribute(`id`);
|
|
|
|
// Append the row to the selection menu
|
|
|
|
archivesSelection.appendChild(row);
|
|
|
|
|
|
|
|
const ts = new Date(a.ts);
|
|
|
|
|
|
|
|
// Update the display date
|
|
|
|
row.querySelector(`.archive-date`).textContent = ts.toLocaleString();
|
|
|
|
|
|
|
|
// Add an event listener
|
|
|
|
row.addEventListener(`click`, e => {
|
|
|
|
|
|
|
|
let resp = confirm("Load contents of pad from memory? This will overwrite the current pad for everyone.");
|
2022-06-01 17:43:05 +03:00
|
|
|
|
|
|
|
if (!!resp) {
|
2022-05-21 16:17:25 +03:00
|
|
|
document.getElementById(`pad-content`).value = a.content;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveLocalArchive() {
|
|
|
|
let resp = confirm("Save a local copy of the current Pad?");
|
|
|
|
|
2022-06-01 17:43:05 +03:00
|
|
|
if (!resp) {
|
2022-05-21 16:17:25 +03:00
|
|
|
// Do not
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all of the previous archives, append this one to them
|
|
|
|
let myArchives = getLocalArchives();
|
|
|
|
|
|
|
|
myArchives.push({
|
|
|
|
ts: new Date().getTime(),
|
|
|
|
content: document.getElementById(`pad-content`).value,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Store the archives
|
|
|
|
storeArchives(myArchives);
|
|
|
|
|
|
|
|
// Re-Render the archives selection
|
|
|
|
renderArchivesSelection();
|
|
|
|
|
|
|
|
// Save
|
|
|
|
alert(`Saved`);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-22 20:42:19 +03:00
|
|
|
function generateQRCode() {
|
|
|
|
var qrcodeContainer = document.getElementById(`qrcode`)
|
|
|
|
// Remove old contents
|
|
|
|
qrcodeContainer.innerHTML = "";
|
|
|
|
// Add new qr
|
|
|
|
new QRCode(qrcodeContainer, {
|
|
|
|
text: window.location.toString(),
|
2022-06-01 17:43:05 +03:00
|
|
|
width: 256,
|
2022-05-22 20:42:19 +03:00
|
|
|
height: 256,
|
|
|
|
colorDark: "#555273",
|
|
|
|
colorLight: "#ffffff",
|
|
|
|
correctLevel: QRCode.CorrectLevel.H
|
|
|
|
});
|
|
|
|
|
|
|
|
// Open the modal
|
|
|
|
MicroModal.show(`qrmodal`)
|
|
|
|
}
|
|
|
|
|
2022-06-01 16:58:09 +03:00
|
|
|
function toggleTextareaPreview() {
|
2022-06-01 17:43:05 +03:00
|
|
|
setTextareaPreview(!document.getElementById(`pad-content-toggler`).classList.contains(`read-only`))
|
2022-06-01 16:58:09 +03:00
|
|
|
}
|
2022-05-21 16:17:25 +03:00
|
|
|
|
2022-06-01 16:58:09 +03:00
|
|
|
// t == true - Read Only
|
|
|
|
// t == false - Edit mode
|
2022-06-01 17:43:05 +03:00
|
|
|
function setTextareaPreview(t = true) {
|
|
|
|
const prev = document.getElementById(`textarea-preview`)
|
2022-06-01 16:58:09 +03:00
|
|
|
const textarea = document.getElementById(`pad-content`);
|
2022-06-01 17:43:05 +03:00
|
|
|
const toggler = document.getElementById(`pad-content-toggler`);
|
|
|
|
const padContentArea = document.getElementById(`pad-content-area`);
|
2022-06-01 16:58:09 +03:00
|
|
|
|
2022-06-01 17:43:05 +03:00
|
|
|
if (t) {
|
2022-06-01 16:58:09 +03:00
|
|
|
// Toggle read only
|
|
|
|
prev.classList.remove(`hidden`)
|
|
|
|
toggler.classList.add(`read-only`);
|
|
|
|
|
2022-06-01 17:43:05 +03:00
|
|
|
padContentArea.classList.add(`read-only-content`);
|
2022-06-01 16:58:09 +03:00
|
|
|
|
|
|
|
textarea.classList.add(`hidden`);
|
|
|
|
} else {
|
|
|
|
// Toggle edit mode
|
|
|
|
prev.classList.add(`hidden`)
|
|
|
|
toggler.classList.remove(`read-only`);
|
2022-05-21 16:17:25 +03:00
|
|
|
|
2022-06-01 17:43:05 +03:00
|
|
|
padContentArea.classList.remove(`read-only-content`);
|
2022-06-01 16:58:09 +03:00
|
|
|
|
|
|
|
|
|
|
|
textarea.classList.remove(`hidden`);
|
2022-05-21 16:17:25 +03:00
|
|
|
// Focus
|
|
|
|
textarea.focus();
|
|
|
|
// Scroll
|
|
|
|
textarea.scrollTop = textarea.scrollHeight;
|
|
|
|
// Move cursor
|
|
|
|
textarea.setSelectionRange(textarea.value.length, textarea.value.length);
|
|
|
|
}
|
|
|
|
|
2022-06-01 16:58:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener(`DOMContentLoaded`, e => {
|
|
|
|
|
|
|
|
{ // Textarea Handling
|
|
|
|
const textarea = document.getElementById(`pad-content`);
|
2022-06-01 17:43:05 +03:00
|
|
|
setTextareaPreview(!!textarea.value);
|
|
|
|
|
|
|
|
// Make sure tabs are taken into consideration
|
|
|
|
textarea.addEventListener('keydown', function (e) {
|
|
|
|
if (e.key == 'Tab') {
|
|
|
|
e.preventDefault();
|
|
|
|
const start = this.selectionStart;
|
|
|
|
const end = this.selectionEnd;
|
|
|
|
|
|
|
|
// set textarea value to: text before caret + tab + text after caret
|
|
|
|
this.value = this.value.substring(0, start) +
|
|
|
|
"\t" + this.value.substring(end);
|
|
|
|
|
|
|
|
// put caret at right position again
|
|
|
|
this.selectionStart =
|
|
|
|
this.selectionEnd = start + 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
try { // highlights
|
|
|
|
hljs.highlightElement(document.getElementById(`textarea-preview`));
|
|
|
|
} catch ( err ) {
|
|
|
|
console.err(err);
|
2022-06-01 16:58:09 +03:00
|
|
|
}
|
|
|
|
|
2022-05-21 16:17:25 +03:00
|
|
|
{ // Archives
|
|
|
|
renderArchivesSelection()
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|