123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { getLineProjection } from "../../../shared";
- import { WholeLineAttrib, WholeLinePointAttrib } from "../view";
- import {
- generateWholeLinePointId,
- getWholeLineLine,
- getWholeLinePolygonRaw,
- mergeChange,
- wholeLineAddLineByPointIds,
- wholeLineLineAddPoint,
- } from "./whole-line-base";
- export type MPoint = Omit<WholeLinePointAttrib, "id"> & { id?: string };
- // 加点
- export const wholeLineAddPoint = <T extends MPoint>(
- config: WholeLineAttrib,
- pointAttrib: T
- ) => {
- if ("id" in pointAttrib && pointAttrib["id"]) {
- return pointAttrib;
- }
- const id = generateWholeLinePointId(config);
- const point = {
- ...pointAttrib,
- id,
- };
- config.points.push(point);
- return point;
- };
- /**
- * 线段加点
- */
- export const wholeLineFixLineAddPoint = <T extends MPoint>(
- config: WholeLineAttrib,
- lineId: string,
- position: T
- ) => {
- const lineAttrib = getWholeLineLine(config, lineId);
- const linePosition = getLineProjection(
- lineAttrib.flatMap(({ x, y }) => [x, y]),
- [position.x, position.y]
- ).point;
- const pointAttrib = wholeLineAddPoint(config, {
- ...position,
- x: linePosition[0],
- y: linePosition[1],
- });
- const { change, addedPoints } = wholeLineLineAddPoint(
- config,
- lineAttrib,
- pointAttrib.id
- );
- change.pointChange.add.push(pointAttrib as any);
- return { change, addedPoints };
- };
- /**
- * 末尾加点
- */
- export const wholeLinePolygonLastAddPoint = <T extends MPoint>(
- config: WholeLineAttrib,
- polygonId: string,
- pointAttribRaw: T
- ) => {
- const polyginAttrib = getWholeLinePolygonRaw(config, polygonId)!;
- return wholeLinePolygonAddPoint(
- config,
- polygonId,
- polyginAttrib.lineIds[polyginAttrib.lineIds.length - 1],
- pointAttribRaw
- );
- };
- /**
- * 基于某一点追加点
- */
- export const wholeLinePolygonAddPoint = <T extends MPoint>(
- config: WholeLineAttrib,
- polygonId: string,
- prevLineId: string | null,
- pointAttribRaw: T
- ) => {
- const polyginAttrib = getWholeLinePolygonRaw(config, polygonId)!;
- const lastLine = getWholeLineLine(config, prevLineId);
- const pointAttrib = wholeLineAddPoint(config, pointAttribRaw);
- let { line, change } = wholeLineAddLineByPointIds(config, [
- lastLine[1].id,
- pointAttrib.id as string,
- ]);
- change = mergeChange(change, {
- pointChange: {
- add: [pointAttrib as any],
- },
- polygonChange: {
- update: [
- {
- before: { ...polyginAttrib, lineIds: [...polyginAttrib.lineIds] },
- after: {
- ...polyginAttrib,
- lineIds: [...polyginAttrib.lineIds, line.id],
- },
- },
- ],
- },
- });
- polyginAttrib.lineIds.push(line.id);
- return { line, change };
- };
|