whole-line-polygon.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { Attrib, CustomizeShape, CustomizeShapeFactory } from "../../../type";
  2. import { polygonShapeFactory, polygon } from "../style";
  3. import { WholeLine, WholeLineAttrib } from "./whole-line";
  4. import { getWholeLinePolygonPoints } from "../service/whole-line-base";
  5. import { Entity, EntityProps } from "../../entity";
  6. import { Line } from "konva/lib/shapes/Line";
  7. import { Shape } from "konva/lib/Shape";
  8. import { Group } from "konva/lib/Group";
  9. export type WholeLinePolygonAttrib = Attrib & {
  10. lineIds: string[];
  11. };
  12. export type WholeLinePolygonProps<
  13. T extends WholeLinePolygonAttrib = WholeLinePolygonAttrib
  14. > = EntityProps<T>;
  15. export class WholeLinePolygon<
  16. T extends WholeLinePolygonAttrib = WholeLinePolygonAttrib,
  17. R extends Shape | Group = Line
  18. > extends Entity<T, R> {
  19. static namespace = "polygon";
  20. get config() {
  21. return (this.parent as WholeLine).attrib;
  22. }
  23. actShape: CustomizeShape<number[], R>;
  24. actShapeFactory: CustomizeShapeFactory<T, number[], R>;
  25. constructor(props: WholeLinePolygonProps<T>) {
  26. props.zIndex = props.zIndex || polygon.zIndex;
  27. props.name = props.name || WholeLinePolygon.namespace + props.attrib.id;
  28. super(props);
  29. this.actShapeFactory =
  30. polygonShapeFactory as unknown as CustomizeShapeFactory<T, number[], R>;
  31. }
  32. setActShapeFactory(actShapeFactory: CustomizeShapeFactory<T, number[], R>) {
  33. this.actShapeFactory = actShapeFactory;
  34. }
  35. initShape() {
  36. this.actShape = this.actShapeFactory(this.attrib, this);
  37. return this.actShape.shape;
  38. }
  39. diffRedraw(): void {
  40. this.actShape.setData(this.getCoords());
  41. }
  42. getCoords() {
  43. const result: number[] = [];
  44. const points = getWholeLinePolygonPoints(this.config, this.attrib.id);
  45. if (!points.some((point) => !point)) {
  46. points.forEach(({ x, y }, ndx) => {
  47. result[ndx * 2] = x;
  48. result[ndx * 2 + 1] = y;
  49. });
  50. }
  51. return result;
  52. }
  53. mounted(): void {
  54. super.mounted();
  55. this.actShape.common(null);
  56. }
  57. }