* fix(fig): read, render, and write Figma slots Instances of a component with a slot showed the component's default content instead of their own, and saving to .fig dropped slot properties, their settings, and every instance's content. Figma stores an instance's slot content as a frame on the internal canvas and assigns the slot property that frame's GUID. The reader follows the assignment while expanding the slot frame and pulls content frames into the dependency closure without making them layers. The scene graph gains a SLOT property type with its settings, a SLOT_CONTENT binding, and the rule that an assigned slot's content belongs to the instance, which component sync now leaves alone. The writer emits content frames on the internal canvas and binds slot frames through the parameter map, as Figma does. The occurrence and diagnostic types move from the interpreter to instance-overrides/occurrence.ts to keep it under the size limit. * fix(fig): keep slot content through swaps and missing content frames A slot assignment whose content frame the archive lacks no longer refuses the document: the reader reports it through onMissingSlotContent, like a missing component, and the slot keeps its component's content. Swapping an instance's component, which variant switches do, now carries the instance's own slot content to the new component's slot of the same name instead of dropping it, as Figma does. Component sync reads which slot a frame is from the component, whose bindings instance copies do not receive. Clipboard export numbers slot content after the other records on its dependency canvas, and the reader and writer share Figma's default slot value. * docs: note slot content kept across variant switches * fix(fig): pair clipboard text by record and drop dangling slot assignments The Figma clipboard paired text records with source text nodes by traversal order, but instance-owned slot content is written after the selected layers, so slotted text and the text after it swapped shaping data. Records are now paired with their nodes through their GUIDs. An instance assignment whose slot content frame is missing is dropped along with the reported diagnostic, so the slot keeps following its component instead of looking instance-owned. * ci: pull the slots fixture for unit tests * test: use GUID and Array.from in the slot tests * refactor(fig): group occurrence types and paths in one folder occurrence.ts and occurrence-path.ts became sibling prefixes when the occurrence types moved out of the interpreter; they now live in instance-overrides/occurrence/ as types.ts and path.ts.
116 lines
4.2 KiB
TypeScript
116 lines
4.2 KiB
TypeScript
import type { NodeChange } from '@open-pencil/kiwi/fig/codec'
|
|
|
|
import { idOf, parentIdOf, type SourceIndex } from '../instance-overrides/source-index'
|
|
import { componentDependencies, slotContentDependencies } from './component/dependencies'
|
|
import { createResourceResolver } from './resource-reference'
|
|
import { styleDependencies } from './style-dependencies'
|
|
|
|
export interface SceneDependencyClosure {
|
|
/** Live page trees plus explicitly referenced component and slot content trees. */
|
|
contentIds: ReadonlySet<string>
|
|
/** Required ownership containers, without automatically including their siblings. */
|
|
ancestorIds: ReadonlySet<string>
|
|
/** Referenced records the archive lacks: broken hierarchy or style references. */
|
|
missingIds: ReadonlySet<string>
|
|
/** Components instances or defaults reference that Figma has since deleted. */
|
|
missingComponentIds: ReadonlySet<string>
|
|
/** Slot content frames instance assignments name that the archive does not contain. */
|
|
missingSlotContentIds: ReadonlySet<string>
|
|
externalPreferredKeys: ReadonlySet<string>
|
|
}
|
|
|
|
function validatePageSelection(
|
|
sources: ReadonlyMap<string, NodeChange>,
|
|
pageIds?: ReadonlySet<string>
|
|
): void {
|
|
if (!pageIds) return
|
|
for (const id of pageIds)
|
|
if (sources.get(id)?.type !== 'CANVAS') throw new Error(`Unknown page ${id}`)
|
|
}
|
|
|
|
/** Ownership chains above content, stopping where an earlier chain already continues. */
|
|
function collectAncestors(
|
|
sources: ReadonlyMap<string, NodeChange>,
|
|
contentIds: ReadonlySet<string>,
|
|
ancestorIds: Set<string>,
|
|
missingIds: Set<string>
|
|
): void {
|
|
for (const id of contentIds) {
|
|
const visited = new Set<string>([id])
|
|
let node = sources.get(id)
|
|
let parent = node ? parentIdOf(node) : undefined
|
|
while (parent !== undefined) {
|
|
if (visited.has(parent)) throw new Error(`Cyclic source hierarchy at ${parent}`)
|
|
visited.add(parent)
|
|
if (ancestorIds.has(parent)) break
|
|
ancestorIds.add(parent)
|
|
node = sources.get(parent)
|
|
if (!node) {
|
|
missingIds.add(parent)
|
|
break
|
|
}
|
|
parent = parentIdOf(node)
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Plan reachability without deleting records or expanding unrelated internal siblings. */
|
|
export function collectSceneDependencies(
|
|
changes: readonly NodeChange[],
|
|
pageIds: ReadonlySet<string> | undefined,
|
|
index: SourceIndex
|
|
): SceneDependencyClosure {
|
|
const resolveReference = createResourceResolver(changes)
|
|
const { sources, children } = index
|
|
const availableIds = new Set(sources.keys())
|
|
const contentIds = new Set<string>()
|
|
const ancestorIds = new Set<string>()
|
|
const externalPreferredKeys = new Set<string>()
|
|
const missingIds = new Set<string>()
|
|
const missingComponentIds = new Set<string>()
|
|
const componentReferences = new Set<string>()
|
|
const slotContentReferences = new Set<string>()
|
|
const missingSlotContentIds = new Set<string>()
|
|
validatePageSelection(sources, pageIds)
|
|
const pending = changes
|
|
.filter(
|
|
(node) =>
|
|
node.type === 'CANVAS' &&
|
|
(pageIds ? pageIds.has(idOf(node) ?? '') : node.internalOnly !== true)
|
|
)
|
|
.flatMap((node) => idOf(node) ?? [])
|
|
while (pending.length) {
|
|
const id = pending.pop()
|
|
if (!id || contentIds.has(id)) continue
|
|
const node = sources.get(id)
|
|
if (!node) {
|
|
if (componentReferences.has(id)) missingComponentIds.add(id)
|
|
else if (slotContentReferences.has(id)) missingSlotContentIds.add(id)
|
|
else missingIds.add(id)
|
|
continue
|
|
}
|
|
contentIds.add(id)
|
|
const components = componentDependencies(node, resolveReference, (key) =>
|
|
externalPreferredKeys.add(key)
|
|
)
|
|
for (const component of components) componentReferences.add(component)
|
|
const slotContent = slotContentDependencies(node)
|
|
for (const content of slotContent) slotContentReferences.add(content)
|
|
pending.push(
|
|
...(children.get(id) ?? []).flatMap((child) => idOf(child) ?? []),
|
|
...styleDependencies(node, resolveReference, availableIds),
|
|
...components,
|
|
...slotContent
|
|
)
|
|
}
|
|
collectAncestors(sources, contentIds, ancestorIds, missingIds)
|
|
return {
|
|
contentIds,
|
|
ancestorIds,
|
|
missingIds,
|
|
missingComponentIds,
|
|
missingSlotContentIds,
|
|
externalPreferredKeys
|
|
}
|
|
}
|