Skip to content

Commit 4f79d4e

Browse files
committed
feat: stringify parse tab 缓存逻辑
1 parent 5131390 commit 4f79d4e

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

src/app/json/stringify-parse/page.tsx

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ import React, {
1010

1111
import { Editor } from '../../../components/editor';
1212

13-
import { Space, Button, ButtonProps, Tabs, Modal, notification } from 'antd';
13+
import {
14+
Space,
15+
Button,
16+
ButtonProps,
17+
Tabs,
18+
Modal,
19+
notification,
20+
TabsProps,
21+
} from 'antd';
1422

1523
import { useMonacoEditor } from '../../../utils/config/editor';
1624
import * as json from '../../../utils/tools/json';
@@ -45,13 +53,13 @@ enum OperateType {
4553

4654
type TargetKey = React.MouseEvent | React.KeyboardEvent | string;
4755

48-
const Content = () => {
56+
const Content = ({ activeKey }: { activeKey: string }) => {
4957
const { editor } = useMonacoEditor();
5058
// console.log('editorRef', editorRef.current, editor);
51-
const cache = storageStringifyParseValue();
59+
const cache = storageStringifyParseValue(activeKey);
5260

5361
useEffect(() => {
54-
const value = editor?.getValue();
62+
const value = editor?.getValue(cache.getStorageKey());
5563

5664
if (value) {
5765
cache.setItem(value);
@@ -231,10 +239,18 @@ const Content = () => {
231239
};
232240

233241
export default function Page() {
234-
const [activeKey, setActiveKey] = useState('tab_1');
235-
const [items, setItems] = useState([
236-
{ label: 'Tab 1', children: <Content />, key: 'tab_1' },
237-
]);
242+
const cacheTabs = storageStringifyParseValue('tabs');
243+
let fistTab: any[] = [{ label: 'Tab 1', key: 'tab_1' }];
244+
245+
try {
246+
if (cacheTabs.getItem()) {
247+
fistTab = JSON.parse(cacheTabs.getItem());
248+
}
249+
} catch (error) {
250+
fistTab = [{ label: 'Tab 1', key: 'tab_1' }];
251+
}
252+
const [activeKey, setActiveKey] = useState(fistTab?.[0]?.key || 'tab_1');
253+
const [items, setItems] = useState(fistTab);
238254
const newTabIndex = useRef(0);
239255
const contentRef = useRef(null);
240256

@@ -247,11 +263,13 @@ export default function Page() {
247263
const newPanes = [...items];
248264
newPanes.push({
249265
label: 'New Tab',
250-
children: <Content />,
266+
// children: <Content activeKey={newActiveKey} />,
251267
key: newActiveKey,
252268
});
253269
setItems(newPanes);
254270
setActiveKey(newActiveKey);
271+
272+
cacheTabs.setItem(JSON.stringify(newPanes));
255273
};
256274

257275
const remove = (targetKey: TargetKey) => {
@@ -272,6 +290,7 @@ export default function Page() {
272290
}
273291
setItems(newPanes);
274292
setActiveKey(newActiveKey);
293+
cacheTabs.setItem(JSON.stringify(newPanes));
275294
};
276295

277296
const onEdit = (
@@ -298,7 +317,11 @@ export default function Page() {
298317
onChange={onChange}
299318
activeKey={activeKey}
300319
onEdit={onEdit}
301-
items={items}
320+
items={items.map((item) => ({
321+
label: item.label,
322+
key: item.key,
323+
children: <Content activeKey={item.key} />,
324+
}))}
302325
/>
303326
</div>
304327
);

src/app/template.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ export default function Template({
4747

4848
useEffect(() => {
4949
const handleBeforeUnload = (event: BeforeUnloadEvent) => {
50-
event.preventDefault();
51-
event.returnValue = '';
50+
if (pathname !== '/') {
51+
event.preventDefault();
52+
event.returnValue = '';
53+
}
5254
};
5355

5456
window.addEventListener('beforeunload', handleBeforeUnload);

src/utils/storage/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getStorageKey, storageTools } from './tools';
22

33
export const storageStringifyParseValue = (key: string = '') => {
4-
return storageTools(getStorageKey(`json_stringify_parse${key}`));
4+
return storageTools(getStorageKey(`json_stringify_parse_${key}`));
55
};

0 commit comments

Comments
 (0)