forked from ajaxorg/ace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
173 lines (145 loc) · 5.05 KB
/
config.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"no use strict";
var lang = require("./lib/lang");
var net = require("./lib/net");
var dom = require("./lib/dom");
var AppConfig = require("./lib/app_config").AppConfig;
module.exports = exports = new AppConfig();
var options = {
packaged: false,
workerPath: null,
modePath: null,
themePath: null,
basePath: "",
suffix: ".js",
$moduleUrls: {},
loadWorkerFromBlob: true,
sharedPopups: false,
useStrictCSP: null
};
exports.get = function(key) {
if (!options.hasOwnProperty(key))
throw new Error("Unknown config key: " + key);
return options[key];
};
exports.set = function(key, value) {
if (options.hasOwnProperty(key))
options[key] = value;
else if (this.setDefaultValue("", key, value) == false)
throw new Error("Unknown config key: " + key);
if (key == "useStrictCSP")
dom.useStrictCSP(value);
};
exports.all = function() {
return lang.copyObject(options);
};
exports.$modes = {};
// module loading
exports.moduleUrl = function(name, component) {
if (options.$moduleUrls[name])
return options.$moduleUrls[name];
var parts = name.split("/");
component = component || parts[parts.length - 2] || "";
// todo make this configurable or get rid of '-'
var sep = component == "snippets" ? "/" : "-";
var base = parts[parts.length - 1];
if (component == "worker" && sep == "-") {
var re = new RegExp("^" + component + "[\\-_]|[\\-_]" + component + "$", "g");
base = base.replace(re, "");
}
if ((!base || base == component) && parts.length > 1)
base = parts[parts.length - 2];
var path = options[component + "Path"];
if (path == null) {
path = options.basePath;
} else if (sep == "/") {
component = sep = "";
}
if (path && path.slice(-1) != "/")
path += "/";
return path + component + sep + base + this.get("suffix");
};
exports.setModuleUrl = function(name, subst) {
return options.$moduleUrls[name] = subst;
};
var loader = function(moduleName, cb) {
if (moduleName === "ace/theme/textmate" || moduleName === "./theme/textmate")
return cb(null, require("./theme/textmate"));
if (customLoader)
return customLoader(moduleName, cb);
console.error("loader is not configured");
};
var customLoader;
exports.setLoader = function(cb) {
customLoader = cb;
};
exports.dynamicModules = Object.create(null);
exports.$loading = {};
exports.$loaded = {};
exports.loadModule = function(moduleName, onLoad) {
var loadedModule, moduleType;
if (Array.isArray(moduleName)) {
moduleType = moduleName[0];
moduleName = moduleName[1];
}
var load = function (module) {
// require(moduleName) can return empty object if called after require([moduleName], callback)
if (module && !exports.$loading[moduleName]) return onLoad && onLoad(module);
if (!exports.$loading[moduleName]) exports.$loading[moduleName] = [];
exports.$loading[moduleName].push(onLoad);
if (exports.$loading[moduleName].length > 1) return;
var afterLoad = function() {
loader(moduleName, function(err, module) {
if (module) exports.$loaded[moduleName] = module;
exports._emit("load.module", {name: moduleName, module: module});
var listeners = exports.$loading[moduleName];
exports.$loading[moduleName] = null;
listeners.forEach(function(onLoad) {
onLoad && onLoad(module);
});
});
};
if (!exports.get("packaged")) return afterLoad();
net.loadScript(exports.moduleUrl(moduleName, moduleType), afterLoad);
reportErrorIfPathIsNotConfigured();
};
if (exports.dynamicModules[moduleName]) {
exports.dynamicModules[moduleName]().then(function (module) {
if (module.default) {
load(module.default);
}
else {
load(module);
}
});
} else {
// backwards compatibility for node and packaged version
try {
loadedModule = this.$require(moduleName);
} catch (e) {}
load(loadedModule || exports.$loaded[moduleName]);
}
};
exports.$require = function(moduleName) {
if (typeof module.require == "function") {
var req = "require";
return module[req](moduleName);
}
};
exports.setModuleLoader = function (moduleName, onLoad) {
exports.dynamicModules[moduleName] = onLoad;
};
var reportErrorIfPathIsNotConfigured = function() {
if (
!options.basePath && !options.workerPath
&& !options.modePath && !options.themePath
&& !Object.keys(options.$moduleUrls).length
) {
console.error(
"Unable to infer path to ace from script src,",
"use ace.config.set('basePath', 'path') to enable dynamic loading of modes and themes",
"or with webpack use ace/webpack-resolver"
);
reportErrorIfPathIsNotConfigured = function() {};
}
};
exports.version = "1.23.1";