| 16 | self.MonacoEnvironment = { getWorker: () => new EditorWorker() }; |
| 17 | |
| 18 | export default function TestSource({ url }: { url: string }) { |
| 19 | const container = useRef<HTMLDivElement>(null); |
| 20 | const [failed, setFailed] = useState(false); |
| 21 | |
| 22 | useEffect(() => { |
| 23 | let editor: monaco.editor.IStandaloneCodeEditor | undefined; |
| 24 | let cancelled = false; |
| 25 | fetch(url) |
| 26 | .then((r) => { |
| 27 | if (!r.ok) throw new Error(String(r.status)); |
| 28 | return r.text(); |
| 29 | }) |
| 30 | .then((text) => { |
| 31 | if (cancelled || !container.current) return; |
| 32 | const lines = text.split("\n").length; |
| 33 | container.current.style.height = `${Math.min(Math.max(lines * 19 + 20, 140), 680)}px`; |
| 34 | editor = monaco.editor.create(container.current, { |
| 35 | value: text, |
| 36 | language: "typescript", |
| 37 | theme: "vs-dark", |
| 38 | readOnly: true, |
| 39 | domReadOnly: true, |
| 40 | minimap: { enabled: false }, |
| 41 | scrollBeyondLastLine: false, |
| 42 | fontSize: 12.5, |
| 43 | lineNumbers: "on", |
| 44 | renderLineHighlight: "none", |
| 45 | contextmenu: false, |
| 46 | folding: false, |
| 47 | automaticLayout: true, |
| 48 | scrollbar: { alwaysConsumeMouseWheel: false }, |
| 49 | stickyScroll: { enabled: false }, |
| 50 | overviewRulerLanes: 0, |
| 51 | }); |
| 52 | }) |
| 53 | .catch(() => setFailed(true)); |
| 54 | return () => { |
| 55 | cancelled = true; |
| 56 | editor?.dispose(); |
| 57 | }; |
| 58 | }, [url]); |
| 59 | |
| 60 | if (failed) return null; |
| 61 | return <div className="code" ref={container} />; |
| 62 | } |