Skip to content

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

fix(core): keep changes to enabled libraries when resaving a .fig (#1503)

Marcos Velásquez9ebad112118b
diff --git a/changes/1503/fixed.md b/changes/1503/fixed.md
new file mode 100644
index 000000000..48244a3db
--- /dev/null
+++ b/changes/1503/fixed.md
@@ -0,0 +1 @@
+- A change to the libraries a `.fig` document enables, such as updating one, is kept when the document is saved again.
diff --git a/packages/core/src/io/formats/fig/export/patch/fields.ts b/packages/core/src/io/formats/fig/export/patch/fields.ts
index fe06925d1..2060d9994 100644
--- a/packages/core/src/io/formats/fig/export/patch/fields.ts
+++ b/packages/core/src/io/formats/fig/export/patch/fields.ts
@@ -48,6 +48,9 @@ function archivedGraph(source: SceneGraph, baseline: ArchiveBaseline): SceneGrap
)
graph.variableCollections = new Map(structuredClone(baseline.collections.collections))
graph.activeMode = new Map(baseline.collections.activeMode)
+ // The document record compares with the settings the archive saved, as the layers do.
+ graph.documentColorSpace = baseline.settings.colorSpace
+ graph.enabledLibraries = new Map(structuredClone(baseline.settings.libraries))
return graph
}
diff --git a/packages/core/src/kiwi/fig/session/archive/changes.ts b/packages/core/src/kiwi/fig/session/archive/changes.ts
index 42a28b6e6..2ad6d5a2d 100644
--- a/packages/core/src/kiwi/fig/session/archive/changes.ts
+++ b/packages/core/src/kiwi/fig/session/archive/changes.ts
@@ -1,6 +1,13 @@
import { isEqual } from 'es-toolkit/predicate'
-import type { SceneGraph, SceneNode, Variable, VariableCollection } from '@open-pencil/scene-graph'
+import type {
+ DocumentColorSpace,
+ EnabledLibraryBinding,
+ SceneGraph,
+ SceneNode,
+ Variable,
+ VariableCollection
+} from '@open-pencil/scene-graph'
/**
* How a document opened from a `.fig` archive has diverged from it, for writing only what
@@ -23,7 +30,13 @@ interface ArchiveChanges {
resourceIds: Set<string>
variables: Map<string, Variable>
collections: CollectionsSnapshot
- settings: unknown
+ settings: SettingsSnapshot
+}
+
+/** The document's own settings, which its record holds rather than any layer. */
+interface SettingsSnapshot {
+ colorSpace: DocumentColorSpace
+ libraries: Array<[string, EnabledLibraryBinding]>
}
/** The variable collections and the modes the document shows them in. */
@@ -34,7 +47,7 @@ interface CollectionsSnapshot {
const changes = new WeakMap<SceneGraph, ArchiveChanges>()
-function settingsSnapshot(graph: SceneGraph): unknown {
+function settingsSnapshot(graph: SceneGraph): SettingsSnapshot {
return structuredClone({
colorSpace: graph.documentColorSpace,
libraries: [...graph.enabledLibraries]
@@ -137,6 +150,7 @@ export interface ArchiveBaseline {
loadedAfterEdits: ReadonlySet<string>
variables: ReadonlyMap<string, Variable>
collections: CollectionsSnapshot
+ settings: SettingsSnapshot
}
export function archiveBaseline(graph: SceneGraph): ArchiveBaseline | undefined {
@@ -147,6 +161,7 @@ export function archiveBaseline(graph: SceneGraph): ArchiveBaseline | undefined
created: entry.created,
loadedAfterEdits: entry.loadedAfterEdits,
variables: entry.variables,
- collections: entry.collections
+ collections: entry.collections,
+ settings: entry.settings
}
}
diff --git a/packages/core/tests/io/formats/fig/export/patch/index.test.ts b/packages/core/tests/io/formats/fig/export/patch/index.test.ts
index 203cfc869..71d266b3e 100644
--- a/packages/core/tests/io/formats/fig/export/patch/index.test.ts
+++ b/packages/core/tests/io/formats/fig/export/patch/index.test.ts
@@ -375,6 +375,15 @@ describe('writing an edited document into its archive', () => {
})
})
+ test('keeps a change to the libraries the document enables', async () => {
+ const binding = { libraryId: 'ds', revisionId: 'two', enabled: true }
+ const { patched } = await exportBothWays((graph) => {
+ graph.enabledLibraries.set('ds', binding)
+ })
+ const reopened = await open(patched)
+ expect(reopened.enabledLibraries.get('ds')).toEqual(binding)
+ })
+
test('keeps every archive record its own when a new variable takes an ID it uses', async () => {
const bytes = await fixtureBytes()
const graph = await open(bytes)
@@ -408,7 +417,8 @@ describe('writing an edited document into its archive', () => {
const before = recordsOf(bytes)
const after = recordsOf(patched)
expect([...after.keys()].toSorted()).toEqual([...before.keys()].toSorted())
- for (const [guid, record] of before) expect(after.get(guid)?.parentIndex).toEqual(record.parentIndex)
+ for (const [guid, record] of before)
+ expect(after.get(guid)?.parentIndex).toEqual(record.parentIndex)
})
test('keeps layers on unopened pages bound to a variable whose value changes', async () => {