Skip to content

Commit 540ecc9

Browse files
committed
fix: update of correction value
1 parent 7ac243a commit 540ecc9

File tree

3 files changed

+111
-24
lines changed

3 files changed

+111
-24
lines changed

frontend/pro/monaco-editor/diff-editor/monaco-editor.diff-editor.tsx

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import {
55
} from '@monaco-editor/react';
66
import { ReactSlot } from '@svelte-preprocess-react/react-slot';
77
import { sveltify } from '@svelte-preprocess-react/sveltify';
8-
import React, { useEffect } from 'react';
8+
import React, { useEffect, useMemo, useRef } from 'react';
99
import { useFunction } from '@utils/hooks/useFunction';
1010
import { Spin } from 'antd';
11+
import { isNumber } from 'lodash-es';
1112
import type { editor, IDisposable } from 'monaco-editor';
1213

14+
import { useValueChange } from '../useValueChange';
15+
1316
import '../monaco-editor.less';
1417

1518
export interface MonacoDiffEditorProps extends DiffEditorProps {
1619
themeMode?: string;
1720
height?: string | number;
1821
children?: React.ReactNode;
22+
readOnly?: boolean;
1923
value?: string;
2024
onValueChange: (value: string | undefined) => void;
2125
onChange?: (
@@ -24,6 +28,7 @@ export interface MonacoDiffEditorProps extends DiffEditorProps {
2428
) => void;
2529
onValidate?: OnValidate;
2630
afterMount?: DiffEditorProps['onMount'];
31+
line?: number;
2732
}
2833

2934
export const MonacoDiffEditor = sveltify<MonacoDiffEditorProps, ['loading']>(
@@ -40,22 +45,35 @@ export const MonacoDiffEditor = sveltify<MonacoDiffEditorProps, ['loading']>(
4045
onChange,
4146
onValueChange,
4247
onValidate,
43-
value,
48+
value: valueProp,
4449
modified,
50+
options,
51+
readOnly,
52+
line,
4553
...props
4654
}) => {
4755
const beforeMountFunction = useFunction(beforeMount);
4856
const afterMountFunction = useFunction(afterMount);
49-
const disposablesRef = React.useRef<IDisposable[]>([]);
50-
57+
const disposablesRef = useRef<IDisposable[]>([]);
58+
const editorRef = useRef<editor.IStandaloneDiffEditor | null>(null);
59+
const [isEditorReady, setIsEditorReady] = React.useState(false);
60+
const [value, setValue] = useValueChange({
61+
onValueChange,
62+
value: valueProp,
63+
});
5164
const handleEditorMount: MonacoDiffEditorProps['onMount'] = (
5265
editor,
5366
monaco
5467
) => {
68+
editorRef.current = editor;
69+
if (isNumber(line)) {
70+
editor.revealLine(line);
71+
}
72+
setIsEditorReady(true);
5573
const modifiedEditor = editor.getModifiedEditor();
5674
const mountDisposable = modifiedEditor.onDidChangeModelContent((e) => {
5775
const newValue = modifiedEditor.getValue();
58-
onValueChange(newValue);
76+
setValue(newValue);
5977
onChange?.(newValue, e);
6078
});
6179

@@ -84,6 +102,13 @@ export const MonacoDiffEditor = sveltify<MonacoDiffEditorProps, ['loading']>(
84102
});
85103
};
86104
}, []);
105+
106+
useEffect(() => {
107+
if (isEditorReady && isNumber(line)) {
108+
editorRef.current?.revealLine(line);
109+
}
110+
}, [line, isEditorReady]);
111+
87112
return (
88113
<>
89114
<div style={{ display: 'none' }}>{children}</div>
@@ -96,6 +121,13 @@ export const MonacoDiffEditor = sveltify<MonacoDiffEditorProps, ['loading']>(
96121
>
97122
<DiffEditor
98123
{...props}
124+
options={useMemo(
125+
() => ({
126+
readOnly,
127+
...(options || {}),
128+
}),
129+
[options, readOnly]
130+
)}
99131
modified={value || modified}
100132
beforeMount={beforeMountFunction}
101133
onMount={(editor, monaco) => {
@@ -107,14 +139,12 @@ export const MonacoDiffEditor = sveltify<MonacoDiffEditorProps, ['loading']>(
107139
slots.loading ? (
108140
<ReactSlot slot={slots.loading} />
109141
) : (
110-
props.loading || (
111-
<Spin
112-
tip="Editor is Loading..."
113-
wrapperClassName="ms-gr-pro-monaco-editor-spin"
114-
>
115-
<div />
116-
</Spin>
117-
)
142+
<Spin
143+
tip={props.loading}
144+
wrapperClassName="ms-gr-pro-monaco-editor-spin"
145+
>
146+
<div />
147+
</Spin>
118148
)
119149
}
120150
theme={themeMode === 'dark' ? 'vs-dark' : 'light'}

frontend/pro/monaco-editor/monaco-editor.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { Editor, type EditorProps } from '@monaco-editor/react';
22
import { ReactSlot } from '@svelte-preprocess-react/react-slot';
33
import { sveltify } from '@svelte-preprocess-react/sveltify';
4-
import React from 'react';
4+
import React, { useMemo } from 'react';
55
import { useFunction } from '@utils/hooks/useFunction';
66
import { Spin } from 'antd';
77

8+
import { useValueChange } from './useValueChange';
9+
810
import './monaco-editor.less';
911

1012
export interface MonacoEditorProps extends EditorProps {
1113
themeMode?: string;
1214
height?: string | number;
15+
readOnly?: boolean;
1316
onValueChange: (value: string | undefined) => void;
1417
children?: React.ReactNode;
1518
afterMount?: EditorProps['onMount'];
@@ -18,7 +21,7 @@ export interface MonacoEditorProps extends EditorProps {
1821
export const MonacoEditor = sveltify<MonacoEditorProps, ['loading']>(
1922
({
2023
height,
21-
value,
24+
value: valueProp,
2225
className,
2326
style,
2427
themeMode,
@@ -29,10 +32,16 @@ export const MonacoEditor = sveltify<MonacoEditorProps, ['loading']>(
2932
afterMount,
3033
children,
3134
onMount,
35+
options,
36+
readOnly,
3237
...props
3338
}) => {
3439
const beforeMountFunction = useFunction(beforeMount);
3540
const afterMountFunction = useFunction(afterMount);
41+
const [value, setValue] = useValueChange({
42+
onValueChange,
43+
value: valueProp,
44+
});
3645

3746
return (
3847
<>
@@ -52,22 +61,27 @@ export const MonacoEditor = sveltify<MonacoEditorProps, ['loading']>(
5261
onMount?.(...args);
5362
afterMountFunction?.(...args);
5463
}}
64+
options={useMemo(
65+
() => ({
66+
readOnly,
67+
...(options || {}),
68+
}),
69+
[options, readOnly]
70+
)}
5571
loading={
5672
slots.loading ? (
5773
<ReactSlot slot={slots.loading} />
5874
) : (
59-
props.loading || (
60-
<Spin
61-
tip="Editor is Loading..."
62-
wrapperClassName="ms-gr-pro-monaco-editor-spin"
63-
>
64-
<div />
65-
</Spin>
66-
)
75+
<Spin
76+
tip={props.loading}
77+
wrapperClassName="ms-gr-pro-monaco-editor-spin"
78+
>
79+
<div />
80+
</Spin>
6781
)
6882
}
6983
onChange={(v, ev) => {
70-
onValueChange(v);
84+
setValue(v);
7185
onChange?.(v, ev);
7286
}}
7387
theme={themeMode === 'dark' ? 'vs-dark' : 'light'}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { useCallback, useEffect, useRef, useState } from 'react';
2+
import { useMemoizedFn } from '@utils/hooks/useMemoizedFn';
3+
4+
export function useValueChange(options: {
5+
value: string | undefined;
6+
onValueChange: (value: string | undefined) => void;
7+
}) {
8+
const { value: valueProp, onValueChange } = options;
9+
const [typing, setTyping] = useState(false);
10+
const [displayValue, setDisplayValue] = useState(valueProp);
11+
const typingTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
12+
const onValueChangeMemoized = useMemoizedFn(onValueChange);
13+
14+
const setValue = useCallback(
15+
(value: string | undefined) => {
16+
typingTimerRef.current && clearTimeout(typingTimerRef.current);
17+
setTyping(true);
18+
typingTimerRef.current = setTimeout(() => {
19+
setTyping(false);
20+
}, 100);
21+
onValueChangeMemoized(value);
22+
},
23+
[onValueChangeMemoized]
24+
);
25+
26+
useEffect(() => {
27+
// if not typing, use the cache value
28+
if (!typing) {
29+
// eslint-disable-next-line react-hooks/set-state-in-effect
30+
setDisplayValue(valueProp);
31+
}
32+
}, [typing, valueProp]);
33+
34+
useEffect(() => {
35+
return () => {
36+
if (typingTimerRef.current) {
37+
clearTimeout(typingTimerRef.current);
38+
typingTimerRef.current = null;
39+
}
40+
};
41+
}, []);
42+
return [displayValue, setValue] as const;
43+
}

0 commit comments

Comments
 (0)