summaryrefslogtreecommitdiff
path: root/plugins/N1ED-editor/plugin.js
blob: 47a6ccf11bdbe43c613f9932c1f44463331a735b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*!
 * Add-on for including N1ED into your CKEditor 4
 * Developer: N1ED
 * Website: https://n1ed.com/
 * License: GPL v3
 */


/*
 *   HOW TO INSTALL THIS ADD-ON
 *
 *   1. Copy the plugin into
 *
 *        ckeditor/
 *          plugins/
 *            N1ED-editor/
 *              plugin.js
 *
 *   2. Add "N1ED-editor" into "extraPlugins" config option
 *   3. Done
 *
 *
 *   VISUAL CONFIGURATION
 *
 *   Configure N1ED visually using the Dashboard:
 *
 *       https://n1ed.com/dashboard
 *
 *   Get there an API key to link with your installation.
 *   Specify it as a CKEditor parameter:
 *
 *       apiKey: "..."
 */

(function() {

	var PLUGIN_NAME = "N1ED-editor";
	var DEFAULT_API_KEY = "N1ED24RR1234123412341234";

	window.n1edPluginVersion = 202409001;
	window.n1edPluginEditor = "ckeditor";

	function get(varName, defaultValue) {
		if (window[varName] !== undefined && window[varName] !== "-")
			return window[varName];
		else
			return defaultValue;
	}

	var apiKey = CKEDITOR.config.apiKey;
	if (!apiKey) {
		for (var i = 0; i < Object.keys(CKEDITOR.instances).length; i++) {
			var id = Object.keys(CKEDITOR.instances)[i];
			if (CKEDITOR.instances[id].config.apiKey)
				apiKey = CKEDITOR.instances[id].config.apiKey;
			if (!apiKey) {
				if (CKEDITOR.instances[id].config.Flmngr && CKEDITOR.instances[id].config.Flmngr.apiKey)
					apiKey = CKEDITOR.instances[id].config.Flmngr.apiKey;
			}
		}
	}

	window.CKEDITOR_OVERRIDE_API_KEY_PARAM = "OVERRIDE_API_KEY";
	apiKey = window.OVERRIDE_API_KEY || window.N1ED_API_KEY || apiKey || DEFAULT_API_KEY;

	var version = get("N1ED_VERSION", "latest");
	var n1edPrefix = get("N1ED_PREFIX", null);
	var n1edHttps = get("N1ED_HTTPS", true);

	var protocol = n1edHttps ? "https" : "http";

	var host = (n1edPrefix ? (n1edPrefix + ".") : "") + "cloud.n1ed.com";
	var urlPlugin = protocol + "://" + host + "/a/" + apiKey + "/plugins/N1EDEco/plugin.js";

	var oldScriptLoaderLoad = window.CKEDITOR.scriptLoader.load;
	window.CKEDITOR.scriptLoader.load = function(scriptUrl, callback, scope, showBusy) {
		return oldScriptLoaderLoad.apply(scope, [
			scriptUrl,
			function(completed, failed) {
				if (!!failed && failed.length) {
					console.error('[CKEDITOR.resourceManager.load] Resource was not found at "' + failed[0]);

					for (var i = 0; i < failed.length; i++) {
						var m = failed[i].match(/^https?:\/\/cloud\.n1ed\.com\/cdn\/[^/]+?\/[^/]+?\/ckeditor\/plugins\/([^/]+?)\/plugin\.js.*?/);

						if (!m) {
							m = failed[i].match(/^https?:\/\/cloud\.n1ed\.com\/.*\/plugin\.js.*?/);
						}
						if (m != null) {
							var pluginName = m[1];
							console.log("Using a stub for '" + pluginName + "' plugin");
							CKEDITOR.plugins.add(pluginName, {});
							if (!completed)
								completed = [];
							completed.push(failed[i]);
						}
					}
					failed = [];
				}

				var elsLoaders = document.querySelectorAll(".n1ed_loading");
				if (!!elsLoaders)
					for (var i = 0; i < elsLoaders.length; i++)
						elsLoaders.item(i).parentElement.removeChild(elsLoaders.item(i));

				callback.apply(scope, [completed, failed]);
			},
			scope,
			showBusy
		]);
	};

	CKEDITOR.plugins.addExternal("N1EDEco", urlPlugin);
	CKEDITOR.plugins.add(PLUGIN_NAME, {
		"requires": ["N1EDEco"], // We can not move Ecosystem in this file due to we need to dynamically
		// embed configuration from your Dashboard into it.
		// So Ecosystem add-on can be loaded only from CDN
	});

	function applyPatch() {
		window.applyPatch && window.applyPatch(PLUGIN_NAME, window.n1edPluginVersion, apiKey);
	}
	var script = document.createElement('script');
	script.type = "text/javascript";
	script.src = protocol + "://" + host + "/static/pluginPatch.js";
	document.head.appendChild(script);
	if (script.readyState) {  //IE
		script.onreadystatechange = function() {
			if (script.readyState === "loaded" || script.readyState === "complete") {
				script.onreadystatechange = null;
				applyPatch();
			}
		};
	} else {  //Others
		script.onload = function() {
			applyPatch();
		};
	}

})()