container.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { watch } from "vue";
  2. import { Attrib, ShapeType } from "../type";
  3. import { Layer } from "konva/lib/Layer";
  4. import { Entity, EntityProps, EntityType } from "./entity";
  5. import { Stage } from "konva/lib/Stage";
  6. import {
  7. IncEntitysFactory,
  8. incEntitysFactoryGenerate,
  9. } from "../shared/entity-utils";
  10. import { Shape } from "konva/lib/Shape";
  11. import { Group } from "konva/lib/Group";
  12. import { getAbsoluteTransform } from "../shared";
  13. import mitt, { Emitter } from "mitt";
  14. export type ContainerProps<
  15. T extends string = string,
  16. R extends Attrib = Attrib,
  17. S extends ShapeType = ShapeType,
  18. C extends EntityType<R, S> = EntityType<R, S>,
  19. TYPES extends { [key in T]: C } = { [key in T]: C },
  20. DATA extends { [key in T]?: R | R[] } = { [key in T]?: R | R[] }
  21. > = {
  22. types: TYPES;
  23. data?: DATA;
  24. dom: HTMLDivElement;
  25. } & Omit<EntityProps<Attrib & { data: DATA }>, "attrib">;
  26. export class Container<
  27. T extends string = string,
  28. R extends Attrib = Attrib,
  29. S extends ShapeType = ShapeType,
  30. C extends EntityType<R, S> = EntityType<R, S>,
  31. TYPES extends { [key in T]: C } = { [key in T]: C },
  32. DATA extends { [key in T]?: R | R[] } = { [key in T]?: R | R[] },
  33. ENTRYS extends { [key in T]: InstanceType<TYPES[key]>[] } = {
  34. [key in T]: InstanceType<TYPES[key]>[];
  35. }
  36. > extends Entity<Attrib & { data: DATA }, Layer> {
  37. bus: Emitter<{
  38. active: Entity;
  39. dataChange: void;
  40. dataChangeBefore: void;
  41. dataChangeAfter: void;
  42. }>;
  43. tempLayer: Layer;
  44. config: ContainerProps<T, R, S, C, TYPES, DATA>;
  45. entrys = {} as ENTRYS;
  46. stage: Stage;
  47. incFactorys = {} as {
  48. [key in T]: IncEntitysFactory<R, InstanceType<C>>;
  49. };
  50. constructor(config: ContainerProps<T, R, S, C, TYPES, DATA>) {
  51. for (const key in config.types) {
  52. config.data[key as any] = config.data[key] || [];
  53. }
  54. for (const key in config.data) {
  55. if (!(key in config.types)) {
  56. delete config.data[key];
  57. }
  58. }
  59. super({
  60. name: "container",
  61. attrib: { id: "0", data: config.data },
  62. ...config,
  63. });
  64. this.config = config;
  65. this.container = this;
  66. const { dom } = this.config;
  67. this.stage = new Stage({
  68. container: dom,
  69. width: dom.offsetWidth,
  70. height: dom.offsetHeight,
  71. });
  72. this.bus = mitt();
  73. // this.tempLayer = new Layer();
  74. // this.stage.add(this.tempLayer);
  75. }
  76. initShape() {
  77. const viewLayer = new Layer();
  78. return viewLayer;
  79. }
  80. init() {
  81. this.initIncFactory();
  82. super.init();
  83. this.mount(this.stage);
  84. this.mounted();
  85. }
  86. initIncFactory() {
  87. const types = this.config.types as any;
  88. let active: Entity | null = null;
  89. for (const key in types) {
  90. this.incFactorys[key] = incEntitysFactoryGenerate(
  91. types[key],
  92. this,
  93. (instance) => {
  94. if (!this.entrys[key]) {
  95. this.entrys[key] = [];
  96. }
  97. this.entrys[key].push(instance);
  98. },
  99. (instance) => {
  100. if (!instance.actShape?.active) return;
  101. const entity = instance as Entity;
  102. entity.enableActive((actived) => {
  103. if (actived) {
  104. if (active !== entity) {
  105. this.bus.emit("active", entity);
  106. active = entity;
  107. }
  108. } else {
  109. if (active === entity) {
  110. this.bus.emit("active", null);
  111. active = null;
  112. }
  113. }
  114. });
  115. const destory = entity.destory.bind(entity);
  116. entity.destory = () => {
  117. if (active === entity) {
  118. this.bus.emit("active", null);
  119. active = null;
  120. }
  121. return destory();
  122. };
  123. }
  124. );
  125. }
  126. }
  127. diffRedraw() {
  128. const data = this.attrib.data as any;
  129. const result = {} as {
  130. [key in T]: ReturnType<IncEntitysFactory<R, InstanceType<C>>>;
  131. };
  132. for (const key in data) {
  133. if (key in this.incFactorys) {
  134. result[key] = this.incFactorys[key](data[key]);
  135. }
  136. }
  137. return result;
  138. }
  139. initReactive() {
  140. return watch(
  141. () => {
  142. const result = {} as { [key in T]: string | string[] };
  143. for (const key in this.attrib.data as any) {
  144. const child = this.attrib.data[key];
  145. result[key] = Array.isArray(child)
  146. ? child.map(({ id }) => id)
  147. : child.id;
  148. }
  149. return result;
  150. },
  151. this.diffRedraw.bind(this),
  152. { immediate: true, flush: "sync" }
  153. );
  154. }
  155. // 临时绘制
  156. tempDraw(...shapes: Entity<Attrib, Group | Shape>[]) {
  157. const tels = shapes.map((shape) => shape.teleport);
  158. shapes.forEach((shape) => {
  159. shape.mount(this.tempLayer);
  160. });
  161. return () => {
  162. shapes.forEach((shape, ndx) => {
  163. shape.mount(tels[ndx]);
  164. });
  165. };
  166. }
  167. getPixelFromStage(pos: number[]) {
  168. const tf = getAbsoluteTransform(this.stage, true);
  169. const { x, y } = tf.point({ x: pos[0], y: pos[1] });
  170. return [x, y];
  171. }
  172. getRealFromStage(pos: number[]) {
  173. const tf = getAbsoluteTransform(this.stage, true).invert();
  174. const { x, y } = tf.point({ x: pos[0], y: pos[1] });
  175. return [x, y];
  176. }
  177. private prevCursor: string;
  178. setCursor(ico: string | null) {
  179. const defs = ["move", "inherit", "pointer"];
  180. ico = defs.includes(ico) ? ico : ico ? `url("${ico}"), auto` : "inherit";
  181. this.config.dom.style.cursor = ico;
  182. this.prevCursor = ico;
  183. return () => {
  184. if (this.prevCursor === ico) {
  185. this.config.dom.style.cursor = "inherit";
  186. this.prevCursor = "inherit";
  187. }
  188. };
  189. }
  190. getSameLevelData<T extends Entity>(
  191. entity: T
  192. ): T extends { attrib: infer A } ? A[] : never {
  193. const parentAttrib = this.attrib.data;
  194. for (const key in parentAttrib) {
  195. if (
  196. Array.isArray(parentAttrib[key]) &&
  197. ~(parentAttrib[key] as Attrib[]).indexOf(entity.attrib)
  198. ) {
  199. return parentAttrib[key] as any;
  200. }
  201. }
  202. }
  203. }