mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-04 20:44:44 -05:00
182 lines
5.1 KiB
JavaScript
182 lines
5.1 KiB
JavaScript
|
"no use strict";
|
||
|
;(function(window) {
|
||
|
if (typeof window.window != "undefined" && window.document) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
window.console = function() {
|
||
|
var msgs = Array.prototype.slice.call(arguments, 0);
|
||
|
postMessage({type: "log", data: msgs});
|
||
|
};
|
||
|
window.console.error =
|
||
|
window.console.warn =
|
||
|
window.console.log =
|
||
|
window.console.trace = window.console;
|
||
|
|
||
|
window.window = window;
|
||
|
window.ace = window;
|
||
|
|
||
|
window.onerror = function(message, file, line, col, err) {
|
||
|
console.error("Worker " + err.stack);
|
||
|
};
|
||
|
|
||
|
window.normalizeModule = function(parentId, moduleName) {
|
||
|
// normalize plugin requires
|
||
|
if (moduleName.indexOf("!") !== -1) {
|
||
|
var chunks = moduleName.split("!");
|
||
|
return window.normalizeModule(parentId, chunks[0]) + "!" + window.normalizeModule(parentId, chunks[1]);
|
||
|
}
|
||
|
// normalize relative requires
|
||
|
if (moduleName.charAt(0) == ".") {
|
||
|
var base = parentId.split("/").slice(0, -1).join("/");
|
||
|
moduleName = (base ? base + "/" : "") + moduleName;
|
||
|
|
||
|
while(moduleName.indexOf(".") !== -1 && previous != moduleName) {
|
||
|
var previous = moduleName;
|
||
|
moduleName = moduleName.replace(/^\.\//, "").replace(/\/\.\//, "/").replace(/[^\/]+\/\.\.\//, "");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return moduleName;
|
||
|
};
|
||
|
|
||
|
window.require = function(parentId, id) {
|
||
|
if (!id) {
|
||
|
id = parentId
|
||
|
parentId = null;
|
||
|
}
|
||
|
if (!id.charAt)
|
||
|
throw new Error("worker.js require() accepts only (parentId, id) as arguments");
|
||
|
|
||
|
id = window.normalizeModule(parentId, id);
|
||
|
|
||
|
var module = window.require.modules[id];
|
||
|
if (module) {
|
||
|
if (!module.initialized) {
|
||
|
module.initialized = true;
|
||
|
module.exports = module.factory().exports;
|
||
|
}
|
||
|
return module.exports;
|
||
|
}
|
||
|
|
||
|
var chunks = id.split("/");
|
||
|
if (!window.require.tlns)
|
||
|
return console.log("unable to load " + id);
|
||
|
chunks[0] = window.require.tlns[chunks[0]] || chunks[0];
|
||
|
var path = chunks.join("/") + ".js";
|
||
|
|
||
|
window.require.id = id;
|
||
|
importScripts(path);
|
||
|
return window.require(parentId, id);
|
||
|
};
|
||
|
window.require.modules = {};
|
||
|
window.require.tlns = {};
|
||
|
|
||
|
window.define = function(id, deps, factory) {
|
||
|
if (arguments.length == 2) {
|
||
|
factory = deps;
|
||
|
if (typeof id != "string") {
|
||
|
deps = id;
|
||
|
id = window.require.id;
|
||
|
}
|
||
|
} else if (arguments.length == 1) {
|
||
|
factory = id;
|
||
|
deps = []
|
||
|
id = window.require.id;
|
||
|
}
|
||
|
|
||
|
if (!deps.length)
|
||
|
// If there is no dependencies, we inject 'require', 'exports' and
|
||
|
// 'module' as dependencies, to provide CommonJS compatibility.
|
||
|
deps = ['require', 'exports', 'module']
|
||
|
|
||
|
if (id.indexOf("text!") === 0)
|
||
|
return;
|
||
|
|
||
|
var req = function(childId) {
|
||
|
return window.require(id, childId);
|
||
|
};
|
||
|
|
||
|
window.require.modules[id] = {
|
||
|
exports: {},
|
||
|
factory: function() {
|
||
|
var module = this;
|
||
|
var returnExports = factory.apply(this, deps.map(function(dep) {
|
||
|
switch(dep) {
|
||
|
// Because 'require', 'exports' and 'module' aren't actual
|
||
|
// dependencies, we must handle them seperately.
|
||
|
case 'require': return req
|
||
|
case 'exports': return module.exports
|
||
|
case 'module': return module
|
||
|
// But for all other dependencies, we can just go ahead and
|
||
|
// require them.
|
||
|
default: return req(dep)
|
||
|
}
|
||
|
}));
|
||
|
if (returnExports)
|
||
|
module.exports = returnExports;
|
||
|
return module;
|
||
|
}
|
||
|
};
|
||
|
};
|
||
|
window.define.amd = {}
|
||
|
|
||
|
window.initBaseUrls = function initBaseUrls(topLevelNamespaces) {
|
||
|
require.tlns = topLevelNamespaces;
|
||
|
}
|
||
|
|
||
|
window.initSender = function initSender() {
|
||
|
|
||
|
var EventEmitter = window.require("ace/lib/event_emitter").EventEmitter;
|
||
|
var oop = window.require("ace/lib/oop");
|
||
|
|
||
|
var Sender = function() {};
|
||
|
|
||
|
(function() {
|
||
|
|
||
|
oop.implement(this, EventEmitter);
|
||
|
|
||
|
this.callback = function(data, callbackId) {
|
||
|
postMessage({
|
||
|
type: "call",
|
||
|
id: callbackId,
|
||
|
data: data
|
||
|
});
|
||
|
};
|
||
|
|
||
|
this.emit = function(name, data) {
|
||
|
postMessage({
|
||
|
type: "event",
|
||
|
name: name,
|
||
|
data: data
|
||
|
});
|
||
|
};
|
||
|
|
||
|
}).call(Sender.prototype);
|
||
|
|
||
|
return new Sender();
|
||
|
}
|
||
|
|
||
|
window.main = null;
|
||
|
window.sender = null;
|
||
|
|
||
|
window.onmessage = function(e) {
|
||
|
var msg = e.data;
|
||
|
if (msg.command) {
|
||
|
if (main[msg.command])
|
||
|
main[msg.command].apply(main, msg.args);
|
||
|
else
|
||
|
throw new Error("Unknown command:" + msg.command);
|
||
|
}
|
||
|
else if (msg.init) {
|
||
|
initBaseUrls(msg.tlns);
|
||
|
require("ace/lib/es5-shim");
|
||
|
sender = initSender();
|
||
|
var clazz = require(msg.module)[msg.classname];
|
||
|
main = new clazz(sender);
|
||
|
}
|
||
|
else if (msg.event && sender) {
|
||
|
sender._emit(msg.event, msg.data);
|
||
|
}
|
||
|
};
|
||
|
})(this);
|