Skip to content

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

fix(vue): restore layer tree reordering

Danila Poyarkovc38778094d36
diff --git a/AGENTS.md b/AGENTS.md
index 795d67e16..9aa18dc85 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -236,6 +236,8 @@ Release commits are the exception: keep using `Release v0.x.y`.
## Code conventions
+- Do not place code or tests ad hoc. Before adding or moving files, inspect the existing folder structure and nearby patterns, then put changes in the established domain-specific location. If no proper location exists, create one deliberately and update docs/conventions as needed.
+
### File and folder naming
OpenPencil follows a Reka UI-inspired component namespace structure:
diff --git a/packages/vue/src/primitives/LayerTree/LayerTreeItem.vue b/packages/vue/src/primitives/LayerTree/LayerTreeItem.vue
index c408a574d..58af65a29 100644
--- a/packages/vue/src/primitives/LayerTree/LayerTreeItem.vue
+++ b/packages/vue/src/primitives/LayerTree/LayerTreeItem.vue
@@ -22,7 +22,7 @@ const emit = defineEmits<{
const ctx = useLayerTree()
const isSelected = computed(() => ctx.selectedIds.value.has(node.id))
-const isDragging = computed(() => false)
+const isDragging = computed(() => ctx.draggingId.value === node.id)
const padLeft = computed(() => `${8 + (level - 1) * ctx.indentPerLevel}px`)
const rowEl = ref<HTMLElement | null>(null)
@@ -33,6 +33,13 @@ function onRef(el: unknown) {
ctx.setRowRef(node.id, htmlEl)
}
+ctx.setupDrag(rowEl, () => ({
+ id: node.id,
+ level,
+ hasChildren,
+ parentId: null
+}))
+
const actions = {
select: (additive: boolean) => {
emit('select', node.id, additive)
diff --git a/packages/vue/src/primitives/LayerTree/LayerTreeRoot.vue b/packages/vue/src/primitives/LayerTree/LayerTreeRoot.vue
index 43791392e..1d23fb8cd 100644
--- a/packages/vue/src/primitives/LayerTree/LayerTreeRoot.vue
+++ b/packages/vue/src/primitives/LayerTree/LayerTreeRoot.vue
@@ -1,8 +1,10 @@
<script setup lang="ts">
import { computed, nextTick, ref, watch } from 'vue'
+import { TreeRoot } from 'reka-ui'
import { useEditor } from '#vue/editor/context'
import { provideLayerTree } from '#vue/primitives/LayerTree/context'
+import { useLayerDrag } from '#vue/primitives/LayerTree/useLayerDrag'
import type { LayerNode } from '#vue/primitives/LayerTree/context'
@@ -19,6 +21,10 @@ const emit = defineEmits<{
}>()
const editor = useEditor()
+const { draggingId, instruction, instructionTargetId, setupItem } = useLayerDrag(
+ editor,
+ indentPerLevel
+)
function buildTree(parentId: string): LayerNode[] {
const parent = editor.graph.getNode(parentId)
@@ -105,6 +111,14 @@ function toggleExpand(id: string) {
else expanded.value = [...expanded.value, id]
}
+function getKey(node: LayerNode) {
+ return node.id
+}
+
+function getChildren(node: LayerNode) {
+ return node.children
+}
+
const actions = {
select,
toggleExpand
@@ -117,6 +131,10 @@ provideLayerTree({
treeKey,
selectedIds,
indentPerLevel,
+ draggingId,
+ instruction,
+ instructionTargetId,
+ setupDrag: setupItem,
select,
toggleExpand,
toggleVisibility: (id: string) => {
@@ -136,13 +154,24 @@ provideLayerTree({
</script>
<template>
- <slot
- :items="items"
+ <TreeRoot
+ :key="treeKey"
+ v-slot="{ flattenItems }"
:expanded="expanded"
- :tree-key="treeKey"
- :selected-ids="selectedIds"
- :actions="actions"
- :get-key="(v: LayerNode) => v.id"
- :get-children="(v: LayerNode) => v.children"
- />
+ :items="items"
+ :get-key="getKey"
+ :get-children="getChildren"
+ >
+ <slot
+ :items="items"
+ :flatten-items="flattenItems"
+ :expanded="expanded"
+ :tree-key="treeKey"
+ :selected-ids="selectedIds"
+ :dragging-id="draggingId"
+ :instruction="instruction"
+ :instruction-target-id="instructionTargetId"
+ :actions="actions"
+ />
+ </TreeRoot>
</template>
diff --git a/packages/vue/src/primitives/LayerTree/context.ts b/packages/vue/src/primitives/LayerTree/context.ts
index 6054d0cd4..cf4b91abf 100644
--- a/packages/vue/src/primitives/LayerTree/context.ts
+++ b/packages/vue/src/primitives/LayerTree/context.ts
@@ -12,6 +12,10 @@ export interface LayerNode {
children?: LayerNode[]
}
+export interface LayerDragInstruction {
+ type: 'reorder-above' | 'reorder-below' | 'make-child'
+}
+
export interface LayerTreeContext {
editor: Editor
items: Ref<LayerNode[]>
@@ -19,6 +23,13 @@ export interface LayerTreeContext {
treeKey: Ref<number>
selectedIds: ComputedRef<Set<string>>
indentPerLevel: number
+ draggingId: Ref<string | null>
+ instruction: Ref<LayerDragInstruction | null>
+ instructionTargetId: Ref<string | null>
+ setupDrag: (
+ el: Ref<HTMLElement | null>,
+ item: () => { id: string; level: number; hasChildren: boolean; parentId: string | null }
+ ) => void
select: (id: string, additive: boolean) => void
toggleExpand: (id: string) => void
toggleVisibility: (id: string) => void
diff --git a/packages/vue/src/primitives/LayerTree/useLayerDrag.ts b/packages/vue/src/primitives/LayerTree/useLayerDrag.ts
index 2156ccf99..acbb9be44 100644
--- a/packages/vue/src/primitives/LayerTree/useLayerDrag.ts
+++ b/packages/vue/src/primitives/LayerTree/useLayerDrag.ts
@@ -13,6 +13,8 @@ import { onScopeDispose, ref, watchEffect, type Ref } from 'vue'
import type { Editor } from '@open-pencil/core/editor'
+import type { LayerDragInstruction } from '#vue/primitives/LayerTree/context'
+
interface DragItem {
id: string
level: number
@@ -20,7 +22,7 @@ interface DragItem {
parentId: string | null
}
-type TreeInstruction = { type: 'reorder-above' | 'reorder-below' | 'make-child' }
+type TreeInstruction = LayerDragInstruction
export function useLayerDrag(editor: Editor, indentPerLevel = 16) {
const draggingId = ref<string | null>(null)
@@ -58,7 +60,7 @@ export function useLayerDrag(editor: Editor, indentPerLevel = 16) {
indentPerLevel,
currentLevel: data.level,
mode,
- block: []
+ block: ['make-child', 'reparent']
}
),
canDrop: ({ source }) => source.data.id !== data.id,
diff --git a/src/components/LayerTree.vue b/src/components/LayerTree.vue
index 051e82816..d4204105f 100644
--- a/src/components/LayerTree.vue
+++ b/src/components/LayerTree.vue
@@ -1,14 +1,13 @@
<script setup lang="ts">
import { useAttrs, watch } from 'vue'
import { templateRef } from '@vueuse/core'
-import { TreeRoot, TreeItem, ContextMenuRoot, ContextMenuTrigger, ContextMenuPortal } from 'reka-ui'
+import { TreeItem, ContextMenuRoot, ContextMenuTrigger, ContextMenuPortal } from 'reka-ui'
import {
LayerTreeRoot,
LayerTreeItem,
useI18n,
- useInlineRename,
- useLayerDrag
+ useInlineRename
} from '@open-pencil/vue'
import { useEditorStore } from '@/app/editor/active-store'
import { nodeIcon, COMPONENT_TYPES } from '@/app/editor/icons'
@@ -23,7 +22,6 @@ const store = useEditorStore()
const renameInput = templateRef<HTMLInputElement>('renameInput')
const rename = useInlineRename((id, name) => store.renameNode(id, name))
const { menu: t } = useI18n()
-const { draggingId, instruction, instructionTargetId } = useLayerDrag(store, INDENT)
watch(renameInput, (input) => {
if (input) void rename.focusInput(input)
@@ -48,20 +46,13 @@ function onTreeSelect(e: CustomEvent, select: (additive: boolean) => void) {
<template>
<LayerTreeRoot
- v-slot="{ items, expanded, treeKey, getKey, getChildren }"
+ v-slot="{ flattenItems, draggingId, instruction, instructionTargetId }"
:indent-per-level="INDENT"
>
<ContextMenuRoot :modal="false">
<ContextMenuTrigger as-child @contextmenu="onLayerRightClick">
<div v-bind="attrs" class="relative scrollbar-thin flex-1 overflow-y-auto px-1">
- <TreeRoot
- :key="treeKey"
- v-slot="{ flattenItems }"
- :expanded="expanded"
- :items="items"
- :get-key="getKey"
- :get-children="getChildren"
- >
+ <template v-if="flattenItems">
<LayerTreeItem
v-for="item in flattenItems"
:key="item._id"
@@ -210,7 +201,7 @@ function onTreeSelect(e: CustomEvent, select: (additive: boolean) => void) {
</button>
</TreeItem>
</LayerTreeItem>
- </TreeRoot>
+ </template>
</div>
</ContextMenuTrigger>
<ContextMenuPortal>
diff --git a/tests/e2e/layers/reorder.spec.ts b/tests/e2e/layers/reorder.spec.ts
new file mode 100644
index 000000000..8ef0eaaaa
--- /dev/null
+++ b/tests/e2e/layers/reorder.spec.ts
@@ -0,0 +1,37 @@
+import { expect, test } from '@playwright/test'
+
+import { CanvasHelper } from '#tests/helpers/canvas'
+
+test('dragging layers reorders scene nodes', async ({ page }) => {
+ await page.goto('/')
+ const canvas = new CanvasHelper(page)
+ await canvas.waitForInit()
+ canvas.errors.length = 0
+ await canvas.clearCanvas()
+
+ const ids = await page.evaluate(() => {
+ const store = window.openPencil?.getStore?.()
+ if (!store) throw new Error('OpenPencil store not initialized')
+ const pageId = store.state.currentPageId
+ const first = store.graph.createNode('RECTANGLE', pageId, { name: 'Layer A', x: 0, y: 0 })
+ const second = store.graph.createNode('RECTANGLE', pageId, { name: 'Layer B', x: 120, y: 0 })
+ const third = store.graph.createNode('RECTANGLE', pageId, { name: 'Layer C', x: 240, y: 0 })
+ store.requestRender()
+ return { first: first.id, second: second.id, third: third.id }
+ })
+ await canvas.waitForRender()
+
+ const source = page.locator(`[data-node-id="${ids.third}"] [data-test-id="layers-item"]`)
+ const target = page.locator(`[data-node-id="${ids.first}"] [data-test-id="layers-item"]`)
+ await source.dragTo(target, { targetPosition: { x: 20, y: 2 } })
+ await canvas.waitForRender()
+
+ const order = await page.evaluate(() => {
+ const store = window.openPencil?.getStore?.()
+ if (!store) throw new Error('OpenPencil store not initialized')
+ return store.graph.getNode(store.state.currentPageId)?.childIds ?? []
+ })
+
+ expect(order).toEqual([ids.third, ids.first, ids.second])
+ canvas.assertNoErrors()
+})