This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdirect.ts
112 lines (89 loc) · 3.52 KB
/
direct.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
import { Node } from "./Node";
import { Base } from "./Base";
import { Element } from "tree2d";
import { Constructor } from "../utils/TypeUtils";
import { mixin } from "../utils/mixin";
import { DirectContainer } from "./DirectContainer";
import { Rectangle, TextTexture } from "./textures";
import { Container } from "./Container";
import { Paragraph } from "./Paragraph";
export class DirectContainerMixin extends Container {
_appendChild(child: Base) {
// Ignore virtual dom operations.
}
_removeChild(child: Base) {
// Ignore virtual dom operations.
}
_insertBefore(child: Base, anchor: Base) {
// Ignore virtual dom operations.
}
add(item: DirectNode) {
item.parent = this;
this.containerElement.childList.add(item.el);
}
itemInList(item: DirectNode): boolean {
return this.containerElement.childList.itemInList(item.el);
}
addAt(item: DirectNode, index: number) {
item.parent = this;
return this.containerElement.childList.addAt(item.el, index);
}
replace(item: DirectNode, prevItem: DirectNode) {
return this.containerElement.childList.replace(item.el, prevItem.el);
}
setAt(item: DirectNode, index: number) {
item.parent = this;
this.containerElement.childList.setAt(item.el, index);
}
getAt(index: number): DirectNode | undefined {
const el = this.containerElement.childList.getAt(index);
return el ? el.data : undefined;
}
getIndex(item: DirectNode): number {
return this.containerElement.childList.getIndex(item.el);
}
getByRef(ref: string): DirectNode | undefined {
return this.containerElement.childList.getByRef(ref)?.data;
}
remove(item: DirectNode) {
item.parent = undefined;
this.containerElement.childList.remove(item.el);
}
removeAt(index: number) {
const item = this.containerElement.childList.removeAt(index);
item.data.parent = undefined;
}
clear() {
this.containerElement.childList.getItems().forEach((item) => (item.data.parent = undefined));
this.containerElement.childList.clear();
}
getChildren(): Node[] {
return this.containerElement.childList.getItems().map((element: Element) => element.data as DirectNode);
}
getDirectChildren(): DirectNode[] {
return this.getChildren() as DirectNode[];
}
setItems(items: DirectNode[]): DirectNode[][] {
const [removed, added] = this.containerElement.childList.setItems(items.map((item) => item.el));
items.forEach((item) => (item.parent = this));
return [removed.map((e) => e.data as DirectNode), added.map((e) => e.data as DirectNode)];
}
}
export type Direct<T extends Node> = T & DirectContainer;
export type DirectNode = Direct<Container> | TextTexture | Paragraph;
export function getDirectType<T extends Node>(nodeType: Constructor<T>): Constructor<ConvertedDirectNode<T>> {
if (nodeType.prototype instanceof Container) {
if (!(nodeType as any).__direct) {
// Create subclass.
const directType = class extends (nodeType as any) {} as Constructor<T>;
mixin(directType, DirectContainer);
(nodeType as any).__direct = directType;
}
return (nodeType as any).__direct;
} else if ((nodeType as any) === Container) {
return DirectContainer as any;
} else {
return nodeType as any;
}
}
export type ConvertedDirectNode<T extends Node> = T extends Container ? Direct<T> : T;