|
| 1 | +import * as monaco from "monaco-editor/esm/vs/editor/editor.main.js" |
| 2 | +import { useEffect, useId, useRef } from "react" |
| 3 | +import type { InputTypecheckDiagnostic } from "../lib/typecheck.ts" |
| 4 | + |
| 5 | +interface MonacoEditorProps { |
| 6 | + readonly label: string |
| 7 | + readonly value: string |
| 8 | + readonly language: "typescript" | "json" |
| 9 | + readonly diagnostics?: readonly InputTypecheckDiagnostic[] |
| 10 | + readonly onChange: (value: string) => void |
| 11 | + readonly minLines?: number |
| 12 | +} |
| 13 | + |
| 14 | +const markerOwner = "tskm-playground" |
| 15 | + |
| 16 | +export function MonacoEditor({ |
| 17 | + label, |
| 18 | + value, |
| 19 | + language, |
| 20 | + diagnostics = [], |
| 21 | + onChange, |
| 22 | + minLines = 16, |
| 23 | +}: MonacoEditorProps) { |
| 24 | + const id = useId().replaceAll(":", "-") |
| 25 | + const containerRef = useRef<HTMLDivElement>(null) |
| 26 | + const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null) |
| 27 | + const modelRef = useRef<monaco.editor.ITextModel | null>(null) |
| 28 | + const initialValueRef = useRef(value) |
| 29 | + const onChangeRef = useRef(onChange) |
| 30 | + const lastValueRef = useRef(value) |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + onChangeRef.current = onChange |
| 34 | + }, [onChange]) |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + const container = containerRef.current |
| 38 | + if (!container) return |
| 39 | + |
| 40 | + const extension = language === "typescript" ? "ts" : "json" |
| 41 | + const uri = monaco.Uri.parse(`file:///tskm-playground/${id}.${extension}`) |
| 42 | + const model = monaco.editor.createModel(initialValueRef.current, language, uri) |
| 43 | + const editor = monaco.editor.create(container, { |
| 44 | + model, |
| 45 | + ariaLabel: label, |
| 46 | + automaticLayout: true, |
| 47 | + bracketPairColorization: { enabled: true }, |
| 48 | + folding: false, |
| 49 | + fontFamily: |
| 50 | + 'ui-monospace, "SF Mono", Menlo, Monaco, Consolas, "Liberation Mono", "Yu Gothic", "YuGothic", monospace', |
| 51 | + fontLigatures: false, |
| 52 | + fontSize: 13, |
| 53 | + lineDecorationsWidth: 10, |
| 54 | + lineHeight: 20, |
| 55 | + lineNumbers: "off", |
| 56 | + minimap: { enabled: false }, |
| 57 | + overviewRulerBorder: false, |
| 58 | + padding: { top: 14, bottom: 14 }, |
| 59 | + renderLineHighlight: "none", |
| 60 | + scrollbar: { |
| 61 | + alwaysConsumeMouseWheel: false, |
| 62 | + horizontalScrollbarSize: 10, |
| 63 | + verticalScrollbarSize: 10, |
| 64 | + }, |
| 65 | + scrollBeyondLastLine: false, |
| 66 | + tabSize: 2, |
| 67 | + theme: "tskm-light", |
| 68 | + wordWrap: "off", |
| 69 | + }) |
| 70 | + const subscription = editor.onDidChangeModelContent(() => { |
| 71 | + const nextValue = model.getValue() |
| 72 | + lastValueRef.current = nextValue |
| 73 | + onChangeRef.current(nextValue) |
| 74 | + }) |
| 75 | + |
| 76 | + modelRef.current = model |
| 77 | + editorRef.current = editor |
| 78 | + |
| 79 | + return () => { |
| 80 | + subscription.dispose() |
| 81 | + editor.dispose() |
| 82 | + model.dispose() |
| 83 | + editorRef.current = null |
| 84 | + modelRef.current = null |
| 85 | + } |
| 86 | + }, [id, label, language]) |
| 87 | + |
| 88 | + useEffect(() => { |
| 89 | + const model = modelRef.current |
| 90 | + if (!model || value === lastValueRef.current) return |
| 91 | + lastValueRef.current = value |
| 92 | + model.setValue(value) |
| 93 | + }, [value]) |
| 94 | + |
| 95 | + useEffect(() => { |
| 96 | + const model = modelRef.current |
| 97 | + if (!model) return |
| 98 | + monaco.editor.setModelMarkers(model, markerOwner, diagnostics.map(toMarker)) |
| 99 | + }, [diagnostics]) |
| 100 | + |
| 101 | + return ( |
| 102 | + <div |
| 103 | + ref={containerRef} |
| 104 | + className="monaco-code-editor" |
| 105 | + style={{ minHeight: `${minLines * 1.55}rem` }} |
| 106 | + /> |
| 107 | + ) |
| 108 | +} |
| 109 | + |
| 110 | +export function defineTskmMonacoTheme() { |
| 111 | + monaco.editor.defineTheme("tskm-light", { |
| 112 | + base: "vs", |
| 113 | + inherit: true, |
| 114 | + rules: [ |
| 115 | + { token: "identifier", foreground: "202428" }, |
| 116 | + { token: "string", foreground: "1d6b4f" }, |
| 117 | + { token: "number", foreground: "7356a6" }, |
| 118 | + { token: "keyword", foreground: "7b3f64" }, |
| 119 | + { token: "delimiter", foreground: "68706a" }, |
| 120 | + ], |
| 121 | + colors: { |
| 122 | + "editor.background": "#fffefb", |
| 123 | + "editor.foreground": "#202428", |
| 124 | + "editor.lineHighlightBackground": "#00000000", |
| 125 | + "editorGutter.background": "#fffefb", |
| 126 | + "editorIndentGuide.background1": "#d6d9d1", |
| 127 | + "editorOverviewRuler.border": "#00000000", |
| 128 | + "editorWarning.foreground": "#c7891e", |
| 129 | + "editorError.foreground": "#c63c2f", |
| 130 | + focusBorder: "#2d6f6d", |
| 131 | + }, |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +function toMarker(diagnostic: InputTypecheckDiagnostic): monaco.editor.IMarkerData { |
| 136 | + const endLineNumber = diagnostic.endLine + 1 |
| 137 | + const endColumn = |
| 138 | + diagnostic.endLine === diagnostic.line |
| 139 | + ? Math.max(diagnostic.endColumn + 1, diagnostic.column + 2) |
| 140 | + : diagnostic.endColumn + 1 |
| 141 | + return { |
| 142 | + code: String(diagnostic.code), |
| 143 | + message: diagnostic.message, |
| 144 | + severity: |
| 145 | + diagnostic.category === "warning" |
| 146 | + ? monaco.MarkerSeverity.Warning |
| 147 | + : monaco.MarkerSeverity.Error, |
| 148 | + startLineNumber: diagnostic.line + 1, |
| 149 | + startColumn: diagnostic.column + 1, |
| 150 | + endLineNumber, |
| 151 | + endColumn, |
| 152 | + } |
| 153 | +} |
0 commit comments