/* DokuWiki MoaiEditor Scroll_to.js file Version : 0.5b (2026-05-08) Author : MoaiTools License : GPL 3 (http://www.gnu.org/licenses/gpl.html) */ /* This class displays a visual indicator whenever a draft is saved. Informing the user whenever a draft is saved ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ In the vanilla editor whenever a draft is saved a message like this appears: 'Draft autosaved on 2026/04/01 13:36'. We show an indicator on the SAVE button itself in order not to use extra screen space. The button's tooltip shows how long ago this draft was saved. Fixing the draft save mechanism (not done yet) ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ Problem 1: Not getting triggered 'lib/scripts/locktimer.js' should save drafts every 30 seconds whenever the user changes the document. However this mechanism does not get triggered in several cases, for example: - When pressing backspace - When pressing tab - When pasting text via mouse - When pasting text via ctrl+v - When adding text via toolbar (e.g. header, bold, wrap) Problem 2: Not informing the user of draft save errors Whenever the ajax request to save the draft and extend the lock fails, the user should be notified (see how Bookstack does it). dw_locktimer.refresh > jQuery.post() should get a .fail() callback. Probably Dokuwiki should call 'dw_locktimer.refresh with an an interval of 30 seconds. Also, it should not save drafts if the document has not changed. Apparently the ajax call should be made anyway to refresh the lock. Structure of the data received from PHP by 'dw_locktimer.refreshed': lock : 1 errors : [] draft : "Draft autosaved on 2026/04/25 15:51" */ MoaiEditor.Draft = class { constructor () { // Variables this.lastSave = null; //this.lastText = moaiEditor.layout.textarea.value; // Add a green dot to the Save button this.button = moaiEditor.buttons.save; this.container = moaiEditor.createHTML('
'); this.button.handle.appendChild(this.container); this.dot = moaiEditor.createHTML('
'); // Add a callback every time the draft is saved dw_locktimer.addRefreshCallback(this.onDraftSave.bind(this)); // Interval this.interval = setInterval(this.onInterval.bind(this), 1000); } // ──────────────────────────────────── onInterval() { // Update tooltip every second if (moaiEditor.layoutReady) { var tooltip = ''; if (this.lastSave) { var dot = ''; let elapsed = Math.round( (Date.now()-this.lastSave)/1000 ); if (elapsed < 30) dot = ''; var ago = elapsed+" seconds"; if (elapsed > 90) ago = Math.floor(elapsed/60)+" minute"; if (elapsed > 119) ago = Math.floor(elapsed/60)+" minutes"; tooltip = '

'+dot+'A draft
was saved
'+ago+'
ago
'; } this.button.tooltips.draft = tooltip; this.button.update(); } } // ──────────────────────────────────── onDraftSave(data) { this.lastSave = Date.now(); // Show the green dot when the draft is saved (only if the text has changed) if (moaiEditor.layoutReady) { this.dot.remove(); this.dot = moaiEditor.createHTML('
'); this.container.appendChild(this.dot); } } }; // End Class