/* DokuWiki MoaiEditor Editor.js file Version : 0.5a (May 6, 2026) Author : MoaiTools License : GPL 3 (http://www.gnu.org/licenses/gpl.html) */ /* Editor class ‾‾‾‾‾‾‾‾‾‾‾‾ Handles different editors (Native, Codemirror, Prosemirror). */ MoaiEditor.Editor = class { constructor() { this.editors = []; this.count = 1; this.current = null; this.name = ''; this._index = new MoaiEditor.LocalStorage('current_editor', 0, 'integer'); // User settings this.linewrap = new MoaiEditor.LocalStorage('linewrap', 'soft', ['off','soft']); } // ┌───────────────────────────────────┐ // │ Public │ // └───────────────────────────────────┘ init () { // Populate list of editors this.editors.push( moaiEditor.mirror ); for (let name in moaiEditor.plugins) { const plugin = moaiEditor.plugins[name]; if (plugin.isEditor && plugin.exists()) this.editors.push(plugin); } // Update count this.count = this.editors.length; // Update current editor name and handle this.current = this.editors[this.index]; this.name = this.current.name; } // ──────────────────────────────────── start () { // Start the current editor this.current.enable(); // Set wrap lines this.current.setWrap(this.linewrap.value); } // ──────────────────────────────────── toggle () { // In phones, scroll to the textarea side (call goRight before to prevent refresh bug) moaiEditor.layout.goRight(); // Stop any scroll loop in progress moaiEditor.scroll.halt(); // Copy the scroll position moaiEditor.scroll.copy(); // Disable previous editor this.current.disable(); // Increment index this.index = this.index + 1; if (this.index >= this.count) this.index = 0; // Update current editor name and handle this.current = this.editors[this.index]; this.name = this.current.name; // Enable current editor this.current.enable(true); // Set wrap lines this.current.setWrap(this.linewrap.value); // Paste the previously saved scroll position moaiEditor.scroll.paste(); // Show and update the dirty area overlay (if applicable) moaiEditor.dirty.showDirtyArea(); } // ──────────────────────────────────── onToolbarButtonInput () { this.current.onToolbarButtonInput(); } // ──────────────────────────────────── toggleWrapLines() { // In phones, scroll to the textarea side (call goRight before to prevent refresh bug) moaiEditor.layout.goRight(); // Toggle the textarea wrap const value = {soft:'off', off:'soft'}[moaiEditor.editor.linewrap.value]; moaiEditor.editor.linewrap.value = value; moaiEditor.editor.current.setWrap(value); } // ──────────────────────────────────── getEditors () { // Return list of available editors var names = []; for (let editor of this.editors) names.push(' - '+editor.name); return names.join('\n'); } // ┌───────────────────────────────────┐ // │ Private │ // └───────────────────────────────────┘ onInput () { // Placeholder } // ──────────────────────────────────── set index(i) { this._index.value = i; } get index() { var i = parseInt(this._index.value); if (i < 0 || i >= this.count) { i = 0; this._index.value = i; } return i; } }; // End Class