Fixes deleting undos

This commit is contained in:
Steve Ruiz 2021-06-04 19:49:27 +01:00
parent fcb11f19e3
commit 9626461ea7
4 changed files with 66 additions and 16 deletions

View file

@ -58,6 +58,18 @@ export default function deleteSelected(data: Data) {
page.shapes[shape.id] = shape page.shapes[shape.id] = shape
} }
for (let shape of childrenToDelete) {
if (shape.parentId !== data.currentPageId) {
const parent = page.shapes[shape.parentId]
getShapeUtils(parent)
.setProperty(parent, 'children', [...parent.children, shape.id])
.onChildrenChange(
parent,
parent.children.map((id) => page.shapes[id])
)
}
}
data.selectedIds = new Set(selectedIds) data.selectedIds = new Set(selectedIds)
}, },
}) })

View file

@ -1,9 +1,10 @@
import Command from './command' import Command from './command'
import history from '../history' import history from '../history'
import { TranslateSnapshot } from 'state/sessions/translate-session' import { TranslateSnapshot } from 'state/sessions/translate-session'
import { Data } from 'types' import { Data, GroupShape, Shape, ShapeType } from 'types'
import { getPage, updateParents } from 'utils/utils' import { getPage, updateParents } from 'utils/utils'
import { getShapeUtils } from 'lib/shape-utils' import { getShapeUtils } from 'lib/shape-utils'
import { v4 as uuid } from 'uuid'
export default function translateCommand( export default function translateCommand(
data: Data, data: Data,
@ -33,6 +34,9 @@ export default function translateCommand(
...parent.children, ...parent.children,
clone.id, clone.id,
]) ])
if (clone.type === ShapeType.Group) {
}
} }
} }
} }

View file

@ -1,4 +1,4 @@
import { Data, GroupShape, ShapeType } from 'types' import { Data, GroupShape, Shape, ShapeType } from 'types'
import * as vec from 'utils/vec' import * as vec from 'utils/vec'
import BaseSession from './base-session' import BaseSession from './base-session'
import commands from 'state/commands' import commands from 'state/commands'
@ -54,13 +54,12 @@ export default class TranslateSession extends BaseSession {
for (const clone of clones) { for (const clone of clones) {
data.selectedIds.add(clone.id) data.selectedIds.add(clone.id)
shapes[clone.id] = { ...clone } shapes[clone.id] = { ...clone }
if (clone.parentId !== data.currentPageId) { const parent = shapes[clone.parentId]
const parent = shapes[clone.parentId] if (!parent) continue
getShapeUtils(parent).setProperty(parent, 'children', [ getShapeUtils(parent).setProperty(parent, 'children', [
...parent.children, ...parent.children,
clone.id, clone.id,
]) ])
}
} }
} }
@ -164,6 +163,7 @@ export function getTranslateSnapshot(data: Data) {
const selectedShapes = getSelectedShapes(cData).filter( const selectedShapes = getSelectedShapes(cData).filter(
(shape) => !shape.isLocked (shape) => !shape.isLocked
) )
const hasUnlockedShapes = selectedShapes.length > 0 const hasUnlockedShapes = selectedShapes.length > 0
const parents = Array.from( const parents = Array.from(
@ -181,13 +181,47 @@ export function getTranslateSnapshot(data: Data) {
point, point,
parentId, parentId,
})), })),
clones: selectedShapes.map((shape) => ({ clones: selectedShapes
...shape, .filter((shape) => shape.type !== ShapeType.Group)
id: uuid(), .flatMap((shape) => {
parentId: shape.parentId, const clone = {
childIndex: getChildIndexAbove(cData, shape.id), ...shape,
})), id: uuid(),
parentId: shape.parentId,
childIndex: getChildIndexAbove(cData, shape.id),
}
return clone
// cloneGroup(cData, {
// ...shape,
// id: uuid(),
// parentId: shape.parentId,
// childIndex: getChildIndexAbove(cData, shape.id),
// })
}),
} }
} }
export type TranslateSnapshot = ReturnType<typeof getTranslateSnapshot> export type TranslateSnapshot = ReturnType<typeof getTranslateSnapshot>
function cloneGroup(data: Data, clone: Shape): Shape[] {
if (clone.type !== ShapeType.Group) {
return [clone]
}
const page = getPage(data)
const childClones = clone.children.flatMap((id) => {
const newId = uuid()
const source = page.shapes[id]
const next = { ...source, id: newId, parentId: clone.id }
if (next.type === ShapeType.Group) {
return [next, ...cloneGroup(data, next)]
}
return [next]
})
return [clone, ...childClones]
}

View file

@ -1385,7 +1385,7 @@ const state = createState({
}, },
restoreSavedData(data) { restoreSavedData(data) {
history.load(data) // history.load(data)
}, },
clearBoundsRotation(data) { clearBoundsRotation(data) {