Files
tlsn-extension/packages/tutorial/src/components/shared/CodeEditor.tsx
tsukino 09e107a871 feat(tutorial): enhance Swiss Bank challenges with unified validation
- Unified challenge validation: all 3 challenges validated together on single test
- Added persistent challenge completion tracking with localStorage
- Removed PEDERSEN references (not yet implemented)
- Updated validators to require part: 'BODY' and part: 'HEADERS' explicitly
- Added reset button to Swiss Bank Basic step
- Fixed step completion bug by using validate() return value instead of stale state
- Added backward compatibility for localStorage schema migration
- Reduced CodeEditor font size to 13px for better readability
- Consolidated documentation with inspection-first approach
2026-01-20 22:25:35 +08:00

74 lines
1.9 KiB
TypeScript

import React, { useEffect, useRef } from 'react';
import { EditorView, basicSetup } from 'codemirror';
import { javascript } from '@codemirror/lang-javascript';
import { EditorState } from '@codemirror/state';
interface CodeEditorProps {
value: string;
onChange: (value: string) => void;
readOnly?: boolean;
height?: string;
}
export const CodeEditor: React.FC<CodeEditorProps> = ({
value,
onChange,
readOnly = false,
height = '400px',
}) => {
const editorRef = useRef<HTMLDivElement>(null);
const viewRef = useRef<EditorView | null>(null);
useEffect(() => {
if (!editorRef.current) return;
const startState = EditorState.create({
doc: value,
extensions: [
basicSetup,
javascript(),
EditorView.editable.of(!readOnly),
EditorView.updateListener.of((update) => {
if (update.docChanged && !readOnly) {
const newValue = update.state.doc.toString();
onChange(newValue);
}
}),
EditorView.theme({
'&': { height },
'.cm-scroller': { overflow: 'auto' },
'.cm-content': {
fontFamily: 'Monaco, Menlo, "Ubuntu Mono", Consolas, monospace',
fontSize: '13px',
},
}),
],
});
const view = new EditorView({
state: startState,
parent: editorRef.current,
});
viewRef.current = view;
return () => {
view.destroy();
};
}, []);
// Update editor content when value prop changes (but not from user input)
useEffect(() => {
if (viewRef.current) {
const currentValue = viewRef.current.state.doc.toString();
if (currentValue !== value) {
viewRef.current.dispatch({
changes: { from: 0, to: currentValue.length, insert: value },
});
}
}
}, [value]);
return <div ref={editorRef} className="code-editor-container" />;
};