dev-setup.js 6.2 KB

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