1/*  DokuWiki MoaiEditor Editor.js file
2    Author  : MoaiTools <info@moaitools.org>
3    License : GPL 3 (http://www.gnu.org/licenses/gpl.html) */
4
5/*  Editor class
6    ‾‾‾‾‾‾‾‾‾‾‾‾
7    Handles different editors (Native, Codemirror, Prosemirror).
8
9*/
10MoaiEditor.Editor = class {
11
12    constructor() {
13
14        this.editors = [];
15        this.count   = 1;
16        this.current = null;
17        this.name    = '';
18        this._index = new MoaiEditor.LocalStorage('current_editor', 0, 'integer');
19        this.crashed = [];
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({object:moaiEditor.mirror, name:'native'});
31        for (let name in moaiEditor.plugins) {
32            const plugin = moaiEditor.plugins[name];
33            if (plugin.isEditor  &&  plugin.exists()  &&  !this.crashed.includes(name))
34                this.editors.push({object:plugin, name:name});
35        }
36        // Update count
37        this.count = this.editors.length;
38        // Update current editor name and handle
39        this.current = this.editors[this.index].object;
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        // Start the current editor
64        this.enableEditor();
65        // Paste the previously saved scroll position
66        moaiEditor.scroll.paste();
67    }
68
69    enableEditor () {
70        // Update current editor name and handle
71        this.current = this.editors[this.index].object;
72        this.name = this.current.name;
73        // Enable current editor
74        this.current.enable(true);
75        // Set wrap lines
76        this.current.setWrap(this.linewrap.value);
77        // Show and update the dirty area overlay (if applicable)
78        moaiEditor.dirty.showDirtyArea();
79    }
80
81    onToolbarButtonInput () {
82        this.current.onToolbarButtonInput();
83    }
84
85    toggleWrapLines() {
86        // In phones, scroll to the textarea side (call goRight before to prevent refresh bug)
87        moaiEditor.layout.goRight();
88        // Toggle the textarea wrap
89        const value = {soft:'off', off:'soft'}[moaiEditor.editor.linewrap.value];
90        moaiEditor.editor.linewrap.value = value;
91        moaiEditor.editor.current.setWrap(value);
92    }
93
94    getEditors () {
95        // Return list of available editors
96        var names = [];
97        for (let editor of this.editors)
98            names.push('        - '+editor.object.name);
99        return names.join('\n');
100    }
101
102    crash (name) {
103        // Before initialization
104        if (this.editors.length == 0) {
105            // Prevent the editor from being added to the list
106            this.crashed.push(name);
107        }
108        // After initialization
109        else {
110            // Activate the native editor
111            this.index = 0;
112            this.enableEditor();
113            // Update the toggle editor button
114            moaiEditor.layout.updateEditorBtn();
115        }
116    }
117    // ┌───────────────────────────────────┐
118    // │ Private                           │
119    // └───────────────────────────────────┘
120
121    onInput () {
122        // Placeholder
123    }
124
125    set index(i) {
126        this._index.value = i;
127    }
128    get index() {
129        var i = parseInt(this._index.value);
130        if (i < 0   ||  i >= this.count) {
131            i = 0;
132            this._index.value = i;
133        }
134        return i;
135    }
136}; // End Class
137
138
139
140
141
142