AI-native design editor. Open-source Figma alternative.
refactor(editor): share the undo entry for created subtrees (#1006)
Danila Poyarkov762c3445eed6
diff --git a/packages/core/src/editor/icons.ts b/packages/core/src/editor/icons.tsindex 1b901c74e..ea5caa68e 100644--- a/packages/core/src/editor/icons.ts+++ b/packages/core/src/editor/icons.ts@@ -4,6 +4,7 @@ import type { Color } from '@open-pencil/scene-graph/primitives'import { restoreSubtree, snapshotSubtree } from '#core/editor/clipboard/subtree-history'import type { EditorContext } from '#core/editor/types'+import { pushCreatedSubtreesUndo } from '#core/editor/undo'import { detachIcon, placeIcon, recolorIcon, swapIcon } from '#core/icons/render'import type { IconData } from '#core/icons/types'@@ -65,21 +66,7 @@ export function createIconActions(ctx: EditorContext) {color,overrides: { x: Math.round(x), y: Math.round(y) }})- const created = snapshotSubtree(ctx.graph, frame.id)- ctx.undo.push({- label: 'Insert icon',- forward: () => {- const root = created.get(frame.id)- if (root && !ctx.graph.getNode(frame.id)) restoreSubtree(ctx.graph, root, parentId, created)- ctx.setSelectedIds(new Set([frame.id]))- ctx.requestRender()- },- inverse: () => {- if (ctx.graph.getNode(frame.id)) ctx.graph.deleteNode(frame.id)- ctx.setSelectedIds(previousSelection)- ctx.requestRender()- }- })+ pushCreatedSubtreesUndo(ctx, 'Insert icon', [frame.id], previousSelection)ctx.setSelectedIds(new Set([frame.id]))ctx.requestRender()return frame.iddiff --git a/packages/core/src/editor/undo.ts b/packages/core/src/editor/undo.tsindex 82ef88659..e83b8b6f2 100644--- a/packages/core/src/editor/undo.ts+++ b/packages/core/src/editor/undo.ts@@ -31,6 +31,42 @@ type ResizeOriginal = Rect &>>+/**+ * Records root subtrees that were just created: redo re-creates any that are missing and selects+ * them, undo deletes them and restores the previous selection.+ */+export function pushCreatedSubtreesUndo(+ ctx: EditorContext,+ label: string,+ rootIds: string[],+ previousSelection: Set<string>+) {+ const snapshots = new Map<string, SceneNode>()+ for (const id of rootIds) {+ const subtree = snapshotSubtree(ctx.graph, id)+ for (const [nodeId, snapshot] of subtree) snapshots.set(nodeId, snapshot)+ }+ const nextSelection = new Set(rootIds)++ ctx.undo.push({+ label,+ forward: () => {+ for (const id of rootIds) {+ if (ctx.graph.getNode(id)) continue+ const snapshot = snapshots.get(id)+ if (!snapshot) continue+ restoreSubtree(ctx.graph, snapshot, snapshot.parentId ?? ctx.state.currentPageId, snapshots)+ ctx.runLayoutForNode(id)+ }+ ctx.setSelectedIds(new Set(nextSelection))+ },+ inverse: () => {+ for (const id of rootIds.toReversed()) ctx.graph.deleteNode(id)+ ctx.setSelectedIds(new Set(previousSelection))+ }+ })+}+export function createUndoActions(ctx: EditorContext) {function commitMove(originals: Map<string, Vector>) {for (const id of originals.keys()) assertNodeEditable(ctx.graph, id)@@ -66,35 +102,7 @@ export function createUndoActions(ctx: EditorContext) {}function commitDuplicateMove(rootIds: string[], previousSelection: Set<string>) {- const snapshots = new Map<string, SceneNode>()- for (const id of rootIds) {- const subtree = snapshotSubtree(ctx.graph, id)- for (const [nodeId, snapshot] of subtree) snapshots.set(nodeId, snapshot)- }- const nextSelection = new Set(rootIds)-- ctx.undo.push({- label: 'Duplicate',- forward: () => {- for (const id of rootIds) {- if (ctx.graph.getNode(id)) continue- const snapshot = snapshots.get(id)- if (!snapshot) continue- restoreSubtree(- ctx.graph,- snapshot,- snapshot.parentId ?? ctx.state.currentPageId,- snapshots- )- ctx.runLayoutForNode(id)- }- ctx.setSelectedIds(new Set(nextSelection))- },- inverse: () => {- for (const id of rootIds.toReversed()) ctx.graph.deleteNode(id)- ctx.setSelectedIds(new Set(previousSelection))- }- })+ pushCreatedSubtreesUndo(ctx, 'Duplicate', rootIds, previousSelection)}function commitResize(nodeId: string, original: ResizeOriginal) {