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 ? D : never; export class EntityGroup< T extends EntityType> > extends Entity< Data[], Group, EntityTree, InstanceType> > { private incFactory: IncEntitysFactory, Data, T>; constructor({ type, ...props }: EntityProps[]> & { type: T }) { super(props); this.incFactory = incEntitysFactoryGenerate(type, this as EntityGroup); 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) { this.attrib.push(data); this.diffRedraw(); } delItem(data: Data) { const ndx = this.attrib.indexOf(data); if (~ndx) { this.attrib.splice(ndx, 1); this.diffRedraw(); } } }