lib/editor/tiny/plugins/autosave/amd/src/options.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. * Options helper for the Moodle Tiny Autosave plugin.
  17. *
  18. * @module tiny_autosave/options
  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 {pluginName} from './common';
  23. import {
  24. getContextId,
  25. getDraftItemId,
  26. getPluginOptionName,
  27. } from 'editor_tiny/options';
  28. import {ensureEditorIsValid} from 'editor_tiny/utils';
  29. const initialisedOptionName = getPluginOptionName(pluginName, 'initialised');
  30. const pageHashName = getPluginOptionName(pluginName, 'pagehash');
  31. const pageInstanceName = getPluginOptionName(pluginName, 'pageinstance');
  32. const backoffTime = getPluginOptionName(pluginName, 'backoffTime');
  33. const autosaveHasReset = getPluginOptionName(pluginName, 'autosaveHasReset');
  34. export const register = (editor) => {
  35. const registerOption = editor.options.register;
  36. registerOption(initialisedOptionName, {
  37. processor: 'boolean',
  38. "default": false,
  39. });
  40. registerOption(pageHashName, {
  41. processor: 'string',
  42. "default": '',
  43. });
  44. registerOption(pageInstanceName, {
  45. processor: 'string',
  46. "default": '',
  47. });
  48. registerOption(pageInstanceName, {
  49. processor: 'string',
  50. "default": '',
  51. });
  52. registerOption(backoffTime, {
  53. processor: 'number',
  54. "default": 500,
  55. });
  56. registerOption(autosaveHasReset, {
  57. processor: 'boolean',
  58. "default": false,
  59. });
  60. };
  61. export const isInitialised = (editor) => {
  62. if (!ensureEditorIsValid(editor)) {
  63. return false;
  64. }
  65. return editor.options.get(initialisedOptionName);
  66. };
  67. export const markInitialised = (editor) => editor.options.set(initialisedOptionName, true);
  68. export const getPageHash = (editor) => editor.options.get(pageHashName);
  69. export const getPageInstance = (editor) => editor.options.get(pageInstanceName);
  70. export const getBackoffTime = (editor) => editor.options.get(backoffTime);
  71. export const setAutosaveHasReset = (editor) => editor.options.set(autosaveHasReset, true);
  72. export const hasAutosaveHasReset = (editor) => editor.options.get(autosaveHasReset);
  73. export {
  74. getContextId,
  75. getDraftItemId,
  76. };