whole-line-line.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {
  2. Attrib,
  3. CustomizeShape,
  4. CustomizeShapeFactory,
  5. ShapeType,
  6. } from "../../../type";
  7. import { lineShapeFactory, line } from "../style";
  8. import { WholeLineAttrib } from "./whole-line";
  9. import { getWholeLinePoints } from "../service/whole-line-base";
  10. import { Entity, EntityProps } from "../../entity";
  11. import { Line } from "konva/lib/shapes/Line";
  12. export type WholeLineLineAttrib = Attrib & {
  13. pointIds: string[];
  14. };
  15. export type WholeLineLineProps<
  16. T extends WholeLineLineAttrib = WholeLineLineAttrib
  17. > = EntityProps<T>;
  18. export class WholeLineLine<
  19. T extends WholeLineLineAttrib = WholeLineLineAttrib,
  20. R extends ShapeType = Line
  21. > extends Entity<T, R> {
  22. static namespace = "line";
  23. private config: WholeLineAttrib;
  24. actShape: CustomizeShape<number[], R>;
  25. actShapeFactory: CustomizeShapeFactory<T, number[], R>;
  26. constructor(props: WholeLineLineProps<T>) {
  27. props.zIndex = props.zIndex || line.zIndex;
  28. props.name = props.name || WholeLineLine.namespace + props.attrib.id;
  29. super(props);
  30. this.actShapeFactory = lineShapeFactory as CustomizeShapeFactory<
  31. T,
  32. number[],
  33. R
  34. >;
  35. }
  36. setActShapeFactory(actShapeFactory: CustomizeShapeFactory<T, number[], R>) {
  37. this.actShapeFactory = actShapeFactory;
  38. }
  39. initShape() {
  40. this.actShape = this.actShapeFactory(this.attrib, this);
  41. return this.actShape.shape;
  42. }
  43. diffRedraw(): void {
  44. const coords = this.getCoords();
  45. if (coords.length) {
  46. this.actShape.setData(coords);
  47. } else {
  48. console.error("line:", this.attrib, "找不到对应的点坐标", [
  49. ...this.config.points,
  50. ]);
  51. }
  52. }
  53. setConfig(config: WholeLineAttrib) {
  54. this.config = config;
  55. }
  56. getCoords() {
  57. const result: number[] = [];
  58. const points = getWholeLinePoints(this.config, this.attrib.pointIds);
  59. if (!points.some((point) => !point)) {
  60. points.forEach(({ x, y }, ndx) => {
  61. result[ndx * 2] = x;
  62. result[ndx * 2 + 1] = y;
  63. });
  64. }
  65. return result;
  66. }
  67. mounted(): void {
  68. super.mounted();
  69. this.actShape.common();
  70. }
  71. }