Add support for file protocol

This commit is contained in:
Alex Dima 2016-08-24 12:35:28 +02:00
parent a4e18e4226
commit 0b41054ac2
3 changed files with 211 additions and 37 deletions

View file

@ -389,7 +389,22 @@ gulp.task('website', ['clean-website', 'playground-samples'], function() {
});
gulp.task('simpleserver', function(cb) {
gulp.task('generate-test-samples', function() {
var sampleNames = fs.readdirSync(path.join(__dirname, 'test/samples'));
var samples = sampleNames.map(function(sampleName) {
var samplePath = path.join(__dirname, 'test/samples', sampleName);
var sampleContent = fs.readFileSync(samplePath).toString();
return {
name: sampleName,
content: sampleContent
};
});
var prefix = '//This is a generated file via gulp generate-test-samples\ndefine([], function() { return';
var suffix = '; });'
fs.writeFileSync(path.join(__dirname, 'test/samples-all.js'), prefix + JSON.stringify(samples, null, '\t') + suffix );
});
gulp.task('simpleserver', ['generate-test-samples'], function(cb) {
httpServer.createServer({ root: '../', cache: 5 }).listen(8080);
httpServer.createServer({ root: '../', cache: 5 }).listen(8088);
console.log('LISTENING on 8080 and 8088');

187
test/samples-all.js Normal file

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,11 @@
/// <reference path="../node_modules/monaco-editor-core/monaco.d.ts" />
define([], function() {
define(['./samples-all'], function(ALL_SAMPLES) {
var XHR_SAMPLES = {};
ALL_SAMPLES.forEach(function(sample) {
XHR_SAMPLES[sample.name] = sample.content;
});
var samples = [];
@ -12,9 +17,7 @@ define([], function() {
name: 'sample - ' + modeId,
mimeType: modeId,
loadText: function() {
return xhr('samples/sample.' + modeId + '.txt').then(function(xhrResponse) {
return xhrResponse.responseText;
});
return monaco.Promise.as(XHR_SAMPLES['sample.' + modeId + '.txt']);
}
});
});
@ -25,9 +28,7 @@ define([], function() {
name: name,
mimeType: mimeType,
loadText: function() {
return xhr('samples/' + modelUrl).then(function(xhrResponse) {
return textModifier(xhrResponse.responseText);
});
return monaco.Promise.as(XHR_SAMPLES[modelUrl]);
}
});
}
@ -239,33 +240,4 @@ define([], function() {
addXHRSample('Z___f12___css','run-editor-sample-f12-css.txt','text/css');
return samples;
function xhr(url) {
var req = null;
return new monaco.Promise(function(c,e,p) {
req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req._canceled) { return; }
if (req.readyState === 4) {
if ((req.status >= 200 && req.status < 300) || req.status === 1223) {
c(req);
} else {
e(req);
}
req.onreadystatechange = function () { };
} else {
p(req);
}
};
req.open("GET", url, true );
req.responseType = "";
req.send(null);
}, function () {
req._canceled = true;
req.abort();
});
}
});