lib/editor/tiny/plugins/autosave/amd/src/repository.js

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