Skip to content

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

Fix layers panel reactivity and accidental reparenting on click

Danila Poyarkovfd0b02642f6b
diff --git a/src/App.vue b/src/App.vue
index 9683e65c7..512ab8e22 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -15,6 +15,8 @@ const store = provideEditorStore()
useKeyboard(store)
useMenu(store)
+;(window as any).__OPEN_PENCIL_STORE__ = store
+
useEventListener(document, 'wheel', (e: WheelEvent) => {
if (e.ctrlKey || e.metaKey) e.preventDefault()
}, { passive: false })
diff --git a/src/components/LayersPanel.vue b/src/components/LayersPanel.vue
index 6e5b8235b..928a369b0 100644
--- a/src/components/LayersPanel.vue
+++ b/src/components/LayersPanel.vue
@@ -1,5 +1,5 @@
<script setup lang="ts">
-import { ref, computed } from 'vue'
+import { ref, computed, watch } from 'vue'
import { useEventListener } from '@vueuse/core'
import { TreeRoot, TreeItem } from 'reka-ui'
@@ -36,25 +36,28 @@ const nodeIcons: Record<string, typeof IconSquare> = {
function buildTree(parentId: string): LayerNode[] {
const parent = store.graph.getNode(parentId)
if (!parent) return []
- const result: LayerNode[] = []
- for (const cid of parent.childIds) {
- const node = store.graph.getNode(cid)
- if (!node) continue
- result.push({
+ return parent.childIds
+ .map((cid) => store.graph.getNode(cid))
+ .filter((n): n is NonNullable<typeof n> => !!n)
+ .map((node) => ({
id: node.id,
name: node.name,
type: node.type,
visible: node.visible,
children: node.childIds.length > 0 ? buildTree(node.id) : undefined,
- })
- }
- return result
+ }))
}
-const items = computed(() => {
- void store.state.renderVersion
- return buildTree(store.graph.rootId)
-})
+const items = ref(buildTree(store.graph.rootId))
+const treeKey = ref(0)
+
+watch(
+ () => store.state.renderVersion,
+ () => {
+ items.value = buildTree(store.graph.rootId)
+ treeKey.value++
+ }
+)
const expanded = ref<string[]>([])
@@ -201,6 +204,7 @@ function updateDropTarget(ev: PointerEvent) {
<header class="shrink-0 px-3 py-2 text-[11px] uppercase tracking-wider text-muted">Layers</header>
<div ref="listRef" class="relative flex-1 overflow-y-auto px-1">
<TreeRoot
+ :key="treeKey"
v-slot="{ flattenItems }"
:items="items"
:get-key="(v: LayerNode) => v.id"
diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.ts
index 978783dc9..9b182377c 100644
--- a/src/composables/use-canvas-input.ts
+++ b/src/composables/use-canvas-input.ts
@@ -726,14 +726,22 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
}
store.setDropTarget(null)
} else {
- store.commitMove(d.originals)
+ // Check if the user actually dragged
+ const moved = [...d.originals].some(([id, orig]) => {
+ const node = store.graph.getNode(id)
+ return node && (node.x !== orig.x || node.y !== orig.y)
+ })
+
+ if (moved) {
+ store.commitMove(d.originals)
- // Reparent into frame if dropped on one
- const dropId = store.state.dropTargetId
- if (dropId) {
- store.reparentNodes([...store.state.selectedIds], dropId)
- store.setDropTarget(null)
+ // Reparent into frame if dropped on one
+ const dropId = store.state.dropTargetId
+ if (dropId) {
+ store.reparentNodes([...store.state.selectedIds], dropId)
+ }
}
+ store.setDropTarget(null)
}
}
diff --git a/tests/e2e/layers-panel.spec.ts b/tests/e2e/layers-panel.spec.ts
new file mode 100644
index 000000000..b1da3459b
--- /dev/null
+++ b/tests/e2e/layers-panel.spec.ts
@@ -0,0 +1,167 @@
+import { test, expect, type Page } from '@playwright/test'
+
+import { CanvasHelper } from '../helpers/canvas'
+
+let page: Page
+let canvas: CanvasHelper
+
+test.describe.configure({ mode: 'serial' })
+
+test.beforeAll(async ({ browser }) => {
+ page = await browser.newPage()
+ await page.goto('/')
+ canvas = new CanvasHelper(page)
+ await canvas.waitForInit()
+})
+
+test.afterAll(async () => {
+ await page.close()
+})
+
+function layerRows() {
+ return page.locator('[data-node-id]')
+}
+
+async function getLayerNames(): Promise<string[]> {
+ const rows = layerRows()
+ const count = await rows.count()
+ const names: string[] = []
+ for (let i = 0; i < count; i++) {
+ const text = await rows.nth(i).innerText()
+ names.push(text.trim())
+ }
+ return names
+}
+
+async function getSceneTree() {
+ return page.evaluate(() => {
+ const store = (window as any).__OPEN_PENCIL_STORE__
+ if (!store) return null
+ const graph = store.graph
+
+ function nodeTree(id: string): any {
+ const node = graph.getNode(id)
+ if (!node) return null
+ return {
+ name: node.name,
+ type: node.type,
+ children: node.childIds.map((cid: string) => nodeTree(cid)).filter(Boolean),
+ }
+ }
+ return nodeTree(graph.rootId)
+ })
+}
+
+async function getSelectedCount(): Promise<number> {
+ return page.evaluate(() => {
+ const store = (window as any).__OPEN_PENCIL_STORE__
+ return store.state.selectedIds.size
+ })
+}
+
+test('demo layers visible in panel', async () => {
+ const names = await getLayerNames()
+ expect(names).toEqual(
+ expect.arrayContaining(['Desktop', 'Blue card', 'Green circle', 'Orange rect', 'Purple pill'])
+ )
+ expect(names).toHaveLength(5)
+})
+
+test('clicking a node inside a frame does not reparent it', async () => {
+ // Blue card is visually inside Desktop but is a root sibling
+ // Clicking it should NOT reparent it under Desktop
+ const beforeTree = await getSceneTree()
+ const rootChildNames = beforeTree.children.map((c: any) => c.name)
+ expect(rootChildNames).toContain('Blue card')
+
+ // Click on Blue card (at ~270, 220 in canvas coords — center of 150,140 + 240,160)
+ await canvas.click(270, 220)
+ await canvas.waitForRender()
+
+ // Blue card should still be a root child, not reparented under Desktop
+ const afterTree = await getSceneTree()
+ const afterRootChildNames = afterTree.children.map((c: any) => c.name)
+ expect(afterRootChildNames).toContain('Blue card')
+
+ // Desktop should still have no children
+ const desktop = afterTree.children.find((c: any) => c.name === 'Desktop')
+ expect(desktop.children).toHaveLength(0)
+
+ canvas.assertNoErrors()
+})
+
+test('creating a shape updates layers', async () => {
+ await canvas.drawRect(600, 500, 50, 50)
+ const names = await getLayerNames()
+ expect(names).toContain('Rectangle')
+ expect(names).toHaveLength(6)
+
+ await canvas.undo()
+ const after = await getLayerNames()
+ expect(after).toHaveLength(5)
+ expect(after).not.toContain('Rectangle')
+})
+
+test('Shift+A wraps selection in auto-layout frame', async () => {
+ await canvas.click(400, 300)
+ await canvas.selectAll()
+ expect(await getSelectedCount()).toBe(5)
+
+ // Snapshot layer names before
+ const before = await getLayerNames()
+
+ await page.keyboard.press('Shift+A')
+ await canvas.waitForRender()
+
+ // Scene graph has the frame
+ const tree = await getSceneTree()
+ expect(tree.children).toHaveLength(1)
+ expect(tree.children[0].name).toBe('Frame')
+ expect(tree.children[0].type).toBe('FRAME')
+ expect(tree.children[0].children.length).toBe(5)
+
+ // Layers panel MUST have changed — old names should be gone
+ const after = await getLayerNames()
+ expect(after).not.toEqual(before)
+ expect(after).toContain('Frame')
+
+ // Old root-level items should NOT be visible (they're children of collapsed Frame)
+ expect(after).not.toContain('Desktop')
+
+ canvas.assertNoErrors()
+})
+
+test('grouping updates layers', async () => {
+ // Undo the auto-layout to restore flat structure
+ await canvas.undo()
+ await canvas.waitForRender()
+
+ await canvas.click(400, 300)
+ await canvas.selectAll()
+ expect(await getSelectedCount()).toBe(5)
+
+ await page.keyboard.press('Meta+g')
+ await canvas.waitForRender()
+
+ const tree = await getSceneTree()
+ expect(tree.children).toHaveLength(1)
+ expect(tree.children[0].name).toBe('Group')
+ expect(tree.children[0].type).toBe('GROUP')
+
+ const names = await getLayerNames()
+ expect(names).toContain('Group')
+
+ canvas.assertNoErrors()
+})
+
+test('ungrouping updates layers', async () => {
+ await page.keyboard.press('Shift+Meta+g')
+ await canvas.waitForRender()
+
+ const names = await getLayerNames()
+ expect(names).not.toContain('Group')
+ expect(names).toHaveLength(5)
+ expect(names).toContain('Desktop')
+
+ canvas.assertNoErrors()
+})