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 |
* Repository helper for the Moodle Tiny Autosave plugin.
|
|
|
18 |
*
|
|
|
19 |
* @module tiny_autosave/repository
|
|
|
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 {call} from 'core/ajax';
|
|
|
25 |
import * as config from 'core/config';
|
|
|
26 |
import * as Options from './options';
|
|
|
27 |
import Pending from 'core/pending';
|
|
|
28 |
import {ensureEditorIsValid} from 'editor_tiny/utils';
|
|
|
29 |
|
|
|
30 |
const fetchOne = (methodname, args) => call([{
|
|
|
31 |
methodname,
|
|
|
32 |
args,
|
|
|
33 |
}])[0];
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Resume an Autosave session.
|
|
|
37 |
*
|
|
|
38 |
* @param {TinyMCE} editor The TinyMCE editor instance
|
|
|
39 |
* @returns {Promise<AutosaveSession>} The Autosave session
|
|
|
40 |
*/
|
|
|
41 |
export const resumeAutosaveSession = (editor) => {
|
|
|
42 |
if (!ensureEditorIsValid(editor)) {
|
|
|
43 |
return Promise.reject('Invalid editor');
|
|
|
44 |
}
|
|
|
45 |
const pendingPromise = new Pending('tiny_autosave/repository:resumeAutosaveSession');
|
|
|
46 |
return fetchOne('tiny_autosave_resume_session', {
|
|
|
47 |
contextid: Options.getContextId(editor),
|
|
|
48 |
pagehash: Options.getPageHash(editor),
|
|
|
49 |
pageinstance: Options.getPageInstance(editor),
|
|
|
50 |
elementid: editor.targetElm.id,
|
|
|
51 |
draftid: Options.getDraftItemId(editor),
|
|
|
52 |
})
|
|
|
53 |
.then((result) => {
|
|
|
54 |
pendingPromise.resolve();
|
|
|
55 |
return result;
|
|
|
56 |
});
|
|
|
57 |
};
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Update the content of the Autosave session.
|
|
|
61 |
*
|
|
|
62 |
* @param {TinyMCE} editor The TinyMCE editor instance
|
|
|
63 |
* @returns {Promise<AutosaveSession>} The Autosave session
|
|
|
64 |
*/
|
|
|
65 |
export const updateAutosaveSession = (editor) => {
|
|
|
66 |
if (!ensureEditorIsValid(editor)) {
|
|
|
67 |
return Promise.reject('Invalid editor');
|
|
|
68 |
}
|
|
|
69 |
if (Options.hasAutosaveHasReset(editor)) {
|
|
|
70 |
return Promise.reject('Skipping store of autosave content - content has been reset');
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
const pendingPromise = new Pending('tiny_autosave/repository:updateAutosaveSession');
|
|
|
74 |
|
|
|
75 |
return fetchOne('tiny_autosave_update_session', {
|
|
|
76 |
contextid: Options.getContextId(editor),
|
|
|
77 |
pagehash: Options.getPageHash(editor),
|
|
|
78 |
pageinstance: Options.getPageInstance(editor),
|
|
|
79 |
elementid: editor.targetElm.id,
|
|
|
80 |
drafttext: editor.getContent(),
|
|
|
81 |
})
|
|
|
82 |
.then((result) => {
|
|
|
83 |
pendingPromise.resolve();
|
|
|
84 |
return result;
|
|
|
85 |
});
|
|
|
86 |
};
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Remove the Autosave session.
|
|
|
90 |
*
|
|
|
91 |
* @param {TinyMCE} editor The TinyMCE editor instance
|
|
|
92 |
*/
|
|
|
93 |
export const removeAutosaveSession = (editor) => {
|
|
|
94 |
if (!ensureEditorIsValid(editor)) {
|
|
|
95 |
throw new Error('Invalid editor');
|
|
|
96 |
}
|
|
|
97 |
Options.setAutosaveHasReset(editor);
|
|
|
98 |
|
|
|
99 |
// Please note that we must use a Beacon send here.
|
|
|
100 |
// The XHR is not guaranteed because it will be aborted on page transition.
|
|
|
101 |
// https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API
|
|
|
102 |
// Note: Moodle does not currently have a sendBeacon API endpoint.
|
|
|
103 |
const requestUrl = new URL(`${config.wwwroot}/lib/ajax/service.php`);
|
|
|
104 |
requestUrl.searchParams.set('sesskey', config.sesskey);
|
|
|
105 |
|
|
|
106 |
const args = {
|
|
|
107 |
contextid: Options.getContextId(editor),
|
|
|
108 |
pagehash: Options.getPageHash(editor),
|
|
|
109 |
pageinstance: Options.getPageInstance(editor),
|
|
|
110 |
elementid: editor.targetElm.id,
|
|
|
111 |
};
|
|
|
112 |
navigator.sendBeacon(requestUrl, JSON.stringify([{
|
|
|
113 |
index: 0,
|
|
|
114 |
methodname: 'tiny_autosave_reset_session',
|
|
|
115 |
args,
|
|
|
116 |
}]));
|
|
|
117 |
};
|