lib/editor/amd/src/events.js

  1. // This file is part of Moodle - http://moodle.org/ //
  2. // Moodle is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, either version 3 of the License, or
  5. // (at your option) any later version.
  6. //
  7. // Moodle is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU General Public License
  13. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  14. /**
  15. * Javascript events for the `core_editor` subsystem.
  16. *
  17. * @module core_editor/events
  18. * @copyright 2021 Andrew Nicols <andrew@nicols.co.uk>
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. * @since 4.0
  21. */
  22. import {dispatchEvent} from 'core/event_dispatcher';
  23. /**
  24. * Events for the `core_editor` subsystem.
  25. *
  26. * @constant
  27. * @property {String} editorContentRestored See {@link event:editorContentRestored}
  28. */
  29. export const eventTypes = {
  30. /**
  31. * An event triggered when an editor restores auto-saved content.
  32. *
  33. * @event editorContentRestored
  34. */
  35. editorContentRestored: 'core_editor/contentRestored',
  36. };
  37. /**
  38. * Trigger an event to indicate that editor content was restored.
  39. *
  40. * @method notifyEditorContentRestored
  41. * @param {HTMLElement|null} editor The element that was modified
  42. * @returns {CustomEvent}
  43. * @fires editorContentRestored
  44. */
  45. export const notifyEditorContentRestored = editor => {
  46. if (!editor) {
  47. window.console.warn(
  48. `The HTMLElement representing the editor that was modified should be provided to notifyEditorContentRestored.`
  49. );
  50. }
  51. return dispatchEvent(
  52. eventTypes.editorContentRestored,
  53. {},
  54. editor || document
  55. );
  56. };