diff --git a/B07_DesignDetail/openwebcad/src/drawControllers/screenCanvas.drawController.ts b/B07_DesignDetail/openwebcad/src/drawControllers/screenCanvas.drawController.ts index 753a1803..f37b3199 100644 --- a/B07_DesignDetail/openwebcad/src/drawControllers/screenCanvas.drawController.ts +++ b/B07_DesignDetail/openwebcad/src/drawControllers/screenCanvas.drawController.ts @@ -69,7 +69,6 @@ export class ScreenCanvasDrawController implements DrawController { } public setScreenScale(newScreenScale: number) { - console.log(`set screen scale: ${newScreenScale}`); this.screenScale = newScreenScale; triggerReactUpdate(StateVariable.screenZoom); } diff --git a/B07_DesignDetail/openwebcad/src/entities/Entity.ts b/B07_DesignDetail/openwebcad/src/entities/Entity.ts index 23dc7403..135f4be9 100644 --- a/B07_DesignDetail/openwebcad/src/entities/Entity.ts +++ b/B07_DesignDetail/openwebcad/src/entities/Entity.ts @@ -86,6 +86,8 @@ export interface JsonEntity { lineWidth: number; lineDash?: number[]; layerId: string; + /** GROUP 묶음 식별자 — 저장·복원에서 그대로 실어 나른다 */ + groupId?: string; shapeData: TShapeJsonData | null; children?: JsonEntity[]; } diff --git a/B07_DesignDetail/openwebcad/src/helpers/import-export-handlers/export-entities-to-json.ts b/B07_DesignDetail/openwebcad/src/helpers/import-export-handlers/export-entities-to-json.ts index d33f56c1..b0d59331 100644 --- a/B07_DesignDetail/openwebcad/src/helpers/import-export-handlers/export-entities-to-json.ts +++ b/B07_DesignDetail/openwebcad/src/helpers/import-export-handlers/export-entities-to-json.ts @@ -14,7 +14,11 @@ export async function exportEntitiesToJsonFile() { export async function exportEntitiesAndLayersToJsonString() { const entities = getEntities(); - const jsonEntities = entities.map((entity) => entity.toJson()); + // groupId는 엔티티 13종의 toJson을 다 고치는 대신 여기 한 곳에서 붙인다. + const jsonEntities = entities.map(async (entity) => { + const json = await entity.toJson(); + return json && entity.groupId ? { ...json, groupId: entity.groupId } : json; + }); const jsonDrawingFile: JsonDrawingFileSerialized = { entities: compact(await Promise.all(jsonEntities)), // TODO use a mapLimit to avoid overloading the event loop layers: getLayers(), diff --git a/B07_DesignDetail/openwebcad/src/helpers/import-export-handlers/group-id-roundtrip.test.ts b/B07_DesignDetail/openwebcad/src/helpers/import-export-handlers/group-id-roundtrip.test.ts new file mode 100644 index 00000000..d0ee722c --- /dev/null +++ b/B07_DesignDetail/openwebcad/src/helpers/import-export-handlers/group-id-roundtrip.test.ts @@ -0,0 +1,30 @@ +import { Point } from '@flatten-js/core'; +import { describe, expect, it } from 'vitest'; +import { LineEntity } from '../../entities/LineEntity.ts'; +import { setEntities, setLayers } from '../../state.ts'; +import { exportEntitiesAndLayersToJsonString } from './export-entities-to-json.ts'; +import { getEntitiesAndLayersFromJsonString } from './import-entities-from-json.ts'; + +describe('groupId 저장·복원', () => { + it('GROUP 묶음이 JSON 왕복 뒤에도 한 덩어리로 남는다', async () => { + const layer = { id: 'layer-1', isLocked: false, isVisible: true, name: '작업' }; + const groupId = 'group-1'; + const first = new LineEntity(layer.id, new Point(0, 0), new Point(1, 0)); + const second = new LineEntity(layer.id, new Point(1, 0), new Point(1, 1)); + const loner = new LineEntity(layer.id, new Point(2, 2), new Point(3, 3)); + first.groupId = groupId; + second.groupId = groupId; + + setLayers([layer]); + setEntities([first, second, loner]); + + const restored = await getEntitiesAndLayersFromJsonString( + await exportEntitiesAndLayersToJsonString() + ); + const byId = new Map(restored.entities.map((entity) => [entity.id, entity])); + + expect(byId.get(first.id)?.groupId).toBe(groupId); + expect(byId.get(second.id)?.groupId).toBe(groupId); + expect(byId.get(loner.id)?.groupId).toBeUndefined(); + }); +}); diff --git a/B07_DesignDetail/openwebcad/src/helpers/import-export-handlers/import-entities-from-json.ts b/B07_DesignDetail/openwebcad/src/helpers/import-export-handlers/import-entities-from-json.ts index c121cf78..6cfbf049 100644 --- a/B07_DesignDetail/openwebcad/src/helpers/import-export-handlers/import-entities-from-json.ts +++ b/B07_DesignDetail/openwebcad/src/helpers/import-export-handlers/import-entities-from-json.ts @@ -77,6 +77,12 @@ export async function getEntitiesAndLayersFromJsonObject( ); const entities = compact(await Promise.all(entityPromises)); + // 순번이 아니라 id로 되돌린다 — compact가 null을 걷어 index가 어긋날 수 있다. + const groupIdById = new Map(data.entities.map((entity) => [entity.id, entity.groupId])); + for (const entity of entities) { + const groupId = groupIdById.get(entity.id); + if (groupId) entity.groupId = groupId; + } let layers = data.layers; if (data.layers.length === 0) { layers = [getNewLayer()];