1/*  DokuWiki MoaiEditor Editor.js file
2    Version : 0.5a (May 6, 2026)
3    Author  : MoaiTools <info@moaitools.org>
4    License : GPL 3 (http://www.gnu.org/licenses/gpl.html) */
5
6/*  Editor class
7    ‾‾‾‾‾‾‾‾‾‾‾‾
8    Handles different editors (Native, Codemirror, Prosemirror).
9
10*/
11MoaiEditor.Editor = class {
12
13    constructor() {
14
15        this.editors = [];
16        this.count   = 1;
17        this.current = null;
18        this.name    = '';
19        this._index = new MoaiEditor.LocalStorage('current_editor', 0, 'integer');
20
21        // User settings
22        this.linewrap = new MoaiEditor.LocalStorage('linewrap', 'soft', ['off','soft']);
23    }
24    // ┌───────────────────────────────────┐
25    // │ Public                            │
26    // └───────────────────────────────────┘
27
28    init () {
29        // Populate list of editors
30        this.editors.push( moaiEditor.mirror );
31        for (let name in moaiEditor.plugins) {
32            const plugin = moaiEditor.plugins[name];
33            if (plugin.isEditor  &&  plugin.exists())
34                this.editors.push(plugin);
35        }
36        // Update count
37        this.count = this.editors.length;
38        // Update current editor name and handle
39        this.current = this.editors[this.index];
40        this.name = this.current.name;
41    }
42    // ────────────────────────────────────
43    start () {
44        // Start the current editor
45        this.current.enable();
46        // Set wrap lines
47        this.current.setWrap(this.linewrap.value);
48    }
49    // ────────────────────────────────────
50    toggle () {
51        // In phones, scroll to the textarea side (call goRight before to prevent refresh bug)
52        moaiEditor.layout.goRight();
53        // Stop any scroll loop in progress
54        moaiEditor.scroll.halt();
55        // Copy the scroll position
56        moaiEditor.scroll.copy();
57        // Disable previous editor
58        this.current.disable();
59        // Increment index
60        this.index = this.index + 1;
61        if (this.index >= this.count)
62            this.index = 0;
63        // Update current editor name and handle
64        this.current = this.editors[this.index];
65        this.name = this.current.name;
66        // Enable current editor
67        this.current.enable(true);
68        // Set wrap lines
69        this.current.setWrap(this.linewrap.value);
70        // Paste the previously saved scroll position
71        moaiEditor.scroll.paste();
72        // Show and update the dirty area overlay (if applicable)
73        moaiEditor.dirty.showDirtyArea();
74    }
75    // ────────────────────────────────────
76    onToolbarButtonInput () {
77        this.current.onToolbarButtonInput();
78    }
79    // ────────────────────────────────────
80    toggleWrapLines() {
81        // In phones, scroll to the textarea side (call goRight before to prevent refresh bug)
82        moaiEditor.layout.goRight();
83        // Toggle the textarea wrap
84        const value = {soft:'off', off:'soft'}[moaiEditor.editor.linewrap.value];
85        moaiEditor.editor.linewrap.value = value;
86        moaiEditor.editor.current.setWrap(value);
87    }
88    // ────────────────────────────────────
89    getEditors () {
90        // Return list of available editors
91        var names = [];
92        for (let editor of this.editors)
93            names.push('        - '+editor.name);
94        return names.join('\n');
95    }
96    // ┌───────────────────────────────────┐
97    // │ Private                           │
98    // └───────────────────────────────────┘
99
100    onInput () {
101        // Placeholder
102    }
103    // ────────────────────────────────────
104    set index(i) {
105        this._index.value = i;
106    }
107    get index() {
108        var i = parseInt(this._index.value);
109        if (i < 0   ||  i >= this.count) {
110            i = 0;
111            this._index.value = i;
112        }
113        return i;
114    }
115}; // End Class
116
117
118
119
120
121