whole-line-edit.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import {
  2. WholeLineAttrib,
  3. WholeLineLine,
  4. WholeLinePoint,
  5. WholeLinePointAttrib,
  6. WholeLinePolygonAttrib,
  7. } from "../view";
  8. import { KonvaEventObject } from "konva/lib/Node";
  9. import { shapeParentsEq } from "../../../shared";
  10. import {
  11. wholeLineAddPoint,
  12. wholeLineFixLineAddPoint,
  13. wholeLinePolygonLastAddPoint,
  14. } from "./whole-line-mouse";
  15. import { Container } from "../../container";
  16. import { Attrib, ShapeType } from "../../../type";
  17. import {
  18. WholeLineChange,
  19. generateWholeLinePoygonId,
  20. getWholeLinePoint,
  21. getWholeLinePolygonPoints,
  22. mergeChange,
  23. wholeLineAddLineByPointIds,
  24. wholeLineDelPointByPointIds,
  25. } from "./whole-line-base";
  26. import { getAdsorbPosition } from "../../../shared/adsorb";
  27. export type PenWholeLinePoygonsEditProps<
  28. P extends Omit<WholeLinePointAttrib, "id">
  29. > = {
  30. tree: Container;
  31. config: WholeLineAttrib<P & Attrib>;
  32. polygonId?: string;
  33. adsorbRadius?: number;
  34. pointAttribFactory?: (pos: number[]) => Omit<P, "id">;
  35. quotePoint?: boolean;
  36. canOper?: (
  37. tree: WholeLineLine | WholeLinePoint,
  38. operShape: ShapeType
  39. ) => boolean;
  40. canDelPoint?: (point: P, evt: KonvaEventObject<any>) => boolean;
  41. changePolygon?: (polygonId: string) => void;
  42. autoClose?: boolean;
  43. };
  44. /**
  45. * 钢笔模式编辑多边形
  46. * @param polygonId
  47. */
  48. export const penWholeLinePoygonsEdit = <
  49. P extends Omit<WholeLinePointAttrib, "id">
  50. >({
  51. tree,
  52. config,
  53. polygonId,
  54. pointAttribFactory,
  55. quotePoint,
  56. canOper,
  57. adsorbRadius,
  58. canDelPoint,
  59. changePolygon,
  60. autoClose,
  61. }: PenWholeLinePoygonsEditProps<P>) => {
  62. pointAttribFactory =
  63. pointAttribFactory ||
  64. ((pos: number[]) =>
  65. ({
  66. x: pos[0],
  67. y: pos[1],
  68. } as P));
  69. const getPolygonAttrib = (polygonId: string) => {
  70. if (!polygonId) {
  71. const id = generateWholeLinePoygonId(config);
  72. config.polygons.push({
  73. id,
  74. lineIds: [],
  75. });
  76. return config.polygons[config.polygons.length - 1];
  77. } else {
  78. return config.polygons.find(({ id }) => id === polygonId);
  79. }
  80. };
  81. let polyginAttrib: WholeLinePolygonAttrib;
  82. let newMode = false;
  83. let prevId: string | null = null;
  84. const start = (currentPolygonId: string | null) => {
  85. polyginAttrib = getPolygonAttrib(currentPolygonId);
  86. if (!polyginAttrib) {
  87. throw `${currentPolygonId}的多边形不存在!`;
  88. }
  89. changePolygon && changePolygon(polyginAttrib.id);
  90. polygonId = polyginAttrib.id;
  91. newMode = !currentPolygonId;
  92. prevId = null;
  93. };
  94. start(polygonId);
  95. const removePolygon = () => {
  96. const ndx = config.polygons.indexOf(polyginAttrib);
  97. if (~ndx) {
  98. config.polygons.splice(ndx, 1);
  99. }
  100. };
  101. const continuous = (
  102. evt: KonvaEventObject<any>
  103. ): { change: WholeLineChange; isClose?: boolean } => {
  104. let change: WholeLineChange = {
  105. lineChange: {
  106. del: [],
  107. update: [],
  108. add: [],
  109. },
  110. polygonChange: {
  111. add: [],
  112. update: [],
  113. del: [],
  114. },
  115. pointChange: {
  116. add: [],
  117. del: [],
  118. },
  119. };
  120. const target = shapeParentsEq(evt.target, (shape) => {
  121. const id = shape.id();
  122. return (
  123. id.includes(WholeLineLine.namespace) ||
  124. id.includes(WholeLinePoint.namespace)
  125. );
  126. });
  127. const child = target && tree.find(target.id());
  128. const pixel = [evt.evt.offsetX, evt.evt.offsetY];
  129. if (child instanceof WholeLineLine) {
  130. if (!canOper || canOper(child, evt.target)) {
  131. if (polyginAttrib.lineIds.includes(child.attrib.id)) {
  132. const { change: cChange } = wholeLineFixLineAddPoint(
  133. config,
  134. child.attrib.id,
  135. pointAttribFactory(tree.getRealFromStage(pixel))
  136. );
  137. return { change: mergeChange(change, cChange), isClose: false };
  138. }
  139. }
  140. }
  141. let pointAttrib: P & Attrib;
  142. if (child instanceof WholeLinePoint) {
  143. if (canOper && !canOper(child, evt.target)) {
  144. return { change, isClose: false };
  145. }
  146. pointAttrib = quotePoint ? child.attrib : { ...child.attrib };
  147. if (polyginAttrib.id === prevId) {
  148. return { change, isClose: false };
  149. }
  150. }
  151. const polygonPoints = getWholeLinePolygonPoints(config, polyginAttrib.id);
  152. if (!pointAttrib) {
  153. let position: number[];
  154. if (adsorbRadius) {
  155. const points = polygonPoints.map(({ x, y }) => [x, y]);
  156. if (prevId) {
  157. const prev = getWholeLinePoint(config, prevId);
  158. points.push([prev.x, prev.y]);
  159. }
  160. position = getAdsorbPosition({
  161. tree,
  162. pixel,
  163. radius: adsorbRadius,
  164. points,
  165. });
  166. } else {
  167. position = tree.getRealFromStage(pixel);
  168. }
  169. pointAttrib = pointAttribFactory(position) as P & Attrib;
  170. }
  171. const curNdx = polygonPoints.findIndex(({ id }) => id === pointAttrib.id);
  172. const isClose = curNdx === 0 && (!autoClose || polygonPoints.length >= 3);
  173. // 存在的情况下删除
  174. if (curNdx >= 0 && !isClose) {
  175. const cChange = wholeLineDelPointByPointIds(
  176. config,
  177. pointAttrib.id,
  178. !canDelPoint || canDelPoint(pointAttrib, evt)
  179. );
  180. change = mergeChange(change, cChange);
  181. if (polyginAttrib.lineIds.length === 0) {
  182. removePolygon();
  183. const repPoint = cChange.pointChange.del.find(
  184. ({ id }) => id !== pointAttrib.id
  185. );
  186. delete repPoint.id;
  187. start(null);
  188. prevId = wholeLineAddPoint(config, repPoint).id;
  189. change.pointChange.add.push({
  190. ...repPoint,
  191. id: prevId,
  192. });
  193. }
  194. return { change, isClose };
  195. }
  196. if (!quotePoint) {
  197. delete pointAttrib.id;
  198. }
  199. if (polyginAttrib.lineIds.length > 0) {
  200. if (newMode) {
  201. const mChange = wholeLinePolygonLastAddPoint(
  202. config,
  203. polygonId,
  204. pointAttrib
  205. ).change;
  206. // 持续添加模式
  207. return { change: mergeChange(change, mChange), isClose };
  208. } else {
  209. // 直接当成新建操作
  210. start(null);
  211. return continuous(evt);
  212. }
  213. } else if (prevId) {
  214. const { line, change: mChange } = wholeLineAddLineByPointIds(config, [
  215. prevId,
  216. wholeLineAddPoint(config, pointAttrib).id,
  217. ]);
  218. change = mergeChange(change, mChange, {
  219. polygonChange: {
  220. update: [
  221. {
  222. before: { ...polyginAttrib, lineIds: [...polyginAttrib.lineIds] },
  223. after: {
  224. ...polyginAttrib,
  225. lineIds: [...polyginAttrib.lineIds, line.id],
  226. },
  227. },
  228. ],
  229. },
  230. pointChange: {
  231. add: [pointAttrib],
  232. },
  233. });
  234. polyginAttrib.lineIds.push(line.id);
  235. prevId = null;
  236. return { change, isClose };
  237. } else {
  238. const addPoint = wholeLineAddPoint(config, pointAttrib)!;
  239. prevId = addPoint.id;
  240. change.pointChange.add.push(addPoint);
  241. return { change, isClose };
  242. }
  243. };
  244. const end = () => {
  245. // 没有两个点以上的多边形直接删除
  246. if (polyginAttrib.lineIds.length === 0) {
  247. if (prevId) {
  248. wholeLineDelPointByPointIds(config, prevId);
  249. }
  250. removePolygon();
  251. }
  252. changePolygon && changePolygon(null);
  253. };
  254. const getStatus = () => ({
  255. newMode,
  256. polyginAttribId: polyginAttrib.id,
  257. prevId,
  258. config,
  259. });
  260. return {
  261. continuous,
  262. end,
  263. getStatus,
  264. setStatus: (status: ReturnType<typeof getStatus>) => {
  265. newMode = status.newMode;
  266. polyginAttrib = status.config.polygons.find(
  267. ({ id }) => id === status.polyginAttribId
  268. );
  269. polygonId = polyginAttrib.id;
  270. prevId = status.prevId;
  271. config = status.config;
  272. },
  273. };
  274. };