monaco.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // @monaco-editor/react fetches the editor from a CDN unless it is handed one.
  2. // A workflow tool that runs on your own machine should not stop working because
  3. // the machine is offline, so Monaco is bundled with the rest of the app and the
  4. // loader is pointed at it before any editor mounts.
  5. //
  6. // Only the pieces this app edits are pulled in. Importing monaco-editor whole
  7. // would add every language it ships - Solidity, PowerQuery, FreeMarker - for an
  8. // app that writes JavaScript and JSON.
  9. // The esm entry ships no type declarations of its own; the types live on the
  10. // package root, which is also the import that would drag in every language.
  11. // @ts-expect-error - untyped esm entry point, deliberately
  12. import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
  13. import { loader } from '@monaco-editor/react'
  14. import 'monaco-editor/esm/vs/editor/editor.all.js'
  15. import 'monaco-editor/esm/vs/basic-languages/javascript/javascript.contribution'
  16. import 'monaco-editor/esm/vs/basic-languages/typescript/typescript.contribution'
  17. // @ts-expect-error - untyped esm entry point, deliberately
  18. import { jsonDefaults } from 'monaco-editor/esm/vs/language/json/monaco.contribution'
  19. import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'
  20. import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker'
  21. // Monaco does its parsing off the main thread and asks for a worker by
  22. // language. JSON has a language service of its own; everything else is served
  23. // by the plain editor worker, which still gives highlighting and editing.
  24. window.MonacoEnvironment = {
  25. getWorker(_workerId: string, label: string) {
  26. if (label === 'json') return new jsonWorker()
  27. return new editorWorker()
  28. },
  29. }
  30. // A graph or request body edited here holds << >> placeholders where values
  31. // will go, so it is JSON with holes in it by design. Validating it would
  32. // underline most of the document in red for no reason.
  33. jsonDefaults.setDiagnosticsOptions({ validate: false, allowComments: true })
  34. loader.config({ monaco })