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