pen-whole-line.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import { Ref, ref } from "vue";
  2. import {
  3. WLL,
  4. WLP,
  5. WLPY,
  6. WholeLine,
  7. WholeLineAttrib,
  8. WholeLineInc,
  9. WholeLineLine,
  10. WholeLineLineAttrib,
  11. WholeLinePoint,
  12. WholeLinePointAttrib,
  13. WholeLinePolygon,
  14. WholeLinePolygonAttrib,
  15. } from "../view";
  16. import {
  17. WholeLineHelperInfoAL,
  18. penWholeLinePoygonsEditWithHelperShapesMouse,
  19. } from "../service/whole-line-helper";
  20. import { lineShapeFactory, polygonShapeFactory } from "../style";
  21. import { getWholeLinePolygonLinesRaw } from "../service";
  22. import { incEntitysFactoryGenerate, openEntityDrag } from "../../../shared";
  23. import { CustomizeShape, ShapeType } from "../../../type";
  24. import { KonvaEventObject } from "konva/lib/Node";
  25. import { EntityEvent } from "../../entity";
  26. export const penLineActShapeFactory = <T extends WholeLineLineAttrib>(
  27. attrib: T,
  28. tree: any
  29. ): CustomizeShape<number[]> => {
  30. const polygons = tree.parent as PenEditWholeLine;
  31. const wll = lineShapeFactory();
  32. const isActive = () => {
  33. if (polygons.editPolygonId.value) {
  34. const lines = getWholeLinePolygonLinesRaw(
  35. polygons.attrib,
  36. polygons.editPolygonId.value
  37. );
  38. if (lines.some(({ id }) => id === attrib.id)) {
  39. return true;
  40. }
  41. }
  42. return false;
  43. };
  44. const common = () => {
  45. const readyDel = polygons.helperInfo?.value?.delLines?.includes(attrib.id);
  46. if (readyDel) {
  47. wll.shape.stroke("rgba(0, 0, 0, 0)");
  48. } else if (isActive()) {
  49. wll.active();
  50. } else {
  51. wll.common();
  52. }
  53. };
  54. return {
  55. shape: wll.shape,
  56. setData(data: number[]) {
  57. wll.setData(data);
  58. common();
  59. },
  60. common,
  61. };
  62. };
  63. const penPolygonActShapeFactoryGen = (autoClose: boolean) => {
  64. return (attrib: WholeLinePolygonAttrib, tree: any) => {
  65. const polygons = tree.parent as PenEditWholeLine;
  66. const isActive = () => polygons.editPolygonId.value === attrib.id;
  67. const wlp = polygonShapeFactory({ autoClose });
  68. const common = (ev?: KonvaEventObject<any>) => {
  69. if (isActive()) {
  70. wlp.active(ev);
  71. } else {
  72. wlp.common(ev);
  73. }
  74. };
  75. return {
  76. shape: wlp.shape,
  77. setData(data: number[]) {
  78. common();
  79. wlp.setData(data);
  80. const needUpdate = polygons.helperInfo?.value?.cloneLines
  81. ? Object.keys(polygons.helperInfo?.value?.cloneLines).includes(
  82. attrib.id
  83. )
  84. : false;
  85. wlp.shape.children[1].visible(!needUpdate);
  86. },
  87. common,
  88. };
  89. };
  90. };
  91. export const closePenPolygonActShapeFactory =
  92. penPolygonActShapeFactoryGen(true);
  93. export const penPolygonActShapeFactory = penPolygonActShapeFactoryGen(false);
  94. export type PenEnterEditProps<P extends WholeLinePointAttrib> = {
  95. polygonId?: string;
  96. canOper?: (
  97. tree: WholeLineLine | WholeLinePoint,
  98. operShape: ShapeType
  99. ) => boolean;
  100. pointAttribFactory?: (pos: number[]) => Omit<P, "id">;
  101. canDelPoint?: (p: P, evt: KonvaEventObject<any>) => boolean;
  102. quitHandler?: () => void;
  103. quotePoint?: boolean;
  104. };
  105. // 钢笔模式 只允许一个多边形一个多边形的编辑
  106. export class PenEditWholeLine<
  107. W extends WholeLineAttrib = WholeLineAttrib,
  108. EV extends EntityEvent = EntityEvent
  109. > extends WholeLine<W, EV> {
  110. editPolygonId = ref<string>();
  111. helperInfo?: Ref<WholeLineHelperInfoAL>;
  112. autoClose = true;
  113. dragAttach(inc: WholeLineInc<W>) {
  114. inc.pointEntityInc.adds.forEach((point) => {
  115. openEntityDrag(point, {
  116. readyHandler: (attrib) => {
  117. return [attrib.x, attrib.y];
  118. },
  119. moveHandler: (pointAttrib, move) => {
  120. if (this.editPolygonId.value) {
  121. pointAttrib.x = move[0];
  122. pointAttrib.y = move[1];
  123. }
  124. },
  125. });
  126. point.enableMouseAct(point.actShape);
  127. });
  128. inc.lineEntityInc.adds.forEach((line) => {
  129. line.enableMouseAct(line.actShape);
  130. });
  131. inc.polygonEntityInc.adds.forEach((py) => {
  132. py.enableMouseAct(py.actShape);
  133. });
  134. }
  135. diffRedraw() {
  136. const inc = super.diffRedraw();
  137. this.dragAttach(inc as any);
  138. return inc;
  139. }
  140. initIncFactory() {
  141. super.initIncFactory();
  142. this.incLinesFactory = incEntitysFactoryGenerate(
  143. WholeLineLine<WLL<W>>,
  144. this,
  145. (line) => {
  146. line.actShapeFactory = penLineActShapeFactory<WLL<W>> as any;
  147. line.setConfig(this.attrib);
  148. }
  149. );
  150. this.incPointsFactory = incEntitysFactoryGenerate(
  151. WholeLinePoint<WLP<W>>,
  152. this
  153. );
  154. this.incPolygonFactory = incEntitysFactoryGenerate(
  155. WholeLinePolygon<WLPY<W>>,
  156. this,
  157. (py) => {
  158. py.actShapeFactory = this.autoClose
  159. ? (closePenPolygonActShapeFactory as any)
  160. : penPolygonActShapeFactory;
  161. py.setConfig(this.attrib);
  162. }
  163. );
  164. }
  165. private _leaveEditMode: (() => void) | void = void 0;
  166. enterEditMode(props: PenEnterEditProps<WLP<W>>) {
  167. this._leaveEditMode && this._leaveEditMode();
  168. this._leaveEditMode = penWholeLinePoygonsEditWithHelperShapesMouse(
  169. {
  170. ...props,
  171. closeAutoQuit: true,
  172. tree: this.container,
  173. autoAdd: false,
  174. autoClose: this.autoClose,
  175. config: this.attrib as any,
  176. changePolygon: (pid: string) => {
  177. this.editPolygonId.value = pid;
  178. },
  179. quitHandler: () => {
  180. props.quitHandler && props.quitHandler();
  181. },
  182. },
  183. this.shape
  184. );
  185. }
  186. leaveEditMode() {
  187. this._leaveEditMode && this._leaveEditMode();
  188. }
  189. destory(): void {
  190. this.leaveEditMode();
  191. super.destory();
  192. }
  193. }