File size: 1,462 Bytes
d9bc819
 
 
 
 
 
a02eccd
 
 
 
 
 
 
d9bc819
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4257e7
 
 
 
 
a02eccd
 
 
 
 
 
 
d9bc819
 
 
 
 
 
a4257e7
 
2c7c9ec
a4257e7
e31f89f
d9bc819
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use client'
import * as React from 'react'
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
import { createConfiguredEditor } from 'vscode/monaco'
import './setup'
import 'monaco-editor/esm/vs/language/typescript/monaco.contribution'
import MonacoEditorCopilot from './copilot';

const config = [  
  {
    testName: 'example with dispose'
  },
]

monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
  jsx: monaco.languages.typescript.JsxEmit.Preserve,
})

export default function Editor({ defaultValue }: { defaultValue: string }) {
  const ref = React.useRef<HTMLDivElement>(null)

  React.useLayoutEffect(() => {
    const model = monaco.editor.createModel(
      defaultValue,
      'typescript',
      monaco.Uri.file('index.ts')
    )
    const editor = createConfiguredEditor(ref.current!, {
      model,
      automaticLayout: true,
    })

    const initialValue = editor.getValue();
    const encodedJs = encodeURIComponent(initialValue);
    const dataUri = "data:text/javascript;charset=utf-8," + encodedJs;
    import(dataUri);

    const dispose = MonacoEditorCopilot(editor, { testName: 'basic example'} as any);
    if (config[0]?.testName === 'example with dispose') {
      setTimeout(() => {
        dispose();
      }, 1000);
    }

    return () => {
      model.dispose()
      editor.dispose()
    }
  }, [])

  return (
    <>
      <div id="editor" ref={ref} style={{ height: "100vh" }} />
    </>
  )
}