Skip to content

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

Auto-layout icons in layer tree, frame title draggable on canvas

Danila Poyarkov5549a2f46fa9
diff --git a/packages/core/src/renderer/renderer.ts b/packages/core/src/renderer/renderer.ts
index 5396ee3ab..03f340a21 100644
--- a/packages/core/src/renderer/renderer.ts
+++ b/packages/core/src/renderer/renderer.ts
@@ -8,6 +8,7 @@ import {
PARENT_OUTLINE_DASH,
DEFAULT_FONT_SIZE,
LABEL_FONT_SIZE,
+ LABEL_OFFSET_Y,
SIZE_FONT_SIZE,
SECTION_TITLE_FONT_SIZE,
SECTION_TITLE_HEIGHT,
@@ -553,6 +554,47 @@ export class SkiaRenderer {
return result
}
+ hitTestFrameTitle(
+ graph: SceneGraph,
+ canvasX: number,
+ canvasY: number,
+ selectedIds: Set<string>
+ ): SceneNode | null {
+ if (!this.labelFont || selectedIds.size !== 1) return null
+
+ const id = [...selectedIds][0]
+ const node = graph.getNode(id)
+ if (!node || node.type !== 'FRAME') return null
+
+ const parent = node.parentId ? graph.getNode(node.parentId) : null
+ const isTopLevel = !parent || parent.type === 'CANVAS' || parent.type === 'SECTION'
+ if (!isTopLevel) return null
+
+ const abs = graph.getAbsolutePosition(id)
+ const font = this.labelFont
+ const glyphIds = font.getGlyphIDs(node.name)
+ const widths = font.getGlyphWidths(glyphIds)
+ let textW = 0
+ for (const w of widths) textW += w
+
+ const labelW = textW / this.zoom
+ const labelH = LABEL_FONT_SIZE / this.zoom
+ const gap = LABEL_OFFSET_Y / this.zoom
+ const labelX = abs.x
+ const labelY = abs.y - gap - labelH
+
+ if (
+ canvasX >= labelX &&
+ canvasX <= labelX + labelW &&
+ canvasY >= labelY &&
+ canvasY <= labelY + labelH
+ ) {
+ return node
+ }
+
+ return null
+ }
+
renderSceneToCanvas(canvas: Canvas, graph: SceneGraph, pageId: string): void {
const prevViewport = this.worldViewport
this.worldViewport = { x: -1e9, y: -1e9, w: 2e9, h: 2e9 }
diff --git a/src/components/EditorCanvas.vue b/src/components/EditorCanvas.vue
index 6b9d9ae7a..e763c1c6a 100644
--- a/src/components/EditorCanvas.vue
+++ b/src/components/EditorCanvas.vue
@@ -12,12 +12,13 @@ const store = useEditorStore()
const collab = useCollabInjected()
const canvasRef = ref<HTMLCanvasElement | null>(null)
-const { hitTestSectionTitle, hitTestComponentLabel } = useCanvas(canvasRef, store)
+const { hitTestSectionTitle, hitTestComponentLabel, hitTestFrameTitle } = useCanvas(canvasRef, store)
const { cursorOverride } = useCanvasInput(
canvasRef,
store,
hitTestSectionTitle,
hitTestComponentLabel,
+ hitTestFrameTitle,
(cx, cy) => collab?.updateCursor(cx, cy, store.state.currentPageId)
)
diff --git a/src/components/LayerTree.vue b/src/components/LayerTree.vue
index c7b31d5d2..28650fabc 100644
--- a/src/components/LayerTree.vue
+++ b/src/components/LayerTree.vue
@@ -8,11 +8,14 @@ import { useInlineRename } from '@/composables/use-inline-rename'
import IconCircle from '~icons/lucide/circle'
import IconComponent from '~icons/lucide/diamond'
import IconComponentSet from '~icons/lucide/component'
+import IconColumns from '~icons/lucide/columns-3'
import IconFrame from '~icons/lucide/frame'
+import IconGrid from '~icons/lucide/grid-3x3'
import IconGroup from '~icons/lucide/group'
import IconMinus from '~icons/lucide/minus'
import IconPenTool from '~icons/lucide/pen-tool'
+import IconRows from '~icons/lucide/rows-3'
import IconSection from '~icons/lucide/layout-grid'
import IconSquare from '~icons/lucide/square'
import IconType from '~icons/lucide/type'
@@ -27,11 +30,12 @@ interface LayerNode {
id: string
name: string
type: string
+ layoutMode: string
visible: boolean
children?: LayerNode[]
}
-const nodeIcons: Record<string, typeof IconSquare> = {
+const nodeIcons: Partial<Record<string, typeof IconSquare>> = {
SECTION: IconSection,
ELLIPSE: IconCircle,
FRAME: IconFrame,
@@ -45,6 +49,19 @@ const nodeIcons: Record<string, typeof IconSquare> = {
RECTANGLE: IconSquare
}
+const autoLayoutIcons: Partial<Record<string, typeof IconSquare>> = {
+ VERTICAL: IconRows,
+ HORIZONTAL: IconColumns,
+ GRID: IconGrid
+}
+
+function nodeIcon(node: LayerNode) {
+ if (node.type === 'FRAME' && node.layoutMode !== 'NONE') {
+ return autoLayoutIcons[node.layoutMode] ?? IconFrame
+ }
+ return nodeIcons[node.type] ?? IconSquare
+}
+
const COMPONENT_TYPES = new Set(['COMPONENT', 'COMPONENT_SET', 'INSTANCE'])
function buildTree(parentId: string): LayerNode[] {
@@ -57,6 +74,7 @@ function buildTree(parentId: string): LayerNode[] {
id: node.id,
name: node.name,
type: node.type,
+ layoutMode: node.layoutMode,
visible: node.visible,
children: node.childIds.length > 0 ? buildTree(node.id) : undefined
}))
@@ -331,7 +349,7 @@ function updateDropTarget(ev: PointerEvent) {
</span>
<span v-else class="w-4 shrink-0" />
<component
- :is="nodeIcons[item.value.type] ?? IconSquare"
+ :is="nodeIcon(item.value)"
class="size-3 shrink-0 opacity-70"
/>
<input
@@ -374,7 +392,7 @@ function updateDropTarget(ev: PointerEvent) {
</span>
<span v-else class="w-4 shrink-0" />
<component
- :is="nodeIcons[item.value.type] ?? IconSquare"
+ :is="nodeIcon(item.value)"
class="size-3 shrink-0"
:class="
COMPONENT_TYPES.has(item.value.type)
diff --git a/src/components/Toolbar.vue b/src/components/Toolbar.vue
index 4ca3c54ec..87bf52148 100644
--- a/src/components/Toolbar.vue
+++ b/src/components/Toolbar.vue
@@ -26,11 +26,11 @@ import IconLock from '~icons/lucide/lock'
import { menuContent, menuItem } from '@/components/ui/menu'
import { ACTION_TOAST_DURATION } from '@/constants'
-import { useEditorStore } from '@/stores/editor'
+import { TOOLS, useEditorStore } from '@/stores/editor'
import { toolIcons } from '@/utils/tools'
import type { Component } from 'vue'
-import type { Tool , TOOLS} from '@/stores/editor'
+import type { Tool, ToolDef } from '@/stores/editor'
const store = useEditorStore()
const breakpoints = useBreakpoints({ mobile: 768 })
@@ -64,12 +64,12 @@ const toolShortcuts: Record<Tool, string> = {
HAND: 'H'
}
-function isActive(tool: (typeof TOOLS)[number]): boolean {
+function isActive(tool: ToolDef): boolean {
if (tool.key === store.state.activeTool) return true
return tool.flyout?.includes(store.state.activeTool) ?? false
}
-function activeKeyForTool(tool: (typeof TOOLS)[number]): Tool {
+function activeKeyForTool(tool: ToolDef): Tool {
if (tool.flyout?.includes(store.state.activeTool)) return store.state.activeTool
return tool.key
}
diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.ts
index 129daabc8..b564698ff 100644
--- a/src/composables/use-canvas-input.ts
+++ b/src/composables/use-canvas-input.ts
@@ -225,6 +225,7 @@ export function useCanvasInput(
store: EditorStore,
hitTestSectionTitle: (cx: number, cy: number) => SceneNode | null,
hitTestComponentLabel: (cx: number, cy: number) => SceneNode | null,
+ hitTestFrameTitle: (cx: number, cy: number) => SceneNode | null,
onCursorMove?: (cx: number, cy: number) => void
) {
const drag = ref<DragState | null>(null)
@@ -352,7 +353,7 @@ export function useCanvasInput(
return false
}
- function handleAltDragDuplicate(cx: number, cy: number): Map<string, Vector> {
+ function duplicateAndDrag(cx: number, cy: number): Map<string, Vector> {
const newIds: string[] = []
const newOriginals = new Map<string, Vector>()
for (const id of store.state.selectedIds) {
@@ -404,6 +405,7 @@ export function useCanvasInput(
if (tryStartResize(sx, sy, cx, cy)) return
const hit =
+ hitTestFrameTitle(cx, cy) ??
hitTestSectionTitle(cx, cy) ??
hitTestComponentLabel(cx, cy) ??
store.graph.hitTest(cx, cy, store.state.currentPageId)
@@ -427,7 +429,7 @@ export function useCanvasInput(
}
if (e.altKey && store.state.selectedIds.size > 0) {
- handleAltDragDuplicate(cx, cy)
+ duplicateAndDrag(cx, cy)
return
}
diff --git a/src/composables/use-canvas.ts b/src/composables/use-canvas.ts
index c6f3cb706..bbc54f9bf 100644
--- a/src/composables/use-canvas.ts
+++ b/src/composables/use-canvas.ts
@@ -230,11 +230,18 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: Edito
return renderer?.hitTestComponentLabel(store.graph, canvasX, canvasY) ?? null
}
+ function hitTestFrameTitle(canvasX: number, canvasY: number) {
+ return (
+ renderer?.hitTestFrameTitle(store.graph, canvasX, canvasY, store.state.selectedIds) ?? null
+ )
+ }
+
return {
render: () => {
dirty = true
},
hitTestSectionTitle,
- hitTestComponentLabel
+ hitTestComponentLabel,
+ hitTestFrameTitle
}
}