271 lines
8.6 KiB
TypeScript
271 lines
8.6 KiB
TypeScript
"use client";
|
|
import type Geometry from "ol/geom/Geometry.js";
|
|
import type * as React from "react";
|
|
import { createGisEditPreviewStyle } from "@/features/gis-edit/lib/preview-styles";
|
|
import { applyGeometryVertexSnaps } from "@/features/gis-edit/lib/geometry-mutations";
|
|
import { useCallback, useEffect, useRef } from "react";
|
|
import GeoJSON from "ol/format/GeoJSON.js";
|
|
import Draw from "ol/interaction/Draw.js";
|
|
import Snap from "ol/interaction/Snap.js";
|
|
import VectorSource from "ol/source/Vector.js";
|
|
import GisApp from "@common/business_components/ui/map/lib/gis.core";
|
|
import { useGisEdit } from "../components/gis-edit/use-gis-edit";
|
|
import { FLOW_DIRECTION_SRS } from "../model/map-constants";
|
|
|
|
/** 신규 도형 Draw·Snap interaction의 생성과 종료를 소유한다. */
|
|
type Options = {
|
|
gisMaps: React.RefObject<any>;
|
|
gisEdit: ReturnType<typeof useGisEdit>;
|
|
mapReady: boolean;
|
|
removeGisEditInteractions: () => void;
|
|
ensureGisEditPreviewLayer: () => VectorSource<Geometry> | null;
|
|
shouldUsePipeSnapSource: boolean;
|
|
ensureGisEditPipeSnapSource: () => VectorSource<Geometry>;
|
|
setGisEditPreviewCursor: (cursor: string) => void;
|
|
gisEditPreviewLayerRef: React.RefObject<any>;
|
|
openGisEditPipeDetail: (changeId: string | null) => void;
|
|
setFeatureData: React.Dispatch<any>;
|
|
setGisEditDetailChangeId: React.Dispatch<React.SetStateAction<string | null>>;
|
|
setIsOpen: React.Dispatch<React.SetStateAction<boolean>>;
|
|
setIsSearchModalOpen: (open: boolean) => void;
|
|
};
|
|
|
|
/** 신규 도형 Draw·Snap interaction의 생성과 종료를 소유한다. */
|
|
export function useGisEditDraw({
|
|
gisMaps,
|
|
gisEdit,
|
|
mapReady,
|
|
removeGisEditInteractions,
|
|
ensureGisEditPreviewLayer,
|
|
shouldUsePipeSnapSource,
|
|
ensureGisEditPipeSnapSource,
|
|
setGisEditPreviewCursor,
|
|
gisEditPreviewLayerRef,
|
|
openGisEditPipeDetail,
|
|
setFeatureData,
|
|
setGisEditDetailChangeId,
|
|
setIsOpen,
|
|
setIsSearchModalOpen,
|
|
}: Options) {
|
|
// 편집 세션 전체 객체 대신 실제 사용하는 상태와 명령만 의존성으로 추적한다.
|
|
const {
|
|
selectedLayer: editSelectedLayer,
|
|
isEditMode: editIsEditMode,
|
|
activeTool: editActiveTool,
|
|
registerAddGeometry: editRegisterAddGeometry,
|
|
deactivateTool: editDeactivateTool,
|
|
announceStatus: editAnnounceStatus,
|
|
isSnapEnabled: editIsSnapEnabled,
|
|
} = gisEdit;
|
|
|
|
const gisEditDrawRef = useRef<any | null>(null);
|
|
|
|
const gisEditDrawSnapRef = useRef<any | null>(null);
|
|
|
|
const removeGisEditDrawInteraction = useCallback(() => {
|
|
const map = gisMaps.current;
|
|
if (!map) return;
|
|
|
|
if (gisEditDrawSnapRef.current) {
|
|
map.removeInteraction(gisEditDrawSnapRef.current);
|
|
gisEditDrawSnapRef.current = null;
|
|
}
|
|
if (gisEditDrawRef.current) {
|
|
map.removeInteraction(gisEditDrawRef.current);
|
|
gisEditDrawRef.current = null;
|
|
}
|
|
}, [gisMaps]);
|
|
|
|
const gisEditSelectedLayerId = editSelectedLayer?.layerId ?? "";
|
|
|
|
const gisEditSelectedLayerGeometryType =
|
|
editSelectedLayer?.geometryType ?? null;
|
|
|
|
const gisEditSelectedLayerPreviewColors =
|
|
editSelectedLayer?.previewColors ?? null;
|
|
|
|
useEffect(() => {
|
|
if (!mapReady || !gisMaps.current || !editIsEditMode) return;
|
|
|
|
const map = gisMaps.current;
|
|
|
|
if (
|
|
editActiveTool !== "ADD" ||
|
|
!gisEditSelectedLayerId ||
|
|
!gisEditSelectedLayerGeometryType
|
|
) {
|
|
removeGisEditDrawInteraction();
|
|
return;
|
|
}
|
|
|
|
removeGisEditInteractions();
|
|
removeGisEditDrawInteraction();
|
|
|
|
const drawSource = ensureGisEditPreviewLayer();
|
|
if (!drawSource) return;
|
|
const drawType =
|
|
gisEditSelectedLayerGeometryType === "Point"
|
|
? "Point"
|
|
: gisEditSelectedLayerGeometryType === "Polygon"
|
|
? "Polygon"
|
|
: "LineString";
|
|
|
|
const draw = new Draw({
|
|
source: drawSource,
|
|
type: drawType,
|
|
});
|
|
const pipeSnapSource = shouldUsePipeSnapSource
|
|
? ensureGisEditPipeSnapSource()
|
|
: null;
|
|
|
|
draw.on("drawstart", () => {
|
|
setGisEditPreviewCursor("crosshair");
|
|
});
|
|
|
|
draw.on("drawend", (event: any) => {
|
|
try {
|
|
const pipeSnapSource = shouldUsePipeSnapSource
|
|
? ensureGisEditPipeSnapSource()
|
|
: null;
|
|
if (pipeSnapSource?.getFeatures?.().length) {
|
|
applyGeometryVertexSnaps(
|
|
event.feature.getGeometry(),
|
|
pipeSnapSource,
|
|
map,
|
|
20,
|
|
);
|
|
}
|
|
const geometryObject = new GeoJSON().writeGeometryObject(
|
|
event.feature.getGeometry(),
|
|
{
|
|
featureProjection: GisApp.TO_PROJ,
|
|
dataProjection: FLOW_DIRECTION_SRS,
|
|
},
|
|
) as Record<string, unknown>;
|
|
const changeId = editRegisterAddGeometry(geometryObject);
|
|
if (changeId) {
|
|
event.feature.setId(`gis-edit:${changeId}`);
|
|
event.feature.set("fid", changeId);
|
|
event.feature.set("editType", "ADD");
|
|
event.feature.set("objectId", changeId);
|
|
event.feature.set("layerId", gisEditSelectedLayerId);
|
|
event.feature.set("changeId", changeId);
|
|
event.feature.set(
|
|
"layerPointColor",
|
|
gisEditSelectedLayerPreviewColors?.pointColor ?? "",
|
|
);
|
|
event.feature.set(
|
|
"layerLineColor",
|
|
gisEditSelectedLayerPreviewColors?.lineColor ?? "",
|
|
);
|
|
event.feature.set(
|
|
"layerFillColor",
|
|
gisEditSelectedLayerPreviewColors?.fillColor ?? "",
|
|
);
|
|
event.feature.set("isInteractionTarget", false);
|
|
event.feature.set("isDragging", false);
|
|
event.feature.set("previewLabel", "신규 추가");
|
|
event.feature.setStyle(createGisEditPreviewStyle(event.feature));
|
|
gisMaps.current?.renderSync?.();
|
|
window.setTimeout(() => {
|
|
const source = ensureGisEditPreviewLayer();
|
|
if (!source) return;
|
|
if (!source.getFeatureById?.(`gis-edit:${changeId}`)) {
|
|
source.addFeature(event.feature);
|
|
}
|
|
event.feature.changed?.();
|
|
source.changed?.();
|
|
gisEditPreviewLayerRef.current?.changed?.();
|
|
gisMaps.current?.renderSync?.();
|
|
}, 0);
|
|
} else {
|
|
window.setTimeout(() => {
|
|
drawSource.removeFeature(event.feature);
|
|
}, 0);
|
|
}
|
|
if (changeId && gisEditSelectedLayerId === "tb_ms_swage_ppl") {
|
|
openGisEditPipeDetail(changeId);
|
|
setFeatureData({
|
|
lyrTypeNm: "tb_ms_swage_ppl",
|
|
fcltMngNo: "",
|
|
mngInstCd: "",
|
|
fclt_mng_no: "",
|
|
inst_mng_no: "",
|
|
});
|
|
window.setTimeout(() => {
|
|
setGisEditDetailChangeId(changeId);
|
|
setIsOpen(true);
|
|
}, 0);
|
|
setIsSearchModalOpen(false);
|
|
window.setTimeout(() => {
|
|
editDeactivateTool();
|
|
removeGisEditDrawInteraction();
|
|
}, 0);
|
|
editAnnounceStatus(
|
|
"하수관로 신규 객체를 지도에 추가했습니다. 우측 상세정보를 입력한 뒤 저장하세요.",
|
|
);
|
|
}
|
|
} catch (error) {
|
|
console.error("GIS 편집 ADD geometry 변환 실패:", error);
|
|
editAnnounceStatus(
|
|
"신규 geometry를 추가하지 못했습니다. 다시 시도해 주세요.",
|
|
);
|
|
} finally {
|
|
setGisEditPreviewCursor("crosshair");
|
|
}
|
|
});
|
|
|
|
map.addInteraction(draw);
|
|
gisEditDrawRef.current = draw;
|
|
if (pipeSnapSource) {
|
|
const drawSnap = new Snap({
|
|
source: pipeSnapSource,
|
|
edge: true,
|
|
vertex: true,
|
|
pixelTolerance: 14,
|
|
});
|
|
map.addInteraction(drawSnap);
|
|
gisEditDrawSnapRef.current = drawSnap;
|
|
}
|
|
|
|
return () => {
|
|
removeGisEditDrawInteraction();
|
|
};
|
|
}, [
|
|
ensureGisEditPreviewLayer,
|
|
ensureGisEditPipeSnapSource,
|
|
editActiveTool,
|
|
editAnnounceStatus,
|
|
editDeactivateTool,
|
|
editIsEditMode,
|
|
editIsSnapEnabled,
|
|
editRegisterAddGeometry,
|
|
gisEditSelectedLayerGeometryType,
|
|
gisEditSelectedLayerId,
|
|
gisEditSelectedLayerPreviewColors,
|
|
mapReady,
|
|
openGisEditPipeDetail,
|
|
removeGisEditDrawInteraction,
|
|
removeGisEditInteractions,
|
|
setGisEditPreviewCursor,
|
|
shouldUsePipeSnapSource,
|
|
gisMaps,
|
|
gisEditPreviewLayerRef,
|
|
setFeatureData,
|
|
setIsSearchModalOpen,
|
|
setGisEditDetailChangeId,
|
|
setIsOpen,
|
|
]);
|
|
|
|
useEffect(() => {
|
|
if (!editIsEditMode || editActiveTool !== "ADD") {
|
|
removeGisEditDrawInteraction();
|
|
}
|
|
}, [editActiveTool, editIsEditMode, removeGisEditDrawInteraction]);
|
|
useEffect(
|
|
() => () => removeGisEditDrawInteraction(),
|
|
[removeGisEditDrawInteraction],
|
|
);
|
|
return { removeGisEditDrawInteraction };
|
|
}
|