@@ -10,7 +10,15 @@ import React, {
1010
1111import { 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
1523import { useMonacoEditor } from '../../../utils/config/editor' ;
1624import * as json from '../../../utils/tools/json' ;
@@ -45,13 +53,13 @@ enum OperateType {
4553
4654type 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
233241export 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 ) ;
0 commit comments