MCPcopy Create free account
hub / github.com/composify-js/composify / CodeEditor

Function CodeEditor

packages/react/src/editor/CodeEditor/CodeEditor.tsx:31–144  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

29};
30
31export const CodeEditor = () => {
32 const editorRef = useRef<Parameters<OnMount>[0] | null>(null);
33 const disableFormatting = useRef(false);
34
35 const { getSource, replaceRoot } = useEditing();
36
37 const [code, setCode] = useState(getSource());
38 const [message, setMessage] = useState('');
39
40 const formatCode = useCallback(() => {
41 if (!editorRef.current) {
42 return;
43 }
44
45 const value = editorRef.current.getValue();
46
47 prettify(value).then(setCode);
48 }, []);
49
50 const updateSource = useCallback(() => {
51 if (!editorRef.current) {
52 return;
53 }
54
55 const value = editorRef.current.getValue();
56
57 try {
58 const node = Parser.parse(value);
59 const types = new NodeManager(node).collectTypes();
60 const missing = Catalog.missing(types);
61
62 if (missing.length === 0) {
63 disableFormatting.current = true;
64 replaceRoot(node);
65 setMessage('');
66 } else {
67 setMessage(`Unregistered Block${missing.length > 1 ? 's' : ''}: ${missing.join(', ')}`);
68 }
69 } catch {
70 // Do nothing if anything fails
71 setMessage('Invalid code');
72 }
73 }, [replaceRoot]);
74
75 const debouncedUpdateSource = useMemo(() => debounce(updateSource, 1000), [updateSource]);
76
77 const handleMount = useCallback<OnMount>(
78 (editor) => {
79 editorRef.current = editor;
80 editorRef.current.onDidBlurEditorText(formatCode);
81 editorRef.current.onDidPaste(formatCode);
82
83 formatCode();
84 },
85 [formatCode],
86 );
87
88 const handleChange = useCallback(

Callers

nothing calls this directly

Calls 2

useEditingFunction · 0.90
prettifyFunction · 0.85

Tested by

no test coverage detected