Skip to content

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

fix(properties): preserve shared corner bindings in collapsed controls

Danila Poyarkoveb38da0d086b
diff --git a/packages/vue/src/controls/appearance/helpers.ts b/packages/vue/src/controls/appearance/helpers.ts
index 31ef6c86e..4695de183 100644
--- a/packages/vue/src/controls/appearance/helpers.ts
+++ b/packages/vue/src/controls/appearance/helpers.ts
@@ -1,10 +1,10 @@
import { computed } from 'vue'
-import type { ComputedRef } from 'vue'
+import type { ComputedRef, Ref } from 'vue'
import type { Editor } from '@open-pencil/core/editor'
import type { BlendMode, SceneNode } from '@open-pencil/scene-graph'
-import type { CornerGeometryKey } from '#vue/controls/appearance/types'
+import type { CornerGeometryKey, CornerRadiusKey } from '#vue/controls/appearance/types'
import { MIXED, type MixedValue } from '#vue/controls/node-props/use'
const CORNER_RADIUS_TYPES = new Set([
@@ -15,7 +15,15 @@ const CORNER_RADIUS_TYPES = new Set([
'INSTANCE'
])
+const CORNER_PATHS: CornerRadiusKey[] = [
+ 'topLeftRadius',
+ 'topRightRadius',
+ 'bottomRightRadius',
+ 'bottomLeftRadius'
+]
+
type AppearanceStateOptions = {
+ expandedCornerNodeId?: Ref<string | null>
node: ComputedRef<SceneNode | null>
nodes: ComputedRef<SceneNode[]>
isMulti: ComputedRef<boolean>
@@ -44,7 +52,13 @@ function hasUnequalCorners(node: SceneNode) {
)
}
-export function createAppearanceState({ node, nodes, isMulti, merged }: AppearanceStateOptions) {
+export function createAppearanceState({
+ node,
+ nodes,
+ isMulti,
+ merged,
+ expandedCornerNodeId
+}: AppearanceStateOptions) {
const hasCornerRadius = computed(() => {
if (isMulti.value) return nodes.value.every((n) => CORNER_RADIUS_TYPES.has(n.type))
return node.value ? CORNER_RADIUS_TYPES.has(node.value.type) : false
@@ -59,14 +73,25 @@ export function createAppearanceState({ node, nodes, isMulti, merged }: Appearan
if (isMulti.value) return false
const selected = node.value
return selected
- ? hasUnequalCorners(selected) ||
+ ? expandedCornerNodeId?.value === selected.id ||
+ hasUnequalCorners(selected) ||
(selected.independentCorners && !cornersHaveEquivalentBindings(selected))
: false
})
const cornerRadiusValue = computed(() => {
if (isMulti.value) return merged('cornerRadius')
- return node.value?.cornerRadius ?? 0
+ const selected = node.value
+ return selected && cornersHaveEquivalentBindings(selected) && !hasUnequalCorners(selected)
+ ? selected.topLeftRadius
+ : (selected?.cornerRadius ?? 0)
+ })
+
+ const cornerRadiusBindingPaths = computed<Array<CornerRadiusKey | 'cornerRadius'>>(() => {
+ const selected = node.value
+ return selected && cornersHaveEquivalentBindings(selected) && !hasUnequalCorners(selected)
+ ? CORNER_PATHS
+ : ['cornerRadius']
})
const cornerSmoothingPercent = computed(() => {
@@ -95,6 +120,7 @@ export function createAppearanceState({ node, nodes, isMulti, merged }: Appearan
independentCorners,
showIndependentCorners,
cornerRadiusValue,
+ cornerRadiusBindingPaths,
cornerSmoothingPercent,
opacityPercent,
blendModeValue,
@@ -102,7 +128,13 @@ export function createAppearanceState({ node, nodes, isMulti, merged }: Appearan
}
}
-export function createAppearanceActions({ editor, node, nodes, isMulti }: AppearanceActionOptions) {
+export function createAppearanceActions({
+ editor,
+ node,
+ nodes,
+ isMulti,
+ expandedCornerNodeId
+}: AppearanceActionOptions) {
const previousCornerValues = new Map<CornerGeometryKey, Map<string, number>>()
function setBlendMode(value: BlendMode) {
@@ -143,6 +175,16 @@ export function createAppearanceActions({ editor, node, nodes, isMulti }: Appear
function toggleIndependentCorners() {
const selected = node.value
+ if (
+ !isMulti.value &&
+ selected &&
+ expandedCornerNodeId &&
+ cornersHaveEquivalentBindings(selected) &&
+ !hasUnequalCorners(selected)
+ ) {
+ expandedCornerNodeId.value = expandedCornerNodeId.value === selected.id ? null : selected.id
+ return
+ }
const targets = isMulti.value ? [...nodes.value] : []
if (!isMulti.value && selected) targets.push(selected)
if (targets.length === 0) return
@@ -223,7 +265,53 @@ export function createAppearanceActions({ editor, node, nodes, isMulti }: Appear
previousCornerValues.delete(key)
}
+ type UniformCorners = Pick<SceneNode, CornerRadiusKey | 'cornerRadius' | 'independentCorners'>
+ const previousUniformCorners = new Map<string, UniformCorners>()
+
+ function updateUniformRadius(value: number) {
+ for (const target of cornerTargets()) {
+ if (!previousUniformCorners.has(target.id)) {
+ previousUniformCorners.set(target.id, {
+ cornerRadius: target.cornerRadius,
+ independentCorners: target.independentCorners,
+ topLeftRadius: target.topLeftRadius,
+ topRightRadius: target.topRightRadius,
+ bottomRightRadius: target.bottomRightRadius,
+ bottomLeftRadius: target.bottomLeftRadius
+ })
+ }
+ const previous = previousUniformCorners.get(target.id)
+ if (
+ previous &&
+ value === (previous.independentCorners ? previous.topLeftRadius : previous.cornerRadius)
+ ) {
+ editor.updateNode(target.id, previous)
+ previousUniformCorners.delete(target.id)
+ continue
+ }
+ editor.updateNode(target.id, {
+ cornerRadius: value,
+ independentCorners: false,
+ topLeftRadius: value,
+ topRightRadius: value,
+ bottomRightRadius: value,
+ bottomLeftRadius: value
+ })
+ }
+ }
+
+ function commitUniformRadius() {
+ editor.undo.runBatch('Change corner radius', () => {
+ for (const [id, previous] of previousUniformCorners) {
+ editor.commitNodeUpdate(id, previous, 'Change corner radius')
+ }
+ })
+ previousUniformCorners.clear()
+ }
+
return {
+ updateUniformRadius,
+ commitUniformRadius,
setBlendMode,
toggleVisibility,
toggleIndependentCorners,
diff --git a/packages/vue/src/controls/appearance/use.ts b/packages/vue/src/controls/appearance/use.ts
index e85bb8e42..05b341db3 100644
--- a/packages/vue/src/controls/appearance/use.ts
+++ b/packages/vue/src/controls/appearance/use.ts
@@ -1,3 +1,5 @@
+import { ref } from 'vue'
+
import { createAppearanceActions, createAppearanceState } from '#vue/controls/appearance/helpers'
import { useNodeProps } from '#vue/controls/node-props/use'
import { useEditor } from '#vue/editor/context'
@@ -12,8 +14,10 @@ export function useAppearance() {
const editor = useEditor()
const { nodes, node, active, isMulti, merged, updateProp, commitProp } = useNodeProps()
- const appearanceState = createAppearanceState({ node, nodes, isMulti, merged })
- const appearanceActions = createAppearanceActions({ editor, node, nodes, isMulti, merged })
+ const expandedCornerNodeId = ref<string | null>(null)
+ const options = { node, nodes, isMulti, merged, expandedCornerNodeId }
+ const appearanceState = createAppearanceState(options)
+ const appearanceActions = createAppearanceActions({ editor, ...options })
return {
editor,
diff --git a/packages/vue/src/primitives/AppearanceControls/AppearanceControlsRoot.vue b/packages/vue/src/primitives/AppearanceControls/AppearanceControlsRoot.vue
index cde5265cb..40391f4f3 100644
--- a/packages/vue/src/primitives/AppearanceControls/AppearanceControlsRoot.vue
+++ b/packages/vue/src/primitives/AppearanceControls/AppearanceControlsRoot.vue
@@ -10,6 +10,8 @@ const actions = {
setBlendMode: ctx.setBlendMode,
toggleVisibility: ctx.toggleVisibility,
toggleIndependentCorners: ctx.toggleIndependentCorners,
+ updateUniformRadius: ctx.updateUniformRadius,
+ commitUniformRadius: ctx.commitUniformRadius,
updateCornerProp: ctx.updateCornerProp,
commitCornerProp: ctx.commitCornerProp
}
@@ -24,6 +26,7 @@ const actions = {
:independent-corners="ctx.independentCorners.value"
:show-independent-corners="ctx.showIndependentCorners.value"
:corner-radius-value="ctx.cornerRadiusValue.value"
+ :corner-radius-binding-paths="ctx.cornerRadiusBindingPaths.value"
:corner-smoothing-percent="ctx.cornerSmoothingPercent.value"
:opacity-percent="ctx.opacityPercent.value"
:blend-mode-value="ctx.blendModeValue.value"
diff --git a/packages/vue/src/primitives/AppearanceControls/types.ts b/packages/vue/src/primitives/AppearanceControls/types.ts
index 157885f64..57b183e66 100644
--- a/packages/vue/src/primitives/AppearanceControls/types.ts
+++ b/packages/vue/src/primitives/AppearanceControls/types.ts
@@ -2,7 +2,7 @@ import type { VNode } from 'vue'
import type { BlendMode, SceneNode } from '@open-pencil/scene-graph'
-import type { CornerGeometryKey } from '#vue/controls/appearance/types'
+import type { CornerGeometryKey, CornerRadiusKey } from '#vue/controls/appearance/types'
import type { MixedValue } from '#vue/controls/node-props/use'
export interface AppearanceControlsActions {
@@ -11,6 +11,8 @@ export interface AppearanceControlsActions {
setBlendMode(value: BlendMode): void
toggleVisibility(): void
toggleIndependentCorners(): void
+ updateUniformRadius(value: number): void
+ commitUniformRadius(): void
updateCornerProp(key: CornerGeometryKey, value: number): void
commitCornerProp(key: CornerGeometryKey, value: number, previous: number): void
}
@@ -23,6 +25,7 @@ export interface AppearanceControlsRootSlotProps {
independentCorners: MixedValue<boolean>
showIndependentCorners: boolean
cornerRadiusValue: MixedValue<number>
+ cornerRadiusBindingPaths: Array<CornerRadiusKey | 'cornerRadius'>
cornerSmoothingPercent: MixedValue<number>
opacityPercent: MixedValue<number>
blendModeValue: MixedValue<BlendMode>
diff --git a/src/components/properties/AppearanceSection.vue b/src/components/properties/AppearanceSection.vue
index efe650833..7313fbc05 100644
--- a/src/components/properties/AppearanceSection.vue
+++ b/src/components/properties/AppearanceSection.vue
@@ -30,9 +30,9 @@ function blendModeOptions(value: BlendMode | typeof MIXED) {
isMulti,
active,
hasCornerRadius,
- independentCorners,
showIndependentCorners,
cornerRadiusValue,
+ cornerRadiusBindingPaths,
cornerSmoothingPercent,
opacityPercent,
blendModeValue,
@@ -110,8 +110,9 @@ function blendModeOptions(value: BlendMode | typeof MIXED) {
:min="0"
:node-id="node.id"
binding-path="cornerRadius"
- @update:model-value="actions.updateProp('cornerRadius', $event)"
- @commit="(v: number, p: number) => actions.commitProp('cornerRadius', v, p)"
+ :binding-paths="cornerRadiusBindingPaths"
+ @update:model-value="actions.updateUniformRadius"
+ @commit="actions.commitUniformRadius"
>
<template #icon>
<icon-lucide-square-round-corner class="size-3" />
@@ -123,8 +124,8 @@ function blendModeOptions(value: BlendMode | typeof MIXED) {
:aria-label="panels.radius"
:model-value="cornerRadiusValue"
:min="0"
- @update:model-value="actions.updateProp('cornerRadius', $event)"
- @commit="(v: number, p: number) => actions.commitProp('cornerRadius', v, p)"
+ @update:model-value="actions.updateUniformRadius"
+ @commit="actions.commitUniformRadius"
>
<template #icon>
<icon-lucide-square-round-corner class="size-3" />
@@ -135,7 +136,7 @@ function blendModeOptions(value: BlendMode | typeof MIXED) {
<IconButton
:label="panels.independentCornerRadii"
size="xs"
- :active="independentCorners === true"
+ :active="showIndependentCorners"
@click="actions.toggleIndependentCorners"
>
<icon-lucide-square-round-corner class="size-3" />
diff --git a/src/components/properties/VariableNumberField.vue b/src/components/properties/VariableNumberField.vue
index fa6ac15c7..3b720d29c 100644
--- a/src/components/properties/VariableNumberField.vue
+++ b/src/components/properties/VariableNumberField.vue
@@ -19,7 +19,8 @@ const {
sensitivity,
placeholder,
nodeId,
- bindingPath
+ bindingPath,
+ bindingPaths
} = defineProps<{
modelValue: number | symbol
min?: number
@@ -32,6 +33,7 @@ const {
placeholder?: string
nodeId: string
bindingPath: NumberBindingPath
+ bindingPaths?: readonly NumberBindingPath[]
}>()
const emit = defineEmits<{
@@ -42,7 +44,9 @@ const emit = defineEmits<{
const { panels, common } = useI18n()
const provider = useNumberBindingProvider()
const attrs = useAttrs()
-const targets = computed<BindingTarget[]>(() => [{ nodeId, path: bindingPath }])
+const targets = computed<BindingTarget[]>(() =>
+ (bindingPaths ?? [bindingPath]).map((path) => ({ nodeId, path }))
+)
const accessibleLabel = computed(() => {
const ariaLabel = attrs['aria-label']
return typeof ariaLabel === 'string' ? ariaLabel : (label ?? bindingPath)
diff --git a/tests/e2e/properties/corner-bindings.spec.ts b/tests/e2e/properties/corner-bindings.spec.ts
new file mode 100644
index 000000000..6f0dd3f39
--- /dev/null
+++ b/tests/e2e/properties/corner-bindings.spec.ts
@@ -0,0 +1,100 @@
+import { expect, test } from '@playwright/test'
+
+import { CanvasHelper } from '#tests/helpers/canvas'
+import { propertyField, propertySection } from '#tests/helpers/properties'
+
+const paths = ['topLeftRadius', 'topRightRadius', 'bottomRightRadius', 'bottomLeftRadius'] as const
+
+test('collapsed equivalent corners preserve their shared token through expansion and edits', async ({
+ page
+}) => {
+ await page.goto('/')
+ const canvas = new CanvasHelper(page)
+ await canvas.waitForInit()
+ const id = await page.evaluate(() => {
+ const editor = window.openPencil?.getStore?.()
+ if (!editor) throw new Error('Editor unavailable')
+ const collection = editor.graph.createCollection('Corners')
+ const variable = editor.graph.createVariable('Radius/shared', 'FLOAT', collection.id, 12)
+ const node = editor.graph.createNode('FRAME', editor.state.currentPageId, {
+ name: 'Shared corner bindings',
+ x: 100,
+ y: 100,
+ width: 160,
+ height: 120,
+ cornerRadius: 0,
+ independentCorners: true,
+ topLeftRadius: 12,
+ topRightRadius: 12,
+ bottomRightRadius: 12,
+ bottomLeftRadius: 12,
+ boundVariables: {
+ topLeftRadius: variable.id,
+ topRightRadius: variable.id,
+ bottomRightRadius: variable.id,
+ bottomLeftRadius: variable.id
+ }
+ })
+ editor.select([node.id])
+ editor.requestRender()
+ return node.id
+ })
+ const section = propertySection(page, 'Appearance')
+ const field = propertyField(page, 'cornerRadius')
+ await expect(field.getByText('Radius/shared', { exact: true })).toBeVisible()
+ if (process.platform === 'darwin') {
+ await expect(section).toHaveScreenshot('shared-corner-token.png')
+ }
+ const readCorners = () =>
+ page.evaluate(
+ ({ id, paths }) => {
+ const node = window.openPencil?.getStore?.().graph.getNode(id)
+ if (!node) throw new Error('Missing fixture')
+ return {
+ values: paths.map((path) => node[path]),
+ bindings: paths.map((path) => node.boundVariables[path] ?? null),
+ independent: node.independentCorners
+ }
+ },
+ { id, paths }
+ )
+ const original = await readCorners()
+ const toggle = section.getByRole('button', { name: 'Independent corner radii' })
+ await toggle.click()
+ for (const path of paths) {
+ await expect(
+ propertyField(page, path).getByText('Radius/shared', { exact: true })
+ ).toBeVisible()
+ }
+ expect(await readCorners()).toEqual(original)
+ await toggle.click()
+ await expect(field.getByText('Radius/shared', { exact: true })).toBeVisible()
+ await field.click({ position: { x: 40, y: 13 } })
+ const input = field.getByRole('spinbutton', { name: 'Radius', exact: true })
+ await expect(input).toHaveValue('12')
+ await input.press('Tab')
+ expect(await readCorners()).toEqual(original)
+ await field.click({ position: { x: 40, y: 13 } })
+ await input.fill('18')
+ await input.press('Escape')
+ await expect(field.getByText('Radius/shared', { exact: true })).toBeVisible()
+ expect(await readCorners()).toEqual(original)
+ await field.click({ position: { x: 40, y: 13 } })
+ await input.fill('24')
+ await input.press('Enter')
+ expect(await readCorners()).toEqual({
+ values: [24, 24, 24, 24],
+ bindings: [null, null, null, null],
+ independent: false
+ })
+ await canvas.pressKey('Meta+z')
+ await expect(field.getByText('Radius/shared', { exact: true })).toBeVisible()
+ expect(await readCorners()).toEqual(original)
+ await canvas.pressKey('Meta+Shift+z')
+ expect(await readCorners()).toEqual({
+ values: [24, 24, 24, 24],
+ bindings: [null, null, null, null],
+ independent: false
+ })
+ canvas.assertNoErrors()
+})
diff --git a/tests/e2e/properties/corner-bindings.spec.ts-snapshots/shared-corner-token-openpencil-darwin.png b/tests/e2e/properties/corner-bindings.spec.ts-snapshots/shared-corner-token-openpencil-darwin.png
new file mode 100644
index 000000000..9c090d822
Binary files /dev/null and b/tests/e2e/properties/corner-bindings.spec.ts-snapshots/shared-corner-token-openpencil-darwin.png differ
diff --git a/tests/e2e/properties/panel.spec.ts b/tests/e2e/properties/panel.spec.ts
index 5aed07218..0df47c5f9 100644
--- a/tests/e2e/properties/panel.spec.ts
+++ b/tests/e2e/properties/panel.spec.ts
@@ -440,8 +440,9 @@ test('bound NumberField detach edit is one undo step', async () => {
expect(await readState()).toEqual({ radius: 0, binding: 'Radius/default' })
await field.getByLabel('Apply variable').click()
- await expect(editor.page.getByPlaceholder('Search')).toBeVisible()
- await editor.page.getByPlaceholder('Search').press('Escape')
+ const variableSearch = editor.page.getByRole('combobox', { name: 'Search…', exact: true })
+ await expect(variableSearch).toBeVisible()
+ await variableSearch.press('Escape')
expect(await readState()).toEqual({ radius: 0, binding: 'Radius/default' })
await field.click({ position: { x: 40, y: 13 } })
diff --git a/tests/engine/vue/controls/appearance.test.ts b/tests/engine/vue/controls/appearance.test.ts
index f2d780c48..158fbb4cb 100644
--- a/tests/engine/vue/controls/appearance.test.ts
+++ b/tests/engine/vue/controls/appearance.test.ts
@@ -59,6 +59,56 @@ describe('appearance control state', () => {
expect(appearanceState(node).showIndependentCorners.value).toBe(false)
})
+ test('collapsed bound corners display their actual shared radius', () => {
+ const node = rectangle()
+ node.independentCorners = true
+ node.cornerRadius = 0
+ node.topLeftRadius = node.topRightRadius = node.bottomLeftRadius = node.bottomRightRadius = 12
+ node.boundVariables = {
+ topLeftRadius: 'radius',
+ topRightRadius: 'radius',
+ bottomLeftRadius: 'radius',
+ bottomRightRadius: 'radius'
+ }
+ const state = appearanceState(node)
+ expect(state.cornerRadiusValue.value).toBe(12)
+ expect(state.cornerRadiusBindingPaths.value).toEqual([
+ 'topLeftRadius',
+ 'topRightRadius',
+ 'bottomRightRadius',
+ 'bottomLeftRadius'
+ ])
+ })
+
+ test('collapsed bound corners can be expanded without detaching their bindings', () => {
+ const graph = makeSceneGraph()
+ const rect = graph.createNode('RECTANGLE', firstPageId(graph), {
+ independentCorners: true,
+ boundVariables: {
+ topLeftRadius: 'radius',
+ topRightRadius: 'radius',
+ bottomLeftRadius: 'radius',
+ bottomRightRadius: 'radius'
+ }
+ })
+ const editor = createEditor({ graph })
+ const options = {
+ expandedCornerNodeId: ref<string | null>(null),
+ editor,
+ node: computed(() => graph.getNode(rect.id) ?? null),
+ nodes: computed(() => []),
+ isMulti: computed(() => false),
+ merged: () => MIXED
+ }
+ const actions = createAppearanceActions(options)
+ actions.toggleIndependentCorners()
+ expect(createAppearanceState(options).showIndependentCorners.value).toBe(true)
+ expect(graph.getNode(rect.id)?.boundVariables.topLeftRadius).toBe('radius')
+ expect(editor.undo.canUndo).toBe(false)
+ actions.toggleIndependentCorners()
+ expect(createAppearanceState(options).showIndependentCorners.value).toBe(false)
+ })
+
test('keeps equal independent corners expanded for distinct bindings', () => {
const node = rectangle()
node.independentCorners = true