UNPKG

@tinymce/tinymce-react

Version:

Official TinyMCE React Component

69 lines (68 loc) 2.2 kB
"use strict"; /** * Copyright (c) 2017-present, Ephox, Inc. * * This source code is licensed under the Apache 2 license found in the * LICENSE file in the root directory of this source tree. * */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ScriptLoader = void 0; var Utils_1 = require("./Utils"); var createState = function () { return ({ listeners: [], scriptId: Utils_1.uuid('tiny-script'), scriptLoading: false, scriptLoaded: false }); }; var CreateScriptLoader = function () { var state = createState(); var injectScriptTag = function (scriptId, doc, url, async, defer, callback) { var scriptTag = doc.createElement('script'); scriptTag.referrerPolicy = 'origin'; scriptTag.type = 'application/javascript'; scriptTag.id = scriptId; scriptTag.src = url; scriptTag.async = async; scriptTag.defer = defer; var handler = function () { scriptTag.removeEventListener('load', handler); callback(); }; scriptTag.addEventListener('load', handler); if (doc.head) { doc.head.appendChild(scriptTag); } }; var load = function (doc, url, async, defer, delay, callback) { var scriptTagInjection = function () { return injectScriptTag(state.scriptId, doc, url, async, defer, function () { state.listeners.forEach(function (fn) { return fn(); }); state.scriptLoaded = true; }); }; if (state.scriptLoaded) { callback(); } else { state.listeners.push(callback); if (!state.scriptLoading) { state.scriptLoading = true; if (delay > 0) { setTimeout(scriptTagInjection, delay); } else { scriptTagInjection(); } } } }; // Only to be used by tests. var reinitialize = function () { state = createState(); }; return { load: load, reinitialize: reinitialize }; }; var ScriptLoader = CreateScriptLoader(); exports.ScriptLoader = ScriptLoader;