Skip to content

Commit 57cdb61

Browse files
committed
Check modification taking care of redo and undo
1 parent 49861d0 commit 57cdb61

File tree

2 files changed

+52
-31
lines changed

2 files changed

+52
-31
lines changed

src/renderer/components/editor/CodeEditor.jsx

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,60 @@ import { useCodeEditor } from './useCodeEditors';
55

66
export default function CodeEditor({ tabKey }) {
77
const containerRef = useRef(null);
8-
const [editor, setEditor] = useState(null);
9-
const [value, setValue] = useState(null);
10-
const { filepath, language, monacoOption, load, setDirty } = useCodeEditor({
8+
const editorRef = useRef(null);
9+
const versionRef = useRef(-1);
10+
const [mounted, setMounted] = useState(false);
11+
const {
12+
filepath,
13+
language,
14+
monacoOption,
15+
register,
16+
unregister,
17+
load,
18+
setDirty,
19+
isDifferentWithSavedValue,
20+
} = useCodeEditor({
1121
tabKey,
12-
ref: editor,
1322
});
1423
const { width, height } = useResizeObserver({
1524
ref: containerRef,
1625
round: Math.floor,
1726
});
1827

28+
useEffect(() => {
29+
if (tabKey && mounted) {
30+
register(tabKey, editorRef.current);
31+
}
32+
33+
return () => {
34+
unregister(tabKey);
35+
};
36+
}, [tabKey, mounted]);
37+
1938
function handleEditorDidMount(editor, monaco) {
20-
setEditor(editor);
39+
editorRef.current = editor;
40+
setMounted(true);
2141
load().then((value) => {
2242
if (value) {
2343
editor.setValue(value);
2444
}
2545
});
2646
}
2747

28-
const handleChange = useCallback(
29-
(newValue) => {
30-
if (value !== null) {
31-
setDirty(true);
32-
}
33-
setValue(newValue);
34-
},
35-
[value]
36-
);
48+
const handleChange = async (newValue, event) => {
49+
console.log(event);
50+
const versionId = event.versionId;
51+
const pervVersionId = versionRef.current;
52+
53+
versionRef.current = versionId;
54+
55+
if (event.isUndoing || event.isRedoing) {
56+
const modified = await isDifferentWithSavedValue(newValue);
57+
setDirty(modified);
58+
} else if (versionId >= 3 && versionId !== pervVersionId) {
59+
setDirty(true);
60+
}
61+
};
3762

3863
return (
3964
<div
@@ -49,7 +74,6 @@ export default function CodeEditor({ tabKey }) {
4974
path={filepath}
5075
defaultLanguage={language}
5176
options={monacoOption}
52-
value={value}
5377
onMount={handleEditorDidMount}
5478
onChange={handleChange}
5579
/>

src/renderer/components/editor/useCodeEditors.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
useRef,
3-
useEffect,
4-
useLayoutEffect,
5-
useState,
6-
useCallback,
7-
useMemo,
8-
} from 'react';
1+
import { useRef, useEffect, useState, useCallback, useMemo } from 'react';
92
import { nanoid } from 'nanoid';
103
import { createContext } from '../../hooks/context';
114
import { defaultOption, toggleEditorOption } from './manacoOption';
@@ -207,7 +200,7 @@ export function useCodeEditorTabs() {
207200
return { tabs, setActiveTabKey };
208201
}
209202

210-
export function useCodeEditor({ tabKey, ref }) {
203+
export function useCodeEditor({ tabKey }) {
211204
const { tabs, monacoOption, setDirty, load, register, unregister } =
212205
useCodeEditorsContext();
213206

@@ -216,25 +209,29 @@ export function useCodeEditor({ tabKey, ref }) {
216209
return { filepath: tab?.filepath, language: tab?.language };
217210
}, [tabs, tabKey]);
218211

219-
useLayoutEffect(() => {
220-
register(tabKey, ref);
221-
return () => {
222-
unregister(tabKey);
223-
};
224-
}, [tabKey, ref]);
225-
226212
const setDirtyCallback = useCallback(
227213
(value) => setDirty(tabKey, value),
228214
[tabKey]
229215
);
230216

231217
const loadCallback = useCallback(async () => await load(tabKey), [tabKey]);
232218

219+
const isDifferentWithSavedValue = useCallback(
220+
async (value) => {
221+
const savedValue = await load(tabKey);
222+
return savedValue !== value;
223+
},
224+
[tabKey]
225+
);
226+
233227
return {
234228
filepath,
235229
language,
236230
monacoOption,
231+
register,
232+
unregister,
237233
load: loadCallback,
238234
setDirty: setDirtyCallback,
235+
isDifferentWithSavedValue,
239236
};
240237
}

0 commit comments

Comments
 (0)