tldraw/state/commands/transform-single.ts

57 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-05-28 20:30:27 +00:00
import Command from './command'
import history from '../history'
import { Data, Corner, Edge } from 'types'
import { getShapeUtils } from 'lib/shape-utils'
import { current } from 'immer'
import { TransformSingleSnapshot } from 'state/sessions/transform-single-session'
import {
getPage,
getSelectedIds,
setSelectedIds,
updateParents,
} from 'utils/utils'
2021-05-19 09:35:00 +00:00
export default function transformSingleCommand(
data: Data,
before: TransformSingleSnapshot,
after: TransformSingleSnapshot,
2021-05-19 21:24:41 +00:00
isCreating: boolean
2021-05-19 09:35:00 +00:00
) {
2021-05-28 20:30:27 +00:00
const shape = current(getPage(data, after.currentPageId).shapes[after.id])
2021-05-19 21:24:41 +00:00
2021-05-19 09:35:00 +00:00
history.execute(
data,
new Command({
2021-05-28 20:30:27 +00:00
name: 'transform_single_shape',
category: 'canvas',
2021-05-19 21:24:41 +00:00
manualSelection: true,
2021-05-19 09:35:00 +00:00
do(data) {
2021-06-03 12:06:39 +00:00
const { id } = after
const { shapes } = getPage(data, after.currentPageId)
2021-05-19 09:35:00 +00:00
setSelectedIds(data, [id])
2021-05-19 09:35:00 +00:00
2021-06-03 12:06:39 +00:00
shapes[id] = shape
2021-06-04 16:08:43 +00:00
updateParents(data, [id])
2021-05-19 09:35:00 +00:00
},
undo(data) {
2021-06-05 19:36:46 +00:00
const { id, initialShape } = before
const { shapes } = getPage(data, before.currentPageId)
2021-05-19 21:24:41 +00:00
if (isCreating) {
setSelectedIds(data, [])
delete shapes[id]
2021-05-19 21:24:41 +00:00
} else {
2021-06-05 19:36:46 +00:00
const page = getPage(data)
page.shapes[id] = initialShape
2021-06-04 16:08:43 +00:00
updateParents(data, [id])
setSelectedIds(data, [id])
2021-05-19 21:24:41 +00:00
}
2021-05-19 09:35:00 +00:00
},
})
)
}