Merge pull request +15875 from c9/ide-offline-demo

tweak offline demo
pull/468/merge
Harutyun Amirjanyan 2017-12-07 15:51:56 +04:00 zatwierdzone przez GitHub
commit 830b0f11ed
5 zmienionych plików z 187 dodań i 24 usunięć

Wyświetl plik

@ -88,12 +88,13 @@ define(function(require, exports, module) {
}
var count = KEYS.length;
var hasJSON = json;
if (!json)
json = {};
KEYS.forEach(function(type) {
if (!skipCloud[type] && json)
if (!skipCloud[type] && hasJSON)
return --count;
if (!json)
json = {};
if (type == "state" && debugMode)
return done(null, localStorage["debugState" + c9.projectName]);
fs.readFile(PATH[type], done);

Wyświetl plik

@ -17,6 +17,7 @@ define(function(require, exports, module) {
var fsData = {};
var Stream = require("stream").Stream;
var basename = require("path").basename;
var noop = function() { console.error("not implemented"); };
var silent = function() {};
var connection = {};
@ -87,6 +88,17 @@ define(function(require, exports, module) {
stream.emit("end");
}
function readBlob(blob, callback) {
var reader = new FileReader();
reader.onload = function() {
callback(null, reader.result);
};
reader.onerror = function(e) {
callback(e);
};
reader.readAsText(blob);
}
plugin.on("load", load);
plugin.on("unload", unload);
@ -104,8 +116,46 @@ define(function(require, exports, module) {
get baseUrl() { return vfsBaseUrl; },
get region() { return ""; },
rest: noop,
download: noop,
rest: function(path, options, callback) {
if (options.method == "PUT") {
if (typeof options.body == "object") {
return readBlob(options.body, function(e, value) {
if (e) return callback(e);
plugin.rest(path, {
method: "PUT",
body: value,
}, callback);
});
}
sendStream(options.body, function(err, stream) {
if (err) return callback(err);
plugin.mkfile(path, stream, callback);
});
} else if (options.method == "GET") {
var result = findNode(path);
setTimeout(function() {
callback(null, result);
}, 20);
}
},
download: function(path, filename, isfile) {
// TODO use jszip for folders
if (Array.isArray(path) && path.length > 1) {
return path.map(function(x) {
plugin.download(x);
});
}
var data = findNode(path);
if (typeof data !== "string")
return console.error("not implemented");
var a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([data], { type: "text/plain" }));
a.download = filename || basename(path);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
},
url: noop,
reconnect: noop,
@ -224,7 +274,7 @@ define(function(require, exports, module) {
var parts = to.split("/");
var toName = "!" + parts.pop();
var toParent = findNode(parts.join("/"));
var toParent = findNode(parts.join("/"), true);
parts = from.split("/");
var fromName = "!" + parts.pop();

Wyświetl plik

@ -47,6 +47,34 @@
<!--dev}-->
<script>
function previewHelper(options, imports, register) {
var fs = imports.fs;
var tabs = imports.tabManager;
var bc = new BroadcastChannel("livePreview");
bc.onmessage = function(e) {
var data = e.data;
var action = data && data.action;
if (action == "getFile") {
var path = data.path;
var tab = tabs.findTab(path);
var value = tab && tab.document.value;
if (value) return done(null, value);
fs.readFile(path, function(e, value) {
if (e) return done(e);
done(null, value);
});
}
function done(error, value) {
bc.postMessage({
action: "callback",
id: data.id,
value: value,
error: error && error.code,
});
}
};
register();
}
function offlineConfig(plugins) {
var excludes = [
"plugins/c9.ide.immediate/evaluators/debugnode",
@ -57,7 +85,6 @@
"plugins/c9.ide.find/find",
"plugins/c9.ide.terminal/link_handler",
"plugins/c9.ide.test/coverage",
"plugins/c9.ide.test/coverage",
"plugins/c9.ide.test/results",
"plugins/c9.ide.test/testrunner",
@ -67,19 +94,13 @@
"plugins/c9.ide.language.python/python",
"plugins/c9.ide.test/coverageview",
"plugins/c9.cli.bridge/bridge_commands",
"plugins/c9.ide.ace.keymaps/cli",
"plugins/c9.ide.configuration/configure",
"plugins/c9.ide.plugins/manager",
"plugins/c9.ide.ace.keymaps/keymaps",
"plugins/c9.ide.ace/themes",
]
];
plugins = plugins.filter(function(p) {
var packagePath = typeof p == "string" ? p : p.packagePath;
if (/\/c9.ide.run/.test(packagePath)) return false;
if (/\/c9.ide.collab/.test(packagePath)) return false;
if (/\/c9.ide.installer/.test(packagePath)) return false;
if (/\/c9.vfs.client/.test(packagePath)) return false;
if (/\/c9.ide.plugins/.test(packagePath)) return false;
if (/\/c9.ide.scm/.test(packagePath)) return false;
if (excludes.indexOf(packagePath) != -1) return false;
@ -105,10 +126,14 @@
});
}
});
plugins.push({
provides: [],
consumes: ["fs", "tabManager"],
setup: previewHelper
});
return plugins;
}
</script>
<script>
}
var start = Date.now();
/*dev{*/
var href = "/static";
@ -122,8 +147,8 @@
}});
/*packed}*/
var plugins;
require(["lib/architect/architect", "configs/ide/default"], function (architect, defaultConfig) {
var plugins = defaultConfig({
require(["lib/architect/architect", "configs/ide/default"], function(architect, defaultConfig) {
plugins = defaultConfig({
staticPrefix: href,
workspaceDir: "/",
workspaceId: "/",
@ -135,7 +160,7 @@
project: {},
user: {},
standalone: true,
previewUrl: "",
previewUrl: "./offline.preview/index.html?u=",
dashboardUrl: "",
/*dev{*/
themePrefix: "/static/standalone/skin/default",
@ -158,13 +183,12 @@
});
}
});
window.plugins = plugins;
architect.resolveConfig(plugins, function (err, config) {
if (err) throw err;
var errored;
var app = architect.createApp(config, function(err, app){
var app = architect.createApp(config, function(err, app) {
if (err) {
errored = true;
console.error(err.stack);
@ -172,7 +196,7 @@
}
});
app.on("error", function(err){
app.on("error", function(err) {
console.error(err.stack);
if (!errored)
alert(err);
@ -185,7 +209,7 @@
app.on("ready", function() {
window.app = app.services;
window.app.__defineGetter__("_ace", function(){
window.app.__defineGetter__("_ace", function() {
return this.tabManager.focussedTab.editor.ace;
});
Object.keys(window.app).forEach(function(n) {

Wyświetl plik

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
function log() {
document.body.appendChild(document.createTextNode(Array.prototype.join.call(arguments, ", ") + "\n"));
console.log.apply(console, arguments);
}
window.onerror = function(err) {
log("Error", err);
};
navigator.serviceWorker.register("serviceWorker.js", {
scope: "./"
}).then(function(sw) {
log("Registered serviceWorker, attempting to claim the page");
window.location.reload();
}).catch(function(err) {
log("Error loading the serviceWorker", err);
});
</script>
</body>
</html>

Wyświetl plik

@ -0,0 +1,61 @@
/*global clients, BroadcastChannel*/
var root = new URL(this.registration.scope);
var bc = new BroadcastChannel("livePreview");
var id = 0;
var callbacks = {};
var callBc = function(data, callback) {
data.id = id++;
callbacks[data.id] = callback;
bc.postMessage(data);
};
bc.onmessage = function(e) {
var data = e.data;
if (data.action == "callback") {
var callback = callbacks[data.id];
if (!callback) return;
delete callbacks[data.id];
callback(data.error, data.value);
}
};
this.onfetch = function(event) {
var url = new URL(event.request.url);
if (url.origin == root.origin) {
var path = url.searchParams.get("u");
if (!path) path = url.pathname;
if (path.startsWith(root.pathname))
path = path.substr(root.pathname.length - 1);
if (path) {
event.respondWith(
new Promise(function(resolve, reject) {
callBc({ action: "getFile", path: path }, function(e, v) {
if (e) reject(e);
resolve(v);
});
}).then(function(v) {
return new Response(v, {
headers: {
"Content-Type": getMime(path)
}
});
}, function(err) {
return new Response("error fetching " + path + " " + err, {
status: 400,
statusText: "Failure",
headers: {
"Content-Type": "text/x-error"
}
});
})
);
}
}
};
function getMime(path) {
if (path.endsWith(".html")) return "text/html";
if (path.endsWith(".js")) return "text/javascript";
if (path.endsWith(".css")) return "text/css";
if (path.endsWith(".svg")) return "image/svg+xml";
}