dev-setup.js 5.6 KB

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