Skip to content

Commit 5297476

Browse files
committed
DRY
1 parent 45a78d3 commit 5297476

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/components/Editor.tsx

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ export default function Editor({
4242
height: "100%",
4343
}}
4444
ref={div => {
45-
if (!div) {
45+
const setExternalRef = (value: editor.IStandaloneCodeEditor | null) => {
4646
if (typeof ref === "function") {
47-
ref(null);
47+
ref(value);
4848
} else if (ref && "current" in ref) {
49-
ref.current = null;
49+
ref.current = value;
5050
}
51+
};
52+
53+
if (!div) {
54+
setExternalRef(null);
5155
return;
5256
}
5357

@@ -62,11 +66,7 @@ export default function Editor({
6266
automaticLayout: true,
6367
});
6468

65-
if (typeof ref === "function") {
66-
ref(e);
67-
} else if (ref && "current" in ref) {
68-
ref.current = e;
69-
}
69+
setExternalRef(e);
7070

7171
registerCommands(e);
7272

@@ -77,11 +77,7 @@ export default function Editor({
7777
import.meta.env.DEV && console.log("Model is null");
7878

7979
return () => {
80-
if (typeof ref === "function") {
81-
ref(null);
82-
} else if (ref && "current" in ref) {
83-
ref.current = null;
84-
}
80+
setExternalRef(null);
8581
e?.dispose();
8682
};
8783
}
@@ -100,11 +96,7 @@ export default function Editor({
10096

10197
return () => {
10298
model.dispose();
103-
if (typeof ref === "function") {
104-
ref(null);
105-
} else if (ref && "current" in ref) {
106-
ref.current = null;
107-
}
99+
setExternalRef(null);
108100
e?.dispose();
109101
};
110102
}}

0 commit comments

Comments
 (0)