lib/editor/tiny/plugins/media/amd/src/configuration.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 Media configuration.
  17. *
  18. * @module tiny_media/configuration
  19. * @copyright 2022 Huong Nguyen <huongnv13@gmail.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import {
  23. imageButtonName,
  24. videoButtonName,
  25. mediaManagerButtonName,
  26. } from './common';
  27. import uploadFile from 'editor_tiny/uploader';
  28. import {
  29. addContextmenuItem,
  30. } from 'editor_tiny/utils';
  31. const configureMenu = (menu) => {
  32. // Replace the standard Media plugin with the Moodle embed.
  33. if (menu.insert.items.match(/\bmedia\b/)) {
  34. menu.insert.items = menu.insert.items.replace(/\bmedia\b/, videoButtonName);
  35. } else {
  36. menu.insert.items = `${videoButtonName} ${menu.insert.items}`;
  37. }
  38. // Replace the standard image plugin with the Moodle image.
  39. if (menu.insert.items.match(/\bimage\b/)) {
  40. menu.insert.items = menu.insert.items.replace(/\bimage\b/, imageButtonName);
  41. } else {
  42. menu.insert.items = `${imageButtonName} ${menu.insert.items}`;
  43. }
  44. // Add the Media Manager to the end of the Tools menu.
  45. menu.tools.items += ` ${mediaManagerButtonName}`;
  46. return menu;
  47. };
  48. const configureToolbar = (toolbar) => {
  49. // The toolbar contains an array of named sections.
  50. // The Moodle integration ensures that there is a section called 'content'.
  51. return toolbar.map((section) => {
  52. if (section.name === 'content') {
  53. // Insert the image, and embed, buttons at the start of it.
  54. section.items.unshift(imageButtonName, videoButtonName);
  55. }
  56. return section;
  57. });
  58. };
  59. export const configure = (instanceConfig) => {
  60. // Update the instance configuration to add the Media menu option to the menus and toolbars and upload_handler.
  61. return {
  62. contextmenu: addContextmenuItem(instanceConfig.contextmenu, imageButtonName, videoButtonName),
  63. menu: configureMenu(instanceConfig.menu),
  64. toolbar: configureToolbar(instanceConfig.toolbar),
  65. // eslint-disable-next-line camelcase
  66. images_upload_handler: (blobInfo, progress) => uploadFile(
  67. window.tinymce.activeEditor,
  68. 'image',
  69. blobInfo.blob(),
  70. blobInfo.filename(),
  71. progress
  72. ),
  73. // eslint-disable-next-line camelcase
  74. images_reuse_filename: true,
  75. };
  76. };