이미지 업데이트 작업중
This commit is contained in:
parent
eae52410e7
commit
71bc5a55ef
@ -1,27 +1,15 @@
|
|||||||
'use server'
|
'use server'
|
||||||
|
|
||||||
import fs from 'fs/promises'
|
|
||||||
|
|
||||||
import { NextResponse } from 'next/server'
|
import { NextResponse } from 'next/server'
|
||||||
import { writeImage } from '@/lib/fileAction'
|
import { writeImage } from '@/lib/fileAction'
|
||||||
|
|
||||||
export async function POST(req) {
|
export async function POST(req) {
|
||||||
const path = 'public/plan-bg-images'
|
|
||||||
|
|
||||||
const formData = await req.formData()
|
const formData = await req.formData()
|
||||||
const file = formData.get('file')
|
const file = formData.get('file')
|
||||||
const fileName = formData.get('fileName')
|
const fileName = formData.get('fileName')
|
||||||
const arrayBuffer = await file.arrayBuffer()
|
const arrayBuffer = await file.arrayBuffer()
|
||||||
const buffer = Buffer.from(arrayBuffer)
|
const buffer = Buffer.from(arrayBuffer)
|
||||||
// const buffer = new Uint8Array(arrayBuffer)
|
|
||||||
await writeImage(fileName, buffer)
|
await writeImage(fileName, buffer)
|
||||||
// try {
|
|
||||||
// await fs.readdir(path)
|
|
||||||
// } catch {
|
|
||||||
// await fs.mkdir(path)
|
|
||||||
// } finally {
|
|
||||||
// await fs.writeFile(`${path}/${fileName}`, buffer)
|
|
||||||
// }
|
|
||||||
|
|
||||||
return NextResponse.json({ fileNm: `${fileName}` })
|
return NextResponse.json({ fileNm: `${fileName}` })
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,9 @@ import { useRefFiles } from '@/hooks/common/useRefFiles'
|
|||||||
import { usePlan } from '@/hooks/usePlan'
|
import { usePlan } from '@/hooks/usePlan'
|
||||||
|
|
||||||
import WithDraggable from '@/components/common/draggable/WithDraggable'
|
import WithDraggable from '@/components/common/draggable/WithDraggable'
|
||||||
|
import { useCanvas } from '@/hooks/useCanvas'
|
||||||
|
import { useRecoilValue } from 'recoil'
|
||||||
|
import { canvasState } from '@/store/canvasAtom'
|
||||||
|
|
||||||
export default function ImgLoad() {
|
export default function ImgLoad() {
|
||||||
const {
|
const {
|
||||||
@ -24,13 +27,21 @@ export default function ImgLoad() {
|
|||||||
handleMapImageDown,
|
handleMapImageDown,
|
||||||
handleAddressDelete,
|
handleAddressDelete,
|
||||||
} = useRefFiles()
|
} = useRefFiles()
|
||||||
const { currentCanvasPlan } = usePlan()
|
const { currentCanvasPlan, setCurrentCanvasPlan } = usePlan()
|
||||||
const { getMessage } = useMessage()
|
const { getMessage } = useMessage()
|
||||||
|
const canvas = useRecoilValue(canvasState)
|
||||||
const { floorPlanState, setFloorPlanState } = useContext(FloorPlanContext)
|
const { floorPlanState, setFloorPlanState } = useContext(FloorPlanContext)
|
||||||
|
|
||||||
const handleModal = () => {
|
const handleModal = () => {
|
||||||
console.log(floorPlanState)
|
console.log('floorPlanState', floorPlanState)
|
||||||
setFloorPlanState({ ...floorPlanState, refFileModalOpen: false })
|
console.log('currentCanvasPlan', currentCanvasPlan)
|
||||||
|
console.log('refImage', refImage)
|
||||||
|
setFloorPlanState({ ...floorPlanState, refFileModalOpen: false, toggleRotate: false })
|
||||||
|
setCurrentCanvasPlan({
|
||||||
|
...currentCanvasPlan,
|
||||||
|
bgImageName: currentCanvasPlan?.bgImageName ?? null,
|
||||||
|
mapPositionAddress,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -39,8 +50,26 @@ export default function ImgLoad() {
|
|||||||
}, [floorPlanState.refFileModalOpen])
|
}, [floorPlanState.refFileModalOpen])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const refFileMethod = currentCanvasPlan?.mapPositionAddress === null ? '1' : '2'
|
const backGroundImage = canvas?.getObjects().filter((obj) => obj.name === 'backGroundImage')
|
||||||
|
if (backGroundImage && backGroundImage.length === 1) {
|
||||||
|
backGroundImage[0].set({
|
||||||
|
lockMovementX: !floorPlanState.toggleRotate,
|
||||||
|
lockMovementY: !floorPlanState.toggleRotate,
|
||||||
|
lockRotation: !floorPlanState.toggleRotate,
|
||||||
|
lockScalingX: !floorPlanState.toggleRotate,
|
||||||
|
lockScalingY: !floorPlanState.toggleRotate,
|
||||||
|
selectable: floorPlanState.toggleRotate,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, [floorPlanState.toggleRotate])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!currentCanvasPlan) return
|
||||||
|
console.log('currentCanvasPlan', currentCanvasPlan)
|
||||||
|
const refFileMethod = currentCanvasPlan?.mapPositionAddress === null || currentCanvasPlan?.mapPositionAddress?.trim() === '' ? '1' : '2'
|
||||||
setRefFileMethod(refFileMethod)
|
setRefFileMethod(refFileMethod)
|
||||||
|
setMapPositionAddress(currentCanvasPlan?.mapPositionAddress ?? '')
|
||||||
|
console.log(currentCanvasPlan)
|
||||||
}, [currentCanvasPlan])
|
}, [currentCanvasPlan])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
import { useEffect, useRef, useState } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
import { useRecoilState } from 'recoil'
|
import { useRecoilState, useRecoilValue } from 'recoil'
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
|
|
||||||
import { useSwal } from '@/hooks/useSwal'
|
import { useSwal } from '@/hooks/useSwal'
|
||||||
import { useAxios } from '../useAxios'
|
import { useAxios } from '../useAxios'
|
||||||
import { currentCanvasPlanState } from '@/store/canvasAtom'
|
import { canvasState, currentCanvasPlanState } from '@/store/canvasAtom'
|
||||||
import { convertDwgToPng, removeImage, writeImageBuffer } from '@/lib/fileAction'
|
import { convertDwgToPng, removeImage, writeImageBuffer } from '@/lib/fileAction'
|
||||||
|
import { useCanvas } from '@/hooks/useCanvas'
|
||||||
|
|
||||||
export function useRefFiles() {
|
export function useRefFiles() {
|
||||||
const converterUrl = process.env.NEXT_PUBLIC_CONVERTER_API_URL
|
const converterUrl = process.env.NEXT_PUBLIC_CONVERTER_API_URL
|
||||||
@ -14,14 +15,10 @@ export function useRefFiles() {
|
|||||||
const [mapPositionAddress, setMapPositionAddress] = useState('')
|
const [mapPositionAddress, setMapPositionAddress] = useState('')
|
||||||
const [currentCanvasPlan, setCurrentCanvasPlan] = useRecoilState(currentCanvasPlanState)
|
const [currentCanvasPlan, setCurrentCanvasPlan] = useRecoilState(currentCanvasPlanState)
|
||||||
const queryRef = useRef(null)
|
const queryRef = useRef(null)
|
||||||
|
const canvas = useRecoilValue(canvasState)
|
||||||
|
|
||||||
const { swalFire } = useSwal()
|
const { swalFire } = useSwal()
|
||||||
const { get, promisePut, promisePost } = useAxios()
|
const { get, promisePut, promiseGet, promisePost } = useAxios()
|
||||||
// const { currentCanvasPlan, setCurrentCanvasPlan } = usePlan()
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
console.log('refImage', refImage)
|
|
||||||
}, [refImage])
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (refFileMethod === '1') {
|
if (refFileMethod === '1') {
|
||||||
@ -41,6 +38,8 @@ export function useRefFiles() {
|
|||||||
if (file && ['image/png', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/gif'].includes(file.type)) {
|
if (file && ['image/png', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/gif'].includes(file.type)) {
|
||||||
// setRefImage(file)
|
// setRefImage(file)
|
||||||
file.name.split('.').pop() === 'dwg' ? handleUploadConvertRefFile(file) : handleUploadImageRefFile(file)
|
file.name.split('.').pop() === 'dwg' ? handleUploadConvertRefFile(file) : handleUploadImageRefFile(file)
|
||||||
|
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
swalFire({
|
swalFire({
|
||||||
text: '이미지가 아닙니다.',
|
text: '이미지가 아닙니다.',
|
||||||
@ -54,6 +53,11 @@ export function useRefFiles() {
|
|||||||
type: 'confirm',
|
type: 'confirm',
|
||||||
confirmFn: () => {
|
confirmFn: () => {
|
||||||
setRefImage(file)
|
setRefImage(file)
|
||||||
|
const backGroundImage = canvas.getObjects().filter((obj) => obj.name === 'backGroundImage')
|
||||||
|
if (backGroundImage.length > 0) {
|
||||||
|
canvas.remove(backGroundImage)
|
||||||
|
}
|
||||||
|
canvas.renderAll()
|
||||||
file.name.split('.').pop() === 'dwg' ? handleUploadConvertRefFile(file) : handleUploadImageRefFile(file)
|
file.name.split('.').pop() === 'dwg' ? handleUploadConvertRefFile(file) : handleUploadImageRefFile(file)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -109,16 +113,20 @@ export function useRefFiles() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await get({ url: `http://localhost:3000/api/html2canvas?q=${queryRef.current.value}&fileNm=${currentCanvasPlan.id}&zoom=20` })
|
const res = await promiseGet({
|
||||||
// console.log('🚀 ~ handleMapImageDown ~ res:', res)
|
url: `http://localhost:3000/api/html2canvas?q=${queryRef.current.value}&fileNm=${currentCanvasPlan.id}&zoom=20`,
|
||||||
console.log('currentCanvasPlan', currentCanvasPlan)
|
}).then((req, res) => {
|
||||||
setCurrentCanvasPlan((prev) => ({ ...prev, bgImageName: currentCanvasPlan.id, mapPositionAddress: queryRef.current.value }))
|
console.log('handleMapImageDown', res)
|
||||||
|
handleBackImageLoadToCanvas(`plan-images/${currentCanvasPlan.id}.png`)
|
||||||
|
setCurrentCanvasPlan((prev) => ({ ...prev, bgImageName: currentCanvasPlan.id, mapPositionAddress: queryRef.current.value }))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 이미지 파일 업로드
|
* 이미지 파일 업로드
|
||||||
* @param {*} file
|
* @param {*} file
|
||||||
*/
|
*/
|
||||||
|
const { handleBackImageLoadToCanvas } = useCanvas()
|
||||||
const handleUploadImageRefFile = async (file) => {
|
const handleUploadImageRefFile = async (file) => {
|
||||||
// console.log('🚀 ~ handleUploadImageRefFile ~ file:', file)
|
// console.log('🚀 ~ handleUploadImageRefFile ~ file:', file)
|
||||||
const formData = new FormData()
|
const formData = new FormData()
|
||||||
@ -130,6 +138,7 @@ export function useRefFiles() {
|
|||||||
body: formData,
|
body: formData,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
handleBackImageLoadToCanvas(`plan-images/${currentCanvasPlan.id}.png`)
|
||||||
const result = await response.json()
|
const result = await response.json()
|
||||||
setRefImage(file)
|
setRefImage(file)
|
||||||
// console.log('🚀 ~ handleUploadImageRefFile ~ res:', result)
|
// console.log('🚀 ~ handleUploadImageRefFile ~ res:', result)
|
||||||
|
|||||||
@ -509,16 +509,34 @@ export function useCanvas(id) {
|
|||||||
const handleBackImageLoadToCanvas = (url) => {
|
const handleBackImageLoadToCanvas = (url) => {
|
||||||
console.log('image load url: ', url)
|
console.log('image load url: ', url)
|
||||||
|
|
||||||
|
canvas
|
||||||
|
.getObjects()
|
||||||
|
.filter((obj) => obj.name === 'backGroundImage')
|
||||||
|
.forEach((img) => {
|
||||||
|
canvas.remove(img)
|
||||||
|
canvas?.renderAll()
|
||||||
|
})
|
||||||
fabric.Image.fromURL(url, function (img) {
|
fabric.Image.fromURL(url, function (img) {
|
||||||
|
console.log(img)
|
||||||
img.set({
|
img.set({
|
||||||
left: 0,
|
left: 0,
|
||||||
top: 0,
|
top: 0,
|
||||||
width: 1500,
|
width: img.width,
|
||||||
height: 1500,
|
height: img.height,
|
||||||
selectable: true,
|
name: 'backGroundImage',
|
||||||
|
selectable: false,
|
||||||
|
hasRotatingPoint: false, // 회전 핸들 활성화
|
||||||
|
lockMovementX: false,
|
||||||
|
lockMovementY: false,
|
||||||
|
lockRotation: false,
|
||||||
|
lockScalingX: false,
|
||||||
|
lockScalingY: false,
|
||||||
})
|
})
|
||||||
canvas.add(img)
|
// image = img
|
||||||
canvas.renderAll()
|
canvas?.add(img)
|
||||||
|
canvas?.sendToBack(img)
|
||||||
|
canvas?.renderAll()
|
||||||
|
console.log(canvas.getObjects().filter((obj) => obj.name === 'backGroundImage'))
|
||||||
setBackImg(img)
|
setBackImg(img)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,8 @@ 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'
|
||||||
import { SAVE_KEY } from '@/common/common'
|
import { SAVE_KEY } from '@/common/common'
|
||||||
import { readImage } from '@/lib/fileAction'
|
import { readImage, removeImage } from '@/lib/fileAction'
|
||||||
|
import { useCanvas } from '@/hooks/useCanvas'
|
||||||
|
|
||||||
export function usePlan() {
|
export function usePlan() {
|
||||||
const [planNum, setPlanNum] = useState(0)
|
const [planNum, setPlanNum] = useState(0)
|
||||||
@ -14,6 +15,7 @@ export function usePlan() {
|
|||||||
const [currentCanvasStatus, setCurrentCanvasStatus] = useState(null)
|
const [currentCanvasStatus, setCurrentCanvasStatus] = useState(null)
|
||||||
|
|
||||||
const [canvas, setCanvas] = useRecoilState(canvasState)
|
const [canvas, setCanvas] = useRecoilState(canvasState)
|
||||||
|
|
||||||
const [currentCanvasPlan, setCurrentCanvasPlan] = useRecoilState(currentCanvasPlanState)
|
const [currentCanvasPlan, setCurrentCanvasPlan] = useRecoilState(currentCanvasPlanState)
|
||||||
const [plans, setPlans] = useRecoilState(plansState) // 전체 plan
|
const [plans, setPlans] = useRecoilState(plansState) // 전체 plan
|
||||||
const [modifiedPlans, setModifiedPlans] = useRecoilState(modifiedPlansState) // 변경된 canvas plan
|
const [modifiedPlans, setModifiedPlans] = useRecoilState(modifiedPlansState) // 변경된 canvas plan
|
||||||
@ -198,7 +200,7 @@ export function usePlan() {
|
|||||||
userId: userId,
|
userId: userId,
|
||||||
imageName: 'image_name', // api 필수항목이여서 임시로 넣음, 이후 삭제 필요
|
imageName: 'image_name', // api 필수항목이여서 임시로 넣음, 이후 삭제 필요
|
||||||
objectNo: objectNo,
|
objectNo: objectNo,
|
||||||
mapPositionAddress: currentCanvasPlan.mapPositionAddress,
|
mapPositionAddress: currentCanvasPlan?.mapPositionAddress ?? null,
|
||||||
canvasStatus: canvasToDbFormat(canvasStatus),
|
canvasStatus: canvasToDbFormat(canvasStatus),
|
||||||
}
|
}
|
||||||
await promisePost({ url: '/api/canvas-management/canvas-statuses', data: planData })
|
await promisePost({ url: '/api/canvas-management/canvas-statuses', data: planData })
|
||||||
@ -218,6 +220,7 @@ export function usePlan() {
|
|||||||
const putCanvasStatus = async (canvasStatus) => {
|
const putCanvasStatus = async (canvasStatus) => {
|
||||||
const planData = {
|
const planData = {
|
||||||
id: currentCanvasPlan.id,
|
id: currentCanvasPlan.id,
|
||||||
|
bgImageName: currentCanvasPlan?.id ?? null,
|
||||||
mapPositionAddress: currentCanvasPlan.mapPositionAddress,
|
mapPositionAddress: currentCanvasPlan.mapPositionAddress,
|
||||||
canvasStatus: canvasToDbFormat(canvasStatus),
|
canvasStatus: canvasToDbFormat(canvasStatus),
|
||||||
}
|
}
|
||||||
@ -272,7 +275,7 @@ export function usePlan() {
|
|||||||
}, [plans])
|
}, [plans])
|
||||||
|
|
||||||
const setBgImage = () => {
|
const setBgImage = () => {
|
||||||
readImage(selectedPlan?.id)
|
// readImage(selectedPlan?.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -321,6 +324,7 @@ export function usePlan() {
|
|||||||
.then((res) => {
|
.then((res) => {
|
||||||
setPlans((plans) => plans.filter((plan) => plan.id !== id))
|
setPlans((plans) => plans.filter((plan) => plan.id !== id))
|
||||||
setModifiedPlans((modifiedPlans) => modifiedPlans.filter((planId) => planId !== currentCanvasPlan.id))
|
setModifiedPlans((modifiedPlans) => modifiedPlans.filter((planId) => planId !== currentCanvasPlan.id))
|
||||||
|
removeImage(currentCanvasPlan.id)
|
||||||
swalFire({ text: getMessage('plan.message.delete') })
|
swalFire({ text: getMessage('plan.message.delete') })
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
@ -358,6 +362,7 @@ export function usePlan() {
|
|||||||
plans,
|
plans,
|
||||||
selectedPlan,
|
selectedPlan,
|
||||||
currentCanvasPlan,
|
currentCanvasPlan,
|
||||||
|
setCurrentCanvasPlan,
|
||||||
modifiedPlans,
|
modifiedPlans,
|
||||||
modifiedPlanFlag,
|
modifiedPlanFlag,
|
||||||
setModifiedPlanFlag,
|
setModifiedPlanFlag,
|
||||||
|
|||||||
@ -90,4 +90,4 @@ const removeImage = async (fileName) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { convertDwgToPng, writeImageBase64, writeImage, readImage, removeImage }
|
export { convertDwgToPng, writeImageBase64, writeImage, removeImage }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user