Matthias Andreas Benkard | 2137b35 | 2022-11-01 10:38:20 +0100 | [diff] [blame] | 1 | import '../web_modules/ContentTools/build/content-tools.min.js'; |
| 2 | |
| 3 | window.addEventListener('DOMContentLoaded', () => { |
| 4 | let editor = ContentTools.EditorApp.get(); |
| 5 | editor.init('*[data-editable]', 'data-name'); |
| 6 | |
Matthias Andreas Benkard | a84be3b | 2022-11-02 05:33:37 +0100 | [diff] [blame^] | 7 | let {pageTitle} = document.getElementById('wiki-page').dataset; |
| 8 | |
Matthias Andreas Benkard | 2137b35 | 2022-11-01 10:38:20 +0100 | [diff] [blame] | 9 | 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 Benkard | 2137b35 | 2022-11-01 10:38:20 +0100 | [diff] [blame] | 25 | 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 Benkard | a84be3b | 2022-11-02 05:33:37 +0100 | [diff] [blame^] | 35 | return; |
Matthias Andreas Benkard | 2137b35 | 2022-11-01 10:38:20 +0100 | [diff] [blame] | 36 | } |
| 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 Benkard | a84be3b | 2022-11-02 05:33:37 +0100 | [diff] [blame^] | 44 | return; |
Matthias Andreas Benkard | 2137b35 | 2022-11-01 10:38:20 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | if (status.hasOwnProperty("content")) { |
| 48 | document.getElementById("wiki-content").innerHTML = status.content; |
| 49 | } |
| 50 | |
Matthias Andreas Benkard | a84be3b | 2022-11-02 05:33:37 +0100 | [diff] [blame^] | 51 | if (status.hasOwnProperty("title")) { |
| 52 | pageTitle = status.title; |
| 53 | } |
| 54 | |
Matthias Andreas Benkard | 2137b35 | 2022-11-01 10:38:20 +0100 | [diff] [blame] | 55 | this.busy(false); |
| 56 | }); |
| 57 | }); |