whole-line-mouse.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { getLineProjection } from "../../../shared";
  2. import { WholeLineAttrib, WholeLinePointAttrib } from "../view";
  3. import {
  4. generateWholeLinePointId,
  5. getWholeLineLine,
  6. getWholeLinePolygonRaw,
  7. mergeChange,
  8. wholeLineAddLineByPointIds,
  9. wholeLineLineAddPoint,
  10. } from "./whole-line-base";
  11. export type MPoint = Omit<WholeLinePointAttrib, "id"> & { id?: string };
  12. // 加点
  13. export const wholeLineAddPoint = <T extends MPoint>(
  14. config: WholeLineAttrib,
  15. pointAttrib: T
  16. ) => {
  17. if ("id" in pointAttrib && pointAttrib["id"]) {
  18. return pointAttrib;
  19. }
  20. const id = generateWholeLinePointId(config);
  21. const point = {
  22. ...pointAttrib,
  23. id,
  24. };
  25. config.points.push(point);
  26. return point;
  27. };
  28. /**
  29. * 线段加点
  30. */
  31. export const wholeLineFixLineAddPoint = <T extends MPoint>(
  32. config: WholeLineAttrib,
  33. lineId: string,
  34. position: T
  35. ) => {
  36. const lineAttrib = getWholeLineLine(config, lineId);
  37. const linePosition = getLineProjection(
  38. lineAttrib.flatMap(({ x, y }) => [x, y]),
  39. [position.x, position.y]
  40. ).point;
  41. const pointAttrib = wholeLineAddPoint(config, {
  42. ...position,
  43. x: linePosition[0],
  44. y: linePosition[1],
  45. });
  46. const { change, addedPoints } = wholeLineLineAddPoint(
  47. config,
  48. lineAttrib,
  49. pointAttrib.id
  50. );
  51. change.pointChange.add.push(pointAttrib as any);
  52. return { change, addedPoints };
  53. };
  54. /**
  55. * 末尾加点
  56. */
  57. export const wholeLinePolygonLastAddPoint = <T extends MPoint>(
  58. config: WholeLineAttrib,
  59. polygonId: string,
  60. pointAttribRaw: T
  61. ) => {
  62. const polyginAttrib = getWholeLinePolygonRaw(config, polygonId)!;
  63. return wholeLinePolygonAddPoint(
  64. config,
  65. polygonId,
  66. polyginAttrib.lineIds[polyginAttrib.lineIds.length - 1],
  67. pointAttribRaw
  68. );
  69. };
  70. /**
  71. * 基于某一点追加点
  72. */
  73. export const wholeLinePolygonAddPoint = <T extends MPoint>(
  74. config: WholeLineAttrib,
  75. polygonId: string,
  76. prevLineId: string | null,
  77. pointAttribRaw: T
  78. ) => {
  79. const polyginAttrib = getWholeLinePolygonRaw(config, polygonId)!;
  80. const lastLine = getWholeLineLine(config, prevLineId);
  81. const pointAttrib = wholeLineAddPoint(config, pointAttribRaw);
  82. let { line, change } = wholeLineAddLineByPointIds(config, [
  83. lastLine[1].id,
  84. pointAttrib.id as string,
  85. ]);
  86. change = mergeChange(change, {
  87. pointChange: {
  88. add: [pointAttrib as any],
  89. },
  90. polygonChange: {
  91. update: [
  92. {
  93. before: { ...polyginAttrib, lineIds: [...polyginAttrib.lineIds] },
  94. after: {
  95. ...polyginAttrib,
  96. lineIds: [...polyginAttrib.lineIds, line.id],
  97. },
  98. },
  99. ],
  100. },
  101. });
  102. polyginAttrib.lineIds.push(line.id);
  103. return { line, change };
  104. };