1
0

dev-setup.js 5.9 KB

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