2017-09-15 15:54:47 +03:00
|
|
|
function autosizeTextarea(textarea, shadow) {
|
|
|
|
shadow.style.width = textarea.clientWidth + "px";
|
|
|
|
shadow.value = textarea.value;
|
|
|
|
textarea.style.height = shadow.scrollHeight + "px";
|
|
|
|
}
|
|
|
|
|
|
|
|
function queryArgsFromForm(form) {
|
|
|
|
const items = [];
|
|
|
|
for (const {name, value} of form.elements) {
|
|
|
|
if (!name) continue;
|
|
|
|
items.push(encodeURIComponent(name) + '=' + encodeURIComponent(value));
|
|
|
|
}
|
|
|
|
return items.join('&');
|
|
|
|
}
|
|
|
|
|
|
|
|
let hasBeenOpen = false;
|
|
|
|
function openEditor() {
|
2017-09-21 11:09:57 +03:00
|
|
|
const container = document.querySelector(".container");
|
|
|
|
const rendered = container.querySelector(".rendered");
|
|
|
|
const editor = container.querySelector(".editor");
|
2017-09-15 15:54:47 +03:00
|
|
|
const textarea = editor.querySelector('textarea[name="body"]');
|
|
|
|
const shadow = editor.querySelector('textarea.shadow-control');
|
|
|
|
const form = editor.querySelector("form");
|
|
|
|
const cancel = editor.querySelector('.cancel');
|
|
|
|
|
|
|
|
const footer = document.querySelector("footer");
|
|
|
|
const lastUpdated = footer.querySelector(".last-updated");
|
|
|
|
|
|
|
|
textarea.style.height = rendered.clientHeight + "px";
|
|
|
|
|
2017-09-21 11:09:57 +03:00
|
|
|
container.classList.add('edit');
|
2017-09-15 15:54:47 +03:00
|
|
|
|
|
|
|
autosizeTextarea(textarea, shadow);
|
|
|
|
|
|
|
|
textarea.focus();
|
|
|
|
|
|
|
|
if (hasBeenOpen) return;
|
|
|
|
hasBeenOpen = true;
|
|
|
|
|
|
|
|
textarea.addEventListener('input', () => autosizeTextarea(textarea, shadow));
|
|
|
|
window.addEventListener('resize', () => autosizeTextarea(textarea, shadow));
|
|
|
|
|
|
|
|
form.addEventListener("submit", function (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2017-09-21 13:18:09 +03:00
|
|
|
const body = queryArgsFromForm(form);
|
|
|
|
textarea.disabled = true;
|
|
|
|
|
|
|
|
fetch(
|
|
|
|
form.getAttribute("action"),
|
|
|
|
{
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
},
|
|
|
|
body: body,
|
2017-09-26 14:39:44 +03:00
|
|
|
credentials: "same-origin",
|
2017-09-21 13:18:09 +03:00
|
|
|
}
|
|
|
|
).then(response => {
|
2017-09-15 15:54:47 +03:00
|
|
|
if (!response.ok) throw new Error("Unexpected status code (" + response.status + ")");
|
|
|
|
|
2017-09-21 13:18:09 +03:00
|
|
|
return response.json();
|
|
|
|
}).then(result => {
|
2017-09-21 11:09:57 +03:00
|
|
|
// Update url-bar, page title and footer
|
2017-09-21 13:11:08 +03:00
|
|
|
window.history.replaceState(null, result.title, result.slug == "" ? "." : result.slug);
|
2017-09-21 11:09:57 +03:00
|
|
|
document.querySelector("title").textContent = result.title;
|
2017-10-19 14:26:54 +03:00
|
|
|
lastUpdated.textContent = result.last_updated;
|
2017-10-19 14:39:03 +03:00
|
|
|
lastUpdated.classList.remove("missing");
|
2017-09-21 11:09:57 +03:00
|
|
|
|
|
|
|
// Update body:
|
2017-09-15 15:54:47 +03:00
|
|
|
rendered.innerHTML = result.rendered;
|
2017-09-21 11:09:57 +03:00
|
|
|
|
|
|
|
// Update form:
|
|
|
|
form.elements.base_revision.value = result.revision;
|
|
|
|
for (const element of form.elements) {
|
|
|
|
element.defaultValue = element.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
container.classList.remove('edit');
|
2017-09-15 15:54:47 +03:00
|
|
|
|
|
|
|
textarea.disabled = false;
|
2017-09-21 13:18:09 +03:00
|
|
|
}).catch(err => {
|
2017-09-15 15:54:47 +03:00
|
|
|
textarea.disabled = false;
|
|
|
|
console.error(err);
|
|
|
|
alert(err);
|
2017-09-21 13:18:09 +03:00
|
|
|
});
|
2017-09-15 15:54:47 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
cancel.addEventListener('click', function (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
2017-09-21 11:09:57 +03:00
|
|
|
container.classList.remove('edit');
|
2017-09-15 15:54:47 +03:00
|
|
|
form.reset();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
document
|
|
|
|
.getElementById("openEditor")
|
|
|
|
.addEventListener("click", function (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
|
|
|
openEditor();
|
|
|
|
})
|
2017-10-02 18:08:59 +03:00
|
|
|
|
|
|
|
if (document.querySelector(".container").classList.contains("edit")) {
|
|
|
|
openEditor();
|
|
|
|
}
|