1- import type { MindElixirData } from '../index'
1+ import type { MindElixirData , NodeObj } from '../index'
22import { type MindElixirInstance } from '../index'
33import { findEle } from '../utils/dom'
44import type { Operation } from '../utils/pubsub'
55
66type History = {
77 prev : MindElixirData
8- currentNodeId : string | undefined
98 next : MindElixirData
9+ currentObject :
10+ | {
11+ type : 'node' | 'summary' | 'customLink'
12+ value : string
13+ }
14+ | {
15+ type : 'nodes'
16+ value : string [ ]
17+ }
18+ }
19+
20+ const calcCurentObject = function ( operation : Operation ) : History [ 'currentObject' ] {
21+ if ( [ 'createSummary' , 'removeSummary' , 'finishEditSummary' ] . includes ( operation . name ) ) {
22+ return {
23+ type : 'summary' ,
24+ value : ( operation as any ) . obj . id ,
25+ }
26+ } else if ( [ 'removeNodes' ] . includes ( operation . name ) ) {
27+ return {
28+ type : 'nodes' ,
29+ value : ( operation as any ) . objs . map ( ( obj : NodeObj ) => obj . id ) ,
30+ }
31+ } else {
32+ return {
33+ type : 'node' ,
34+ value : ( operation as any ) . obj . id ,
35+ }
36+ }
1037}
1138
1239export default function ( mei : MindElixirInstance ) {
@@ -17,9 +44,7 @@ export default function (mei: MindElixirInstance) {
1744 if ( operation . name === 'beginEdit' ) return
1845 history = history . slice ( 0 , currentIndex + 1 )
1946 const next = mei . getData ( )
20- let currentNodeId = undefined
21- if ( 'obj' in operation ) currentNodeId = operation . obj . id
22- history . push ( { prev : current , currentNodeId, next } )
47+ history . push ( { prev : current , currentObject : calcCurentObject ( operation ) , next } )
2348 current = next
2449 currentIndex = history . length - 1
2550 // console.log('operation', operation.obj.id, history)
@@ -29,7 +54,7 @@ export default function (mei: MindElixirInstance) {
2954 const h = history [ currentIndex ]
3055 current = h . prev
3156 mei . refresh ( h . prev )
32- if ( h . currentNodeId ) mei . selectNode ( findEle ( h . currentNodeId ) )
57+ if ( h . currentObject . type === 'node' ) mei . selectNode ( findEle ( h . currentObject . value ) )
3358 currentIndex --
3459 console . log ( 'current' , current )
3560 }
@@ -40,7 +65,7 @@ export default function (mei: MindElixirInstance) {
4065 const h = history [ currentIndex ]
4166 current = h . next
4267 mei . refresh ( h . next )
43- if ( h . currentNodeId ) mei . selectNode ( findEle ( h . currentNodeId ) )
68+ if ( h . currentObject . type === 'node' ) mei . selectNode ( findEle ( h . currentObject . value ) )
4469 }
4570 }
4671 mei . map . addEventListener ( 'keydown' , ( e : KeyboardEvent ) => {
0 commit comments