/* DokuWiki MoaiEditor Localstorage.js file Version : 0.5a (May 6, 2026) Author : MoaiTools License : GPL 3 (http://www.gnu.org/licenses/gpl.html) */ /* Storage of client-side user settings. Users: - Button.storage -- button mode/state valid: list of mode names - Buttons.dividerPos -- divider position valid: [0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100] - Editor._index -- index of editor valid: integer (handles invalid indexes by itself, even negative ones) - Toc.depthValue -- Toc depth valid: [2,3,4,5] - Main.enabled -- editor enabled valid: ['on','off'] - Main.isHidden -- editor hidden valid: [0,1] - Layout.linewrap -- editor linewrap valid: ['off','soft'] */ MoaiEditor.LocalStorage = class { constructor(varname, deflt=null, valid=null) { this.varname = DOKU_BASE+'__dokuwiki__moaied__'+varname; this.default = deflt; this.valid = valid; } // ┌───────────────────────────────────┐ // │ Public │ // └───────────────────────────────────┘ set value(value) { if (this.localStorageFailed()) return null; localStorage.setItem(this.varname, value); } // ──────────────────────────────────── get value() { if (this.localStorageFailed()) return this.default; var value = this._get(); // If value needs to be integer const isnum = /^\d+$/.test(value); if (this.valid === 'integer' && isnum) return value; // If value needs to be among a list of given values else if (this.valid.constructor === Array && this._inList(value)) return value; // If it did not pass the validation return this.default; } // ┌───────────────────────────────────┐ // │ Private │ // └───────────────────────────────────┘ _inList(value) { for (let v of this.valid) if (v == value) return true; return false; } // ──────────────────────────────────── _get() { var value = localStorage[this.varname]; if (!value) { localStorage.setItem(this.varname, this.default); value = this.default; } return value; } // ──────────────────────────────────── localStorageFailed() { if (typeof(Storage) === "undefined") { console.warn("MoaiEditor :: Local Storage is not available in your browser."); return true; } return false; } }; // End Class