dev-setup.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. (function() {
  2. var IS_FILE_PROTOCOL = (window.location.protocol === 'file:');
  3. var DIRNAME = null;
  4. if (IS_FILE_PROTOCOL) {
  5. var port = window.location.port;
  6. if (port.length > 0) {
  7. port = ':' + port;
  8. }
  9. DIRNAME = window.location.protocol + '//' + window.location.hostname + port + window.location.pathname.substr(0, window.location.pathname.lastIndexOf('/'));
  10. }
  11. var LOADER_OPTS = (function() {
  12. function parseQueryString() {
  13. var str = window.location.search;
  14. str = str.replace(/^\?/, '');
  15. var pieces = str.split(/&/);
  16. var result = {};
  17. pieces.forEach(function(piece) {
  18. var config = piece.split(/=/);
  19. result[config[0]] = config[1];
  20. });
  21. return result;
  22. }
  23. var overwrites = parseQueryString();
  24. var result = {};
  25. result['editor'] = overwrites['editor'] || 'npm';
  26. METADATA.PLUGINS.map(function(plugin) {
  27. result[plugin.name] = overwrites[plugin.name] || 'npm';
  28. });
  29. return result;
  30. })();
  31. function toHREF(search) {
  32. var port = window.location.port;
  33. if (port.length > 0) {
  34. port = ':' + port;
  35. }
  36. return window.location.protocol + '//' + window.location.hostname + port + window.location.pathname + search + window.location.hash;
  37. }
  38. function Component(name, modulePrefix, paths, contrib) {
  39. this.name = name;
  40. this.modulePrefix = modulePrefix;
  41. this.paths = paths;
  42. this.contrib = contrib;
  43. this.selectedPath = LOADER_OPTS[name];
  44. }
  45. Component.prototype.isRelease = function() {
  46. return /release/.test(this.selectedPath);
  47. };
  48. Component.prototype.getResolvedPath = function() {
  49. var resolvedPath = this.paths[this.selectedPath];
  50. if (this.selectedPath === 'npm' || this.isRelease()) {
  51. if (IS_FILE_PROTOCOL) {
  52. resolvedPath = DIRNAME + '/../' + resolvedPath;
  53. } else {
  54. resolvedPath = '/monaco-editor/' + resolvedPath;
  55. }
  56. } else {
  57. if (IS_FILE_PROTOCOL) {
  58. resolvedPath = DIRNAME + '/../..' + resolvedPath;
  59. }
  60. }
  61. return resolvedPath;
  62. };
  63. Component.prototype.generateLoaderConfig = function(dest) {
  64. dest[this.modulePrefix] = this.getResolvedPath();
  65. };
  66. Component.prototype.generateUrlForPath = function(pathName) {
  67. var NEW_LOADER_OPTS = {};
  68. Object.keys(LOADER_OPTS).forEach(function(key) {
  69. NEW_LOADER_OPTS[key] = (LOADER_OPTS[key] === 'npm' ? undefined : LOADER_OPTS[key]);
  70. });
  71. NEW_LOADER_OPTS[this.name] = (pathName === 'npm' ? undefined : pathName);
  72. var search = Object.keys(NEW_LOADER_OPTS).map(function(key) {
  73. var value = NEW_LOADER_OPTS[key];
  74. if (value) {
  75. return key + '=' + value;
  76. }
  77. return '';
  78. }).filter(function(assignment) { return !!assignment; }).join('&');
  79. if (search.length > 0) {
  80. search = '?' + search;
  81. }
  82. return toHREF(search);
  83. };
  84. Component.prototype.renderLoadingOptions = function() {
  85. return '<strong style="width:130px;display:inline-block;">' + this.name + '</strong>:&nbsp;&nbsp;&nbsp;' + Object.keys(this.paths).map(function(pathName) {
  86. if (pathName === this.selectedPath) {
  87. return '<strong>' + pathName + '</strong>';
  88. }
  89. return '<a href="' + this.generateUrlForPath(pathName) + '">' + pathName + '</a>';
  90. }.bind(this)).join('&nbsp;&nbsp;&nbsp;');
  91. };
  92. var RESOLVED_CORE = new Component('editor', 'vs', METADATA.CORE.paths);
  93. self.RESOLVED_CORE_PATH = RESOLVED_CORE.getResolvedPath();
  94. var RESOLVED_PLUGINS = METADATA.PLUGINS.map(function(plugin) {
  95. return new Component(plugin.name, plugin.modulePrefix, plugin.paths, plugin.contrib);
  96. });
  97. METADATA = null;
  98. function loadScript(path, callback) {
  99. var script = document.createElement('script');
  100. script.onload = callback;
  101. script.async = true;
  102. script.type = 'text/javascript';
  103. script.src = path;
  104. document.head.appendChild(script);
  105. }
  106. (function() {
  107. var allComponents = [RESOLVED_CORE];
  108. if (!RESOLVED_CORE.isRelease()) {
  109. allComponents = allComponents.concat(RESOLVED_PLUGINS);
  110. }
  111. var div = document.createElement('div');
  112. div.style.position = 'fixed';
  113. div.style.top = 0;
  114. div.style.right = 0;
  115. div.style.background = 'lightgray';
  116. div.style.padding = '5px 20px 5px 5px';
  117. div.innerHTML = '<ul><li>' + allComponents.map(function(component) { return component.renderLoadingOptions(); }).join('</li><li>') + '</li></ul>';
  118. document.body.appendChild(div);
  119. })();
  120. self.loadEditor = function(callback, PATH_PREFIX) {
  121. PATH_PREFIX = PATH_PREFIX || '';
  122. loadScript(PATH_PREFIX + RESOLVED_CORE.getResolvedPath() + '/loader.js', function() {
  123. var loaderPathsConfig = {};
  124. if (!RESOLVED_CORE.isRelease()) {
  125. RESOLVED_PLUGINS.forEach(function(plugin) {
  126. plugin.generateLoaderConfig(loaderPathsConfig);
  127. });
  128. }
  129. RESOLVED_CORE.generateLoaderConfig(loaderPathsConfig);
  130. console.log('LOADER CONFIG: ');
  131. console.log(JSON.stringify(loaderPathsConfig, null, '\t'));
  132. require.config({
  133. paths: loaderPathsConfig
  134. });
  135. require(['vs/editor/editor.main'], function() {
  136. if (!RESOLVED_CORE.isRelease()) {
  137. // At this point we've loaded the monaco-editor-core
  138. require(RESOLVED_PLUGINS.map(function(plugin) { return plugin.contrib; }), function() {
  139. // At this point we've loaded all the plugins
  140. callback();
  141. });
  142. } else {
  143. callback();
  144. }
  145. });
  146. });
  147. }
  148. })();