From 6483bf8884d885aaa9a9cd237a517aeb73141f9a Mon Sep 17 00:00:00 2001 From: Rishabh Gupta Date: Sun, 22 Dec 2024 02:07:09 +0530 Subject: [PATCH] refactor it to a separate function --- .../primitive-components/Group/Group.ts | 107 ++++++++++-------- ...-group-offset-schematic-schematic.snap.svg | 4 +- 2 files changed, 59 insertions(+), 52 deletions(-) diff --git a/lib/components/primitive-components/Group/Group.ts b/lib/components/primitive-components/Group/Group.ts index 2247688e..d9811592 100644 --- a/lib/components/primitive-components/Group/Group.ts +++ b/lib/components/primitive-components/Group/Group.ts @@ -308,67 +308,74 @@ export class Group = typeof groupProps> } } - doInitialSchematicLayout(): void { + _computeSchematicPositionBeforeLayout(): void { const { db } = this.root! - const props = this._parsedProps + const props = this.children[0]._parsedProps as SubcircuitGroupProps - if (this.isSubcircuit) { - if (!props.schAutoLayoutEnabled) return + const offsetX = Number(props.schX ?? 0) + const offsetY = Number(props.schY ?? 0) - const descendants = this.getDescendants() - const components: SchematicComponent[] = [] - const ports: SchematicPort[] = [] + const descendants = this.getDescendants() - for (const descendant of descendants) { - if ("schematic_component_id" in descendant) { - const component = db.schematic_component.get( - descendant.schematic_component_id!, - ) - if (component) { - const schPorts = db.schematic_port - .list() - .filter( - (p) => - p.schematic_component_id === component.schematic_component_id, - ) - - components.push(component) - ports.push(...schPorts) - } + for (const descendant of descendants) { + if ("schematic_component_id" in descendant) { + const component = db.schematic_component.get( + descendant.schematic_component_id!, + ) + if (component) { + db.schematic_component.update(component.schematic_component_id!, { + ...component, + center: { + x: component.center.x + offsetX, + y: component.center.y + offsetY, + }, + }) } } + } + } - // Apply auto-layout for subcircuits - const scene = SAL.convertSoupToScene(db.toArray()) - const laidOutScene = SAL.ascendingCentralLrBug1(scene) - SAL.mutateSoupForScene(db.toArray(), laidOutScene) - } else { - // Handle non-subcircuit group offset - const offsetX = props.schX ?? 0 - const offsetY = props.schY ?? 0 - - // Skip if no offset - if (offsetX === 0 && offsetY === 0) return - - // Move all child components by the group's offset - for (const child of this.children) { - if ("schematic_component_id" in child) { - const component = db.schematic_component.get( - child.schematic_component_id! - ) - if (!component) continue + doInitialSchematicLayout(): void { + // The schematic_components are rendered in our children + if (!this.isSubcircuit) return - // Preserve all original properties and only update position - db.schematic_component.update(child.schematic_component_id!, { - ...component, // Keep all original properties - center: { - x: component.center.x + offsetX, - y: component.center.y + offsetY - } - }) + this._computeSchematicPositionBeforeLayout() + + const props = this._parsedProps as SubcircuitGroupProps + if (!props.schAutoLayoutEnabled) return + const { db } = this.root! + + const descendants = this.getDescendants() + + const components: SchematicComponent[] = [] + const ports: SchematicPort[] = [] + + for (const descendant of descendants) { + if ("schematic_component_id" in descendant) { + const component = db.schematic_component.get( + descendant.schematic_component_id!, + ) + if (component) { + // Get all ports associated with this component + const schPorts = db.schematic_port + .list() + .filter( + (p) => + p.schematic_component_id === component.schematic_component_id, + ) + + components.push(component) + ports.push(...schPorts) } } } + + // TODO only move components that belong to this subcircuit + const scene = SAL.convertSoupToScene(db.toArray()) + + const laidOutScene = SAL.ascendingCentralLrBug1(scene) + + SAL.mutateSoupForScene(db.toArray(), laidOutScene) } /** diff --git a/tests/components/primitive-components/__snapshots__/non-subcircuit-group-offset-schematic-schematic.snap.svg b/tests/components/primitive-components/__snapshots__/non-subcircuit-group-offset-schematic-schematic.snap.svg index 8945f8c5..685e899e 100644 --- a/tests/components/primitive-components/__snapshots__/non-subcircuit-group-offset-schematic-schematic.snap.svg +++ b/tests/components/primitive-components/__snapshots__/non-subcircuit-group-offset-schematic-schematic.snap.svg @@ -1,4 +1,4 @@ -3,-13,03,14,-14,04,15,-15,05,16,-16,06,17,-17,07,18,-18,08,19,-19,09,110,-110,010,1R11kΩC11kF \ No newline at end of file