Skip to content

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

test(editor): harden paste to replace coverage

Danila Poyarkovc322f80893c5
diff --git a/packages/core/src/editor/clipboard.ts b/packages/core/src/editor/clipboard.ts
index 84b18ef4b..d66aee23e 100644
--- a/packages/core/src/editor/clipboard.ts
+++ b/packages/core/src/editor/clipboard.ts
@@ -118,6 +118,13 @@ export function createClipboardActions(ctx: EditorContext) {
return selected.filter((node) => !node.parentId || !selectedSet.has(node.parentId))
}
+ function reorderCreatedAtReplacementIndex(created: string[], deleted: DeletedEntry[]) {
+ const insertParentId = deleted[0]?.parentId
+ if (!insertParentId) return
+ const insertIndex = deleted[0]?.index ?? 0
+ for (let i = 0; i < created.length; i++) ctx.graph.reorderChild(created[i], insertParentId, insertIndex + i)
+ }
+
function pushPasteReplaceUndo(
created: string[],
deleted: DeletedEntry[],
@@ -130,6 +137,7 @@ export function createClipboardActions(ctx: EditorContext) {
forward: () => {
for (const { id } of deleted) ctx.graph.deleteNode(id)
recreateSnapshots(createdSnapshots, pageId)
+ reorderCreatedAtReplacementIndex(created, deleted)
computeAllLayouts(ctx.graph, pageId)
ctx.setSelectedIds(new Set(created))
},
@@ -165,9 +173,7 @@ export function createClipboardActions(ctx: EditorContext) {
targetBounds.x + targetBounds.width / 2,
targetBounds.y + targetBounds.height / 2
)
- const insertParentId = deleted[0]?.parentId ?? ctx.state.currentPageId
- const insertIndex = deleted[0]?.index ?? 0
- for (let i = 0; i < created.length; i++) ctx.graph.reorderChild(created[i], insertParentId, insertIndex + i)
+ reorderCreatedAtReplacementIndex(created, deleted)
for (const { id } of deleted) ctx.graph.deleteNode(id)
computeAllLayouts(ctx.graph, ctx.state.currentPageId)
ctx.setSelectedIds(new Set(created))
diff --git a/tests/engine/editor/clipboard/paste-to-replace.test.ts b/tests/engine/editor/clipboard/paste-to-replace.test.ts
index d485ad17c..8821e76bc 100644
--- a/tests/engine/editor/clipboard/paste-to-replace.test.ts
+++ b/tests/engine/editor/clipboard/paste-to-replace.test.ts
@@ -2,29 +2,57 @@ import { describe, expect, test } from 'bun:test'
import { buildOpenPencilClipboardHTML } from '@open-pencil/core/clipboard'
import { createEditor } from '@open-pencil/core/editor'
+import type { Editor } from '@open-pencil/core/editor'
+
+function copiedRectangleHtml(name = 'Pasted') {
+ const source = createEditor()
+ const sourcePageId = source.state.currentPageId
+ const pasted = source.graph.createNode('RECTANGLE', sourcePageId, {
+ name,
+ x: 0,
+ y: 0,
+ width: 20,
+ height: 20
+ })
+ return buildOpenPencilClipboardHTML([pasted], source.graph)
+}
+
+function copiedFrameHtml() {
+ const source = createEditor()
+ const sourcePageId = source.state.currentPageId
+ const frame = source.graph.createNode('FRAME', sourcePageId, {
+ name: 'Pasted frame',
+ x: 0,
+ y: 0,
+ width: 40,
+ height: 40
+ })
+ source.graph.createNode('RECTANGLE', frame.id, {
+ name: 'Pasted child',
+ x: 5,
+ y: 6,
+ width: 10,
+ height: 12
+ })
+ return buildOpenPencilClipboardHTML([frame], source.graph)
+}
+
+function createTarget(editor: Editor, parentId = editor.state.currentPageId) {
+ return editor.graph.createNode('ELLIPSE', parentId, {
+ name: 'Target',
+ x: 100,
+ y: 100,
+ width: 40,
+ height: 40
+ })
+}
describe('paste to replace', () => {
test('replaces selected nodes with pasted OpenPencil nodes', async () => {
- const source = createEditor()
- const sourcePageId = source.state.currentPageId
- const pasted = source.graph.createNode('RECTANGLE', sourcePageId, {
- name: 'Pasted',
- x: 0,
- y: 0,
- width: 20,
- height: 20
- })
- const html = buildOpenPencilClipboardHTML([pasted], source.graph)
-
+ const html = copiedRectangleHtml()
const editor = createEditor()
const pageId = editor.state.currentPageId
- const target = editor.graph.createNode('ELLIPSE', pageId, {
- name: 'Target',
- x: 100,
- y: 100,
- width: 40,
- height: 40
- })
+ const target = createTarget(editor)
editor.select([target.id])
await editor.pasteFromHTML(html, undefined, { replaceSelection: true })
@@ -39,26 +67,10 @@ describe('paste to replace', () => {
})
test('undo and redo preserve replaced and pasted subtrees', async () => {
- const source = createEditor()
- const sourcePageId = source.state.currentPageId
- const pasted = source.graph.createNode('RECTANGLE', sourcePageId, {
- name: 'Pasted',
- x: 0,
- y: 0,
- width: 20,
- height: 20
- })
- const html = buildOpenPencilClipboardHTML([pasted], source.graph)
-
+ const html = copiedRectangleHtml()
const editor = createEditor()
const pageId = editor.state.currentPageId
- const target = editor.graph.createNode('ELLIPSE', pageId, {
- name: 'Target',
- x: 100,
- y: 100,
- width: 40,
- height: 40
- })
+ const target = createTarget(editor)
editor.select([target.id])
await editor.pasteFromHTML(html, undefined, { replaceSelection: true })
@@ -76,4 +88,99 @@ describe('paste to replace', () => {
expect(editor.graph.getNode(createdId)?.parentId).toBe(pageId)
expect(editor.state.selectedIds).toEqual(new Set([createdId]))
})
+
+ test('falls back to regular paste when all selected targets are locked', async () => {
+ const html = copiedRectangleHtml()
+ const editor = createEditor()
+ const pageId = editor.state.currentPageId
+ const target = editor.graph.createNode('ELLIPSE', pageId, {
+ name: 'Locked target',
+ x: 100,
+ y: 100,
+ width: 40,
+ height: 40,
+ locked: true
+ })
+
+ editor.select([target.id])
+ await editor.pasteFromHTML(html, undefined, { replaceSelection: true })
+
+ expect(editor.graph.getNode(target.id)?.parentId).toBe(pageId)
+ const [createdId] = [...editor.state.selectedIds]
+ expect(createdId).not.toBe(target.id)
+ expect(editor.graph.getNode(createdId)?.parentId).toBe(pageId)
+ })
+
+ test('replaces only top-level selected targets', async () => {
+ const html = copiedRectangleHtml()
+ const editor = createEditor()
+ const pageId = editor.state.currentPageId
+ const parent = editor.graph.createNode('FRAME', pageId, {
+ name: 'Parent target',
+ x: 100,
+ y: 100,
+ width: 80,
+ height: 80
+ })
+ const child = editor.graph.createNode('RECTANGLE', parent.id, {
+ name: 'Child target',
+ x: 10,
+ y: 10,
+ width: 20,
+ height: 20
+ })
+
+ editor.select([parent.id, child.id])
+ await editor.pasteFromHTML(html, undefined, { replaceSelection: true })
+
+ expect(editor.graph.getNode(parent.id)).toBeUndefined()
+ expect(editor.graph.getNode(child.id)).toBeUndefined()
+ const [createdId] = [...editor.state.selectedIds]
+ expect(editor.graph.getNode(createdId)?.parentId).toBe(pageId)
+ })
+
+ test('replaces targets inside a frame and preserves parent order through undo and redo', async () => {
+ const html = copiedRectangleHtml()
+ const editor = createEditor()
+ const pageId = editor.state.currentPageId
+ const frame = editor.graph.createNode('FRAME', pageId, {
+ name: 'Container',
+ x: 50,
+ y: 50,
+ width: 300,
+ height: 300
+ })
+ const before = editor.graph.createNode('RECTANGLE', frame.id, { name: 'Before', x: 0, y: 0 })
+ const target = createTarget(editor, frame.id)
+ const after = editor.graph.createNode('RECTANGLE', frame.id, { name: 'After', x: 200, y: 200 })
+
+ editor.select([target.id])
+ await editor.pasteFromHTML(html, undefined, { replaceSelection: true })
+ const [createdId] = [...editor.state.selectedIds]
+
+ expect(editor.graph.getNode(createdId)?.parentId).toBe(frame.id)
+ expect(editor.graph.getNode(frame.id)?.childIds).toEqual([before.id, createdId, after.id])
+
+ editor.undo.undo()
+ expect(editor.graph.getNode(frame.id)?.childIds).toEqual([before.id, target.id, after.id])
+
+ editor.undo.redo()
+ expect(editor.graph.getNode(frame.id)?.childIds).toEqual([before.id, createdId, after.id])
+ })
+
+ test('preserves pasted child subtrees', async () => {
+ const html = copiedFrameHtml()
+ const editor = createEditor()
+ const target = createTarget(editor)
+
+ editor.select([target.id])
+ await editor.pasteFromHTML(html, undefined, { replaceSelection: true })
+
+ const [createdId] = [...editor.state.selectedIds]
+ const created = editor.graph.getNode(createdId)
+ expect(created?.type).toBe('FRAME')
+ expect(created?.childIds).toHaveLength(1)
+ const childId = created?.childIds[0]
+ expect(childId ? editor.graph.getNode(childId)?.name : undefined).toBe('Pasted child')
+ })
})