Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * Storage helper for the Moodle Tiny Autosave plugin.
18
 *
19
 * @module      tiny_autosave/storage
20
 * @copyright   2022 Andrew Lyons <andrew@nicols.co.uk>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import * as Repository from "./repository";
25
import Pending from 'core/pending';
26
import {
27
    markInitialised,
28
    getBackoffTime,
29
} from "./options";
30
import Log from 'core/log';
31
import {getLogSource} from './common';
32
 
33
/** @property {Map} A map of debounced draft saves */
34
const saveDebounceMap = new Map();
35
 
36
/**
37
 * Attempt to restore a draft into the editor
38
 *
39
 * @param {TinyMCE} editor The Editor to restore a draft for
40
 */
41
export const restoreDraft = async(editor) => {
42
    const pendingPromise = new Pending('tiny_autosave/restoreDraft');
43
    try {
44
        const session = await Repository.resumeAutosaveSession(editor);
45
        if (session && session.drafttext) {
46
            editor.undoManager.ignore(() => {
47
                editor.setContent(session.drafttext);
48
                editor.save();
49
            });
50
        }
51
    } catch (error) {
52
        // Ignore any errors as drafts are optional.
53
        Log.warn(`Failed to restore draft: ${error}`, getLogSource(editor));
54
    }
55
    markInitialised(editor);
56
    pendingPromise.resolve();
57
};
58
 
59
/**
60
 * Save the current content of the editor as a draft.
61
 *
62
 * @param {TinyMCE} editor
63
 */
64
export const saveDraft = (editor) => {
65
    const timerId = saveDebounceMap.get(editor);
66
    if (timerId) {
67
        clearTimeout(timerId);
68
    }
69
    saveDebounceMap.set(editor, setTimeout(() => {
70
        Log.debug(`Saving draft`, getLogSource(editor));
71
        Repository.updateAutosaveSession(editor)
72
        .catch((error) => window.console.warn(error));
73
    }, getBackoffTime(editor)));
74
};
75
 
76
/**
77
 * Delete the draft for the current editor.
78
 *
79
 * @param {TinyMCE} editor
80
 */
81
export const removeAutosaveSession = (editor) => {
82
    Log.debug(`Removing Autosave session`, getLogSource(editor));
83
    Repository.removeAutosaveSession(editor);
84
};