Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 Premium plugin for Moodle.
18
 *
19
 * @module      tiny_premium/plugin
20
 * @copyright   2023 David Woloszyn <david.woloszyn@moodle.com>
21
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import {getTinyMCE} from 'editor_tiny/loader';
25
import {getPluginMetadata} from 'editor_tiny/utils';
26
import {component, pluginName} from 'tiny_premium/common';
27
import * as Configuration from 'tiny_premium/configuration';
28
import {getApiKey} from 'tiny_premium/external';
29
import Config from 'core/config';
30
 
31
const currentContextId = Config.contextid;
32
 
33
// eslint-disable-next-line no-async-promise-executor
34
export default new Promise(async(resolve) => {
35
    const [
36
        tinyMCE,
37
        pluginMetadata,
38
        externalData
39
    ] = await Promise.all([
40
        getTinyMCE(),
41
        getPluginMetadata(component, pluginName),
42
        getApiKey(currentContextId)
43
    ]);
44
 
45
    tinyMCE.PluginManager.add(`${component}/plugin`, () => {
46
        return pluginMetadata;
47
    });
48
 
49
    // Load the Tiny Premium script using the provided API key.
50
    await getTinyPremium(tinyMCE, externalData.apikey);
51
 
52
    resolve([`${component}/plugin`, Configuration]);
53
});
54
 
55
let tinyPremiumPromise;
56
 
57
/**
58
 * Promise for Tiny Premium plugins script.
59
 *
60
 * @param {TinyMCE} tinyMCE
61
 * @param {string} apikey
62
 * @return {Promise}
63
 */
64
const getTinyPremium = (tinyMCE, apikey) => {
65
    if (tinyPremiumPromise) {
66
        return tinyPremiumPromise;
67
    }
68
 
69
    tinyPremiumPromise = new Promise((resolve, reject) => {
70
        const head = document.querySelector('head');
71
        const script = document.createElement('script');
72
        const tinyVersion = `${tinyMCE.majorVersion}.${tinyMCE.minorVersion}`;
73
 
74
        script.dataset.tinymce = 'premium';
75
        script.src = `https://cdn.tiny.cloud/1/${apikey}/tinymce/${tinyVersion}/plugins.min.js`;
76
        script.referrerpolicy = "origin";
77
 
78
        script.addEventListener('load', () => {
79
            resolve();
80
        }, false);
81
 
82
        script.addEventListener('error', (err) => {
83
            reject(err);
84
        }, false);
85
 
86
        head.append(script);
87
    });
88
 
89
    return tinyPremiumPromise;
90
};