/
model.ts
978 lines (822 loc) 路 23.4 KB
/
model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
import * as util from '../util'
import { Events } from '../common'
import { Cell } from './cell'
import { Style } from '../types'
import { Geometry } from './geometry'
import { Point } from '../struct'
import {
IChange,
RootChange,
DataChange,
ChildChange,
StyleChange,
TerminalChange,
CollapseChange,
VisibleChange,
GeometryChange,
UndoableEdit,
} from '../change'
export class Model extends Events<Model.EventArgs> {
private root: Cell
private cells: { [id: string]: Cell }
private currentEdit: UndoableEdit
private maintainEdgeParent = true
constructor(root?: Cell) {
super()
this.currentEdit = this.createUndoableEdit()
if (root != null) {
this.setRoot(root)
} else {
this.clear()
}
}
// #region cell id
public cellIdPrefix = 'cell-'
public cellIdPostfix = ''
public autoCreateCellId = true
private nextCellId = 0
private createCellId(cell: Cell) {
const id = this.nextCellId
this.nextCellId += 1
return `${this.cellIdPrefix}${id}${this.cellIdPostfix}`
}
// #endregion
// #region root
clear() {
this.setRoot(this.createRoot())
}
createRoot() {
const root = new Cell()
root.insertChild(this.createLayer())
return root
}
isRoot(cell: Cell | null) {
return cell != null && this.root === cell
}
getRoot(cell?: Cell) {
let root: Cell = this.root
let curr = cell
while (curr != null) {
root = curr
curr = this.getParent(curr) as any
}
return root
}
setRoot(root: Cell | null) {
this.execute(new RootChange(this, root))
}
doRootChange(newRoot: Cell) {
const oldRoot = this.root
this.root = newRoot
this.cells = {}
this.nextCellId = 0
this.cellAdded(newRoot)
return oldRoot
}
// #endregion
// #region layer
createLayer(): Cell {
return new Cell()
}
isLayer(cell: Cell | null): boolean {
const parent = this.getParent(cell)
return parent != null ? this.isRoot(parent) : false
}
getLayers(): Cell[] {
return this.getRoot().children || []
}
eachLayer(
iterator: (layer: Cell, index: number, layers: Cell[]) => void,
context?: any,
) {
util.forEach(this.getLayers(), iterator, context)
}
// #endregion
getCell(id: string | number) {
return this.cells != null ? this.cells[id] : null
}
isNode(cell: Cell | null) {
return cell != null ? cell.isNode() : false
}
isEdge(cell: Cell | null) {
return cell != null ? cell.isEdge() : false
}
getDefaultParent() {
return this.getRoot().getChildAt(0)
}
isOrphan(cell: Cell | null): boolean {
return cell != null ? cell.isOrphan() : true
}
isAncestor(ancestor: Cell, descendant: Cell | null): boolean {
return ancestor.isAncestor(descendant)
}
contains(cell: Cell | null): boolean
contains(ancestor: Cell, descendant?: Cell | null): boolean {
if (descendant === undefined) {
descendant = ancestor // tslint:disable-line:no-parameter-reassignment
ancestor = this.root // tslint:disable-line:no-parameter-reassignment
}
return this.isAncestor(ancestor, descendant)
}
getAncestors(descendant: Cell | null): Cell[] {
return descendant != null ? descendant.getAncestors() : []
}
getDescendants(ancestor: Cell | null): Cell[] {
return ancestor != null ? ancestor.getDescendants() : []
}
filterDescendants(
filter: (cell: Cell) => boolean,
parent: Cell = this.getRoot(),
) {
const result: Cell[] = []
// Checks if the filter returns true for the cell
// and adds it to the result array
if (filter == null || filter(parent)) {
result.push(parent)
}
parent.eachChild(child => {
result.push(...this.filterDescendants(filter, child))
})
return result
}
getParent(cell: Cell | null) {
return cell != null ? cell.getParent() : null
}
getChildren(
parent: Cell | null,
includeNodes: boolean = false,
includeEdges: boolean = false,
) {
const result: Cell[] = []
const children = parent != null ? parent.getChildren() : null
if (children && children.length) {
children.forEach(child => {
if (
(!includeEdges && !includeNodes) ||
(includeEdges && this.isEdge(child)) ||
(includeNodes && this.isNode(child))
) {
result.push(child)
}
})
}
return result
}
getChildNodes(parent: Cell | null) {
return this.getChildren(parent, true, false)
}
getChildEdges(parent: Cell | null) {
return this.getChildren(parent, false, true)
}
getChildCount(cell: Cell | null) {
return cell != null ? cell.getChildCount() : 0
}
getChildAt(cell: Cell | null, index: number) {
return cell != null ? cell.getChildAt(index) : null
}
eachChild(
cell: Cell | null,
iterator: (child: Cell, index: number, children: Cell[]) => void,
thisArg?: any,
) {
if (cell != null) {
cell.eachChild(iterator, thisArg)
}
}
filterCells(
cells: Cell[],
filter: (cell: Cell, index: number, arr: Cell[]) => boolean,
thisArg?: any,
): Cell[] {
return util.filter(cells, filter, thisArg)
}
/**
* Returns the nearest common ancestor for the specified cells.
*/
getNearestCommonAncestor(cell1: Cell | null, cell2: Cell | null) {
return Cell.getNearestCommonAncestor(cell1, cell2)
}
add(parent: Cell | null, child: Cell | null, index?: number) {
if (child !== parent && parent != null && child != null) {
if (index == null) {
// tslint:disable-next-line
index = this.getChildCount(parent)
}
const changed = parent !== this.getParent(child)
this.execute(new ChildChange(this, parent, child, index))
// Maintains the edges parents by moving the edges
// into the nearest common ancestor of its terminals
if (this.maintainEdgeParent && changed) {
this.updateEdgeParents(child)
}
}
return child
}
cellAdded(cell: Cell | null) {
if (cell != null) {
if (cell.getId() == null && this.autoCreateCellId) {
cell.setId(this.createCellId(cell))
}
if (cell.getId() != null) {
let collision = this.getCell(cell.getId()!)
if (collision !== cell) {
// ensure unique id
while (collision != null) {
const id = this.createCellId(cell)
cell.setId(id)
collision = this.getCell(id)
}
if (this.cells == null) {
this.cells = {}
}
this.cells[cell.getId()!] = cell
}
}
const id = cell.getId()
if (id != null && util.isNumeric(id)) {
this.nextCellId = Math.max(this.nextCellId, +id)
}
cell.eachChild(child => this.cellAdded(child))
}
}
protected updateEdgeParents(cell: Cell, ancestor: Cell = this.getRoot(cell)) {
cell.eachChild(child => this.updateEdgeParents(child, ancestor))
cell.eachEdge(edge => {
// Updates edge parent if edge and child have
// a common ancestor node (does not need to be
// the model's root node)
if (this.isAncestor(ancestor, edge)) {
this.updateEdgeParent(edge, ancestor)
}
})
}
protected getNonRelativeTerminal(edge: Cell, isSource: boolean) {
let ret = this.getTerminal(edge, isSource)
while (
ret != null &&
!this.isEdge(ret) &&
ret.geometry != null &&
ret.geometry.relative
) {
ret = this.getParent(ret)
}
return ret
}
protected updateEdgeParent(edge: Cell, ancestor: Cell) {
const source = this.getNonRelativeTerminal(edge, true)
const target = this.getNonRelativeTerminal(edge, false)
if (
this.isAncestor(ancestor, source) &&
this.isAncestor(ancestor, target)
) {
const cell =
source === target
? this.getParent(source)
: this.getNearestCommonAncestor(source, target)
if (
cell != null &&
(this.getParent(cell) !== this.root || this.isAncestor(cell, edge)) &&
this.getParent(edge) !== cell
) {
let geo = this.getGeometry(edge)
if (geo != null) {
const origin1 = this.getOrigin(this.getParent(edge))
const origin2 = this.getOrigin(cell)
const dx = origin2.x - origin1.x
const dy = origin2.y - origin1.y
geo = geo.clone()
geo.translate(-dx, -dy)
this.setGeometry(edge, geo)
}
this.add(cell, edge, this.getChildCount(cell))
}
}
}
protected getOrigin(cell: Cell | null) {
let result: Point
if (cell != null) {
result = this.getOrigin(this.getParent(cell))
if (!this.isEdge(cell)) {
const geo = this.getGeometry(cell)
if (geo != null) {
result.x += geo.bounds.x
result.y += geo.bounds.y
}
}
} else {
result = new Point()
}
return result
}
remove(cell: Cell | null) {
if (cell != null) {
if (cell === this.root) {
this.setRoot(null)
} else if (this.getParent(cell) != null) {
this.execute(new ChildChange(this, null, cell))
}
}
return cell
}
cellRemoved(cell: Cell | null) {
if (cell != null && this.cells != null) {
cell.eachChild(child => this.cellRemoved(child))
const id = cell.getId()
if (id != null) {
delete this.cells[id]
}
}
}
doChildChange(cell: Cell, parent: Cell | null, index?: number) {
const previous = this.getParent(cell)
if (parent != null) {
if (parent !== previous || previous.getChildIndex(cell) !== index) {
parent.insertChild(cell, index)
}
} else if (previous != null) {
previous.removeChild(cell)
}
// Adds or removes the cell from the model
const par = this.contains(parent)
const pre = this.contains(previous)
if (par && !pre) {
this.cellAdded(cell)
} else if (pre && !par) {
this.cellRemoved(cell)
}
return previous
}
getTerminal(edge: Cell | null, isSource?: boolean) {
return edge != null ? edge.getTerminal(isSource) : null
}
setTerminal(edge: Cell, terminal: Cell | null, isSource: boolean = false) {
const changed = terminal !== this.getTerminal(edge, isSource)
this.execute(new TerminalChange(this, edge, terminal, isSource))
if (this.maintainEdgeParent && changed) {
this.updateEdgeParent(edge, this.getRoot())
}
}
doTerminalChange(edge: Cell, terminal: Cell | null, isSource: boolean) {
const previous = this.getTerminal(edge, isSource)
if (terminal != null) {
terminal.insertEdge(edge, isSource)
} else if (previous != null) {
previous.removeEdge(edge, isSource)
}
return previous
}
getTerminals(edge: Cell | null) {
return [this.getTerminal(edge, true), this.getTerminal(edge, false)]
}
setTerminals(edge: Cell, source: Cell, target: Cell) {
this.beginUpdate()
this.setTerminal(edge, source, true)
this.setTerminal(edge, target, false)
this.endUpdate()
}
getEdgeCount(node: Cell | null) {
return node != null ? node.getEdgeCount() : 0
}
/**
* Get the count of incoming or outgoing edges, ignoring the given edge.
*
* @param node The node whose edge count should be returned.
* @param outgoing Specifies if the number of outgoing or incoming
* edges should be returned.
* @param ignore The edge to be ignored.
*/
getDirectedEdgeCount(
node: Cell | null,
outgoing: boolean,
ignore?: Cell | null,
) {
let count = 0
this.eachEdge(node, edge => {
if (edge !== ignore && this.getTerminal(edge, outgoing) === node) {
count += 1
}
})
return count
}
getEdgeAt(node: Cell | null, index: number) {
return node != null ? node.getEdgeAt(index) : null
}
/**
* Get all edges of the given cell without loops.
*/
getConnections(node: Cell | null) {
return this.getEdges(node, true, true, false)
}
/**
* Get the incoming edges of the given cell without loops.
*/
getIncomingEdges(node: Cell | null) {
return this.getEdges(node, true, false, false)
}
/**
* Get the outgoing edges of the given cell without loops.
*/
getOutgoingEdges(node: Cell | null) {
return this.getEdges(node, false, true, false)
}
getEdges(
node: Cell | null,
incoming: boolean = true,
outgoing: boolean = true,
includeLoops: boolean = true,
) {
const result: Cell[] = []
this.eachEdge(node, edge => {
const source = this.getTerminal(edge, true)
const target = this.getTerminal(edge, false)
if (
(includeLoops && source === target) ||
(source !== target &&
((incoming && target === node) || (outgoing && source === node)))
) {
result.push(edge)
}
})
return result
}
eachEdge(
node: Cell | null,
iterator: (edge: Cell, index: number, edges: Cell[]) => void,
thisArg?: any,
) {
if (node != null) {
node.eachEdge(iterator, thisArg)
}
}
/**
* Get all edges between the given source and target node. If `directed`
* is `true`, then only edges from the source to the target are returned,
* otherwise, all edges between the two cells are returned.
*
* @param source The source node of the edge to be returned.
* @param target The target node of the edge to be returned.
* @param directed Optional boolean that specifies if the direction
* of the edge should be taken into account. Default is `false`.
*/
getEdgesBetween(
source: Cell | null,
target: Cell | null,
directed: boolean = false,
) {
const tmp1 = this.getEdgeCount(source)
const tmp2 = this.getEdgeCount(target)
// Assumes the source has less connected edges.
let terminal = source
let count = tmp1
// Uses the smaller array of connected edges for searching the edge.
if (tmp2 < tmp1) {
count = tmp2
terminal = target
}
const result = []
// Checks if the edge is connected to the correct cell
// and collects the first match.
for (let i = 0; i < count; i += 1) {
const edge = this.getEdgeAt(terminal, i)
if (edge != null) {
const [s, t] = this.getTerminals(edge)
const directedMatch = s === source && t === target
const oppositeMatch = t === source && s === target
if (directedMatch || (!directed && oppositeMatch)) {
result.push(edge)
}
}
}
return result
}
getOpposites(
edges: Cell[],
terminal: Cell,
isSource: boolean = true,
isTarget: boolean = true,
) {
const terminals: Cell[] = []
if (edges != null) {
edges.forEach(edge => {
const [source, target] = this.getTerminals(edge)
if (
// Checks if the terminal is the source of the edge
// and if the target should be stored in the result.
source === terminal &&
target != null &&
target !== terminal &&
isTarget
) {
terminals.push(target)
} else if (
// Checks if the terminal is the taget of the edge
// and if the source should be stored in the result.
target === terminal &&
source != null &&
source !== terminal &&
isSource
) {
terminals.push(source)
}
})
}
return terminals
}
getTopmostCells(cells: Cell[]) {
const dict = new WeakMap<Cell, boolean>()
const rest: Cell[] = []
cells.forEach(cell => dict.set(cell, true))
cells.forEach(cell => {
let topmost = true
let parent = this.getParent(cell)
while (parent != null) {
if (dict.get(parent)) {
topmost = false
break
}
parent = this.getParent(parent)
}
if (topmost) {
rest.push(cell)
}
})
return rest
}
getData(cell: Cell | null) {
return cell != null ? cell.getData() : null
}
setData(cell: Cell, data: any) {
this.execute(new DataChange(this, cell, data))
}
doDataChange(cell: Cell, newValue: any) {
const previous = cell.getData()
cell.setData(newValue)
return previous
}
getGeometry(cell: Cell | null) {
return cell != null ? cell.getGeometry() : null
}
setGeometry(cell: Cell, geometry: Geometry) {
if (geometry !== this.getGeometry(cell)) {
this.execute(new GeometryChange(this, cell, geometry))
}
}
doGeometryChange(cell: Cell, geometry: Geometry) {
const previous = this.getGeometry(cell)!
cell.setGeometry(geometry)
return previous
}
getStyle(cell: Cell | null) {
return cell != null ? cell.getStyle() : null
}
setStyle(cell: Cell, style: Style) {
if (style !== this.getStyle(cell)) {
this.execute(new StyleChange(this, cell, style))
}
}
doStyleChange(cell: Cell, style: Style) {
const previous = this.getStyle(cell)!
cell.setStyle(style)
return previous
}
isCollapsed(node: Cell | null) {
return node != null ? node.isCollapsed() : false
}
collapse(node: Cell) {
this.setCollapsed(node, true)
}
expand(node: Cell) {
this.setCollapsed(node, false)
}
toggleCollapse(node: Cell) {
this.isCollapsed(node) ? this.expand(node) : this.collapse(node)
}
setCollapsed(node: Cell, collapsed: boolean) {
if (collapsed !== this.isCollapsed(node)) {
this.execute(new CollapseChange(this, node, collapsed))
}
}
doCollapseChange(node: Cell, collapsed: boolean) {
const previous = this.isCollapsed(node)
node.setCollapsed(collapsed)
return previous
}
isVisible(cell: Cell | null) {
return cell != null ? cell.isVisible() : false
}
show(cell: Cell) {
return this.setVisible(cell, true)
}
hide(cell: Cell) {
return this.setVisible(cell, false)
}
toggleVisible(cell: Cell) {
return this.isVisible(cell) ? this.hide(cell) : this.show(cell)
}
setVisible(cell: Cell, visible: boolean) {
if (visible !== this.isVisible(cell)) {
this.execute(new VisibleChange(this, cell, visible))
}
}
doVisibleChange(cell: Cell, visible: boolean) {
const previous = this.isVisible(cell)
cell.setVisible(visible)
return previous
}
// #region update
private updateLevel: number = 0
private endingUpdate: boolean = false
execute(change: IChange) {
this.trigger('execute', change)
change.execute()
this.trigger('executed', change)
this.beginUpdate()
this.currentEdit.add(change)
this.endUpdate()
}
beginUpdate() {
this.updateLevel += 1
this.trigger('beginUpdate')
if (this.updateLevel === 1) {
this.trigger('startEdit')
}
}
endUpdate() {
this.updateLevel -= 1
if (this.updateLevel === 0) {
this.trigger('endEdit')
}
if (!this.endingUpdate) {
this.endingUpdate = this.updateLevel === 0
const edit = this.currentEdit
this.trigger('endUpdate', edit)
try {
if (this.endingUpdate && !edit.isEmpty()) {
this.trigger('beforeUndo', edit)
this.currentEdit = this.createUndoableEdit()
edit.notify()
this.trigger('afterUndo', edit)
}
} finally {
this.endingUpdate = false
}
}
}
batchUpdate(update: () => void) {
this.beginUpdate()
try {
update()
} finally {
this.endUpdate()
}
}
createUndoableEdit(significant: boolean = true) {
return new UndoableEdit(this, {
significant,
onChange: (edit: UndoableEdit) => {
this.trigger('change', edit.changes)
},
})
}
// #endregion
mergeChildren(from: Cell, to: Cell, cloneAllEdges: boolean = true) {
this.batchUpdate(() => {
const mapping: { [path: string]: Cell } = {}
this.mergeChildrenImpl(from, to, cloneAllEdges, mapping)
// Post-processes all edges in the mapping and reconnects the
// terminals to the corresponding cells in the target model.
for (const key in mapping) {
const cell = mapping[key]
let terminal = this.getTerminal(cell, true)
if (terminal != null) {
terminal = mapping[Cell.getCellPath(terminal)]
this.setTerminal(cell, terminal, true)
}
terminal = this.getTerminal(cell, false)
if (terminal != null) {
terminal = mapping[Cell.getCellPath(terminal)]
this.setTerminal(cell, terminal, false)
}
}
})
}
private mergeChildrenImpl(
from: Cell,
to: Cell,
cloneAllEdges: boolean,
mapping: { [path: string]: Cell },
) {
this.batchUpdate(() => {
from.eachChild(cell => {
const id = cell.getId()
let target =
id != null && (!this.isEdge(cell) || !cloneAllEdges)
? this.getCell(id)
: null
// Clones and adds the child if no cell exists for the id
if (target == null) {
const cloned = cell.clone()
cloned.setId(id)
// Sets the terminals from the original cell to the clone
// because the lookup uses strings not cells in JS
cloned.setTerminal(cell.getTerminal(true), true)
cloned.setTerminal(cell.getTerminal(false), false)
target = to.insertChild(cloned)
this.cellAdded(target)
}
mapping[Cell.getCellPath(cell)] = target!
this.mergeChildrenImpl(cell, target, cloneAllEdges, mapping)
})
})
}
getParents(cells: Cell[]) {
const parents: Cell[] = []
if (cells != null) {
const dict = new WeakMap<Cell, boolean>()
cells.forEach(cell => {
const parent = this.getParent(cell)
if (parent != null && !dict.get(parent)) {
dict.set(parent, true)
parents.push(parent)
}
})
}
return parents
}
cloneCell(cell: Cell) {
if (cell != null) {
return this.cloneCells([cell], true)[0]
}
return null
}
cloneCells(
cells: Cell[],
includeChildren: boolean,
cache: WeakMap<Cell, Cell> = new WeakMap<Cell, Cell>(),
) {
const clones = []
for (let i = 0; i < cells.length; i += 1) {
if (cells[i] != null) {
clones.push(this.cloneCellImpl(cells[i], includeChildren, cache))
} else {
clones.push(null)
}
}
for (let i = 0; i < clones.length; i += 1) {
if (clones[i] != null) {
this.restoreClone(clones[i]!, cells[i], cache)
}
}
return clones
}
private cloneCellImpl(
cell: Cell,
includeChildren: boolean,
cache: WeakMap<Cell, Cell>,
) {
let clone = cache.get(cell)
if (clone == null) {
clone = cell.clone()
cache.set(cell, clone)
if (includeChildren) {
cell.eachChild(child => {
const cloneChild = this.cloneCellImpl(child, true, cache)
clone!.insertChild(cloneChild)
})
}
}
return clone
}
private restoreClone(clone: Cell, cell: Cell, cache: WeakMap<Cell, Cell>) {
const source = this.getTerminal(cell, true)
if (source != null) {
const tmp = cache.get(source)
if (tmp != null) {
tmp.insertEdge(clone, true)
}
}
const target = this.getTerminal(cell, false)
if (target != null) {
const tmp = cache.get(target)
if (tmp != null) {
tmp.insertEdge(clone, false)
}
}
clone.eachChild((c, i) => {
this.restoreClone(c, this.getChildAt(cell, i)!, cache)
})
}
}
export namespace Model {
export interface EventArgs {
change: [IChange[]]
execute: IChange
executed: IChange
beginUpdate?: null
endUpdate: UndoableEdit
startEdit?: null
endEdit?: null
beforeUndo: UndoableEdit
afterUndo: UndoableEdit
}
}