dev-setup.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //@ts-check
  2. (function () {
  3. const corePaths = {
  4. src: '/vscode/out/vs',
  5. 'npm/dev': 'node_modules/monaco-editor-core/dev/vs',
  6. 'npm/min': 'node_modules/monaco-editor-core/min/vs',
  7. built: '/vscode/out-monaco-editor-core/min/vs',
  8. releaseDev: 'out/monaco-editor/dev/vs',
  9. releaseMin: 'out/monaco-editor/min/vs'
  10. };
  11. const pluginPaths = {
  12. src: 'out/amd',
  13. dev: 'out/release/dev/vs',
  14. min: 'out/release/min/vs'
  15. };
  16. const parsedQuery = parseQueryString();
  17. const editorQueryName = 'editor';
  18. const pluginsQueryName = 'plugins';
  19. const defaultEditorQueryValue = 'npm/dev';
  20. const defaultPluginsQueryValue = 'src';
  21. const editorQueryValue = parsedQuery[editorQueryName] || defaultEditorQueryValue;
  22. const pluginsQueryValue = parsedQuery[pluginsQueryName] || defaultPluginsQueryValue;
  23. const corePath = resolvePath(corePaths[editorQueryValue]);
  24. const pluginPath = resolvePath(pluginPaths[pluginsQueryValue]);
  25. const isRelease = /release/.test(editorQueryValue);
  26. (function () {
  27. let div = document.createElement('div');
  28. div.className = 'dev-setup-control';
  29. div.style.position = 'fixed';
  30. div.style.top = '0';
  31. div.style.right = '0';
  32. div.style.background = 'lightgray';
  33. div.style.padding = '5px 20px 5px 5px';
  34. div.style.zIndex = '1000';
  35. div.innerHTML = // CodeQL [SM03712] This code is not deployed and serves as local test code. No risk of malicious input.
  36. '<ul><li>' + // CodeQL [SM03712] This code is not deployed and serves as local test code. No risk of malicious input.
  37. renderLoadingOptions(true) + // CodeQL [SM03712] This code is not deployed and serves as local test code. No risk of malicious input.
  38. (isRelease ? '' : `</li><li>${renderLoadingOptions(false)}`) + // CodeQL [SM03712] This code is not deployed and serves as local test code. No risk of malicious input.
  39. '</li></ul>'; // CodeQL [SM03712] This code is not deployed and serves as local test code. No risk of malicious input.
  40. document.body.appendChild(div);
  41. let aElements = document.getElementsByTagName('a');
  42. for (let i = 0; i < aElements.length; i++) {
  43. let aElement = aElements[i];
  44. if (aElement.className === 'loading-opts') {
  45. aElement.href += window.location.search; // CodeQL [SM01507] This code is not deployed and serves as local test code. No risk of malicious input.
  46. }
  47. }
  48. })();
  49. /** @type {any} */
  50. const global = self;
  51. global.getCodiconPath = () => {
  52. return `${corePath}/base/browser/ui/codicons/codicon/codicon.ttf`;
  53. };
  54. /**
  55. *
  56. * @param {()=>void} callback
  57. * @param {string} [PATH_PREFIX]
  58. */
  59. global.loadEditor = (callback, PATH_PREFIX) => {
  60. PATH_PREFIX = PATH_PREFIX || '';
  61. loadScript(`${PATH_PREFIX}${corePath}/loader.js`, () => {
  62. global.AMD = true;
  63. /** @type {{[name:string]: string;}} */
  64. const loaderPathsConfig = {};
  65. if (isRelease) {
  66. loaderPathsConfig['vs'] = `${PATH_PREFIX}${corePath}`;
  67. } else {
  68. loaderPathsConfig[
  69. 'vs/fillers/monaco-editor-core'
  70. ] = `${PATH_PREFIX}/monaco-editor/out/amd/fillers/monaco-editor-core-amd`;
  71. loaderPathsConfig['vs/language'] = `${PATH_PREFIX}${pluginPath}/language`;
  72. loaderPathsConfig['vs/basic-language'] = `${PATH_PREFIX}${pluginPath}/basic-language`;
  73. loaderPathsConfig['vs'] = `${PATH_PREFIX}${corePath}`;
  74. }
  75. console.log('LOADER CONFIG: ');
  76. console.log(JSON.stringify(loaderPathsConfig, null, '\t'));
  77. /** @type {any} */
  78. const req = require;
  79. req.config({
  80. paths: loaderPathsConfig
  81. // 'vs/nls' : {
  82. // availableLanguages: {
  83. // '*': 'de'
  84. // }
  85. // }
  86. });
  87. req(['vs/editor/editor.main'], () => {
  88. if (isRelease) {
  89. callback();
  90. return;
  91. }
  92. // At this point we've loaded the monaco-editor-core
  93. req(
  94. [
  95. 'vs/basic-languages/monaco.contribution',
  96. 'vs/language/css/monaco.contribution',
  97. 'vs/language/html/monaco.contribution',
  98. 'vs/language/json/monaco.contribution',
  99. 'vs/language/typescript/monaco.contribution'
  100. ],
  101. callback
  102. );
  103. });
  104. });
  105. };
  106. function parseQueryString() {
  107. const str = window.location.search.replace(/^\?/, '');
  108. const pieces = str.split(/&/);
  109. /** @type {{[name:string]: string;}} */
  110. const result = {};
  111. pieces.forEach((piece) => {
  112. const config = piece.split(/=/);
  113. result[config[0]] = config[1];
  114. });
  115. return result;
  116. }
  117. /**
  118. * @param {string} path
  119. */
  120. function resolvePath(path) {
  121. if (/^\//.test(path)) {
  122. return path;
  123. }
  124. return `/monaco-editor/${path}`;
  125. }
  126. /**
  127. * @param {boolean} isEditor
  128. */
  129. function renderLoadingOptions(isEditor) {
  130. const name = isEditor ? 'editor' : 'plugins';
  131. const paths = isEditor ? corePaths : pluginPaths;
  132. const selectedPath = isEditor ? editorQueryValue : pluginsQueryValue;
  133. return (
  134. '<strong style="width:130px;display:inline-block;">' +
  135. name +
  136. '</strong>:&#160;&#160;&#160;' +
  137. Object.keys(paths)
  138. .map((path) => {
  139. if (path === selectedPath) {
  140. return '<strong>' + path + '</strong>';
  141. }
  142. return '<a href="' + generateUrlForLoadingOption(isEditor, path) + '">' + path + '</a>';
  143. })
  144. .join('&#160;&#160;&#160;')
  145. );
  146. }
  147. /**
  148. * @param {boolean} isEditor
  149. * @param {string} value
  150. */
  151. function generateUrlForLoadingOption(isEditor, value) {
  152. /** @type {{[name:string]: string;}} */
  153. const newQueryOptions = {};
  154. const newEditorQueryValue = isEditor ? value : editorQueryValue;
  155. const newPluginsQueryValue = isEditor ? pluginsQueryValue : value;
  156. if (newEditorQueryValue !== defaultEditorQueryValue) {
  157. newQueryOptions[editorQueryName] = newEditorQueryValue;
  158. }
  159. if (newPluginsQueryValue !== defaultPluginsQueryValue) {
  160. newQueryOptions[pluginsQueryName] = newPluginsQueryValue;
  161. }
  162. let search = Object.keys(newQueryOptions)
  163. .map((key) => `${key}=${newQueryOptions[key]}`)
  164. .join('&amp;');
  165. if (search.length > 0) {
  166. search = '?' + search;
  167. }
  168. return toHREF(search);
  169. }
  170. /**
  171. * @param {string} search
  172. */
  173. function toHREF(search) {
  174. let port = window.location.port;
  175. if (port.length > 0) {
  176. port = ':' + port;
  177. }
  178. return (
  179. window.location.protocol +
  180. '//' +
  181. window.location.hostname +
  182. port +
  183. window.location.pathname +
  184. search +
  185. window.location.hash
  186. );
  187. }
  188. function loadScript(path, callback) {
  189. const script = document.createElement('script');
  190. script.onload = callback;
  191. script.async = true;
  192. script.type = 'text/javascript';
  193. script.src = path;
  194. document.head.appendChild(script);
  195. }
  196. })();