2018-08-22 09:14:25 +03:00
|
|
|
"use strict";
|
|
|
|
|
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 = [];
|
2018-09-24 09:43:36 +03:00
|
|
|
for (const {name, value, type, checked} of form.elements) {
|
2017-09-15 15:54:47 +03:00
|
|
|
if (!name) continue;
|
2018-09-24 09:43:36 +03:00
|
|
|
if (type === "radio" && !checked) continue;
|
2017-09-15 15:54:47 +03:00
|
|
|
items.push(encodeURIComponent(name) + '=' + encodeURIComponent(value));
|
|
|
|
}
|
|
|
|
return items.join('&');
|
|
|
|
}
|
|
|
|
|
2017-10-28 16:21:47 +03:00
|
|
|
function isEdited(form) {
|
2018-09-24 09:43:36 +03:00
|
|
|
for (const {name, value, defaultValue, checked, defaultChecked} of form.elements) {
|
|
|
|
if (name && ((value !== defaultValue) || (checked !== defaultChecked))) return true;
|
2017-10-28 16:21:47 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:01:24 +03:00
|
|
|
function instantiate(templateId) {
|
|
|
|
return document.getElementById(templateId).firstElementChild.cloneNode(true);
|
2017-12-01 18:52:39 +03:00
|
|
|
}
|
|
|
|
|
2017-12-02 13:01:24 +03:00
|
|
|
function popup(dialog) {
|
2017-11-24 19:04:36 +03:00
|
|
|
document.body.appendChild(dialog);
|
2017-12-02 13:01:24 +03:00
|
|
|
dialog.querySelector(".primary").focus();
|
2017-11-24 19:04:36 +03:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-02 13:01:24 +03:00
|
|
|
function handler(ev) {
|
|
|
|
document.body.removeChild(dialog);
|
|
|
|
resolve(ev.target.getAttribute("data-value"));
|
|
|
|
}
|
|
|
|
|
|
|
|
const buttons = dialog.querySelectorAll('.btn-row>*');
|
|
|
|
for (let i = 0; i < buttons.length; ++i)
|
|
|
|
buttons[i].addEventListener("click", handler);
|
2017-11-24 19:04:36 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:01:24 +03:00
|
|
|
function loginDialog(loginUrl) {
|
|
|
|
const dialog = instantiate("login");
|
|
|
|
dialog.querySelector("a").setAttribute("href", loginUrl);
|
|
|
|
return popup(dialog);
|
|
|
|
}
|
2017-12-01 18:37:41 +03:00
|
|
|
|
2017-12-02 13:01:24 +03:00
|
|
|
function alertAsync(message) {
|
|
|
|
const dialog = instantiate("alert");
|
|
|
|
dialog.querySelector(".message").textContent = message;
|
|
|
|
return popup(dialog);
|
2017-12-01 18:37:41 +03:00
|
|
|
}
|
|
|
|
|
2017-12-02 13:06:20 +03:00
|
|
|
function confirmDiscard() {
|
|
|
|
return popup(instantiate("confirm-discard"));
|
2017-12-01 18:44:07 +03:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:30:52 +03:00
|
|
|
const state = {
|
|
|
|
hasBeenOpen: false,
|
|
|
|
saving: false,
|
|
|
|
editing: function () { return document.querySelector(".container").classList.contains('edit'); },
|
|
|
|
hasCancelUrl: function () { return document.querySelector("a.button-cancel").getAttribute('href') !== ""; }
|
|
|
|
};
|
|
|
|
|
2017-09-15 15:54:47 +03:00
|
|
|
function openEditor() {
|
2018-09-24 09:43:36 +03:00
|
|
|
const bodyElement = document.querySelector("body");
|
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');
|
2018-07-16 23:11:53 +03:00
|
|
|
const form = document.getElementById('article-editor');
|
|
|
|
const cancel = form.querySelector('.cancel');
|
2018-10-05 12:03:41 +03:00
|
|
|
const cancelButton = form.querySelector('button.button-cancel');
|
2018-09-30 23:30:52 +03:00
|
|
|
const cancelInteractionGroup = form.querySelector(".cancel-interaction-group");
|
2017-09-15 15:54:47 +03:00
|
|
|
|
|
|
|
const footer = document.querySelector("footer");
|
|
|
|
const lastUpdated = footer.querySelector(".last-updated");
|
|
|
|
|
|
|
|
textarea.style.height = rendered.clientHeight + "px";
|
|
|
|
|
2018-10-01 10:01:27 +03:00
|
|
|
retainScrollRatio(() => {
|
|
|
|
container.classList.add('edit');
|
|
|
|
autosizeTextarea(textarea, shadow);
|
|
|
|
});
|
2018-09-30 23:30:52 +03:00
|
|
|
updateFormEnabledState();
|
2017-09-15 15:54:47 +03:00
|
|
|
|
2018-09-16 13:16:03 +03:00
|
|
|
textarea.setSelectionRange(textarea.value.length, textarea.value.length);
|
2017-09-15 15:54:47 +03:00
|
|
|
|
2018-09-30 23:30:52 +03:00
|
|
|
if (state.hasBeenOpen) return;
|
|
|
|
state.hasBeenOpen = true;
|
2017-09-15 15:54:47 +03:00
|
|
|
|
|
|
|
textarea.addEventListener('input', () => autosizeTextarea(textarea, shadow));
|
|
|
|
window.addEventListener('resize', () => autosizeTextarea(textarea, shadow));
|
|
|
|
|
2018-09-30 23:30:52 +03:00
|
|
|
function updateFormEnabledState() {
|
|
|
|
const baseEnabled = !state.saving && state.editing();
|
|
|
|
const enabled = {
|
|
|
|
cancel: baseEnabled && state.hasCancelUrl(),
|
|
|
|
};
|
|
|
|
|
|
|
|
cancelInteractionGroup.classList.remove(!enabled.cancel ? "interaction-group--root--enabled" : "interaction-group--root--disabled");
|
|
|
|
cancelInteractionGroup.classList.add(enabled.cancel ? "interaction-group--root--enabled" : "interaction-group--root--disabled");
|
|
|
|
|
|
|
|
for (const el of form.elements) {
|
|
|
|
el.disabled = !baseEnabled;
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:03:41 +03:00
|
|
|
cancelButton.disabled = true;
|
|
|
|
|
2018-09-30 23:30:52 +03:00
|
|
|
// TODO: edit-link in footer?
|
|
|
|
}
|
|
|
|
|
2018-10-01 10:01:27 +03:00
|
|
|
function retainScrollRatio(innerFunction) {
|
|
|
|
const scrollElement = document.body.parentElement;
|
|
|
|
const savedScrollRatio = scrollElement.scrollTop / (scrollElement.scrollHeight - scrollElement.clientHeight);
|
|
|
|
innerFunction();
|
|
|
|
scrollElement.scrollTop = (scrollElement.scrollHeight - scrollElement.clientHeight) * savedScrollRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeEditor() {
|
|
|
|
retainScrollRatio(() => container.classList.remove('edit'));
|
|
|
|
document.activeElement && document.activeElement.blur();
|
|
|
|
}
|
|
|
|
|
2018-08-22 09:14:25 +03:00
|
|
|
function doSave() {
|
2018-09-30 23:30:52 +03:00
|
|
|
state.saving = true;
|
|
|
|
updateFormEnabledState();
|
|
|
|
|
2017-09-21 13:18:09 +03:00
|
|
|
const body = queryArgsFromForm(form);
|
|
|
|
|
|
|
|
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-11-29 14:53:41 +03:00
|
|
|
// I don't know how to more precisely determine that we hit a login redirect:
|
|
|
|
const probablyLoginRedirect = response.redirected &&
|
|
|
|
(response.headers.get("content-type") !== "application/json");
|
|
|
|
|
|
|
|
if (probablyLoginRedirect) {
|
|
|
|
return loginDialog(response.url)
|
2017-11-24 19:04:36 +03:00
|
|
|
.then(() => {
|
2018-09-30 23:30:52 +03:00
|
|
|
state.saving = false;
|
|
|
|
updateFormEnabledState();
|
2017-11-24 19:04:36 +03:00
|
|
|
});
|
2017-09-21 11:09:57 +03:00
|
|
|
}
|
|
|
|
|
2017-11-24 19:04:36 +03:00
|
|
|
if (!response.ok) throw new Error("Unexpected status code (" + response.status + ")");
|
2017-11-20 18:37:52 +03:00
|
|
|
|
2017-11-24 19:04:36 +03:00
|
|
|
return response.json()
|
|
|
|
.then(result => {
|
2018-09-30 23:30:52 +03:00
|
|
|
// Update url-bar, page title, footer and cancel link
|
|
|
|
const url = result.slug == "" ? "." : result.slug;
|
|
|
|
window.history.replaceState(null, result.title, url);
|
|
|
|
cancel.setAttribute("href", url);
|
2017-11-24 19:04:36 +03:00
|
|
|
document.querySelector("title").textContent = result.title;
|
|
|
|
lastUpdated.innerHTML = result.last_updated;
|
|
|
|
lastUpdated.classList.remove("missing");
|
|
|
|
|
|
|
|
// Update body:
|
|
|
|
rendered.innerHTML = result.rendered;
|
|
|
|
|
|
|
|
form.elements.title.value = result.title;
|
|
|
|
shadow.value = textarea.value = result.body;
|
|
|
|
|
2018-09-24 09:43:36 +03:00
|
|
|
form.querySelector(`.theme-picker--option[value=${JSON.stringify(result.theme)}]`).checked = true;
|
|
|
|
bodyElement.className = `theme-${result.theme}`;
|
|
|
|
|
2017-11-24 19:04:36 +03:00
|
|
|
// Update form:
|
|
|
|
form.elements.base_revision.value = result.revision;
|
|
|
|
for (const element of form.elements) {
|
|
|
|
element.defaultValue = element.value;
|
2018-09-24 09:43:36 +03:00
|
|
|
element.defaultChecked = element.checked;
|
2017-11-24 19:04:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.conflict) {
|
2018-10-01 10:01:27 +03:00
|
|
|
closeEditor();
|
2017-11-24 19:04:36 +03:00
|
|
|
}
|
|
|
|
|
2018-09-30 23:30:52 +03:00
|
|
|
state.saving = false;
|
|
|
|
updateFormEnabledState();
|
2017-11-24 19:04:36 +03:00
|
|
|
autosizeTextarea(textarea, shadow);
|
|
|
|
|
|
|
|
if (result.conflict) {
|
2017-12-01 18:37:41 +03:00
|
|
|
return alertAsync("Your edit came into conflict with another change " +
|
|
|
|
"and has not been saved.\n" +
|
2017-11-24 19:04:36 +03:00
|
|
|
"Please resolve the merge conflict and save again.");
|
|
|
|
}
|
|
|
|
});
|
2017-09-21 13:18:09 +03:00
|
|
|
}).catch(err => {
|
2018-09-30 23:30:52 +03:00
|
|
|
state.saving = false;
|
|
|
|
updateFormEnabledState();
|
2017-09-15 15:54:47 +03:00
|
|
|
console.error(err);
|
2017-12-01 18:37:41 +03:00
|
|
|
return alertAsync(err.toString());
|
2017-09-21 13:18:09 +03:00
|
|
|
});
|
2018-08-22 09:14:25 +03:00
|
|
|
}
|
2017-09-15 15:54:47 +03:00
|
|
|
|
2018-08-22 09:14:25 +03:00
|
|
|
function doCancel() {
|
2017-12-02 13:06:20 +03:00
|
|
|
Promise.resolve(!isEdited(form) || confirmDiscard())
|
2017-12-01 18:44:07 +03:00
|
|
|
.then(doReset => {
|
|
|
|
if (doReset) {
|
2018-10-01 10:01:27 +03:00
|
|
|
closeEditor();
|
2018-09-30 23:30:52 +03:00
|
|
|
updateFormEnabledState();
|
2017-12-01 18:44:07 +03:00
|
|
|
form.reset();
|
2018-09-24 19:21:50 +03:00
|
|
|
|
|
|
|
let selectedTheme = form.querySelector(`.theme-picker--option[checked]`).value;
|
|
|
|
bodyElement.className = `theme-${selectedTheme}`;
|
2017-12-01 18:44:07 +03:00
|
|
|
}
|
|
|
|
});
|
2018-08-22 09:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
form.addEventListener("submit", function (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
doSave();
|
|
|
|
});
|
|
|
|
|
|
|
|
cancel.addEventListener('click', function (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
doCancel();
|
2017-09-15 15:54:47 +03:00
|
|
|
});
|
2017-10-28 16:26:50 +03:00
|
|
|
|
|
|
|
window.addEventListener("beforeunload", function (ev) {
|
|
|
|
if (isEdited(form)) {
|
|
|
|
ev.preventDefault();
|
|
|
|
return ev.returnValue = "Discard changes?";
|
|
|
|
}
|
|
|
|
});
|
2018-08-22 09:14:25 +03:00
|
|
|
|
|
|
|
document.addEventListener("keypress", function (ev) {
|
|
|
|
const accel = ev.ctrlKey || ev.metaKey; // Imprecise, but works cross platform
|
|
|
|
if (ev.key === "Enter" && accel) {
|
2018-09-30 23:30:52 +03:00
|
|
|
if (!state.editing()) return;
|
2018-08-22 09:14:25 +03:00
|
|
|
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
|
|
|
|
doSave();
|
|
|
|
}
|
|
|
|
});
|
2018-09-24 09:43:36 +03:00
|
|
|
|
|
|
|
const themeOptions = form.querySelectorAll(".theme-picker--option");
|
|
|
|
for (let themeOption of themeOptions) {
|
|
|
|
themeOption.addEventListener("click", function (ev) {
|
|
|
|
bodyElement.className = `theme-${ev.target.value}`;
|
|
|
|
});
|
|
|
|
}
|
2017-09-15 15:54:47 +03:00
|
|
|
}
|
|
|
|
|
2018-10-05 12:19:47 +03:00
|
|
|
function initializeTheme() {
|
|
|
|
const form = document.getElementById('article-editor');
|
|
|
|
|
|
|
|
let preSelectedTheme = form.querySelector(`.theme-picker--option[checked]`);
|
|
|
|
if (preSelectedTheme) return;
|
|
|
|
|
|
|
|
let themes = form.querySelectorAll(`.theme-picker--option`);
|
|
|
|
let randomThemeId = (Math.random() * themes.length) | 0;
|
|
|
|
|
|
|
|
let theme = themes[randomThemeId];
|
|
|
|
theme.defaultChecked = theme.checked = true;
|
|
|
|
document.querySelector("body").className = `theme-${theme.value}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
initializeTheme();
|
|
|
|
|
2017-09-15 15:54:47 +03:00
|
|
|
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();
|
|
|
|
}
|