Line data Source code
1 : import type { Command, Direction } from '#/history'
2 : import { Dirty } from '#/history'
3 : import type { GroupData, SceneNode } from '#/node'
4 : import { getNodeKind, hasNodeKind } from '#/nodeKind'
5 :
6 8 : function nodeContentRefs(node: SceneNode): string[] {
7 8 : if (!hasNodeKind(node.kind)) return []
8 8 : return getNodeKind(node.kind).contentIds(node)
9 : }
10 :
11 2 : function insert(parent: GroupData, node: SceneNode, index: number): void {
12 2 : parent.children.splice(
13 : Math.max(0, Math.min(index, parent.children.length)),
14 : 0,
15 : node
16 : )
17 : }
18 :
19 2 : function remove(parent: GroupData, node: SceneNode): void {
20 2 : const i = parent.children.indexOf(node)
21 2 : if (i >= 0) parent.children.splice(i, 1)
22 : }
23 :
24 2 : export class AddNodeCommand implements Command {
25 : readonly dirtyMask = Dirty.STRUCTURE
26 : constructor(
27 6 : readonly label: string,
28 6 : private readonly parent: GroupData,
29 6 : private readonly node: SceneNode,
30 6 : private readonly index: number
31 : ) {}
32 : apply(dir: Direction): void {
33 0 : if (dir === 'redo') insert(this.parent, this.node, this.index)
34 0 : else remove(this.parent, this.node)
35 : }
36 : sizeBytes(): number {
37 6 : return 128
38 : }
39 : contentRefs(): string[] {
40 8 : return nodeContentRefs(this.node)
41 : }
42 : }
43 :
44 2 : export class ReorderCommand implements Command {
45 : readonly dirtyMask = Dirty.STRUCTURE
46 : constructor(
47 1 : readonly label: string,
48 1 : private readonly node: SceneNode,
49 1 : private readonly fromParent: GroupData,
50 1 : private readonly fromIndex: number,
51 1 : private readonly toParent: GroupData,
52 1 : private readonly toIndex: number
53 : ) {}
54 : apply(dir: Direction): void {
55 2 : if (dir === 'redo') {
56 1 : remove(this.fromParent, this.node)
57 1 : insert(this.toParent, this.node, this.toIndex)
58 : } else {
59 1 : remove(this.toParent, this.node)
60 1 : insert(this.fromParent, this.node, this.fromIndex)
61 : }
62 : }
63 : sizeBytes(): number {
64 1 : return 128
65 : }
66 : }
|