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/autosaver
|
|
|
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 Options from './options';
|
|
|
25 |
import * as Storage from './storage';
|
|
|
26 |
import Log from 'core/log';
|
|
|
27 |
import {eventTypes} from 'core_form/events';
|
|
|
28 |
import {getLogSource} from './common';
|
|
|
29 |
|
|
|
30 |
export const register = (editor) => {
|
|
|
31 |
const undoHandler = () => {
|
|
|
32 |
if (!editor.undoManager.hasUndo()) {
|
|
|
33 |
Log.debug(`Ignoring undo event as there is no undo history`, getLogSource(editor));
|
|
|
34 |
return;
|
|
|
35 |
}
|
|
|
36 |
Storage.saveDraft(editor);
|
|
|
37 |
};
|
|
|
38 |
|
|
|
39 |
const visibilityChangedHandler = () => {
|
|
|
40 |
if (document.visibilityState === 'hidden') {
|
|
|
41 |
if (Options.isInitialised(editor)) {
|
|
|
42 |
Storage.saveDraft(editor);
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
};
|
|
|
46 |
|
|
|
47 |
// Javascript form submission handler.
|
|
|
48 |
const handleFormSubmittedByJavascript = (e) => {
|
|
|
49 |
if (Options.isInitialised(editor) && e.target.contains(editor.getElement())) {
|
|
|
50 |
removeAutoSaveSession();
|
|
|
51 |
}
|
|
|
52 |
};
|
|
|
53 |
|
|
|
54 |
// Remove the auto save session.
|
|
|
55 |
const removeAutoSaveSession = () => {
|
|
|
56 |
document.removeEventListener('visibilitychange', visibilityChangedHandler);
|
|
|
57 |
document.removeEventListener(eventTypes.formSubmittedByJavascript, handleFormSubmittedByJavascript);
|
|
|
58 |
Storage.removeAutosaveSession(editor);
|
|
|
59 |
};
|
|
|
60 |
|
|
|
61 |
// Attempt to store the draft one final time before the page unloads.
|
|
|
62 |
// Note: This may need to be sent as a beacon instead.
|
|
|
63 |
document.addEventListener('visibilitychange', visibilityChangedHandler);
|
|
|
64 |
|
|
|
65 |
// When the page is submitted as a form, remove the draft.
|
|
|
66 |
editor.on('submit', removeAutoSaveSession);
|
|
|
67 |
document.addEventListener(eventTypes.formSubmittedByJavascript, handleFormSubmittedByJavascript);
|
|
|
68 |
|
|
|
69 |
editor.on('init', () => {
|
|
|
70 |
// Setup the Undo handler.
|
|
|
71 |
editor.on('AddUndo', undoHandler);
|
|
|
72 |
|
|
|
73 |
if (editor.dom.isEmpty(editor.getBody())) {
|
|
|
74 |
Log.info(`Attempting to restore draft`, getLogSource(editor));
|
|
|
75 |
Storage.restoreDraft(editor);
|
|
|
76 |
} else {
|
|
|
77 |
// There was nothing to restore, so we can mark the editor as initialised.
|
|
|
78 |
Log.warn(`Skipping draft restoration. The editor is not empty.`, getLogSource(editor));
|
|
|
79 |
Options.markInitialised(editor);
|
|
|
80 |
}
|
|
|
81 |
});
|
|
|
82 |
};
|