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 commands.
|
|
|
18 |
*
|
|
|
19 |
* @module tiny_media/commands
|
|
|
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 {getStrings} from 'core/str';
|
|
|
25 |
import {
|
|
|
26 |
component,
|
|
|
27 |
imageButtonName,
|
|
|
28 |
videoButtonName,
|
|
|
29 |
mediaManagerButtonName
|
|
|
30 |
} from './common';
|
|
|
31 |
import MediaImage from './image';
|
|
|
32 |
import MediaEmbed from './embed';
|
|
|
33 |
import MediaManager from './manager';
|
|
|
34 |
import {getButtonImage} from 'editor_tiny/utils';
|
|
|
35 |
|
|
|
36 |
const isImage = (node) => node.nodeName.toLowerCase() === 'img';
|
|
|
37 |
const isVideo = (node) => node.nodeName.toLowerCase() === 'video' || node.nodeName.toLowerCase() === 'audio';
|
|
|
38 |
|
|
|
39 |
const registerImageCommand = (editor, imageButtonText) => {
|
|
|
40 |
const imageIcon = 'image';
|
|
|
41 |
const handleImageAction = () => {
|
|
|
42 |
const mediaImage = new MediaImage(editor);
|
|
|
43 |
mediaImage.displayDialogue();
|
|
|
44 |
};
|
|
|
45 |
|
|
|
46 |
// Register the Menu Button as a toggle.
|
|
|
47 |
// This means that when highlighted over an existing Media Image element it will show as toggled on.
|
|
|
48 |
editor.ui.registry.addToggleButton(imageButtonName, {
|
|
|
49 |
icon: imageIcon,
|
|
|
50 |
tooltip: imageButtonText,
|
|
|
51 |
onAction: handleImageAction,
|
|
|
52 |
onSetup: api => {
|
|
|
53 |
return editor.selection.selectorChangedWithUnbind(
|
|
|
54 |
'img:not([data-mce-object]):not([data-mce-placeholder]),figure.image',
|
|
|
55 |
api.setActive
|
|
|
56 |
).unbind;
|
|
|
57 |
}
|
|
|
58 |
});
|
|
|
59 |
|
|
|
60 |
editor.ui.registry.addMenuItem(imageButtonName, {
|
|
|
61 |
icon: imageIcon,
|
|
|
62 |
text: imageButtonText,
|
|
|
63 |
onAction: handleImageAction,
|
|
|
64 |
});
|
|
|
65 |
|
|
|
66 |
editor.ui.registry.addContextToolbar(imageButtonName, {
|
|
|
67 |
predicate: isImage,
|
|
|
68 |
items: imageButtonName,
|
|
|
69 |
position: 'node',
|
|
|
70 |
scope: 'node'
|
|
|
71 |
});
|
|
|
72 |
|
|
|
73 |
editor.ui.registry.addContextMenu(imageButtonName, {
|
|
|
74 |
update: isImage,
|
|
|
75 |
});
|
|
|
76 |
};
|
|
|
77 |
|
|
|
78 |
const registerEmbedCommand = (editor, videoButtonText) => {
|
|
|
79 |
const videoIcon = 'embed';
|
|
|
80 |
const handleEmbedAction = () => {
|
|
|
81 |
const mediaImage = new MediaEmbed(editor);
|
|
|
82 |
mediaImage.displayDialogue();
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
// Register the Menu Button as a toggle.
|
|
|
86 |
// This means that when highlighted over an existing Media Video element it will show as toggled on.
|
|
|
87 |
editor.ui.registry.addToggleButton(videoButtonName, {
|
|
|
88 |
icon: videoIcon,
|
|
|
89 |
tooltip: videoButtonText,
|
|
|
90 |
onAction: handleEmbedAction,
|
|
|
91 |
onSetup: api => {
|
|
|
92 |
return editor.selection.selectorChangedWithUnbind(
|
|
|
93 |
'video:not([data-mce-object]):not([data-mce-placeholder]),' +
|
|
|
94 |
'audio:not([data-mce-object]):not([data-mce-placeholder])',
|
|
|
95 |
api.setActive
|
|
|
96 |
).unbind;
|
|
|
97 |
}
|
|
|
98 |
});
|
|
|
99 |
|
|
|
100 |
editor.ui.registry.addMenuItem(videoButtonName, {
|
|
|
101 |
icon: videoIcon,
|
|
|
102 |
text: videoButtonText,
|
|
|
103 |
onAction: handleEmbedAction,
|
|
|
104 |
});
|
|
|
105 |
|
|
|
106 |
editor.ui.registry.addContextMenu(videoButtonName, {
|
|
|
107 |
update: isVideo,
|
|
|
108 |
});
|
|
|
109 |
|
|
|
110 |
editor.ui.registry.addContextToolbar(videoButtonName, {
|
|
|
111 |
predicate: isVideo,
|
|
|
112 |
items: videoButtonName,
|
|
|
113 |
position: 'node',
|
|
|
114 |
scope: 'node'
|
|
|
115 |
});
|
|
|
116 |
|
|
|
117 |
};
|
|
|
118 |
|
|
|
119 |
const registerManagerCommand = (editor, mediaManagerButtonText, mediaManagerButtonImage) => {
|
|
|
120 |
const mediaManagerIcon = 'filemanager';
|
|
|
121 |
const handleMediaManager = () => {
|
|
|
122 |
const mediaManager = new MediaManager(editor);
|
|
|
123 |
mediaManager.displayDialogue();
|
|
|
124 |
};
|
|
|
125 |
|
|
|
126 |
// Register the Menu Button as a toggle.
|
|
|
127 |
editor.ui.registry.addIcon(mediaManagerIcon, mediaManagerButtonImage.html);
|
|
|
128 |
editor.ui.registry.addButton(mediaManagerButtonName, {
|
|
|
129 |
icon: mediaManagerIcon,
|
|
|
130 |
tooltip: mediaManagerButtonText,
|
|
|
131 |
onAction: () => {
|
|
|
132 |
handleMediaManager();
|
|
|
133 |
}
|
|
|
134 |
});
|
|
|
135 |
|
|
|
136 |
editor.ui.registry.addMenuItem(mediaManagerButtonName, {
|
|
|
137 |
icon: mediaManagerIcon,
|
|
|
138 |
text: mediaManagerButtonText,
|
|
|
139 |
onAction: () => {
|
|
|
140 |
handleMediaManager();
|
|
|
141 |
}
|
|
|
142 |
});
|
|
|
143 |
};
|
|
|
144 |
|
|
|
145 |
export const getSetup = async() => {
|
|
|
146 |
const [
|
|
|
147 |
imageButtonText,
|
|
|
148 |
mediaButtonText,
|
|
|
149 |
mediaManagerButtonText
|
|
|
150 |
] = await getStrings(['imagebuttontitle', 'mediabuttontitle', 'mediamanagerbuttontitle'].map((key) => ({key, component})));
|
|
|
151 |
|
|
|
152 |
const [
|
|
|
153 |
mediaManagerButtonImage,
|
|
|
154 |
] = await Promise.all([
|
|
|
155 |
getButtonImage('filemanager', component)
|
|
|
156 |
]);
|
|
|
157 |
|
|
|
158 |
// Note: The function returned here must be synchronous and cannot use promises.
|
|
|
159 |
// All promises must be resolved prior to returning the function.
|
|
|
160 |
return (editor) => {
|
|
|
161 |
registerImageCommand(editor, imageButtonText);
|
|
|
162 |
registerEmbedCommand(editor, mediaButtonText);
|
|
|
163 |
registerManagerCommand(editor, mediaManagerButtonText, mediaManagerButtonImage);
|
|
|
164 |
};
|
|
|
165 |
};
|