| 1441 |
ariadna |
1 |
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Commands helper for the Moodle tiny_aiplacement plugin.
|
|
|
18 |
*
|
|
|
19 |
* @module tiny_aiplacement/commands
|
|
|
20 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
import {getButtonImage, addDynamicToolbarMenu} from 'editor_tiny/utils';
|
|
|
25 |
import {get_string as getString} from 'core/str';
|
|
|
26 |
import {
|
|
|
27 |
component,
|
|
|
28 |
placement,
|
|
|
29 |
contextMenuName,
|
|
|
30 |
generateImageName,
|
|
|
31 |
generateTextName,
|
|
|
32 |
contextMenuIcon,
|
|
|
33 |
generateImageIcon,
|
|
|
34 |
generateTextIcon
|
|
|
35 |
} from './common';
|
|
|
36 |
import GenerateImage from './generateimage';
|
|
|
37 |
import GenerateText from './generatetext';
|
|
|
38 |
import {isTextAllowed, isImageAllowed} from './options';
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Get the setup function for the buttons.
|
|
|
42 |
*
|
|
|
43 |
* This is performed in an async function which ultimately returns the registration function as the
|
|
|
44 |
* Tiny.AddOnManager.Add() function does not support async functions.
|
|
|
45 |
*
|
|
|
46 |
* @returns {function} The registration function to call within the Plugin.add function.
|
|
|
47 |
*/
|
|
|
48 |
export const getSetup = async() => {
|
|
|
49 |
const [
|
|
|
50 |
contextMenuIconText,
|
|
|
51 |
generateImageIconText,
|
|
|
52 |
generateTextIconText,
|
|
|
53 |
contextMenuIconImage,
|
|
|
54 |
generateImageIconImage,
|
|
|
55 |
generateTextIconImage,
|
|
|
56 |
] = await Promise.all([
|
|
|
57 |
getString('generatecontent', placement),
|
|
|
58 |
getString('generateimage', placement),
|
|
|
59 |
getString('generatetext', placement),
|
|
|
60 |
getButtonImage(contextMenuIcon, component),
|
|
|
61 |
getButtonImage(generateImageIcon, component),
|
|
|
62 |
getButtonImage(generateTextIcon, component),
|
|
|
63 |
]);
|
|
|
64 |
|
|
|
65 |
const getToolbarParts = (editor) => {
|
|
|
66 |
// Add the context menu button.
|
|
|
67 |
const menuItems = [];
|
|
|
68 |
|
|
|
69 |
if (isTextAllowed(editor)) {
|
|
|
70 |
const textGenerator = new GenerateText(editor);
|
|
|
71 |
|
|
|
72 |
editor.ui.registry.addMenuItem(generateTextName, {
|
|
|
73 |
icon: generateTextIcon,
|
|
|
74 |
text: generateTextIconText,
|
|
|
75 |
onAction: () => {
|
|
|
76 |
textGenerator.displayContentModal(editor);
|
|
|
77 |
},
|
|
|
78 |
});
|
|
|
79 |
|
|
|
80 |
menuItems.push(generateTextName);
|
|
|
81 |
|
|
|
82 |
if (!isImageAllowed(editor)) {
|
|
|
83 |
// Only text generation is allowed.
|
|
|
84 |
return {
|
|
|
85 |
menuItems,
|
|
|
86 |
singleButton: generateTextIcon,
|
|
|
87 |
singleButtonTitle: generateTextIconText,
|
|
|
88 |
generator: textGenerator,
|
|
|
89 |
};
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
if (isImageAllowed(editor)) {
|
|
|
94 |
const imageGenerator = new GenerateImage(editor);
|
|
|
95 |
|
|
|
96 |
editor.ui.registry.addMenuItem(generateImageName, {
|
|
|
97 |
icon: generateImageIcon,
|
|
|
98 |
text: generateImageIconText,
|
|
|
99 |
onAction: () => {
|
|
|
100 |
imageGenerator.displayContentModal(editor);
|
|
|
101 |
},
|
|
|
102 |
});
|
|
|
103 |
|
|
|
104 |
menuItems.push(generateImageName);
|
|
|
105 |
|
|
|
106 |
if (!isTextAllowed(editor)) {
|
|
|
107 |
// Only image generation is allowed.
|
|
|
108 |
return {
|
|
|
109 |
menuItems,
|
|
|
110 |
singleButton: generateImageIcon,
|
|
|
111 |
singleButtonTitle: generateImageIconText,
|
|
|
112 |
generator: imageGenerator,
|
|
|
113 |
};
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
return {
|
|
|
118 |
menuItems,
|
|
|
119 |
singleButton: null,
|
|
|
120 |
singleButtonTitle: null,
|
|
|
121 |
generator: null,
|
|
|
122 |
};
|
|
|
123 |
};
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
return (editor) => {
|
|
|
127 |
if (!isTextAllowed(editor) && !isImageAllowed(editor)) {
|
|
|
128 |
return;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
// Register the icon SVG files as an icon suitable for use in TinyMCE toolbars and buttons.
|
|
|
132 |
editor.ui.registry.addIcon(contextMenuIcon, contextMenuIconImage.html);
|
|
|
133 |
editor.ui.registry.addIcon(generateImageIcon, generateImageIconImage.html);
|
|
|
134 |
editor.ui.registry.addIcon(generateTextIcon, generateTextIconImage.html);
|
|
|
135 |
|
|
|
136 |
const {menuItems, singleButton, singleButtonTitle, generator} = getToolbarParts(editor);
|
|
|
137 |
|
|
|
138 |
addDynamicToolbarMenu(
|
|
|
139 |
editor,
|
|
|
140 |
menuItems,
|
|
|
141 |
contextMenuName,
|
|
|
142 |
contextMenuIcon,
|
|
|
143 |
contextMenuIconText,
|
|
|
144 |
singleButton,
|
|
|
145 |
singleButtonTitle,
|
|
|
146 |
() => {
|
|
|
147 |
generator.displayContentModal(editor);
|
|
|
148 |
},
|
|
|
149 |
);
|
|
|
150 |
};
|
|
|
151 |
};
|