lib/editor/tiny/plugins/premium/amd/src/plugin.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. * Tiny Premium plugin for Moodle.
  17. *
  18. * @module tiny_premium/plugin
  19. * @copyright 2023 David Woloszyn <david.woloszyn@moodle.com>
  20. * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import {getTinyMCE} from 'editor_tiny/loader';
  23. import {getPluginMetadata} from 'editor_tiny/utils';
  24. import {component, pluginName} from 'tiny_premium/common';
  25. import * as Configuration from 'tiny_premium/configuration';
  26. import {getApiKey} from 'tiny_premium/external';
  27. import Config from 'core/config';
  28. const currentContextId = Config.contextid;
  29. // eslint-disable-next-line no-async-promise-executor
  30. export default new Promise(async(resolve) => {
  31. const [
  32. tinyMCE,
  33. pluginMetadata,
  34. externalData
  35. ] = await Promise.all([
  36. getTinyMCE(),
  37. getPluginMetadata(component, pluginName),
  38. getApiKey(currentContextId)
  39. ]);
  40. tinyMCE.PluginManager.add(`${component}/plugin`, () => {
  41. return pluginMetadata;
  42. });
  43. // Load the Tiny Premium script using the provided API key.
  44. await getTinyPremium(tinyMCE, externalData.apikey);
  45. resolve([`${component}/plugin`, Configuration]);
  46. });
  47. let tinyPremiumPromise;
  48. /**
  49. * Promise for Tiny Premium plugins script.
  50. *
  51. * @param {TinyMCE} tinyMCE
  52. * @param {string} apikey
  53. * @return {Promise}
  54. */
  55. const getTinyPremium = (tinyMCE, apikey) => {
  56. if (tinyPremiumPromise) {
  57. return tinyPremiumPromise;
  58. }
  59. tinyPremiumPromise = new Promise((resolve, reject) => {
  60. const head = document.querySelector('head');
  61. const script = document.createElement('script');
  62. const tinyVersion = `${tinyMCE.majorVersion}.${tinyMCE.minorVersion}`;
  63. script.dataset.tinymce = 'premium';
  64. script.src = `https://cdn.tiny.cloud/1/${apikey}/tinymce/${tinyVersion}/plugins.min.js`;
  65. script.referrerpolicy = "origin";
  66. script.addEventListener('load', () => {
  67. resolve();
  68. }, false);
  69. script.addEventListener('error', (err) => {
  70. reject(err);
  71. }, false);
  72. head.append(script);
  73. });
  74. return tinyPremiumPromise;
  75. };