Skip to content

Commit 323d156

Browse files
committed
feat: allow redo/undo summary
1 parent d696f5c commit 323d156

File tree

5 files changed

+63
-36
lines changed

5 files changed

+63
-36
lines changed

src/interact.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ export const refresh = function (this: MindElixirInstance, data?: MindElixirData
344344
data = JSON.parse(JSON.stringify(data)) as MindElixirData // it shouldn't contanimate the original data
345345
this.nodeData = data.nodeData
346346
this.linkData = data.linkData || {}
347+
this.summaries = data.summaries || []
347348
}
348349
fillParent(this.nodeData)
349350
// create dom element for every node

src/plugin/keypress.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,10 @@ export default function (mind: MindElixirInstance) {
150150
// bug
151151
if (mind.currentLink) mind.removeLink()
152152
else if (mind.currentSummary) mind.removeSummary(mind.currentSummary.summaryObj.id)
153-
else {
154-
if (mind.currentNode) {
155-
mind.removeNode()
156-
} else if (mind.currentNodes) {
157-
mind.removeNodes(mind.currentNodes)
158-
}
153+
else if (mind.currentNode) {
154+
mind.removeNode()
155+
} else if (mind.currentNodes) {
156+
mind.removeNodes(mind.currentNodes)
159157
}
160158
} else {
161159
const keyHandler = key2func[e.keyCode]

src/plugin/operationHistory.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
1-
import type { MindElixirData } from '../index'
1+
import type { MindElixirData, NodeObj } from '../index'
22
import { type MindElixirInstance } from '../index'
33
import { findEle } from '../utils/dom'
44
import type { Operation } from '../utils/pubsub'
55

66
type 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

1239
export 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) => {

src/summary.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ export const createSummary = function (this: MindElixirInstance) {
169169
const g = drawSummary(this, summary) as SummarySvgGroup
170170
this.summaries.push(summary)
171171
this.editSummary(g)
172+
this.bus.fire('operation', {
173+
name: 'createSummary',
174+
obj: summary,
175+
})
172176
}
173177

174178
export const removeSummary = function (this: MindElixirInstance, id: string) {
@@ -177,6 +181,10 @@ export const removeSummary = function (this: MindElixirInstance, id: string) {
177181
this.summaries.splice(index, 1)
178182
document.querySelector('#s-' + id)?.remove()
179183
}
184+
this.bus.fire('operation', {
185+
name: 'removeSummary',
186+
obj: { id },
187+
})
180188
}
181189

182190
export const selectSummary = function (this: MindElixirInstance, el: SummarySvgGroup) {
@@ -228,7 +236,6 @@ export const editSummary = function (this: MindElixirInstance, el: SummarySvgGro
228236
this.bus.fire('operation', {
229237
name: 'finishEditSummary',
230238
obj: node,
231-
origin,
232239
})
233240
})
234241
console.timeEnd('editSummary')

src/utils/pubsub.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
import type { Summary } from '../summary'
22
import type { NodeObj } from '../types/index'
3-
export type OperationType =
4-
| 'moveNode'
5-
| 'moveNodeAfter'
6-
| 'moveNodeBefore'
7-
| 'removeNode'
8-
| 'removeNodes'
9-
| 'addChild'
10-
| 'copyNode'
11-
| 'reshapeNode'
12-
| 'insertSibling'
13-
| 'insertBefore'
14-
| 'insertParent'
15-
| 'moveUpNode'
16-
| 'moveDownNode'
17-
| 'beginEdit'
18-
| 'finishEdit'
193

20-
export type Operation =
4+
type NodeOperation =
215
| {
226
name: 'moveNode' | 'moveDownNode' | 'moveUpNode' | 'copyNode' | 'addChild' | 'insertParent' | 'insertBefore' | 'insertSibling' | 'beginEdit'
237
obj: NodeObj
@@ -32,11 +16,6 @@ export type Operation =
3216
obj: NodeObj
3317
origin: string
3418
}
35-
| {
36-
name: 'finishEditSummary'
37-
obj: Summary
38-
origin: string
39-
}
4019
| {
4120
name: 'moveNodeAfter' | 'moveNodeBefore' | 'moveNode'
4221
obj: NodeObj
@@ -54,6 +33,23 @@ export type Operation =
5433
objs: NodeObj[]
5534
}
5635

36+
export type SummaryOperation =
37+
| {
38+
name: 'createSummary'
39+
obj: Summary
40+
}
41+
| {
42+
name: 'removeSummary'
43+
obj: { id: string }
44+
}
45+
| {
46+
name: 'finishEditSummary'
47+
obj: Summary
48+
}
49+
50+
export type Operation = NodeOperation | SummaryOperation
51+
export type OperationType = Operation['name']
52+
5753
export type EventMap = {
5854
operation: (info: Operation) => void
5955
selectNode: (nodeObj: NodeObj, e?: MouseEvent) => void

0 commit comments

Comments
 (0)