blob: 0e35d44cb5c2acc9fc9ae2e25ed2bcd371fd80f9 [file] [log] [blame]
Matthias Andreas Benkard2137b352022-11-01 10:38:20 +01001import '../web_modules/ContentTools/build/content-tools.min.js';
2
3window.addEventListener('DOMContentLoaded', () => {
4 let editor = ContentTools.EditorApp.get();
5 editor.init('*[data-editable]', 'data-name');
6
Matthias Andreas Benkarda84be3b2022-11-02 05:33:37 +01007 let {pageTitle} = document.getElementById('wiki-page').dataset;
8
Matthias Andreas Benkard2137b352022-11-01 10:38:20 +01009 editor.addEventListener('saved', async function (ev) {
10 document.getElementById("warning-panel").close();
11
12 let {regions} = ev.detail();
13 if (Object.getOwnPropertyNames(regions).length === 0) {
14 // Nothing changed.
15 return;
16 }
17
18 this.busy(true);
19
20 let requestParams = new URLSearchParams();
21 for (let name of Object.getOwnPropertyNames(regions)) {
22 requestParams.append(name, regions[name]);
23 }
24
Matthias Andreas Benkard2137b352022-11-01 10:38:20 +010025 let response = await fetch(`/wiki/${pageTitle}`, {
26 method: 'POST',
27 body: requestParams
28 });
29
30 if (!response.ok) {
31 document.getElementById("warning-panel").open();
32 document.getElementById("warning-text").innerText = `Failed to save page: ${response.statusText}`;
33 this.busy(false);
34
Matthias Andreas Benkarda84be3b2022-11-02 05:33:37 +010035 return;
Matthias Andreas Benkard2137b352022-11-01 10:38:20 +010036 }
37
38 let status = await response.json();
39 if (status.status !== "ok") {
40 document.getElementById("warning-panel").open();
41 document.getElementById("warning-text").innerText = `Failed to save page: ${JSON.stringify(status)}`;
42 this.busy(false);
43
Matthias Andreas Benkarda84be3b2022-11-02 05:33:37 +010044 return;
Matthias Andreas Benkard2137b352022-11-01 10:38:20 +010045 }
46
47 if (status.hasOwnProperty("content")) {
48 document.getElementById("wiki-content").innerHTML = status.content;
49 }
50
Matthias Andreas Benkarda84be3b2022-11-02 05:33:37 +010051 if (status.hasOwnProperty("title")) {
52 pageTitle = status.title;
53 }
54
Matthias Andreas Benkard2137b352022-11-01 10:38:20 +010055 this.busy(false);
56 });
57});