fix: multi file editing fix

This commit is contained in:
Khairul Hidayat 2024-03-12 07:16:18 +07:00
parent c1e28ac512
commit 7703eea760
2 changed files with 29 additions and 6 deletions

View File

@ -23,6 +23,7 @@ type Props = {
const CodeEditor = (props: Props) => { const CodeEditor = (props: Props) => {
const { filename, path, value, formatOnSave, wordWrap, onChange } = props; const { filename, path, value, formatOnSave, wordWrap, onChange } = props;
const editorRef = useRef<any>(null); const editorRef = useRef<any>(null);
const [isMounted, setMounted] = useState(false);
const [data, setData] = useState(value); const [data, setData] = useState(value);
const [debounceChange, resetDebounceChange] = useDebounce(onChange, 3000); const [debounceChange, resetDebounceChange] = useDebounce(onChange, 3000);
const language = useMemo(() => getLanguage(filename), [filename]); const language = useMemo(() => getLanguage(filename), [filename]);
@ -47,6 +48,8 @@ const CodeEditor = (props: Props) => {
noSemanticValidation: false, noSemanticValidation: false,
noSyntaxValidation: false, noSyntaxValidation: false,
}); });
setMounted(true);
}, []); }, []);
const onSave = useCallback(async () => { const onSave = useCallback(async () => {
@ -77,7 +80,15 @@ const CodeEditor = (props: Props) => {
} }
); );
model.setValue(formatted); // model.setValue(formatted);
editor.executeEdits("", [
{
range: editor.getModel()!.getFullModelRange(),
text: formatted,
forceMoveMarkers: true,
},
]);
const newCursor = model.getPositionAt(cursorOffset); const newCursor = model.getPositionAt(cursorOffset);
editor.setPosition(newCursor); editor.setPosition(newCursor);
@ -93,6 +104,19 @@ const CodeEditor = (props: Props) => {
useEffect(() => { useEffect(() => {
setData(value); setData(value);
const editor = editorRef.current;
const model = editor?.getModel();
if (editor && model?.uri.path.substring(1) === path) {
editor.executeEdits("", [
{
range: editor.getModel()!.getFullModelRange(),
text: value,
forceMoveMarkers: true,
},
]);
}
}, [value]); }, [value]);
useCommandKey("s", onSave); useCommandKey("s", onSave);
@ -100,10 +124,10 @@ const CodeEditor = (props: Props) => {
return ( return (
<div className="w-full h-full code-editor"> <div className="w-full h-full code-editor">
<CodeiumEditor <CodeiumEditor
language={language.name} defaultLanguage={language.name}
theme="vs-dark" theme="vs-dark"
path={path || filename} path={path}
value={data} defaultValue={value}
height="100%" height="100%"
onMount={onMount} onMount={onMount}
onChange={(e: string) => debounceChange(e)} onChange={(e: string) => debounceChange(e)}

View File

@ -17,7 +17,7 @@ const FileViewer = ({ id }: Props) => {
const { initialFiles } = useData<Data>(); const { initialFiles } = useData<Data>();
const initialData = initialFiles.find((i) => i.id === id) as any; const initialData = initialFiles.find((i) => i.id === id) as any;
const { data, isLoading, refetch } = trpc.file.getById.useQuery(id, { const { data, isLoading } = trpc.file.getById.useQuery(id, {
initialData, initialData,
}); });
@ -29,7 +29,6 @@ const FileViewer = ({ id }: Props) => {
const updateFileContent = trpc.file.update.useMutation({ const updateFileContent = trpc.file.update.useMutation({
onSuccess: () => { onSuccess: () => {
onFileContentChange(); onFileContentChange();
refetch();
}, },
}); });