Skip to content

Commit 45a78d3

Browse files
committed
Expose ref
1 parent 2851b88 commit 45a78d3

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/components/Editor.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { editor, languages } from "monaco-editor/esm/vs/editor/editor.api.js";
22
import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
3+
import type React from "react";
34

45
import { registerCommands, service } from "../dot-monaco/index.js";
56

@@ -25,12 +26,14 @@ export type EditorProps = {
2526
initialValue?: string | undefined;
2627
onChangeValue: (value: string) => editor.IMarkerData[];
2728
onValueError: (errorCount: number) => void;
29+
ref?: React.Ref<editor.IStandaloneCodeEditor | null>;
2830
};
2931

3032
export default function Editor({
3133
initialValue,
3234
onChangeValue,
3335
onValueError,
36+
ref,
3437
}: EditorProps) {
3538
return (
3639
<div
@@ -40,6 +43,11 @@ export default function Editor({
4043
}}
4144
ref={div => {
4245
if (!div) {
46+
if (typeof ref === "function") {
47+
ref(null);
48+
} else if (ref && "current" in ref) {
49+
ref.current = null;
50+
}
4351
return;
4452
}
4553

@@ -54,14 +62,28 @@ export default function Editor({
5462
automaticLayout: true,
5563
});
5664

65+
if (typeof ref === "function") {
66+
ref(e);
67+
} else if (ref && "current" in ref) {
68+
ref.current = e;
69+
}
70+
5771
registerCommands(e);
5872

5973
e.focus();
6074

6175
const model = e.getModel();
6276
if (model === null) {
6377
import.meta.env.DEV && console.log("Model is null");
64-
return () => e?.dispose();
78+
79+
return () => {
80+
if (typeof ref === "function") {
81+
ref(null);
82+
} else if (ref && "current" in ref) {
83+
ref.current = null;
84+
}
85+
e?.dispose();
86+
};
6587
}
6688

6789
model.onDidChangeContent(() => {
@@ -78,6 +100,11 @@ export default function Editor({
78100

79101
return () => {
80102
model.dispose();
103+
if (typeof ref === "function") {
104+
ref(null);
105+
} else if (ref && "current" in ref) {
106+
ref.current = null;
107+
}
81108
e?.dispose();
82109
};
83110
}}

0 commit comments

Comments
 (0)