1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import { Group } from "konva/lib/Group";
- import {
- Entity,
- EntityProps,
- EntityShape,
- EntityTree,
- EntityType,
- } from "./entity";
- import { IncEntitysFactory, incEntitysFactoryGenerate } from "./entity-factory";
- type Data<T extends EntityType> = T extends EntityType<infer D> ? D : never;
- export class EntityGroup<
- T extends EntityType<any, EntityShape, EntityTree<any, any>>
- > extends Entity<
- Data<T>[],
- Group,
- EntityTree<EntityGroup<T>, InstanceType<T>>
- > {
- private incFactory: IncEntitysFactory<EntityGroup<T>, Data<T>, T>;
- constructor({ type, ...props }: EntityProps<Data<T>[]> & { type: T }) {
- super(props);
- this.incFactory = incEntitysFactoryGenerate(type, this as EntityGroup<T>);
- this.diffRedraw();
- }
- diffRedraw() {
- const { adds } = this.incFactory(this.attrib);
- adds.forEach((add) => {
- add.bus.on("destroyed", () => {
- const ndx = this.attrib.indexOf(add.attrib);
- if (~ndx) {
- this.attrib.splice(ndx, 1);
- }
- });
- add.bus.on("updateAttrib", ([newAttrib, oldAttrib]) => {
- const ndx = this.attrib.indexOf(oldAttrib);
- if (~ndx) {
- this.attrib[ndx] = newAttrib;
- }
- });
- });
- }
- addItem(data: Data<T>) {
- this.attrib.push(data);
- this.diffRedraw();
- }
- delItem(data: Data<T>) {
- const ndx = this.attrib.indexOf(data);
- if (~ndx) {
- this.attrib.splice(ndx, 1);
- this.diffRedraw();
- }
- }
- }
|