lib/editor/tiny/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. * Option helper for TinyMCE Editor Manager.
  17. *
  18. * @module editor_tiny/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. const optionContextId = 'moodle:contextid';
  23. const optionDraftItemId = 'moodle:draftitemid';
  24. const filePickers = 'moodle:filepickers';
  25. const optionsMoodleLang = 'moodle:language';
  26. const currentLanguage = 'moodle:currentLanguage';
  27. const optionPlaceholderSelectors = 'moodle:placeholderSelectors';
  28. export const register = (editor, options) => {
  29. const registerOption = editor.options.register;
  30. const setOption = editor.options.set;
  31. registerOption(optionContextId, {
  32. processor: 'number',
  33. "default": 0,
  34. });
  35. setOption(optionContextId, options.context);
  36. registerOption(filePickers, {
  37. processor: 'object',
  38. "default": {},
  39. });
  40. setOption(filePickers, Object.assign({}, options.filepicker));
  41. registerOption(optionDraftItemId, {
  42. processor: 'number',
  43. "default": 0,
  44. });
  45. setOption(optionDraftItemId, options.draftitemid);
  46. registerOption(currentLanguage, {
  47. processor: 'string',
  48. "default": 'en',
  49. });
  50. setOption(currentLanguage, options.currentLanguage);
  51. // This is primarily used by the media plugin, but it may be re-used elsewhere so is included here as it is large.
  52. registerOption(optionsMoodleLang, {
  53. processor: 'object',
  54. "default": {},
  55. });
  56. setOption(optionsMoodleLang, options.language);
  57. registerOption(optionPlaceholderSelectors, {
  58. processor: 'array',
  59. "default": [],
  60. });
  61. setOption(optionPlaceholderSelectors, options.placeholderSelectors);
  62. };
  63. export const getContextId = (editor) => editor.options.get(optionContextId);
  64. export const getDraftItemId = (editor) => editor.options.get(optionDraftItemId);
  65. export const getFilepickers = (editor) => editor.options.get(filePickers);
  66. export const getFilePicker = (editor, type) => getFilepickers(editor)[type];
  67. export const getMoodleLang = (editor) => editor.options.get(optionsMoodleLang);
  68. export const getCurrentLanguage = (editor) => editor.options.get(currentLanguage);
  69. /**
  70. * Get a set of namespaced options for all defined plugins.
  71. *
  72. * @param {object} options
  73. * @returns {object}
  74. */
  75. export const getInitialPluginConfiguration = (options) => {
  76. const config = {};
  77. Object.entries(options.plugins).forEach(([pluginName, pluginConfig]) => {
  78. const values = Object.entries(pluginConfig.config ?? {});
  79. values.forEach(([optionName, value]) => {
  80. config[getPluginOptionName(pluginName, optionName)] = value;
  81. });
  82. });
  83. return config;
  84. };
  85. /**
  86. * Get the namespaced option name for a plugin.
  87. *
  88. * @param {string} pluginName
  89. * @param {string} optionName
  90. * @returns {string}
  91. */
  92. export const getPluginOptionName = (pluginName, optionName) => `${pluginName}:${optionName}`;
  93. /**
  94. * Get the placeholder selectors.
  95. *
  96. * @param {TinyMCE} editor
  97. * @returns {array}
  98. */
  99. export const getPlaceholderSelectors = (editor) => editor.options.get(optionPlaceholderSelectors);
  100. /**
  101. * Register placeholder selectos.
  102. *
  103. * @param {TinyMCE} editor
  104. * @param {array} selectors
  105. */
  106. export const registerPlaceholderSelectors = (editor, selectors) => {
  107. if (selectors.length) {
  108. let existingData = getPlaceholderSelectors(editor);
  109. existingData = existingData.concat(selectors);
  110. editor.options.set(optionPlaceholderSelectors, existingData);
  111. }
  112. };