children 검색 수정

This commit is contained in:
hyojun.choi 2024-11-07 14:08:49 +09:00
parent d6041b4322
commit 16262f4c3e
2 changed files with 39 additions and 2 deletions

View File

@ -1,7 +1,14 @@
import { fabric } from 'fabric'
import { v4 as uuidv4 } from 'uuid'
import { QLine } from '@/components/fabric/QLine'
import { distanceBetweenPoints, findTopTwoIndexesByDistance, getDirectionByPoint, sortedPointLessEightPoint, sortedPoints } from '@/util/canvas-util'
import {
distanceBetweenPoints,
findTopTwoIndexesByDistance,
getAllRelatedObjects,
getDirectionByPoint,
sortedPointLessEightPoint,
sortedPoints,
} from '@/util/canvas-util'
import { calculateAngle, drawRidgeRoof, drawHippedRoof, inPolygon, toGeoJSON } from '@/util/qpolygon-utils'
import * as turf from '@turf/turf'
import { LINE_TYPE } from '@/common/common'
@ -131,7 +138,7 @@ export const QPolygon = fabric.util.createClass(fabric.Polygon, {
})
this.on('removed', () => {
const children = this.canvas.getObjects().filter((obj) => obj.parentId === this.id)
const children = getAllRelatedObjects(this.id, this.canvas)
children.forEach((child) => {
this.canvas.remove(child)
})
@ -169,6 +176,7 @@ export const QPolygon = fabric.util.createClass(fabric.Polygon, {
offset: 0,
},
parent: this,
parentId: this.id,
direction: getDirectionByPoint(point, nextPoint),
idx: i + 1,
})

View File

@ -925,3 +925,32 @@ export function checkLineOrientation(line) {
return 'diagonal' // 대각선
}
}
// 최상위 parentId를 통해 모든 하위 객체를 찾는 함수
export const getAllRelatedObjects = (id, canvas) => {
const result = []
const map = new Map()
// Create a map of objects by their id
canvas.getObjects().forEach((obj) => {
map.set(obj.id, obj)
})
// Helper function to recursively find all related objects
function findRelatedObjects(id) {
const obj = map.get(id)
if (obj) {
result.push(obj)
canvas.getObjects().forEach((o) => {
if (o.parentId === id) {
findRelatedObjects(o.id)
}
})
}
}
// Start the search with the given parentId
findRelatedObjects(id)
return result
}