RunMonaco.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. function buildEditor(text) {
  2. const container = document.getElementById('container');
  3. const editor = monaco.editor.create(container, {
  4. theme: 'vs-light',
  5. value: text,
  6. language: 'csharp',
  7. name: 'monacoEditor',
  8. automaticLayout: true
  9. });
  10. editor.getModel().onDidChangeContent((event) => {
  11. window.chrome.webview.postMessage(editor.getValue());
  12. });
  13. // we need the parent of the editor
  14. const parent = container.parentElement
  15. window.addEventListener('resize', () => {
  16. // make editor as small as possible
  17. editor.layout({ width: 0, height: 0 })
  18. // wait for next frame to ensure last layout finished
  19. window.requestAnimationFrame(() => {
  20. // get the parent dimensions and re-layout the editor
  21. const rect = parent.getBoundingClientRect()
  22. editor.layout({ width: rect.width, height: rect.height })
  23. })
  24. })
  25. }
  26. function doAction(action) {
  27. editor.focus()
  28. editor.trigger('dunno', action);
  29. }