xref: /plugin/ckeditor5markdown/script.js (revision c21c9f378b444ba1684285354be01b9cba5389a7)
1*c21c9f37SJohannes Rebhandocument.addEventListener("DOMContentLoaded", () => {
2*c21c9f37SJohannes Rebhan    const textarea = document.querySelector("textarea[name='wikitext']");
3*c21c9f37SJohannes Rebhan    if (!textarea) return;
4*c21c9f37SJohannes Rebhan
5*c21c9f37SJohannes Rebhan    const container = document.createElement("div");
6*c21c9f37SJohannes Rebhan    container.id = "ckeditor-container";
7*c21c9f37SJohannes Rebhan    textarea.style.display = "none";
8*c21c9f37SJohannes Rebhan    textarea.parentNode.insertBefore(container, textarea);
9*c21c9f37SJohannes Rebhan
10*c21c9f37SJohannes Rebhan    const script = document.createElement("script");
11*c21c9f37SJohannes Rebhan    script.src = DOKU_BASE + "lib/plugins/ckeditor5markdown/ckeditor.js";
12*c21c9f37SJohannes Rebhan    script.onload = () => {
13*c21c9f37SJohannes Rebhan        ClassicEditor
14*c21c9f37SJohannes Rebhan        .default
15*c21c9f37SJohannes Rebhan        .create(container)
16*c21c9f37SJohannes Rebhan        .then(editor => {
17*c21c9f37SJohannes Rebhan            window.editor = editor;
18*c21c9f37SJohannes Rebhan            editor.setData(textarea.value);
19*c21c9f37SJohannes Rebhan
20*c21c9f37SJohannes Rebhan            editor.model.document.on("change:data", () => {
21*c21c9f37SJohannes Rebhan                textarea.value = editor.getData();
22*c21c9f37SJohannes Rebhan            });
23*c21c9f37SJohannes Rebhan
24*c21c9f37SJohannes Rebhan            const form = textarea.closest("form");
25*c21c9f37SJohannes Rebhan            if (form) {
26*c21c9f37SJohannes Rebhan                form.addEventListener("submit", () => {
27*c21c9f37SJohannes Rebhan                    textarea.value = editor.getData();
28*c21c9f37SJohannes Rebhan                });
29*c21c9f37SJohannes Rebhan            }
30*c21c9f37SJohannes Rebhan        })
31*c21c9f37SJohannes Rebhan        .catch(console.error);
32*c21c9f37SJohannes Rebhan    };
33*c21c9f37SJohannes Rebhan
34*c21c9f37SJohannes Rebhan    document.body.appendChild(script);
35*c21c9f37SJohannes Rebhan});
36