1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-04-27 11:01:14 +00:00

time-base saving & toastTips (#7662)

* time-base saving & toastTips

* refactor hasChanged

---------

Co-authored-by: First <first@FirstdeMacBook-Pro.local>
This commit is contained in:
zhichaona 2025-03-26 10:25:36 +08:00 committed by GitHub
parent 055bd575b9
commit 3ec456fff5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 2 deletions

View File

@ -59,8 +59,18 @@ const ExcaliEditor = () => {
}
}, SAVE_INTERVAL_TIME);
const handleBeforeUnload = (event) => {
if (isChangedRef.current) {
event.preventDefault();
event.returnValue = '';
}
};
window.addEventListener('beforeunload', handleBeforeUnload);
return () => {
clearInterval(saveInterval);
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, [saveSceneContent]);

View File

@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { Excalidraw, MainMenu } from '@excalidraw/excalidraw';
import isHotkey from 'is-hotkey';
import CodeMirrorLoading from '../../components/code-mirror-loading';
@ -13,6 +13,7 @@ const SimpleEditor = ({
isFetching
}) => {
const [excalidrawAPI, setExcalidrawAPI] = useState(null);
const prevElementsRef = useRef([]);
const UIOptions = {
canvasActions: {
saveToActiveFile: false,
@ -23,7 +24,10 @@ const SimpleEditor = ({
const handleChange = () => {
const elements = excalidrawAPI.getSceneElements();
onChangeContent(elements);
if (hasChanged(elements, prevElementsRef.current)) {
onChangeContent(elements);
}
prevElementsRef.current = elements;
};
useEffect(() => {
@ -40,6 +44,14 @@ const SimpleEditor = ({
};
}, [excalidrawAPI, onSaveContent]);
const hasChanged = (prev, current) => {
if (prev.length !== current.length) return true;
return current.some((element, index) => {
return element.version !== prev[index]?.version;
});
};
if (isFetching) {
return (
<div className='excali-container'>