refactor: 캔버스 변경사항 확인 방법을 실시간 오브젝트 이벤트를 감지하도록 수정 변경
This commit is contained in:
parent
f7d0971792
commit
1387c8aa0b
@ -172,7 +172,7 @@ export const QPolygon = fabric.util.createClass(fabric.Polygon, {
|
|||||||
addLengthText() {
|
addLengthText() {
|
||||||
this.canvas
|
this.canvas
|
||||||
?.getObjects()
|
?.getObjects()
|
||||||
.filter((obj) => obj.name === 'lengthText' && obj.parent === this)
|
.filter((obj) => obj.name === 'lengthText' && obj.parentId === this.id)
|
||||||
.forEach((text) => {
|
.forEach((text) => {
|
||||||
this.canvas.remove(text)
|
this.canvas.remove(text)
|
||||||
})
|
})
|
||||||
|
|||||||
@ -2,18 +2,20 @@
|
|||||||
|
|
||||||
import { useEffect, useRef } from 'react'
|
import { useEffect, useRef } from 'react'
|
||||||
|
|
||||||
|
import { useRecoilState, useRecoilValue } from 'recoil'
|
||||||
|
|
||||||
import { useCanvas } from '@/hooks/useCanvas'
|
import { useCanvas } from '@/hooks/useCanvas'
|
||||||
import { useEvent } from '@/hooks/useEvent'
|
import { useEvent } from '@/hooks/useEvent'
|
||||||
import { usePlan } from '@/hooks/usePlan'
|
import { usePlan } from '@/hooks/usePlan'
|
||||||
import { useContextMenu } from '@/hooks/useContextMenu'
|
import { useContextMenu } from '@/hooks/useContextMenu'
|
||||||
import { useRecoilValue } from 'recoil'
|
import { currentObjectState, modifiedPlanFlagState } from '@/store/canvasAtom'
|
||||||
import { currentObjectState } from '@/store/canvasAtom'
|
|
||||||
import { useCanvasEvent } from '@/hooks/useCanvasEvent'
|
import { useCanvasEvent } from '@/hooks/useCanvasEvent'
|
||||||
import QContextMenu from '@/components/common/context-menu/QContextMenu'
|
import QContextMenu from '@/components/common/context-menu/QContextMenu'
|
||||||
import { useCanvasConfigInitialize } from '@/hooks/common/useCanvasConfigInitialize'
|
import { useCanvasConfigInitialize } from '@/hooks/common/useCanvasConfigInitialize'
|
||||||
|
|
||||||
export default function CanvasFrame({ plan }) {
|
export default function CanvasFrame({ plan }) {
|
||||||
const canvasRef = useRef(null)
|
const canvasRef = useRef(null)
|
||||||
|
const [modifiedPlanFlag, setModifiedPlanFlag] = useRecoilState(modifiedPlanFlagState)
|
||||||
const { canvas } = useCanvas('canvas')
|
const { canvas } = useCanvas('canvas')
|
||||||
const { handleZoomClear } = useCanvasEvent()
|
const { handleZoomClear } = useCanvasEvent()
|
||||||
const { contextMenu, currentContextMenu, setCurrentContextMenu, handleClick } = useContextMenu({
|
const { contextMenu, currentContextMenu, setCurrentContextMenu, handleClick } = useContextMenu({
|
||||||
@ -21,7 +23,7 @@ export default function CanvasFrame({ plan }) {
|
|||||||
handleZoomClear,
|
handleZoomClear,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const { checkCanvasObjectEvent, checkUnsavedCanvasPlan } = usePlan()
|
const { checkCanvasObjectEvent, resetModifiedPlans } = usePlan()
|
||||||
const { canvasLoadInit, gridInit } = useCanvasConfigInitialize()
|
const { canvasLoadInit, gridInit } = useCanvasConfigInitialize()
|
||||||
const currentObject = useRecoilValue(currentObjectState)
|
const currentObject = useRecoilValue(currentObjectState)
|
||||||
|
|
||||||
@ -40,8 +42,15 @@ export default function CanvasFrame({ plan }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (modifiedPlanFlag && plan?.id) {
|
||||||
|
checkCanvasObjectEvent(plan.id)
|
||||||
|
}
|
||||||
|
}, [modifiedPlanFlag])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadCanvas()
|
loadCanvas()
|
||||||
|
resetModifiedPlans()
|
||||||
}, [plan, canvas])
|
}, [plan, canvas])
|
||||||
|
|
||||||
const onClickContextMenu = (index) => {}
|
const onClickContextMenu = (index) => {}
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useContext, useEffect, useState } from 'react'
|
import { useContext, useEffect, useState } from 'react'
|
||||||
import { useRecoilValue } from 'recoil'
|
import { useRecoilState, useRecoilValue } from 'recoil'
|
||||||
import CanvasFrame from './CanvasFrame'
|
import CanvasFrame from './CanvasFrame'
|
||||||
import { useMessage } from '@/hooks/useMessage'
|
import { useMessage } from '@/hooks/useMessage'
|
||||||
import { useSwal } from '@/hooks/useSwal'
|
import { useSwal } from '@/hooks/useSwal'
|
||||||
import { usePlan } from '@/hooks/usePlan'
|
import { usePlan } from '@/hooks/usePlan'
|
||||||
|
import { modifiedPlansState } from '@/store/canvasAtom'
|
||||||
import { globalLocaleStore } from '@/store/localeAtom'
|
import { globalLocaleStore } from '@/store/localeAtom'
|
||||||
import { SessionContext } from '@/app/SessionProvider'
|
import { SessionContext } from '@/app/SessionProvider'
|
||||||
|
|
||||||
@ -13,11 +14,12 @@ export default function CanvasLayout(props) {
|
|||||||
const { menuNumber } = props
|
const { menuNumber } = props
|
||||||
const { session } = useContext(SessionContext)
|
const { session } = useContext(SessionContext)
|
||||||
const [objectNo, setObjectNo] = useState('test123240822001') // 이후 삭제 필요
|
const [objectNo, setObjectNo] = useState('test123240822001') // 이후 삭제 필요
|
||||||
|
const [modifiedPlans, setModifiedPlans] = useRecoilState(modifiedPlansState) // 변경된 canvas plan
|
||||||
const globalLocaleState = useRecoilValue(globalLocaleStore)
|
const globalLocaleState = useRecoilValue(globalLocaleStore)
|
||||||
|
|
||||||
const { getMessage } = useMessage()
|
const { getMessage } = useMessage()
|
||||||
const { swalFire } = useSwal()
|
const { swalFire } = useSwal()
|
||||||
const { plans, modifiedPlans, loadCanvasPlanData, handleCurrentPlan, handleAddPlan, handleDeletePlan } = usePlan()
|
const { plans, loadCanvasPlanData, handleCurrentPlan, handleAddPlan, handleDeletePlan } = usePlan()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadCanvasPlanData(session.userId, objectNo)
|
loadCanvasPlanData(session.userId, objectNo)
|
||||||
|
|||||||
@ -1,7 +1,15 @@
|
|||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { useRecoilState, useRecoilValue } from 'recoil'
|
import { useRecoilState, useRecoilValue } from 'recoil'
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
import { canvasSizeState, canvasState, canvasZoomState, currentObjectState, fontFamilyState, fontSizeState } from '@/store/canvasAtom'
|
import {
|
||||||
|
canvasSizeState,
|
||||||
|
canvasState,
|
||||||
|
canvasZoomState,
|
||||||
|
currentObjectState,
|
||||||
|
fontFamilyState,
|
||||||
|
fontSizeState,
|
||||||
|
modifiedPlanFlagState,
|
||||||
|
} from '@/store/canvasAtom'
|
||||||
import { QPolygon } from '@/components/fabric/QPolygon'
|
import { QPolygon } from '@/components/fabric/QPolygon'
|
||||||
|
|
||||||
// 캔버스에 필요한 이벤트
|
// 캔버스에 필요한 이벤트
|
||||||
@ -13,6 +21,7 @@ export function useCanvasEvent() {
|
|||||||
const fontSize = useRecoilValue(fontSizeState)
|
const fontSize = useRecoilValue(fontSizeState)
|
||||||
const fontFamily = useRecoilValue(fontFamilyState)
|
const fontFamily = useRecoilValue(fontFamilyState)
|
||||||
const [canvasZoom, setCanvasZoom] = useRecoilState(canvasZoomState)
|
const [canvasZoom, setCanvasZoom] = useRecoilState(canvasZoomState)
|
||||||
|
const [modifiedPlanFlag, setModifiedPlanFlag] = useRecoilState(modifiedPlanFlagState)
|
||||||
|
|
||||||
// 기본적인 이벤트 필요시 추가
|
// 기본적인 이벤트 필요시 추가
|
||||||
const attachDefaultEventOnCanvas = () => {
|
const attachDefaultEventOnCanvas = () => {
|
||||||
@ -34,14 +43,32 @@ export function useCanvasEvent() {
|
|||||||
onChange: (e) => {
|
onChange: (e) => {
|
||||||
const target = e.target
|
const target = e.target
|
||||||
|
|
||||||
|
if (target.name !== 'mouseLine') {
|
||||||
|
if (!modifiedPlanFlag) {
|
||||||
|
setModifiedPlanFlag((prev) => !prev)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (target) {
|
if (target) {
|
||||||
target.uuid = uuidv4()
|
|
||||||
// settleDown(target)
|
// settleDown(target)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
addEvent: (e) => {
|
addEvent: (e) => {
|
||||||
const target = e.target
|
const target = e.target
|
||||||
|
|
||||||
|
if (!target.id) {
|
||||||
|
target.id = uuidv4()
|
||||||
|
}
|
||||||
|
if (!target.uuid) {
|
||||||
|
target.uuid = uuidv4()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target.name !== 'mouseLine') {
|
||||||
|
if (!modifiedPlanFlag) {
|
||||||
|
setModifiedPlanFlag((prev) => !prev)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (target.type === 'QPolygon' || target.type === 'QLine') {
|
if (target.type === 'QPolygon' || target.type === 'QLine') {
|
||||||
const textObjs = canvas?.getObjects().filter((obj) => obj.name === 'lengthText')
|
const textObjs = canvas?.getObjects().filter((obj) => obj.name === 'lengthText')
|
||||||
textObjs.forEach((obj) => {
|
textObjs.forEach((obj) => {
|
||||||
@ -141,6 +168,9 @@ export function useCanvasEvent() {
|
|||||||
})*/
|
})*/
|
||||||
|
|
||||||
target.on('moving', (e) => {
|
target.on('moving', (e) => {
|
||||||
|
target.uuid = uuidv4()
|
||||||
|
setModifiedPlanFlag((prev) => !prev)
|
||||||
|
|
||||||
if (target.parentDirection === 'left' || target.parentDirection === 'right') {
|
if (target.parentDirection === 'left' || target.parentDirection === 'right') {
|
||||||
const minX = target.minX
|
const minX = target.minX
|
||||||
const maxX = target.maxX
|
const maxX = target.maxX
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { useRecoilState } from 'recoil'
|
import { useRecoilState } from 'recoil'
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
import { v4 as uuidv4, validate as isValidUUID } from 'uuid'
|
||||||
import { canvasState, currentCanvasPlanState, initCanvasPlansState, plansState, modifiedPlansState } from '@/store/canvasAtom'
|
import { canvasState, currentCanvasPlanState, initCanvasPlansState, plansState, modifiedPlansState, modifiedPlanFlagState } from '@/store/canvasAtom'
|
||||||
import { useAxios } from '@/hooks/useAxios'
|
import { useAxios } from '@/hooks/useAxios'
|
||||||
import { useMessage } from '@/hooks/useMessage'
|
import { useMessage } from '@/hooks/useMessage'
|
||||||
import { useSwal } from '@/hooks/useSwal'
|
import { useSwal } from '@/hooks/useSwal'
|
||||||
@ -14,6 +14,7 @@ export function usePlan() {
|
|||||||
const [initCanvasPlans, setInitCanvasPlans] = useRecoilState(initCanvasPlansState) // DB에 저장된 plan
|
const [initCanvasPlans, setInitCanvasPlans] = useRecoilState(initCanvasPlansState) // DB에 저장된 plan
|
||||||
const [plans, setPlans] = useRecoilState(plansState) // 전체 plan (DB에 저장된 plan + 저장 안된 새로운 plan)
|
const [plans, setPlans] = useRecoilState(plansState) // 전체 plan (DB에 저장된 plan + 저장 안된 새로운 plan)
|
||||||
const [modifiedPlans, setModifiedPlans] = useRecoilState(modifiedPlansState) // 변경된 canvas plan
|
const [modifiedPlans, setModifiedPlans] = useRecoilState(modifiedPlansState) // 변경된 canvas plan
|
||||||
|
const [modifiedPlanFlag, setModifiedPlanFlag] = useRecoilState(modifiedPlanFlagState) // 캔버스 실시간 오브젝트 이벤트 감지 flag
|
||||||
|
|
||||||
const { swalFire } = useSwal()
|
const { swalFire } = useSwal()
|
||||||
const { getMessage } = useMessage()
|
const { getMessage } = useMessage()
|
||||||
@ -90,30 +91,29 @@ export function usePlan() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 캔버스에서 발생하는 실시간 오브젝트 이벤트를 감지하여 수정 여부를 판단
|
* 캔버스에서 발생하는 실시간 오브젝트 이벤트를 감지하여 수정 여부를 확인 후 관리
|
||||||
*/
|
*/
|
||||||
const checkCanvasObjectEvent = (e, planId) => {
|
const checkCanvasObjectEvent = (planId) => {
|
||||||
if (!modifiedPlans.some((modifiedPlan) => modifiedPlan === planId) && checkModifiedCanvasPlan(planId)) {
|
if (!modifiedPlans.some((modifiedPlan) => modifiedPlan === planId) && checkModifiedCanvasPlan(planId)) {
|
||||||
setModifiedPlans([...modifiedPlans, planId])
|
setModifiedPlans((prev) => [...prev, planId])
|
||||||
|
setModifiedPlanFlag(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 현재 캔버스 상태와 DB에 저장된 캔버스 상태를 비교하여 수정 여부를 판단
|
* 현재 캔버스 상태와 DB에 저장된 캔버스 상태를 비교하여 수정 여부를 판단
|
||||||
*/
|
*/
|
||||||
const checkModifiedCanvasPlan = (planId) => {
|
const checkModifiedCanvasPlan = (planId) => {
|
||||||
const canvasStatus = currentCanvasData()
|
const canvasStatus = currentCanvasData()
|
||||||
const initPlanData = initCanvasPlans.find((plan) => plan.id === planId)
|
if (isValidUUID(planId)) {
|
||||||
|
|
||||||
if (!initPlanData) {
|
|
||||||
// 새로운 캔버스
|
// 새로운 캔버스
|
||||||
return JSON.parse(canvasStatus).objects.length > 0
|
return JSON.parse(canvasStatus).objects.length > 0
|
||||||
} else {
|
} else {
|
||||||
// 저장된 캔버스
|
// 저장된 캔버스
|
||||||
// 각각 object들의 uuid 목록을 추출하여 비교
|
// 각각 object들의 uuid 목록을 추출하여 비교
|
||||||
const canvasObjsUuids = getObjectUuids(JSON.parse(canvasStatus).objects)
|
const canvasObjsUuids = getObjectUuids(JSON.parse(canvasStatus).objects)
|
||||||
|
const initPlanData = initCanvasPlans.find((plan) => plan.id === planId)
|
||||||
const dbObjsUuids = getObjectUuids(JSON.parse(initPlanData.canvasStatus).objects)
|
const dbObjsUuids = getObjectUuids(JSON.parse(initPlanData.canvasStatus).objects)
|
||||||
return canvasObjsUuids.length !== dbObjsUuids.length || !canvasObjsUuids.every((id, index) => id === dbObjsUuids[index])
|
return canvasObjsUuids.length !== dbObjsUuids.length || !canvasObjsUuids.every((uuid, index) => uuid === dbObjsUuids[index])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const getObjectUuids = (objects) => {
|
const getObjectUuids = (objects) => {
|
||||||
@ -122,10 +122,12 @@ export function usePlan() {
|
|||||||
.map((obj) => obj.uuid)
|
.map((obj) => obj.uuid)
|
||||||
.sort()
|
.sort()
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* 캔버스에 저장되지 않은 변경사항이 있는 경우 저장 여부를 확인 후 저장
|
const resetModifiedPlans = () => {
|
||||||
*/
|
setModifiedPlans([])
|
||||||
const checkUnsavedCanvasPlan = async () => {
|
setModifiedPlanFlag(false)
|
||||||
|
}
|
||||||
|
const checkUnsavedCanvasPlan = (str) => {
|
||||||
if (modifiedPlans.length > 0) {
|
if (modifiedPlans.length > 0) {
|
||||||
swalFire({
|
swalFire({
|
||||||
text: `${currentCanvasPlan.name} ` + getMessage('plan.message.confirm.save'),
|
text: `${currentCanvasPlan.name} ` + getMessage('plan.message.confirm.save'),
|
||||||
@ -365,7 +367,7 @@ export function usePlan() {
|
|||||||
plans,
|
plans,
|
||||||
modifiedPlans,
|
modifiedPlans,
|
||||||
checkCanvasObjectEvent,
|
checkCanvasObjectEvent,
|
||||||
checkUnsavedCanvasPlan,
|
resetModifiedPlans,
|
||||||
saveCanvas,
|
saveCanvas,
|
||||||
handleCurrentPlan,
|
handleCurrentPlan,
|
||||||
handleAddPlan,
|
handleAddPlan,
|
||||||
|
|||||||
@ -275,6 +275,11 @@ export const modifiedPlansState = atom({
|
|||||||
key: 'modifiedPlansState',
|
key: 'modifiedPlansState',
|
||||||
default: [],
|
default: [],
|
||||||
})
|
})
|
||||||
|
// 변경감지 flag
|
||||||
|
export const modifiedPlanFlagState = atom({
|
||||||
|
key: 'modifiedPlanFlagState',
|
||||||
|
default: false,
|
||||||
|
})
|
||||||
|
|
||||||
export const tempGridModeState = atom({
|
export const tempGridModeState = atom({
|
||||||
key: 'tempGridModeState',
|
key: 'tempGridModeState',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user