Skip to content

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

fix(core): keep instance overrides in published library revisions (#1468)

Marcos Velásquez5ce6d4ebd8fb
diff --git a/changes/1468/fixed.md b/changes/1468/fixed.md
new file mode 100644
index 000000000..307641546
--- /dev/null
+++ b/changes/1468/fixed.md
@@ -0,0 +1 @@
+- Library components keep their instances' overrides and instance-swap defaults when inserted into a document, and libraries published to a CLI catalog keep overrides and vector shapes.
diff --git a/packages/cli/src/library/catalog.ts b/packages/cli/src/library/catalog.ts
index 63106828b..631f8d980 100644
--- a/packages/cli/src/library/catalog.ts
+++ b/packages/cli/src/library/catalog.ts
@@ -13,11 +13,11 @@ import type {
import {
createLibraryRevision,
deserializeLibraryRevision,
+ encodeStoredLibraryRevision,
librarySummarySchema,
MAX_LIBRARY_REVISION_BYTES,
- serializedLibraryRevisionSchema,
- serializeLibraryRevision,
storedLibraryLatestManifestSchema,
+ storedLibraryRevisionSchema,
validateLibraryRevision
} from '@open-pencil/core/library'
@@ -63,7 +63,7 @@ export class FileSystemLibraryCatalog implements LibraryCatalog {
.summary.latestRevisionId
const data = await this.#readJSON(
`${libraryId}/revisions/${resolvedRevisionId}.json`,
- serializedLibraryRevisionSchema,
+ storedLibraryRevisionSchema,
MAX_LIBRARY_REVISION_BYTES
)
const revision = deserializeLibraryRevision(data)
@@ -89,7 +89,7 @@ export class FileSystemLibraryCatalog implements LibraryCatalog {
const manifest = revision.manifest
await this.#writeJSON(
`${manifest.libraryId}/revisions/${manifest.revisionId}.json`,
- serializeLibraryRevision(revision)
+ encodeStoredLibraryRevision(revision)
)
const summary: LibrarySummary = {
libraryId: manifest.libraryId,
diff --git a/packages/cli/tests/library-catalog.test.ts b/packages/cli/tests/library-catalog.test.ts
index c7c637e29..090bbeb81 100644
--- a/packages/cli/tests/library-catalog.test.ts
+++ b/packages/cli/tests/library-catalog.test.ts
@@ -3,9 +3,9 @@ import { mkdtemp, readFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
-import { SceneGraph } from '@open-pencil/scene-graph'
-
import { FileSystemLibraryCatalog } from '@open-pencil/cli/library'
+import { SceneGraph } from '@open-pencil/scene-graph'
+import { recordInstanceOverride } from '@open-pencil/scene-graph/instances'
describe('filesystem library catalog', () => {
test('publishes and restores revisions under a bounded root', async () => {
@@ -65,6 +65,39 @@ describe('filesystem library catalog', () => {
expect(restored.graph.images.get('image-hash')).toEqual(new Uint8Array([1, 2, 3]))
})
+ test('restores vector geometry and instance overrides from stored revisions', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'open-pencil-libraries-'))
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const icon = graph.createNode('COMPONENT', page.id, { name: 'Icon', componentKey: 'icon' })
+ const commandsBlob = new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0, 0])
+ graph.createNode('VECTOR', icon.id, {
+ name: 'Glyph',
+ fillGeometry: [{ windingRule: 'NONZERO', commandsBlob }]
+ })
+ const button = graph.createNode('COMPONENT', page.id, {
+ name: 'Button',
+ componentKey: 'button'
+ })
+ const nested = graph.createInstance(icon.id, button.id)
+ const glyph = nested ? graph.getChildren(nested.id)[0] : undefined
+ if (!nested || !glyph) throw new Error('Expected the nested icon')
+ graph.updateNode(glyph.id, { opacity: 0.5 })
+ recordInstanceOverride(graph, glyph.id, ['opacity'])
+
+ const catalog = new FileSystemLibraryCatalog(root)
+ await catalog.publishRevision({ libraryId: 'design-system', name: 'Design system', graph })
+ const restored = (await catalog.getRevision('design-system')).graph
+ const glyphs = [...restored.getAllNodes()].filter((node) => node.name === 'Glyph')
+ const instance = [...restored.getAllNodes()].find((node) => node.type === 'INSTANCE')
+
+ expect(glyphs.map((node) => node.fillGeometry[0]?.commandsBlob)).toEqual([
+ commandsBlob,
+ commandsBlob
+ ])
+ expect(instance?.instanceOverrides.descendants.size).toBe(1)
+ })
+
test('rejects revisions that are malformed or fail integrity validation', async () => {
const root = await mkdtemp(join(tmpdir(), 'open-pencil-libraries-'))
const graph = new SceneGraph()
diff --git a/packages/core/src/library/copied-references.ts b/packages/core/src/library/copied-references.ts
new file mode 100644
index 000000000..3cde1356a
--- /dev/null
+++ b/packages/core/src/library/copied-references.ts
@@ -0,0 +1,61 @@
+import { mapValues } from 'es-toolkit'
+
+import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'
+import { remapInstanceOverrideState } from '@open-pencil/scene-graph/instances/overrides'
+
+function remappedReferences(
+ node: SceneNode,
+ mapped: (id: string) => string,
+ swapProperties: ReadonlySet<string>
+): Partial<SceneNode> {
+ const changes: Partial<SceneNode> = {}
+ if (node.componentId && mapped(node.componentId) !== node.componentId)
+ changes.componentId = mapped(node.componentId)
+ const overrides = node.instanceOverrides
+ if (overrides.self.size > 0 || overrides.descendants.size > 0) {
+ changes.instanceOverrides = remapInstanceOverrideState(overrides, {
+ node: mapped,
+ variable: (id) => id
+ })
+ }
+ if (node.componentPropertyDefinitions.some((definition) => definition.type === 'INSTANCE_SWAP')) {
+ changes.componentPropertyDefinitions = node.componentPropertyDefinitions.map((definition) =>
+ definition.type === 'INSTANCE_SWAP' && definition.defaultValue
+ ? { ...definition, defaultValue: mapped(definition.defaultValue) }
+ : definition
+ )
+ }
+ if (Object.keys(node.componentPropertyAssignments).some((id) => swapProperties.has(id))) {
+ changes.componentPropertyAssignments = mapValues(
+ node.componentPropertyAssignments,
+ (value, id) => (swapProperties.has(id) ? mapped(value) : value)
+ )
+ }
+ return changes
+}
+
+/**
+ * Points the layers a library copy made at one another, as `prepareNodeTransfer` does for a
+ * paste: an instance's main component, the layers its overrides change and the components they
+ * swap in, and the components an instance-swap property defaults to or is set to. `mappedIds`
+ * maps each copied layer to its copy; a reference outside the copy stays as it is, and so do
+ * variable IDs, since the copies keep their source's variables.
+ */
+export function remapCopiedReferences(
+ graph: SceneGraph,
+ mappedIds: ReadonlyMap<string, string>
+): void {
+ const mapped = (id: string) => mappedIds.get(id) ?? id
+ const copies = [...mappedIds.values()].flatMap((id) => graph.getNode(id) ?? [])
+ const swapProperties = new Set(
+ copies.flatMap((node) =>
+ node.componentPropertyDefinitions
+ .filter((definition) => definition.type === 'INSTANCE_SWAP')
+ .map((definition) => definition.id)
+ )
+ )
+ for (const node of copies) {
+ const changes = remappedReferences(node, mapped, swapProperties)
+ if (Object.keys(changes).length > 0) graph.updateNode(node.id, changes)
+ }
+}
diff --git a/packages/core/src/library/materialize.ts b/packages/core/src/library/materialize.ts
index 476965233..dd5649358 100644
--- a/packages/core/src/library/materialize.ts
+++ b/packages/core/src/library/materialize.ts
@@ -1,6 +1,7 @@
import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'
import { cloneNodeProps } from '@open-pencil/scene-graph/copy'
+import { remapCopiedReferences } from './copied-references'
import { findLibraryDefinition } from './definitions'
import { assertLibraryAssetKey, assertLibraryId, libraryAssetIdentityKey } from './identity'
import type { ComponentLibraryRevision, LibraryAssetDescriptor } from './types'
@@ -76,10 +77,10 @@ function markDefinitions(
const descriptorsBySourceId = new Map(
revision.manifest.assets.map((asset) => [asset.sourceNodeId, asset])
)
+ remapCopiedReferences(graph, mappedIds)
for (const [sourceId, targetId] of mappedIds) {
const target = graph.getNode(targetId)
if (!target) continue
- const componentId = target.componentId ? mappedIds.get(target.componentId) : undefined
const descriptor = descriptorsBySourceId.get(sourceId)
const source = revision.graph.getNode(sourceId)
const sourceAssetKey =
@@ -88,7 +89,6 @@ function markDefinitions(
? (source.componentKey ?? source.sourceLibraryKey ?? source.publishId)
: null)
graph.updateNode(targetId, {
- componentId: componentId ?? target.componentId,
locked: true,
librarySource: sourceAssetKey
? {
diff --git a/packages/core/src/library/schema.ts b/packages/core/src/library/schema.ts
index fd1914e9c..e781c64e8 100644
--- a/packages/core/src/library/schema.ts
+++ b/packages/core/src/library/schema.ts
@@ -2,6 +2,7 @@ import * as v from 'valibot'
import type { SceneNode, Variable, VariableCollection } from '@open-pencil/scene-graph'
+import { decodeStoredLibraryValue } from './serialization'
import type { SerializedComponentLibraryRevision } from './serialization'
import { COMPONENT_LIBRARY_SCHEMA_VERSION } from './types'
import type { LibrarySummary, StoredLibraryLatestManifest } from './types'
@@ -40,19 +41,7 @@ const serializedCollectionSchema = v.custom<VariableCollection>((value) =>
v.is(serializedCollectionShapeSchema, value)
)
-/** Image bytes: a Uint8Array, or the index-keyed object `JSON.stringify` writes for one. */
-const serializedBytesSchema = v.union([
- v.instance(Uint8Array),
- v.pipe(
- v.record(v.string(), v.pipe(v.number(), v.integer(), v.minValue(0), v.maxValue(255))),
- // A gap or a stray key would silently shift the bytes; asset hashes do not cover images.
- v.check(
- (bytes) => Object.keys(bytes).every((key, index) => key === String(index)),
- 'Expected byte indexes 0 to length - 1'
- ),
- v.transform((bytes) => Uint8Array.from(Object.values(bytes)))
- )
-])
+const serializedBytesSchema = v.instance(Uint8Array)
const libraryAssetDescriptorSchema = v.object({
key: v.string(),
@@ -86,6 +75,13 @@ export const serializedLibraryRevisionSchema = v.object({
})
}) satisfies v.GenericSchema<unknown, SerializedComponentLibraryRevision>
+/** A revision as a catalog reads it back from JSON, decoded from `encodeStoredLibraryRevision`. */
+export const storedLibraryRevisionSchema = v.pipe(
+ v.unknown(),
+ v.transform(decodeStoredLibraryValue),
+ serializedLibraryRevisionSchema
+)
+
export const librarySummarySchema = v.object({
libraryId: v.string(),
name: v.string(),
diff --git a/packages/core/src/library/serialization.ts b/packages/core/src/library/serialization.ts
index 4bdbbf9f3..c0545765e 100644
--- a/packages/core/src/library/serialization.ts
+++ b/packages/core/src/library/serialization.ts
@@ -1,3 +1,6 @@
+import { mapValues } from 'es-toolkit'
+import { fromUint8Array, isValid, toUint8Array } from 'js-base64'
+
import { SceneGraph } from '@open-pencil/scene-graph'
import type { SceneNode } from '@open-pencil/scene-graph'
@@ -35,6 +38,66 @@ export function serializeLibraryRevision(
}
}
+const MAP_TAG = 'openpencil/map'
+const OBJECT_TAG = 'openpencil/object'
+
+function encodeStoredValue(value: unknown): unknown {
+ if (value instanceof Map) {
+ return {
+ $openPencilType: MAP_TAG,
+ entries: [...value].map(([key, entry]) => [encodeStoredValue(key), encodeStoredValue(entry)])
+ }
+ }
+ if (value instanceof Uint8Array) return { $bytes: fromUint8Array(value) }
+ if (Array.isArray(value)) return value.map(encodeStoredValue)
+ if (value && typeof value === 'object') {
+ const encoded = mapValues(value, encodeStoredValue)
+ // An object that already looks like a tag is wrapped, so decoding cannot mistake it for one.
+ return '$openPencilType' in value || '$bytes' in value
+ ? { $openPencilType: OBJECT_TAG, value: encoded }
+ : encoded
+ }
+ return value
+}
+
+/**
+ * A revision as a catalog writes it to JSON: the serialized revision with its `Map`s (instance
+ * overrides) and byte arrays (images, geometry) tagged, since JSON keeps neither.
+ */
+export function encodeStoredLibraryRevision(revision: ComponentLibraryRevision): unknown {
+ return encodeStoredValue(serializeLibraryRevision(revision))
+}
+
+/** Reverses `encodeStoredLibraryRevision` on parsed JSON; the result still needs validating. */
+export function decodeStoredLibraryValue(value: unknown): unknown {
+ if (Array.isArray(value)) return value.map(decodeStoredLibraryValue)
+ if (!value || typeof value !== 'object') return value
+ if ('$openPencilType' in value) {
+ if (
+ value.$openPencilType === OBJECT_TAG &&
+ 'value' in value &&
+ value.value &&
+ typeof value.value === 'object' &&
+ !Array.isArray(value.value)
+ ) {
+ return mapValues(value.value, decodeStoredLibraryValue)
+ }
+ if (value.$openPencilType === MAP_TAG && 'entries' in value && Array.isArray(value.entries)) {
+ return new Map(
+ value.entries.flatMap((entry) =>
+ Array.isArray(entry) && entry.length === 2
+ ? [[decodeStoredLibraryValue(entry[0]), decodeStoredLibraryValue(entry[1])] as const]
+ : []
+ )
+ )
+ }
+ }
+ if ('$bytes' in value && typeof value.$bytes === 'string' && isValid(value.$bytes)) {
+ return toUint8Array(value.$bytes)
+ }
+ return mapValues(value, decodeStoredLibraryValue)
+}
+
function cloneLibraryNode(node: SceneNode): SceneNode {
const cloned = structuredClone(node)
return Array.isArray(cloned.guides) ? cloned : { ...cloned, guides: [] }
diff --git a/packages/core/src/library/snapshot.ts b/packages/core/src/library/snapshot.ts
index ccfd1fe43..ff474412d 100644
--- a/packages/core/src/library/snapshot.ts
+++ b/packages/core/src/library/snapshot.ts
@@ -2,6 +2,7 @@ import { SceneGraph, type SceneNode } from '@open-pencil/scene-graph'
import { cloneNodeProps } from '@open-pencil/scene-graph/copy'
import { contentPluginData } from '@open-pencil/scene-graph/plugin-data'
+import { remapCopiedReferences } from './copied-references'
import { contentHash } from './hash'
import { assertLibraryAssetKey } from './identity'
import type { LibraryAssetDescriptor, PortableLibrarySnapshot } from './types'
@@ -60,18 +61,6 @@ function addSnapshotNode(
addSnapshotNode(source, target, childId, created.id, mappedIds)
}
-function remapSnapshotReferences(graph: SceneGraph, mappedIds: Map<string, string>): void {
- for (const [sourceId, targetId] of mappedIds) {
- const sourceTarget = graph.getNode(targetId)
- if (!sourceTarget) continue
- const componentId = sourceTarget.componentId
- if (componentId && mappedIds.has(componentId)) {
- graph.updateNode(targetId, { componentId: mappedIds.get(componentId) ?? null })
- }
- if (sourceId === targetId) throw new Error('Snapshot IDs must be remapped')
- }
-}
-
export function extractLibrarySnapshot(
source: SceneGraph,
assetNodeIds?: string[]
@@ -91,21 +80,15 @@ export function extractLibrarySnapshot(
page.name = 'Library definitions'
page.internalOnly = true
const mappedIds = new Map<string, string>()
- const sourceComponentIds = new Map<string, string | null>()
const closureRoots = [...closure]
.map((id) => source.getNode(id))
.filter((node): node is SceneNode =>
Boolean(node && (!node.parentId || !closure.has(node.parentId)))
)
for (const root of closureRoots) addSnapshotNode(source, snapshot, root.id, page.id, mappedIds)
- for (const sourceId of mappedIds.keys()) {
- sourceComponentIds.set(sourceId, source.getNode(sourceId)?.componentId ?? null)
- }
- for (const [sourceId, targetId] of mappedIds) {
- const componentId = sourceComponentIds.get(sourceId)
- if (componentId) snapshot.updateNode(targetId, { componentId })
- }
- remapSnapshotReferences(snapshot, mappedIds)
+ for (const [sourceId, targetId] of mappedIds)
+ if (sourceId === targetId) throw new Error('Snapshot IDs must be remapped')
+ remapCopiedReferences(snapshot, mappedIds)
for (const imageHash of new Set(
[...closure].flatMap((id) => {
diff --git a/packages/core/src/library/validation.ts b/packages/core/src/library/validation.ts
index da0864a43..776fdc220 100644
--- a/packages/core/src/library/validation.ts
+++ b/packages/core/src/library/validation.ts
@@ -1,5 +1,5 @@
import { contentHash } from './hash'
-import { serializeLibraryRevision } from './serialization'
+import { encodeStoredLibraryRevision } from './serialization'
import { describeSnapshotAssets } from './snapshot'
import type { ComponentLibraryRevision } from './types'
@@ -26,8 +26,7 @@ function integrityError(message: string): never {
}
function serializedRevisionBytes(revision: ComponentLibraryRevision): number {
- const serialized = serializeLibraryRevision(revision)
- return new TextEncoder().encode(JSON.stringify(serialized)).byteLength
+ return new TextEncoder().encode(JSON.stringify(encodeStoredLibraryRevision(revision))).byteLength
}
export async function validateLibraryRevision(revision: ComponentLibraryRevision): Promise<void> {
diff --git a/packages/core/tests/library/copied-references.test.ts b/packages/core/tests/library/copied-references.test.ts
new file mode 100644
index 000000000..ab8403eb0
--- /dev/null
+++ b/packages/core/tests/library/copied-references.test.ts
@@ -0,0 +1,59 @@
+import { describe, expect, test } from 'bun:test'
+
+import { createLibraryRevision, materializeLibraryAsset } from '@open-pencil/core/library'
+import { SceneGraph } from '@open-pencil/scene-graph'
+import { applyComponentPropertyValue } from '@open-pencil/scene-graph/components'
+
+/** A library whose button swaps its icon through an instance-swap property. */
+function libraryWithIconProperty() {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const star = graph.createNode('COMPONENT', page.id, { name: 'Star', componentKey: 'star' })
+ const heart = graph.createNode('COMPONENT', page.id, { name: 'Heart', componentKey: 'heart' })
+ const button = graph.createNode('COMPONENT', page.id, {
+ name: 'Button',
+ componentKey: 'button',
+ componentPropertyDefinitions: [
+ { id: 'icon', name: 'Icon', type: 'INSTANCE_SWAP', defaultValue: star.id }
+ ]
+ })
+ const icon = graph.createInstance(star.id, button.id)
+ if (!icon) throw new Error('Expected the icon instance')
+ graph.updateNode(icon.id, {
+ componentPropertyReferences: [{ propertyId: 'icon', field: 'INSTANCE_SWAP' }]
+ })
+ const card = graph.createNode('COMPONENT', page.id, { name: 'Card', componentKey: 'card' })
+ const cardButton = graph.createInstance(button.id, card.id)
+ if (!cardButton) throw new Error('Expected the card button')
+ const property = graph.getNode(button.id)?.componentPropertyDefinitions[0]
+ if (!property) throw new Error('Expected the icon property')
+ applyComponentPropertyValue(graph, cardButton.id, property, heart.id)
+ return graph
+}
+
+const named = (graph: SceneGraph, id: string | undefined) => (id ? graph.getNode(id)?.name : null)
+
+describe('references between copied library layers', () => {
+ test('point instance-swap properties at the copied components', async () => {
+ const revision = await createLibraryRevision({
+ libraryId: 'design-system',
+ name: 'Design system',
+ graph: libraryWithIconProperty()
+ })
+ const stored = revision.graph
+ const button = [...stored.getAllNodes()].find((node) => node.componentKey === 'button')
+ const cardButton = [...stored.getAllNodes()].find(
+ (node) => node.type === 'INSTANCE' && node.name === 'Button'
+ )
+ expect(named(stored, button?.componentPropertyDefinitions[0]?.defaultValue)).toBe('Star')
+ expect(named(stored, cardButton?.componentPropertyAssignments.icon)).toBe('Heart')
+
+ const consumer = new SceneGraph()
+ const { componentId } = materializeLibraryAsset(consumer, revision, 'card')
+ const inserted = consumer.getChildren(componentId)[0]
+ const definition = consumer.getNode(inserted?.componentId ?? '')
+ ?.componentPropertyDefinitions[0]
+ expect(named(consumer, definition?.defaultValue)).toBe('Star')
+ expect(named(consumer, inserted?.componentPropertyAssignments.icon)).toBe('Heart')
+ })
+})
diff --git a/packages/core/tests/library/instance-overrides.test.ts b/packages/core/tests/library/instance-overrides.test.ts
new file mode 100644
index 000000000..d5ab8f7b2
--- /dev/null
+++ b/packages/core/tests/library/instance-overrides.test.ts
@@ -0,0 +1,69 @@
+import { describe, expect, test } from 'bun:test'
+
+import * as v from 'valibot'
+
+import {
+ createLibraryRevision,
+ deserializeLibraryRevision,
+ encodeStoredLibraryRevision,
+ materializeLibraryAsset,
+ storedLibraryRevisionSchema
+} from '@open-pencil/core/library'
+import { SceneGraph } from '@open-pencil/scene-graph'
+import { recordInstanceOverride } from '@open-pencil/scene-graph/instances'
+
+/** A library whose button holds an icon instance that overrides its glyph's opacity. */
+function libraryWithOverride(opacity: number) {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const icon = graph.createNode('COMPONENT', page.id, { name: 'Icon', componentKey: 'icon' })
+ graph.createNode('ELLIPSE', icon.id, { name: 'Glyph' })
+ const button = graph.createNode('COMPONENT', page.id, { name: 'Button', componentKey: 'button' })
+ const nested = graph.createInstance(icon.id, button.id)
+ const glyph = nested ? graph.getChildren(nested.id)[0] : undefined
+ if (!glyph) throw new Error('Expected the nested icon copy')
+ graph.updateNode(glyph.id, { opacity })
+ recordInstanceOverride(graph, glyph.id, ['opacity'])
+ return { graph, button }
+}
+
+const revisionOf = (graph: SceneGraph) =>
+ createLibraryRevision({ libraryId: 'design-system', name: 'Design system', graph })
+
+/** The nested icon instance of a button, and its glyph copy. */
+function nestedIcon(graph: SceneGraph, buttonId: string) {
+ const nested = graph.getChildren(buttonId).find((node) => node.type === 'INSTANCE')
+ const glyph = nested ? graph.getChildren(nested.id)[0] : undefined
+ if (!nested || !glyph) throw new Error('Expected the nested icon and its glyph')
+ return { nested, glyph }
+}
+
+describe('instance overrides in library revisions', () => {
+ test('keeps them, under the IDs of the stored layers, through a revision stored as JSON', async () => {
+ const { graph } = libraryWithOverride(0.5)
+ const revision = await revisionOf(graph)
+ // A catalog that stores revisions as JSON, as files and remote storage do.
+ const stored = v.parse(
+ v.pipe(v.string(), v.parseJson(), storedLibraryRevisionSchema),
+ JSON.stringify(encodeStoredLibraryRevision(revision))
+ )
+ const restored = deserializeLibraryRevision(stored).graph
+ const button = [...restored.getAllNodes()].find((node) => node.componentKey === 'button')
+ const { nested, glyph } = nestedIcon(restored, button?.id ?? '')
+
+ expect([...nested.instanceOverrides.descendants.keys()]).toEqual([glyph.id])
+ expect(nested.instanceOverrides.descendants.get(glyph.id)?.has('opacity')).toBe(true)
+ })
+
+ test('keeps them under the IDs of the materialized layers', async () => {
+ const { graph } = libraryWithOverride(0.25)
+ const consumer = new SceneGraph()
+ const { componentId } = materializeLibraryAsset(consumer, await revisionOf(graph), 'button')
+ const { nested, glyph } = nestedIcon(consumer, componentId)
+
+ expect([...nested.instanceOverrides.descendants.keys()]).toEqual([glyph.id])
+ // A sync, as reopening the file runs, keeps the overridden value.
+ consumer.syncInstance(nested.id)
+ expect(consumer.getNode(glyph.id)?.opacity).toBe(0.25)
+ })
+})
diff --git a/packages/core/tests/library/serialization.test.ts b/packages/core/tests/library/serialization.test.ts
new file mode 100644
index 000000000..7948bcf07
--- /dev/null
+++ b/packages/core/tests/library/serialization.test.ts
@@ -0,0 +1,59 @@
+import { describe, expect, test } from 'bun:test'
+
+import * as v from 'valibot'
+
+import {
+ createLibraryRevision,
+ deserializeLibraryRevision,
+ encodeStoredLibraryRevision,
+ storedLibraryRevisionSchema
+} from '@open-pencil/core/library'
+import { SceneGraph } from '@open-pencil/scene-graph'
+import {
+ getInstanceOverride,
+ setInstanceOverride
+} from '@open-pencil/scene-graph/instances/overrides'
+
+describe('stored library revisions', () => {
+ test('keep values shaped like the tags they are stored with', async () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const icon = graph.createNode('COMPONENT', page.id, { name: 'Icon', componentKey: 'icon' })
+ const button = graph.createNode('COMPONENT', page.id, {
+ name: 'Button',
+ componentKey: 'button'
+ })
+ const instance = graph.createInstance(icon.id, button.id)
+ if (!instance) throw new Error('Expected the icon instance')
+ const values = [{ $bytes: 'AQID' }, { $openPencilType: 'openpencil/map', entries: [] }]
+ for (const [index, value] of values.entries())
+ setInstanceOverride(
+ instance.instanceOverrides,
+ instance.id,
+ instance.id,
+ `pluginData/${index}`,
+ value
+ )
+
+ const revision = await createLibraryRevision({ libraryId: 'design-system', name: 'DS', graph })
+ const stored = v.parse(
+ v.pipe(v.string(), v.parseJson(), storedLibraryRevisionSchema),
+ JSON.stringify(encodeStoredLibraryRevision(revision))
+ )
+ const restored = [...deserializeLibraryRevision(stored).graph.getAllNodes()].find(
+ (node) => node.type === 'INSTANCE'
+ )
+ if (!restored) throw new Error('Expected the stored instance')
+
+ expect(
+ values.map((_, index) =>
+ getInstanceOverride(
+ restored.instanceOverrides,
+ restored.id,
+ restored.id,
+ `pluginData/${index}`
+ )
+ )
+ ).toEqual(values)
+ })
+})
diff --git a/src/app/libraries/catalog/storage.ts b/src/app/libraries/catalog/storage.ts
index 15db64e17..7b73f5c26 100644
--- a/src/app/libraries/catalog/storage.ts
+++ b/src/app/libraries/catalog/storage.ts
@@ -1,13 +1,12 @@
-import { fromUint8Array, toUint8Array } from 'js-base64'
import * as v from 'valibot'
import {
createLibraryRevision,
deserializeLibraryRevision,
+ encodeStoredLibraryRevision,
MAX_LIBRARY_REVISION_BYTES,
- serializedLibraryRevisionSchema,
- serializeLibraryRevision,
storedLibraryLatestManifestSchema,
+ storedLibraryRevisionSchema,
validateLibraryRevision
} from '@open-pencil/core/library'
import type {
@@ -32,94 +31,11 @@ function revisionKey(libraryId: string, revisionId: string): string {
return `${PREFIX}/${libraryId}/revisions/${revisionId}.json`
}
-const MAP_TAG = 'openpencil/map'
-
-interface EncodedMap {
- $openPencilType: typeof MAP_TAG
- entries: unknown[]
-}
-
-function isEncodedMap(value: object): value is EncodedMap {
- return (
- '$openPencilType' in value &&
- value.$openPencilType === MAP_TAG &&
- 'entries' in value &&
- Array.isArray(value.entries)
- )
-}
-
-function isMap(value: unknown): value is Map<unknown, unknown> {
- return value instanceof Map
-}
-
-function isMarkerShapedObject(value: object): boolean {
- return '$openPencilType' in value
-}
-
-function encodeValue(value: unknown): unknown {
- if (isMap(value)) {
- return {
- $openPencilType: MAP_TAG,
- entries: [...value].map(([key, entry]) => [encodeValue(key), encodeValue(entry)])
- }
- }
- if (value instanceof Uint8Array) return { $bytes: fromUint8Array(value) }
- if (Array.isArray(value)) return value.map(encodeValue)
- if (value && typeof value === 'object') {
- const encoded = Object.fromEntries(
- Object.entries(value).map(([key, entry]) => [key, encodeValue(entry)])
- )
- return isMarkerShapedObject(value)
- ? { $openPencilType: 'openpencil/object', value: encoded }
- : encoded
- }
- return value
-}
-
-function decodeValue(value: unknown): unknown {
- if (Array.isArray(value)) return value.map(decodeValue)
- if (value && typeof value === 'object') {
- if (
- '$openPencilType' in value &&
- value.$openPencilType === 'openpencil/object' &&
- 'value' in value &&
- value.value &&
- typeof value.value === 'object' &&
- !Array.isArray(value.value)
- ) {
- return Object.fromEntries(
- Object.entries(value.value).map(([key, entry]) => [key, decodeValue(entry)])
- )
- }
- if (isEncodedMap(value)) {
- return new Map(
- value.entries.flatMap((entry) =>
- Array.isArray(entry) && entry.length === 2
- ? [[decodeValue(entry[0]), decodeValue(entry[1])] as const]
- : []
- )
- )
- }
- if ('$bytes' in value && typeof (value as { $bytes?: unknown }).$bytes === 'string') {
- return toUint8Array((value as { $bytes: string }).$bytes)
- }
- return Object.fromEntries(
- Object.entries(value).map(([key, entry]) => [key, decodeValue(entry)])
- )
- }
- return value
-}
-
function encodeRevision(revision: ComponentLibraryRevision): Uint8Array {
- return textEncoder.encode(JSON.stringify(encodeValue(serializeLibraryRevision(revision))))
+ return textEncoder.encode(JSON.stringify(encodeStoredLibraryRevision(revision)))
}
-const revisionSchema = v.pipe(
- v.string(),
- v.parseJson(),
- v.transform(decodeValue),
- serializedLibraryRevisionSchema
-)
+const revisionSchema = v.pipe(v.string(), v.parseJson(), storedLibraryRevisionSchema)
const latestManifestSchema = v.pipe(v.string(), v.parseJson(), storedLibraryLatestManifestSchema)