Skip to content

Commit

Permalink
refactor it to a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
imrishabh18 committed Dec 21, 2024
1 parent 6295e9d commit 6483bf8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 52 deletions.
107 changes: 57 additions & 50 deletions lib/components/primitive-components/Group/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,67 +308,74 @@ export class Group<Props extends z.ZodType<any, any, any> = 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)
}

/**
Expand Down
Loading

0 comments on commit 6483bf8

Please sign in to comment.