AI-native design editor. Open-source Figma alternative.
Revert "fix(fig): preserve OpenPencil round trips"
Danila Poyarkov9ce6096fc114
diff --git a/packages/core/src/kiwi/node-change/convert.ts b/packages/core/src/kiwi/node-change/convert.tsindex 4f6c17241..6bd47b35b 100644--- a/packages/core/src/kiwi/node-change/convert.ts+++ b/packages/core/src/kiwi/node-change/convert.ts@@ -17,7 +17,6 @@ import {extractPluginRelaunchData,getOpenPencilPluginValue,LAYOUT_DIRECTION_PLUGIN_KEY,- NODE_TYPE_PLUGIN_KEY,TEXT_DIRECTION_PLUGIN_KEY} from './plugin-data'import { resolveGeometryPaths, resolveVectorNetwork } from './vector-geometry'@@ -256,8 +255,7 @@ function convertCornerProps(function importedTextLineHeight(nc: NodeChange): number | null {const derivedLineHeight = nc.derivedTextData?.baselines?.[0]?.lineHeight- if (derivedLineHeight !== undefined && Number.isFinite(derivedLineHeight))- return derivedLineHeight+ if (derivedLineHeight !== undefined && Number.isFinite(derivedLineHeight)) return derivedLineHeightreturn convertLineHeight(nc.lineHeight, nc.fontSize)}@@ -350,9 +348,7 @@ function visibleContainerDerivedLayout(}}-function convertLayoutProps(- nc: NodeChange-): Pick<+function convertLayoutProps(nc: NodeChange): Pick<SceneNode,| 'layoutMode'| 'itemSpacing'@@ -448,12 +444,7 @@ export function nodeChangeToProps(blobs: Uint8Array[]): Partial<SceneNode> & { nodeType: NodeType | 'DOCUMENT' | 'VARIABLE' } {let nodeType = mapNodeType(nc.type)- if (- (nodeType === 'FRAME' && isComponentSet(nc)) ||- getOpenPencilPluginValue(nc, NODE_TYPE_PLUGIN_KEY) === 'COMPONENT_SET'- ) {- nodeType = 'COMPONENT_SET'- }+ if (nodeType === 'FRAME' && isComponentSet(nc)) nodeType = 'COMPONENT_SET'const vectorAndStrokeProps = convertVectorAndStrokeProps(nc, blobs)diff --git a/packages/core/src/kiwi/node-change/export-node.ts b/packages/core/src/kiwi/node-change/export-node.tsindex b3240761f..0f62e4fef 100644--- a/packages/core/src/kiwi/node-change/export-node.ts+++ b/packages/core/src/kiwi/node-change/export-node.ts@@ -3,12 +3,7 @@ import type { SceneGraph, SceneNode } from '#core/scene-graph'import type { Color, GUID, Matrix } from '#core/types'import { stringToGuid } from './guid'-import {- mergePluginData,- NODE_TYPE_PLUGIN_KEY,- serializePluginRelaunchData,- upsertPluginData-} from './plugin-data'+import { mergePluginData, serializePluginRelaunchData } from './plugin-data'export type KiwiNodeChange = NodeChange & Record<string, unknown>@@ -50,37 +45,14 @@ interface SceneNodeToKiwiContext {const DEFAULT_STROKE_WEIGHT = 1-function applyColorVariableBinding(- context: SceneNodeToKiwiContext,- node: SceneNode,- paint: Paint,- field: string-): Paint {- const variableId = node.boundVariables[field]- if (!variableId) return paint- return {- ...paint,- colorVariableBinding: {- variableID: context.varIdToGuid?.get(variableId) ?? stringToGuid(variableId)- }- }-}-function createStrokePaints(context: SceneNodeToKiwiContext, node: SceneNode): Paint[] {- return node.strokes.map((stroke, index) =>- applyColorVariableBinding(- context,- node,- {- type: 'SOLID',- color: context.safeColor(stroke.color),- opacity: stroke.opacity,- visible: stroke.visible,- blendMode: 'NORMAL'- },- `strokes/${index}/color`- )- )+ return node.strokes.map((stroke) => ({+ type: 'SOLID',+ color: context.safeColor(stroke.color),+ opacity: stroke.opacity,+ visible: stroke.visible,+ blendMode: 'NORMAL'+ }))}function componentPropertyValue(value: string) {@@ -147,16 +119,7 @@ function applyNodeVisualProps(nc.borderLeftWeight = node.borderLeftWeight}- if (node.fills.length > 0) {- nc.fillPaints = node.fills.map((fill, index) =>- applyColorVariableBinding(- context,- node,- context.fillToKiwiPaint(fill),- `fills/${index}/color`- )- )- }+ if (node.fills.length > 0) nc.fillPaints = node.fills.map(context.fillToKiwiPaint)context.serializeCornerRadii(node, nc)@@ -221,7 +184,6 @@ export function sceneNodeToKiwiWithContext(applyNodeVisualProps(context, node, nc)applyComponentMetadata(node, nc)- if (node.type === 'COMPONENT_SET') upsertPluginData(node, NODE_TYPE_PLUGIN_KEY, node.type)if (strokePaints.length > 0) nc.strokePaints = strokePaintscontext.serializeLayoutProps(node, nc)diff --git a/packages/core/src/kiwi/node-change/plugin-data.ts b/packages/core/src/kiwi/node-change/plugin-data.tsindex a21b382cf..8b827fb08 100644--- a/packages/core/src/kiwi/node-change/plugin-data.ts+++ b/packages/core/src/kiwi/node-change/plugin-data.ts@@ -6,8 +6,6 @@ import { guidToString } from './guid'export const OPEN_PENCIL_PLUGIN_ID = 'open-pencil'export const TEXT_DIRECTION_PLUGIN_KEY = 'textDirection'export const LAYOUT_DIRECTION_PLUGIN_KEY = 'layoutDirection'-export const NODE_TYPE_PLUGIN_KEY = 'nodeType'-export const BOUND_VARIABLES_PLUGIN_KEY = 'boundVariables'export function upsertPluginData(node: { pluginData: PluginDataEntry[] },@@ -21,26 +19,8 @@ export function upsertPluginData(node.pluginData = pluginData}-function parseBoundVariablesPluginValue(value: string | null): Record<string, string> {- if (!value) return {}- try {- const parsed = JSON.parse(value) as unknown- if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {}- return Object.fromEntries(- Object.entries(parsed).filter(- (entry): entry is [string, string] =>- typeof entry[0] === 'string' && typeof entry[1] === 'string'- )- )- } catch {- return {}- }-}-export function extractBoundVariables(nc: NodeChange): Record<string, string> {- const bindings = parseBoundVariablesPluginValue(- getOpenPencilPluginValue(nc, BOUND_VARIABLES_PLUGIN_KEY)- )+ const bindings: Record<string, string> = {}nc.fillPaints?.forEach((paint, i) => {if (paint.colorVariableBinding) {bindings[`fills/${i}/color`] = guidToString(paint.colorVariableBinding.variableID)diff --git a/packages/core/src/kiwi/node-change/serialize.ts b/packages/core/src/kiwi/node-change/serialize.tsindex 867dd58a2..c22fe9f7d 100644--- a/packages/core/src/kiwi/node-change/serialize.ts+++ b/packages/core/src/kiwi/node-change/serialize.ts@@ -1,7 +1,7 @@import { hexToBytes } from '#core/bytes/hex'-import { encodePathCommandsBlob } from '#core/kiwi/node-change/path-commands'import { buildDerivedTextData as buildSharedDerivedTextData } from '#core/text/derived-text/data'import { normalizeFontFamily, weightToFigmaStyle, weightToStyle } from '#core/text/fonts'+import { encodePathCommandsBlob } from '#core/kiwi/node-change/path-commands'import { getGlyphOutlineMetricsSync } from '#core/text/opentype'import { encodeVectorNetworkBlob, buildStyleOverrideTable } from '#core/vector'export {@@ -17,10 +17,9 @@ import type { NodeChange, Paint, VariableConsumptionEntry } from '#core/kiwi/binimport type { SceneGraph, SceneNode, CharacterStyleOverride } from '#core/scene-graph'import type { Color, GUID, Matrix } from '#core/types'-import { guidToString, stringToGuid, VARIABLE_BINDING_FIELDS } from './convert'+import { stringToGuid, VARIABLE_BINDING_FIELDS } from './convert'import { sceneNodeToKiwiWithContext, type KiwiNodeChange } from './export-node'import {- BOUND_VARIABLES_PLUGIN_KEY,LAYOUT_DIRECTION_PLUGIN_KEY,TEXT_DIRECTION_PLUGIN_KEY,upsertPluginData@@ -107,8 +106,7 @@ function buildDerivedTextData(}const style = weightToStyle(node.fontWeight, node.italic)- const glyphMetrics =- getGlyphOutlineMetricsSync(node.fontFamily, style, node.text, node.fontSize) ?? []+ const glyphMetrics = getGlyphOutlineMetricsSync(node.fontFamily, style, node.text, node.fontSize) ?? []const lineHeight = node.lineHeight ?? Math.ceil(node.fontSize * 1.2)const glyphAdvance = node.text.length > 0 ? node.width / Math.max(node.text.length, 1) : 0@@ -346,16 +344,13 @@ function serializeVariableBindings(): void {if (Object.keys(node.boundVariables).length === 0) returnconst entries: VariableConsumptionEntry[] = []- const roundtripBindings: Record<string, string> = {}const typeMap: Record<string, string> = { COLOR: 'COLOR', BOOLEAN: 'BOOLEAN', STRING: 'STRING' }for (const [field, varId] of Object.entries(node.boundVariables)) {+ const kiwiField = VARIABLE_BINDING_FIELDS[field]+ if (!kiwiField) continueconst variable = graph.variables.get(varId)if (!variable) continueconst varGuid = varIdToGuid?.get(varId) ?? stringToGuid(varId)- roundtripBindings[field] = guidToString(varGuid)-- const kiwiField = VARIABLE_BINDING_FIELDS[field]- if (!kiwiField) continueconst resolvedType = typeMap[variable.type] ?? 'FLOAT'entries.push({variableData: {@@ -366,9 +361,6 @@ function serializeVariableBindings(variableField: kiwiField})}- if (Object.keys(roundtripBindings).length > 0) {- upsertPluginData(node, BOUND_VARIABLES_PLUGIN_KEY, JSON.stringify(roundtripBindings))- }if (entries.length > 0) nc.variableConsumptionMap = { entries }}diff --git a/packages/core/src/scene-graph/index.ts b/packages/core/src/scene-graph/index.tsindex f196b89ad..6bec41eeb 100644--- a/packages/core/src/scene-graph/index.ts+++ b/packages/core/src/scene-graph/index.ts@@ -403,9 +403,7 @@ export class SceneGraph {if (node.figmaDerivedTextGlyphs && 'text' in changes) node.figmaDerivedTextGlyphs = null}const entries = Object.entries(changes) as Array<[string, unknown]>- changes = Object.fromEntries(- entries.filter(([, value]) => value !== undefined)- ) as Partial<SceneNode>+ changes = Object.fromEntries(entries.filter(([, value]) => value !== undefined)) as Partial<SceneNode>if (changes.vectorNetwork) {changes = { ...changes, vectorNetwork: normalizeVectorNetwork(changes.vectorNetwork) }}@@ -593,7 +591,9 @@ export class SceneGraph {const child = this.nodes.get(childId)if (!child) continueresult.push({ node: child, depth })- if (child.childIds.length > 0) result.push(...this.flattenTree(childId, depth + 1))+ if (child.childIds.length > 0) {+ result.push(...this.flattenTree(childId, depth + 1))+ }}return result}diff --git a/packages/vue/src/shared/input/wheel.ts b/packages/vue/src/shared/input/wheel.tsindex 5bd84f71b..43ff7c9eb 100644--- a/packages/vue/src/shared/input/wheel.ts+++ b/packages/vue/src/shared/input/wheel.ts@@ -2,7 +2,6 @@ import { useEventListener } from '@vueuse/core'import type { Ref } from 'vue'import type { Editor } from '@open-pencil/core/editor'-import { createRafScheduler } from '#vue/shared/input/raf-scheduler'type WheelAccum = {@@ -32,14 +31,14 @@ function normalizeWheelDelta(e: WheelEvent): { dx: number; dy: number } {const WHEEL_ZOOM_SPEED = 1.25-function wheelDeltaModeScale(event: WheelEvent) {- if (event.deltaMode === 1) return 0.05- return event.deltaMode ? 1 : 0.002-}-function wheelZoomDelta(event: WheelEvent) {const factor = event.ctrlKey && isMacOs() ? 10 : 1- return -event.deltaY * wheelDeltaModeScale(event) * factor * WHEEL_ZOOM_SPEED+ return (+ -event.deltaY *+ (event.deltaMode === 1 ? 0.05 : (event.deltaMode ? 1 : 0.002)) *+ factor *+ WHEEL_ZOOM_SPEED+ )}export function setupWheelPanZoom(canvasRef: Ref<HTMLCanvasElement | null>, editor: Editor) {diff --git a/src/app/document/io/imported-document.ts b/src/app/document/io/imported-document.tsindex 000b8b64a..98a4f0356 100644--- a/src/app/document/io/imported-document.ts+++ b/src/app/document/io/imported-document.ts@@ -1,9 +1,7 @@import type { Editor } from '@open-pencil/core/editor'-import { computeAllLayouts } from '@open-pencil/core/layout'import type { SceneGraph, SceneNode } from '@open-pencil/core/scene-graph'export async function applyImportedDocument(editor: Editor, imported: SceneGraph) {- computeAllLayouts(imported)editor.replaceGraph(imported)editor.undo.clear()editor.clearSelection()diff --git a/src/app/document/io/read.ts b/src/app/document/io/read.tsindex e755bceed..d145aa3b4 100644--- a/src/app/document/io/read.ts+++ b/src/app/document/io/read.ts@@ -1,6 +1,5 @@import type { Editor, EditorState } from '@open-pencil/core/editor'import { readFigFile } from '@open-pencil/core/io/formats/fig'-import { computeAllLayouts } from '@open-pencil/core/layout'import { yieldToUI } from '@/app/document/io/browser'import { applyImportedDocument } from '@/app/document/io/imported-document'@@ -81,7 +80,6 @@ export function createReloadActions({fileHandle})if (!imported) return- computeAllLayouts(imported)editor.replaceGraph(imported)editor.undo.clear()diff --git a/src/app/tabs/index.ts b/src/app/tabs/index.tsindex f08777e7d..3aca12909 100644--- a/src/app/tabs/index.ts+++ b/src/app/tabs/index.ts@@ -2,7 +2,6 @@ import { shallowRef, computed, triggerRef } from 'vue'import { BUILTIN_IO_FORMATS, IORegistry } from '@open-pencil/core/io'import { readFigFile } from '@open-pencil/core/io/formats/fig'-import { computeAllLayouts } from '@open-pencil/core/layout'import type { SceneGraph } from '@open-pencil/core/scene-graph'import { setOpenPencilStore } from '@/app/browser-bridge'@@ -116,7 +115,6 @@ export async function openFileInNewTab(data: new Uint8Array(await file.arrayBuffer())})- computeAllLayouts(imported)store.replaceGraph(imported)store.undo.clear()store.setDocumentSource(file.name, sourceFormat, handle, path)diff --git a/tests/engine/io/fig/roundtrip/basic.test.ts b/tests/engine/io/fig/roundtrip/basic.test.tsindex b35730b1b..356c15e97 100644--- a/tests/engine/io/fig/roundtrip/basic.test.ts+++ b/tests/engine/io/fig/roundtrip/basic.test.ts@@ -180,30 +180,6 @@ describe('roundtrip: export → re-import', () => {expect(types.get('ELLIPSE')).toBe(1)})- test('preserves component sets', async () => {- const graph = new SceneGraph()- const page = graph.getPages()[0]- const componentSet = graph.createNode('COMPONENT_SET', page.id, {- name: 'Button',- width: 240,- height: 80- })- graph.createNode('COMPONENT', componentSet.id, {- name: 'Primary',- x: 16,- y: 16,- width: 96,- height: 40- })-- const figBytes = await exportFigFile(graph)- const parsed = await parseFigFile(figBytes.buffer as ArrayBuffer)- const parsedSet = parsed.getAllNodes().find((node) => node.name === 'Button')-- expect(parsedSet).toBeDefined()- expect(expectDefined(parsedSet, 'parsedSet').type).toBe('COMPONENT_SET')- })-test('preserves node names', () => {const names = new Set(reImportedNodes.map((n) => n.name))expect(names.has('Container')).toBe(true)diff --git a/tests/engine/io/fig/roundtrip/variables.test.ts b/tests/engine/io/fig/roundtrip/variables.test.tsindex e585df5e8..478ceea77 100644--- a/tests/engine/io/fig/roundtrip/variables.test.ts+++ b/tests/engine/io/fig/roundtrip/variables.test.ts@@ -64,56 +64,22 @@ describe('variable roundtrip', () => {const graph = new SceneGraph()const col = graph.createCollection('Tokens')const floatVar = graph.createVariable('radius', 'FLOAT', col.id, 12)- const fillVar = graph.createVariable('surface', 'COLOR', col.id, { r: 1, g: 1, b: 1, a: 1 })- const strokeVar = graph.createVariable('border', 'COLOR', col.id, {- r: 0.1,- g: 0.2,- b: 0.3,- a: 1- })const page = graph.getPages()[0]const rect = graph.createNode('RECTANGLE', page.id, {name: 'Bound Rect',width: 100,height: 100,- cornerRadius: 12,- fills: [- {- type: 'SOLID',- color: { r: 1, g: 1, b: 1, a: 1 },- opacity: 1,- visible: true,- blendMode: 'NORMAL'- }- ],- strokes: [- {- color: { r: 0.1, g: 0.2, b: 0.3, a: 1 },- weight: 1,- opacity: 1,- visible: true,- align: 'INSIDE',- cap: 'NONE',- join: 'MITER',- dashPattern: []- }- ]+ cornerRadius: 12})graph.bindVariable(rect.id, 'cornerRadius', floatVar.id)- graph.bindVariable(rect.id, 'fills/0/color', fillVar.id)- graph.bindVariable(rect.id, 'strokes/0/color', strokeVar.id)const exported = await exportFigFile(graph)const reimported = await parseFigFile(exported.buffer as ArrayBuffer)const reimportedRect = [...reimported.getAllNodes()].find((n) => n.name === 'Bound Rect')expect(reimportedRect).toBeDefined()- expect(Object.keys(expectDefined(reimportedRect, 'reimportedRect').boundVariables)).toContain(- 'cornerRadius'- )- expect(Object.keys(reimportedRect.boundVariables)).toContain('fills/0/color')- expect(Object.keys(reimportedRect.boundVariables)).toContain('strokes/0/color')+ expect(Object.keys(reimportedRect.boundVariables)).toContain('cornerRadius')})test('material3.fig variables survive round-trip', async () => {