container.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. const entity = instance as Entity;
  95. if (!this.entrys[key]) {
  96. this.entrys[key] = [];
  97. }
  98. this.entrys[key].push(entity);
  99. const destory = entity.destory.bind(entity);
  100. entity.destory = () => {
  101. destory();
  102. const ndx = this.entrys[key].indexOf(entity);
  103. if (~ndx) {
  104. this.entrys[key].splice(ndx, 1);
  105. }
  106. };
  107. },
  108. (instance) => {
  109. if (!instance.actShape?.active) return;
  110. const entity = instance as Entity;
  111. entity.enableActive((actived) => {
  112. if (actived) {
  113. if (active !== entity) {
  114. this.bus.emit("active", entity);
  115. active = entity;
  116. }
  117. } else {
  118. if (active === entity) {
  119. this.bus.emit("active", null);
  120. active = null;
  121. }
  122. }
  123. });
  124. const destory = entity.destory.bind(entity);
  125. entity.destory = () => {
  126. if (active === entity) {
  127. this.bus.emit("active", null);
  128. active = null;
  129. }
  130. return destory();
  131. };
  132. }
  133. );
  134. }
  135. }
  136. diffRedraw() {
  137. const data = this.attrib.data as any;
  138. const result = {} as {
  139. [key in T]: ReturnType<IncEntitysFactory<R, InstanceType<C>>>;
  140. };
  141. for (const key in data) {
  142. if (key in this.incFactorys) {
  143. result[key] = this.incFactorys[key](data[key]);
  144. }
  145. }
  146. return result;
  147. }
  148. initReactive() {
  149. return watch(
  150. () => {
  151. const result = {} as { [key in T]: string | string[] };
  152. for (const key in this.attrib.data as any) {
  153. const child = this.attrib.data[key];
  154. result[key] = Array.isArray(child)
  155. ? child.map(({ id }) => id)
  156. : child.id;
  157. }
  158. return result;
  159. },
  160. this.diffRedraw.bind(this),
  161. { immediate: true, flush: "sync" }
  162. );
  163. }
  164. // 临时绘制
  165. tempDraw(...shapes: Entity<Attrib, Group | Shape>[]) {
  166. const tels = shapes.map((shape) => shape.teleport);
  167. shapes.forEach((shape) => {
  168. shape.mount(this.tempLayer);
  169. });
  170. return () => {
  171. shapes.forEach((shape, ndx) => {
  172. shape.mount(tels[ndx]);
  173. });
  174. };
  175. }
  176. getPixelFromStage(pos: number[]) {
  177. const tf = getAbsoluteTransform(this.stage, true);
  178. const { x, y } = tf.point({ x: pos[0], y: pos[1] });
  179. return [x, y];
  180. }
  181. getRealFromStage(pos: number[]) {
  182. const tf = getAbsoluteTransform(this.stage, true).invert();
  183. const { x, y } = tf.point({ x: pos[0], y: pos[1] });
  184. return [x, y];
  185. }
  186. private prevCursor: string;
  187. setCursor(ico: string | null) {
  188. const defs = ["move", "inherit", "pointer"];
  189. ico = defs.includes(ico) ? ico : ico ? `url("${ico}"), auto` : "inherit";
  190. this.config.dom.style.cursor = ico;
  191. this.prevCursor = ico;
  192. return () => {
  193. if (this.prevCursor === ico) {
  194. this.config.dom.style.cursor = "inherit";
  195. this.prevCursor = "inherit";
  196. }
  197. };
  198. }
  199. getSameLevelData<T extends Entity>(
  200. entity: T
  201. ): T extends { attrib: infer A } ? A[] : never {
  202. const parentAttrib = this.attrib.data;
  203. for (const key in parentAttrib) {
  204. if (
  205. Array.isArray(parentAttrib[key]) &&
  206. ~(parentAttrib[key] as Attrib[]).indexOf(entity.attrib)
  207. ) {
  208. return parentAttrib[key] as any;
  209. }
  210. }
  211. }
  212. }