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 |
* Option helper for TinyMCE Editor Manager.
|
|
|
18 |
*
|
|
|
19 |
* @module editor_tiny/options
|
|
|
20 |
* @copyright 2022 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
const optionContextId = 'moodle:contextid';
|
|
|
25 |
const optionDraftItemId = 'moodle:draftitemid';
|
|
|
26 |
const filePickers = 'moodle:filepickers';
|
|
|
27 |
const optionsMoodleLang = 'moodle:language';
|
|
|
28 |
const currentLanguage = 'moodle:currentLanguage';
|
|
|
29 |
const optionPlaceholderSelectors = 'moodle:placeholderSelectors';
|
|
|
30 |
|
|
|
31 |
export const register = (editor, options) => {
|
|
|
32 |
const registerOption = editor.options.register;
|
|
|
33 |
const setOption = editor.options.set;
|
|
|
34 |
|
|
|
35 |
registerOption(optionContextId, {
|
|
|
36 |
processor: 'number',
|
|
|
37 |
"default": 0,
|
|
|
38 |
});
|
|
|
39 |
setOption(optionContextId, options.context);
|
|
|
40 |
|
|
|
41 |
registerOption(filePickers, {
|
|
|
42 |
processor: 'object',
|
|
|
43 |
"default": {},
|
|
|
44 |
});
|
|
|
45 |
setOption(filePickers, Object.assign({}, options.filepicker));
|
|
|
46 |
|
|
|
47 |
registerOption(optionDraftItemId, {
|
|
|
48 |
processor: 'number',
|
|
|
49 |
"default": 0,
|
|
|
50 |
});
|
|
|
51 |
setOption(optionDraftItemId, options.draftitemid);
|
|
|
52 |
|
|
|
53 |
registerOption(currentLanguage, {
|
|
|
54 |
processor: 'string',
|
|
|
55 |
"default": 'en',
|
|
|
56 |
});
|
|
|
57 |
setOption(currentLanguage, options.currentLanguage);
|
|
|
58 |
|
|
|
59 |
// This is primarily used by the media plugin, but it may be re-used elsewhere so is included here as it is large.
|
|
|
60 |
registerOption(optionsMoodleLang, {
|
|
|
61 |
processor: 'object',
|
|
|
62 |
"default": {},
|
|
|
63 |
});
|
|
|
64 |
setOption(optionsMoodleLang, options.language);
|
|
|
65 |
|
|
|
66 |
registerOption(optionPlaceholderSelectors, {
|
|
|
67 |
processor: 'array',
|
|
|
68 |
"default": [],
|
|
|
69 |
});
|
|
|
70 |
setOption(optionPlaceholderSelectors, options.placeholderSelectors);
|
|
|
71 |
};
|
|
|
72 |
|
|
|
73 |
export const getContextId = (editor) => editor.options.get(optionContextId);
|
|
|
74 |
export const getDraftItemId = (editor) => editor.options.get(optionDraftItemId);
|
|
|
75 |
export const getFilepickers = (editor) => editor.options.get(filePickers);
|
|
|
76 |
export const getFilePicker = (editor, type) => getFilepickers(editor)[type];
|
|
|
77 |
export const getMoodleLang = (editor) => editor.options.get(optionsMoodleLang);
|
|
|
78 |
export const getCurrentLanguage = (editor) => editor.options.get(currentLanguage);
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Get a set of namespaced options for all defined plugins.
|
|
|
82 |
*
|
|
|
83 |
* @param {object} options
|
|
|
84 |
* @returns {object}
|
|
|
85 |
*/
|
|
|
86 |
export const getInitialPluginConfiguration = (options) => {
|
|
|
87 |
const config = {};
|
|
|
88 |
|
|
|
89 |
Object.entries(options.plugins).forEach(([pluginName, pluginConfig]) => {
|
|
|
90 |
const values = Object.entries(pluginConfig.config ?? {});
|
|
|
91 |
values.forEach(([optionName, value]) => {
|
|
|
92 |
config[getPluginOptionName(pluginName, optionName)] = value;
|
|
|
93 |
});
|
|
|
94 |
});
|
|
|
95 |
|
|
|
96 |
return config;
|
|
|
97 |
};
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Get the namespaced option name for a plugin.
|
|
|
101 |
*
|
|
|
102 |
* @param {string} pluginName
|
|
|
103 |
* @param {string} optionName
|
|
|
104 |
* @returns {string}
|
|
|
105 |
*/
|
|
|
106 |
export const getPluginOptionName = (pluginName, optionName) => `${pluginName}:${optionName}`;
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Get the placeholder selectors.
|
|
|
110 |
*
|
|
|
111 |
* @param {TinyMCE} editor
|
|
|
112 |
* @returns {array}
|
|
|
113 |
*/
|
|
|
114 |
export const getPlaceholderSelectors = (editor) => editor.options.get(optionPlaceholderSelectors);
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Register placeholder selectos.
|
|
|
118 |
*
|
|
|
119 |
* @param {TinyMCE} editor
|
|
|
120 |
* @param {array} selectors
|
|
|
121 |
*/
|
|
|
122 |
export const registerPlaceholderSelectors = (editor, selectors) => {
|
|
|
123 |
if (selectors.length) {
|
|
|
124 |
let existingData = getPlaceholderSelectors(editor);
|
|
|
125 |
existingData = existingData.concat(selectors);
|
|
|
126 |
editor.options.set(optionPlaceholderSelectors, existingData);
|
|
|
127 |
}
|
|
|
128 |
};
|