@@ -5,46 +5,32 @@ import { useEffect, useState, useRef } from "react";
5
5
import clsx from "clsx" ;
6
6
import TypeScriptSandboxEditor from "./TypeScriptSandboxEditor" ;
7
7
import styles from "./styles.module.css" ;
8
-
9
- function useSavedCode ( key : string , defaultValue : string ) {
10
- const [ code , setCode ] = useLocalStorage ( key , defaultValue ) ;
11
-
12
- return {
13
- code,
14
- setCode,
15
- } ;
16
- }
17
-
18
- function useShouldSaveCode ( ) {
19
- const [ shouldAutosaveCode , setShouldAutosaveCode ] = useLocalStorage (
20
- "shouldAutosaveCode" ,
21
- true
22
- ) ;
23
-
24
- return [ shouldAutosaveCode , setShouldAutosaveCode ] as const ;
25
- }
8
+ import BrowserOnly from "@docusaurus/BrowserOnly" ;
9
+ import CopyToClipboardButton from "./CopyToClipboardButton" ;
26
10
27
11
type CodeEditorProps = {
28
12
codeKey : string ;
29
13
defaultValue : string ;
30
14
solution : string ;
31
15
} ;
32
16
33
- export default function CodeEditor ( {
17
+ function BrowserCodeEditor ( {
34
18
codeKey,
35
19
defaultValue,
36
20
solution,
37
21
} : CodeEditorProps ) {
38
22
const monaco = useMonaco ( ) ;
23
+ const [ shouldAutosaveCode , setShouldAutosaveCode ] = useLocalStorage (
24
+ "shouldAutosaveCode" ,
25
+ true
26
+ ) ;
27
+ const [ code , setCode ] = useLocalStorage ( codeKey , defaultValue ) ;
28
+
39
29
const [ shouldShowDiff , setShouldShowDiff ] = useState ( false ) ;
40
- const { code, setCode } = useSavedCode ( codeKey , defaultValue ) ;
41
30
const [ shouldShowSolution , setShouldShowSolution ] = useState ( false ) ;
42
31
const [ value , setValue ] = useState ( code ) ;
43
- const [ shouldAutosaveCode , setShouldAutosaveCode ] = useShouldSaveCode ( ) ;
44
- const { colorMode } = useColorMode ( ) ;
45
- const [ clipboardTick , setClipboardTick ] = useState ( false ) ;
46
- const clipboardTimeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
47
32
33
+ const { colorMode } = useColorMode ( ) ;
48
34
const theme = colorMode === "dark" ? "vs-dark" : "light" ;
341A
tr>49
35
50
36
const handleCodeChange = ( newValue : string | undefined ) => {
@@ -54,26 +40,12 @@ export default function CodeEditor({
54
40
setValue ( newValue || "" ) ;
55
41
} ;
56
42
57
- const handleCopyToClipboard = ( ) => {
58
- navigator . clipboard . writeText ( value ) ;
59
- setClipboardTick ( true ) ;
60
-
61
- if ( clipboardTimeoutRef . current ) {
62
- clearTimeout ( clipboardTimeoutRef . current ) ;
63
- }
64
-
65
- clipboardTimeoutRef . current = setTimeout ( ( ) => {
66
- setClipboardTick ( false ) ;
67
- } , 1000 ) ;
68
- } ;
69
-
70
43
useEffect ( ( ) => {
71
44
if ( shouldShowSolution ) {
72
45
return ;
73
46
}
74
47
75
48
if ( shouldAutosaveCode ) {
76
- console . log ( "Autosaving code..." , value ) ;
77
49
setCode ( value ) ;
78
50
}
79
51
} , [ value , shouldAutosaveCode , setCode , shouldShowSolution ] ) ;
@@ -83,9 +55,6 @@ export default function CodeEditor({
83
55
return ;
84
56
}
85
57
86
- // Show inlay hints
87
- // monaco.languages.registerInlayHintsProvider("typescript", )
88
-
89
58
monaco . languages . typescript . typescriptDefaults . setDiagnosticsOptions ( {
90
59
noSemanticValidation : false ,
91
60
noSyntaxValidation : false ,
@@ -123,12 +92,7 @@ export default function CodeEditor({
123
92
>
124
93
Reset
125
94
</ button >
126
- < button
127
- className = "button button--secondary button--outline"
128
- onClick = { handleCopyToClipboard }
129
- >
130
- { clipboardTick ? "✔ Copied" : "Copy" }
131
- </ button >
95
+ < CopyToClipboardButton value = { value } />
132
96
</ div >
133
97
< div style = { { display : "flex" , gap : "1rem" } } >
134
98
{ shouldShowSolution && (
@@ -205,3 +169,7 @@ export default function CodeEditor({
205
169
</ div >
206
170
) ;
207
171
}
172
+
173
+ export default function CodeEditor ( props : CodeEditorProps ) {
174
+ return < BrowserOnly > { ( ) => < BrowserCodeEditor { ...props } /> } </ BrowserOnly > ;
175
+ }
0 commit comments