1
0

dev-setup.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. (function() {
  2. var LOADER_OPTS = (function() {
  3. function parseQueryString() {
  4. var str = window.location.search;
  5. str = str.replace(/^\?/, '');
  6. var pieces = str.split(/&/);
  7. var result = {};
  8. pieces.forEach(function(piece) {
  9. var config = piece.split(/=/);
  10. result[config[0]] = config[1];
  11. });
  12. return result;
  13. }
  14. var overwrites = parseQueryString();
  15. var result = {};
  16. result['editor'] = overwrites['editor'] || 'npm';
  17. METADATA.PLUGINS.map(function(plugin) {
  18. result[plugin.name] = overwrites[plugin.name] || 'npm';
  19. });
  20. return result;
  21. })();
  22. function Component(name, modulePrefix, paths, contrib) {
  23. this.name = name;
  24. this.modulePrefix = modulePrefix;
  25. this.paths = paths;
  26. this.contrib = contrib;
  27. this.selectedPath = LOADER_OPTS[name];
  28. }
  29. Component.prototype.getResolvedPath = function() {
  30. var resolvedPath = this.paths[this.selectedPath];
  31. if (this.selectedPath === 'npm') {
  32. resolvedPath = '/monaco-editor/' + resolvedPath;
  33. }
  34. return resolvedPath;
  35. };
  36. Component.prototype.generateLoaderConfig = function(dest) {
  37. dest[this.modulePrefix] = this.getResolvedPath();
  38. };
  39. var RESOLVED_CORE = new Component('editor', 'vs', METADATA.CORE.paths);
  40. var RESOLVED_PLUGINS = METADATA.PLUGINS.map(function(plugin) {
  41. return new Component(plugin.name, plugin.modulePrefix, plugin.paths, plugin.contrib);
  42. });
  43. self.METADATA = null;
  44. function loadScript(path, callback) {
  45. var script = document.createElement('script');
  46. script.onload = callback;
  47. script.async = true;
  48. script.type = 'text/javascript';
  49. script.src = path;
  50. document.head.appendChild(script);
  51. }
  52. self.loadDevEditor = function() {
  53. return (getQueryStringValue('editor') === 'dev');
  54. }
  55. function getQueryStringValue (key) {
  56. return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
  57. }
  58. self.loadEditor = function(callback, PATH_PREFIX) {
  59. PATH_PREFIX = PATH_PREFIX || '';
  60. loadScript(PATH_PREFIX + RESOLVED_CORE.getResolvedPath() + '/loader.js', function() {
  61. var loaderPathsConfig = {};
  62. RESOLVED_PLUGINS.forEach(function(plugin) {
  63. plugin.generateLoaderConfig(loaderPathsConfig);
  64. });
  65. RESOLVED_CORE.generateLoaderConfig(loaderPathsConfig);
  66. console.log('LOADER CONFIG: ');
  67. console.log(JSON.stringify(loaderPathsConfig, null, '\t'));
  68. require.config({
  69. paths: loaderPathsConfig
  70. });
  71. require(['vs/editor/editor.main'], function() {
  72. // At this point we've loaded the monaco-editor-core
  73. require(RESOLVED_PLUGINS.map(function(plugin) { return plugin.contrib; }), function() {
  74. // At this point we've loaded all the plugins
  75. callback();
  76. });
  77. });
  78. });
  79. }
  80. })();