Browse Source

Add support for file protocol

Alex Dima 9 năm trước cách đây
mục cha
commit
0b41054ac2
3 tập tin đã thay đổi với 36 bổ sung37 xóa
  1. 16 1
      gulpfile.js
  2. 12 0
      test/samples-all.js
  3. 8 36
      test/samples.js

+ 16 - 1
gulpfile.js

@@ -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');

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 12 - 0
test/samples-all.js


+ 8 - 36
test/samples.js

@@ -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();
-		});
-	}
 });

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác