AI-native design editor. Open-source Figma alternative.
feat: show each vector's own shape as its layer icon, as in Figma (#1187)
rcoenen9d787d124a6e
diff --git a/changes/1187/added.md b/changes/1187/added.mdnew file mode 100644index 000000000..c1a440e5a--- /dev/null+++ b/changes/1187/added.md@@ -0,0 +1 @@+- Each vector's row in the layers panel shows the vector's own shape as its icon, as in Figma.diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.tsindex 1dbe5bcfd..d6142a968 100644--- a/packages/vue/src/index.ts+++ b/packages/vue/src/index.ts@@ -253,7 +253,8 @@ export type {LayerRow,LayerSelectionMode,LayerTreeContext,- LayerTreeVirtualizer+ LayerTreeVirtualizer,+ LayerVectorIcon} from '#vue/primitives/LayerTree'export { LayoutControlsRoot, useLayoutControlsContext } from '#vue/primitives/LayoutControls'export type {diff --git a/packages/vue/src/primitives/LayerTree/LayerTreeRoot.vue b/packages/vue/src/primitives/LayerTree/LayerTreeRoot.vueindex 5ca402c67..a5b061247 100644--- a/packages/vue/src/primitives/LayerTree/LayerTreeRoot.vue+++ b/packages/vue/src/primitives/LayerTree/LayerTreeRoot.vue@@ -84,7 +84,14 @@ const PATCHABLE_NODE_KEYS = new Set<keyof SceneNode>(['layoutMode','visible','locked',- 'variableModes'+ 'variableModes',+ // A vector's row icon is drawn from its network in its box, turned and reflected.+ 'vectorNetwork',+ 'width',+ 'height',+ 'rotation',+ 'flipX',+ 'flipY'])function patchTreeNode(id: string, changes: Partial<SceneNode>) {diff --git a/packages/vue/src/primitives/LayerTree/context.ts b/packages/vue/src/primitives/LayerTree/context.tsindex a5c8fb512..77d68ba0b 100644--- a/packages/vue/src/primitives/LayerTree/context.ts+++ b/packages/vue/src/primitives/LayerTree/context.ts@@ -1,8 +1,19 @@import { type ComputedRef, type InjectionKey, type Ref, inject, provide } from 'vue'import type { Editor } from '@open-pencil/core/editor'+import type { VectorNetwork } from '@open-pencil/scene-graph'import type { ShapeWithTextType } from '@open-pencil/scene-graph/shape-with-text'+/** What a vector's row draws as its icon: its network in its box, turned and reflected. */+export interface LayerVectorIcon {+ network: VectorNetwork+ width: number+ height: number+ rotation: number+ flipX: boolean+ flipY: boolean+}+export interface LayerNode {id: stringname: string@@ -14,6 +25,8 @@ export interface LayerNode {icon?: boolean/** A shape with text's kind, which its row draws. */shapeWithTextType?: ShapeWithTextType+ /** A vector's own shape, which its row draws as its icon. */+ vector?: LayerVectorIconvisible: booleanlocked: boolean/** The variable modes set on the layer itself, which Figma tags its row with. */diff --git a/packages/vue/src/primitives/LayerTree/index.ts b/packages/vue/src/primitives/LayerTree/index.tsindex 901af85f8..f21da333b 100644--- a/packages/vue/src/primitives/LayerTree/index.ts+++ b/packages/vue/src/primitives/LayerTree/index.ts@@ -7,7 +7,8 @@ export type {LayerRow,LayerSelectionMode,LayerTreeContext,- LayerTreeVirtualizer+ LayerTreeVirtualizer,+ LayerVectorIcon} from '#vue/primitives/LayerTree/context'export {buildLayerTreeModel,diff --git a/packages/vue/src/primitives/LayerTree/model.ts b/packages/vue/src/primitives/LayerTree/model.tsindex bad2f0d5f..c1d3cfd02 100644--- a/packages/vue/src/primitives/LayerTree/model.ts+++ b/packages/vue/src/primitives/LayerTree/model.ts@@ -1,10 +1,16 @@import { isEqual } from 'es-toolkit'+import { toRaw } from 'vue'import { isGeneratedFrame, readIcon, slotPropertyId } from '@open-pencil/scene-graph'import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'import type { ShapeWithTextType } from '@open-pencil/scene-graph/shape-with-text'-import type { LayerNode, LayerRow, LayerSelectionMode } from '#vue/primitives/LayerTree/context'+import type {+ LayerNode,+ LayerRow,+ LayerSelectionMode,+ LayerVectorIcon+} from '#vue/primitives/LayerTree/context'function explicitModeIds(node: SceneNode): string[] | undefined {const ids = Object.values(node.variableModes)@@ -15,6 +21,26 @@ function shapeWithTextTypeOf(node: SceneNode): ShapeWithTextType | undefined {return node.type === 'SHAPE_WITH_TEXT' ? node.shapeWithTextType : undefined}+/** A vector's row draws its own shape in its box, turned and reflected, as Figma's does. */+function vectorIconOf(node: SceneNode): LayerVectorIcon | undefined {+ if (node.type !== 'VECTOR' || !node.vectorNetwork) return undefined+ const { vectorNetwork: network, width, height, rotation, flipX, flipY } = node+ return { network, width, height, rotation, flipX, flipY }+}++function sameVectorIcon(target: LayerVectorIcon | undefined, source: LayerVectorIcon | undefined) {+ if (!target || !source) return target === source+ // The target may be a reactive row, which hands back a proxy of the network it holds.+ return (+ toRaw(target.network) === source.network &&+ target.width === source.width &&+ target.height === source.height &&+ target.rotation === source.rotation &&+ target.flipX === source.flipX &&+ target.flipY === source.flipY+ )+}+function nodeToLayerNode(node: SceneNode): LayerNode {return {id: node.id,@@ -24,6 +50,7 @@ function nodeToLayerNode(node: SceneNode): LayerNode {slot: !!slotPropertyId(node),icon: !!readIcon(node),shapeWithTextType: shapeWithTextTypeOf(node),+ vector: vectorIconOf(node),visible: node.visible,locked: node.locked,modeIds: explicitModeIds(node)@@ -72,6 +99,7 @@ export function indexLayerNodes(items: readonly LayerNode[]): Map<string, LayerN}export function patchLayerNode(target: LayerNode, source: SceneNode): boolean {+ const vector = vectorIconOf(source)const changed =target.name !== source.name ||target.type !== source.type ||@@ -79,6 +107,7 @@ export function patchLayerNode(target: LayerNode, source: SceneNode): boolean {target.slot !== !!slotPropertyId(source) ||target.icon !== !!readIcon(source) ||target.shapeWithTextType !== shapeWithTextTypeOf(source) ||+ !sameVectorIcon(target.vector, vector) ||target.visible !== source.visible ||target.locked !== source.locked ||!isEqual(target.modeIds, explicitModeIds(source))@@ -89,6 +118,7 @@ export function patchLayerNode(target: LayerNode, source: SceneNode): boolean {target.slot = !!slotPropertyId(source)target.icon = !!readIcon(source)target.shapeWithTextType = shapeWithTextTypeOf(source)+ target.vector = vectortarget.visible = source.visibletarget.locked = source.lockedtarget.modeIds = explicitModeIds(source)diff --git a/packages/vue/tests/primitives/LayerTree/model.test.ts b/packages/vue/tests/primitives/LayerTree/model.test.tsindex cf5d9a552..844cb54d4 100644--- a/packages/vue/tests/primitives/LayerTree/model.test.ts+++ b/packages/vue/tests/primitives/LayerTree/model.test.ts@@ -1,5 +1,7 @@import { describe, expect, test } from 'bun:test'+import { reactive } from 'vue'+import { createShapeWithText, SceneGraph, withIcon } from '@open-pencil/scene-graph'import { buildLayerTreeModel, patchLayerNode } from '@open-pencil/vue'@@ -54,6 +56,37 @@ describe('layer tree model', () => {expect(patchLayerNode(row, shape)).toBe(true)expect(row.shapeWithTextType).toBe('ELLIPSE')})++ test("carries a vector's network and placement, so its row draws the shape", () => {+ const graph = new SceneGraph()+ const pageId = graph.getPages()[0].id+ const line = (x: number) => ({+ vertices: [+ { x: 0, y: 0 },+ { x, y: 10 }+ ],+ segments: [{ start: 0, end: 1, tangentStart: { x: 0, y: 0 }, tangentEnd: { x: 0, y: 0 } }],+ regions: []+ })+ const vector = graph.createNode('VECTOR', pageId, { vectorNetwork: line(10) })+ const rect = graph.createNode('RECTANGLE', pageId)++ const model = buildLayerTreeModel(graph, pageId)+ const row = model.byId.get(vector.id)+ if (!row) throw new Error('Expected the vector in the model')+ expect(row.vector?.network).toBe(vector.vectorNetwork ?? undefined)+ expect(model.byId.get(rect.id)?.vector).toBeUndefined()++ // The layer tree patches its rows through their reactive proxies.+ expect(patchLayerNode(reactive(row), vector)).toBe(false)+ graph.updateNode(vector.id, { vectorNetwork: line(20) })+ expect(patchLayerNode(reactive(row), vector)).toBe(true)+ expect(row.vector?.network).toBe(vector.vectorNetwork ?? undefined)++ graph.updateNode(vector.id, { rotation: 30, flipX: true })+ expect(patchLayerNode(reactive(row), vector)).toBe(true)+ expect(row.vector).toMatchObject({ rotation: 30, flipX: true })+ })})describe('drop index', () => {diff --git a/src/components/LayerTree/examples/States.vue b/src/components/LayerTree/examples/States.vueindex 331a31c75..d12ba9189 100644--- a/src/components/LayerTree/examples/States.vue+++ b/src/components/LayerTree/examples/States.vue@@ -1,5 +1,6 @@<script setup lang="ts">-import type { LayerNode } from '@open-pencil/vue'+import type { VectorNetwork } from '@open-pencil/scene-graph'+import type { LayerNode, LayerVectorIcon } from '@open-pencil/vue'import type { LayerIssueMark } from '@/app/editor/design-check/layers'import LayerTreeNodeRow from '@/components/LayerTree/LayerTreeNodeRow.vue'@@ -46,6 +47,34 @@ function node(id: string, overrides: Partial<LayerNode> = {}): LayerNode {}}+const STRAIGHT = { tangentStart: { x: 0, y: 0 }, tangentEnd: { x: 0, y: 0 } }++/** A closed triangle and an open curve, which vector rows draw as their icons. */+const triangleNetwork: VectorNetwork = {+ vertices: [+ { x: 0, y: 100 },+ { x: 50, y: 0 },+ { x: 100, y: 100 }+ ],+ segments: [+ { start: 0, end: 1, ...STRAIGHT },+ { start: 1, end: 2, ...STRAIGHT },+ { start: 2, end: 0, ...STRAIGHT }+ ],+ regions: [{ windingRule: 'NONZERO', loops: [[0, 1, 2]] }]+}+const curveNetwork: VectorNetwork = {+ vertices: [+ { x: 0, y: 75 },+ { x: 100, y: 75 }+ ],+ segments: [{ start: 0, end: 1, tangentStart: { x: 0, y: -100 }, tangentEnd: { x: 0, y: -100 } }],+ regions: []+}+const placed = { rotation: 0, flipX: false, flipY: false }+const triangle: LayerVectorIcon = { network: triangleNetwork, width: 100, height: 100, ...placed }+const curve: LayerVectorIcon = { network: curveNetwork, width: 100, height: 75, ...placed }+function chrome(overrides: Partial<LayerTreeChrome> = {}): LayerTreeChrome {return {draggingId: null,@@ -119,6 +148,30 @@ const states: Array<{selected: false,chrome: chrome()},+ {+ label: 'Vector',+ node: node('Vector', { type: 'VECTOR', vector: triangle }),+ selected: false,+ chrome: chrome()+ },+ {+ label: 'Selected vector',+ node: node('Selected vector', { type: 'VECTOR', vector: curve }),+ selected: true,+ chrome: chrome({ focused: true })+ },+ {+ label: 'Hidden vector',+ node: node('Hidden vector', { type: 'VECTOR', vector: triangle, visible: false }),+ selected: false,+ chrome: chrome()+ },+ {+ label: 'Turned vector',+ node: node('Turned vector', { type: 'VECTOR', vector: { ...triangle, rotation: -45 } }),+ selected: false,+ chrome: chrome()+ },{label: 'Dragging',node: node('Dragging'),diff --git a/src/components/icons/index.ts b/src/components/icons/index.tsindex cb840cfed..6fbea1921 100644--- a/src/components/icons/index.ts+++ b/src/components/icons/index.ts@@ -26,9 +26,10 @@ import IconType from '~icons/lucide/type'import { readIcon, slotPropertyId, type SceneNode } from '@open-pencil/scene-graph'import type { ShapeWithTextType } from '@open-pencil/scene-graph/shape-with-text'-import type { Tool } from '@open-pencil/vue'+import type { LayerVectorIcon, Tool } from '@open-pencil/vue'import { SHAPE_KIND_ICONS } from './shapes'+import { vectorIcon } from './vectors'export const toolIcons: Record<Tool, Component> = {SELECT: IconMousePointer,@@ -76,8 +77,8 @@ export const COMPONENT_TYPES = new Set(['COMPONENT', 'COMPONENT_SET', 'INSTANCE'export { IconFrame, IconSquare }/**- * The glyph for a layer: its kind, or a slot or icon frame. A component keeps its own glyph, and- * a shape with text shows its outline.+ * The glyph for a layer: its kind, or a slot or icon frame. A component keeps its own glyph, a+ * shape with text shows its outline, and a layer row's vector shows its own shape.*/export function nodeIcon(node: {type: string@@ -85,11 +86,16 @@ export function nodeIcon(node: {slot?: booleanicon?: booleanshapeWithTextType?: ShapeWithTextType+ vector?: LayerVectorIcon}) {if (node.slot) return IconSlotif (node.type === 'SHAPE_WITH_TEXT' && node.shapeWithTextType) {return SHAPE_KIND_ICONS[node.shapeWithTextType]}+ if (node.vector) {+ const icon = vectorIcon(node.vector)+ if (icon) return icon+ }if (node.icon && node.type === 'FRAME') return IconSmileif (node.type === 'FRAME' && node.layoutMode !== 'NONE')return AUTO_LAYOUT_ICONS[node.layoutMode] ?? IconFramediff --git a/src/components/icons/vectors.ts b/src/components/icons/vectors.tsnew file mode 100644index 000000000..a8a6d8ea6--- /dev/null+++ b/src/components/icons/vectors.ts@@ -0,0 +1,74 @@+import { toRaw, type FunctionalComponent } from 'vue'++import { vectorNetworkToSVGPaths } from '@open-pencil/core/io'+import {+ transformVectorNetwork,+ validateVectorNetwork,+ type VectorNetwork+} from '@open-pencil/scene-graph'+import { getNodeLocalMatrix } from '@open-pencil/scene-graph/coordinate'+import Matrix from '@open-pencil/scene-graph/matrix'+import type { LayerVectorIcon } from '@open-pencil/vue'++import { maskIcon } from './mask'++/** The layer row's icon slot (`size-3` in the layer tree theme), so the outline draws at 1:1. */+const SLOT = 12+/** Figma draws a vector's row icon 9 px across, inside a 10 px square, stroked 1 px. */+const SHAPE = 9+const STROKE = 1+/** Figma shows its generic vector glyph once vertices and segments together reach this many. */+const MAX_ELEMENTS = 129++/**+ * A vector's outline as icon markup, as Figma draws its layer row (`generateObjectsPanelIcon`):+ * turned and reflected as the layer is, with the layer's box fitted to the icon and centred, and+ * never filled. Null for a network with nothing to draw, too much to draw, or broken indices.+ */+export function vectorIconSVG(vector: LayerVectorIcon): string | null {+ const { network, width, height } = vector+ const { vertices, segments } = network+ if (segments.length === 0 || vertices.length + segments.length >= MAX_ELEMENTS) return null+ // A network whose segments or region loops name missing parts keeps the generic glyph.+ if (validateVectorNetwork(network).length > 0) return null++ // Figma fits the layer's turned box, not the outline, so a turned shape keeps its size.+ const placement = getNodeLocalMatrix({ ...vector, type: 'VECTOR', x: 0, y: 0 })+ const corners = Matrix.mapPoints(placement, [0, 0, width, 0, width, height, 0, height])+ const xs = corners.filter((_, index) => index % 2 === 0)+ const ys = corners.filter((_, index) => index % 2 === 1)+ const minX = Math.min(...xs)+ const minY = Math.min(...ys)+ const boxWidth = Math.max(...xs) - minX+ const boxHeight = Math.max(...ys) - minY+ const extent = Math.max(boxWidth, boxHeight)+ if (!(extent > 0)) return null++ const scale = SHAPE / extent+ const fit = Matrix.multiply(+ Matrix.translated(+ (SLOT - boxWidth * scale) / 2 - minX * scale,+ (SLOT - boxHeight * scale) / 2 - minY * scale+ ),+ Matrix.scaled(scale, scale),+ placement+ )+ const d = vectorNetworkToSVGPaths(transformVectorNetwork(fit, network)).join('')+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${SLOT} ${SLOT}"><path d="${d}" fill="none" stroke="black" stroke-width="${STROKE}"/></svg>`+}++/** Each network's latest icon only, so a rotate or resize drag does not keep every frame's. */+const icons = new WeakMap<VectorNetwork, { key: string; icon: FunctionalComponent | null }>()++/** A vector's own outline as an icon component, built once per network and placement. */+export function vectorIcon(vector: LayerVectorIcon): FunctionalComponent | null {+ const network = toRaw(vector.network)+ const { width, height, rotation, flipX, flipY } = vector+ const key = `${width} ${height} ${rotation} ${flipX} ${flipY}`+ const cached = icons.get(network)+ if (cached?.key === key) return cached.icon+ const svg = vectorIconSVG({ ...vector, network })+ const icon = svg ? maskIcon(svg) : null+ icons.set(network, { key, icon })+ return icon+}