Skip to content

Commit e0ed2aa

Browse files
authored
Merge pull request #52 from AnasSarkiz/main
Fix broken LBRN export XML formatting and standardize .lbrn2 filenames
2 parents 3bf4170 + 13ea6d0 commit e0ed2aa

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

lib/components/workspace-context.tsx

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ export function WorkspaceProvider({ children }: { children: ReactNode }) {
234234

235235
const finalOptions = { ...lbrnOptions, ...options }
236236

237-
const xml = await convertCircuitJsonToLbrn(circuitJson, finalOptions)
237+
const rawXml = await convertCircuitJsonToLbrn(circuitJson, finalOptions)
238+
const xml = formatLbrnXml(rawXml)
238239

239240
setLbrnFileContent({
240241
xml,
@@ -275,6 +276,44 @@ export function WorkspaceProvider({ children }: { children: ReactNode }) {
275276
)
276277
}
277278

279+
const formatLbrnXml = (value: unknown): string => {
280+
if (typeof value === "string") {
281+
return value
282+
}
283+
284+
if (value && typeof value === "object") {
285+
const candidate = value as { xml?: unknown; outerHTML?: unknown }
286+
if (typeof candidate.xml === "string") {
287+
return candidate.xml
288+
}
289+
if (typeof candidate.outerHTML === "string") {
290+
return candidate.outerHTML
291+
}
292+
}
293+
294+
if (typeof window !== "undefined" && value instanceof XMLDocument) {
295+
return new XMLSerializer().serializeToString(value)
296+
}
297+
298+
if (typeof window !== "undefined" && value instanceof Element) {
299+
return new XMLSerializer().serializeToString(value)
300+
}
301+
302+
if (Array.isArray(value)) {
303+
return value.map(formatLbrnXml).join("")
304+
}
305+
306+
const fallback = String(value)
307+
if (fallback.startsWith("[object ") && fallback.endsWith("]")) {
308+
const inner = fallback.slice(8, -1)
309+
if (inner.trim().startsWith("<?xml")) {
310+
return inner
311+
}
312+
}
313+
314+
return fallback
315+
}
316+
278317
export function useWorkspace() {
279318
const context = useContext(WorkspaceContext)
280319
if (context === undefined) {

lib/components/workspace-toolbar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Input } from "@/components/ui/input"
1414
export function WorkspaceToolbar() {
1515
const { lbrnFileContent } = useWorkspace()
1616
const [open, setOpen] = useState(false)
17-
const [filename, setFilename] = useState("circuit.lbrn")
17+
const [filename, setFilename] = useState("circuit.lbrn2")
1818

1919
const handleExport = () => {
2020
if (!lbrnFileContent) return
@@ -24,9 +24,9 @@ export function WorkspaceToolbar() {
2424
const handleConfirmExport = () => {
2525
if (!lbrnFileContent) return
2626

27-
const finalFilename = filename.endsWith(".lbrn")
27+
const finalFilename = filename.endsWith(".lbrn2")
2828
? filename
29-
: `${filename}.lbrn`
29+
: `${filename}.lbrn2`
3030

3131
const blob = new Blob([lbrnFileContent.xml], { type: "application/xml" })
3232
const url = URL.createObjectURL(blob)

0 commit comments

Comments
 (0)