Skip to content

AI-native design editor. Open-source Figma alternative.

fix: image fills lost after save . hex hash corrupted in Kiwi byte[] encoding

Guilherme Maurercefa4331e317
diff --git a/packages/core/src/io/formats/fig/export.ts b/packages/core/src/io/formats/fig/export.ts
index a237eeea8..f3110ff95 100644
--- a/packages/core/src/io/formats/fig/export.ts
+++ b/packages/core/src/io/formats/fig/export.ts
@@ -318,22 +318,11 @@ function compressViaWorker(
worker.terminate()
}
- const imgCopies = imageEntries.map((e) => ({
- name: e.name,
- data: new Uint8Array(e.data)
- }))
-
- const transferables = [
- schemaDeflated.buffer,
- kiwiData.buffer,
- thumbnailPng.buffer,
- ...imgCopies.map((e) => e.data.buffer)
- ]
-
- worker.postMessage(
- { schemaDeflated, kiwiData, thumbnailPng, metaJson, images: imgCopies },
- transferables
- )
+ // Do NOT use transferables here. toUint8Array() in ByteBuffer returns a view of the
+ // internal buffer, so transferring kiwiData.buffer or schemaDeflated.buffer detaches
+ // buffers that may be shared with other views, causing "already detached" errors on
+ // subsequent saves. Structured clone (the default) copies the data safely.
+ worker.postMessage({ schemaDeflated, kiwiData, thumbnailPng, metaJson, images: imageEntries })
})
}
diff --git a/packages/core/src/kiwi/binary/codec.ts b/packages/core/src/kiwi/binary/codec.ts
index e7ddb0010..f8278cf94 100644
--- a/packages/core/src/kiwi/binary/codec.ts
+++ b/packages/core/src/kiwi/binary/codec.ts
@@ -195,7 +195,7 @@ export interface Paint {
blendMode?: string
stops?: { color: Color; position: number }[]
transform?: Matrix
- image?: { hash: string }
+ image?: { hash: string | Uint8Array }
imageScaleMode?: string
colorVariableBinding?: VariableBinding
colorVar?: {
diff --git a/packages/core/src/kiwi/node-change/serialize.ts b/packages/core/src/kiwi/node-change/serialize.ts
index cd0d940d4..a31d0886d 100644
--- a/packages/core/src/kiwi/node-change/serialize.ts
+++ b/packages/core/src/kiwi/node-change/serialize.ts
@@ -192,6 +192,14 @@ export function safeColor(c: { r: number; g: number; b: number; a?: number }): C
return { r: c.r, g: c.g, b: c.b, a: c.a ?? 1 }
}
+function hexToBytes(hex: string): Uint8Array {
+ const bytes = new Uint8Array(hex.length / 2)
+ for (let i = 0; i < bytes.length; i++) {
+ bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16)
+ }
+ return bytes
+}
+
function fillToKiwiPaint(f: SceneNode['fills'][number]): Paint {
const paint: Paint = {
type: f.type,
@@ -204,7 +212,7 @@ function fillToKiwiPaint(f: SceneNode['fills'][number]): Paint {
paint.stops = f.gradientStops.map((s) => ({ color: safeColor(s.color), position: s.position }))
}
if (f.gradientTransform) paint.transform = f.gradientTransform
- if (f.imageHash) paint.image = { hash: f.imageHash }
+ if (f.imageHash) paint.image = { hash: hexToBytes(f.imageHash) }
if (f.imageScaleMode) paint.imageScaleMode = f.imageScaleMode
if (f.imageTransform) paint.transform = f.imageTransform
return paint