tldraw/state/commands/transform-single.ts

51 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-05-28 20:30:27 +00:00
import Command from './command'
import history from '../history'
2021-06-19 17:22:46 +00:00
import { Data } from 'types'
2021-05-28 20:30:27 +00:00
import { TransformSingleSnapshot } from 'state/sessions/transform-single-session'
2021-06-29 12:00:59 +00:00
import tld from 'utils/tld'
2021-06-29 14:54:46 +00:00
import { deepClone } from '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-06-21 21:35:28 +00:00
): void {
2021-06-29 14:54:46 +00:00
const shape = deepClone(tld.getPage(data).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
2021-06-29 12:00:59 +00:00
const { shapes } = tld.getPage(data)
2021-05-19 09:35:00 +00:00
2021-06-29 12:00:59 +00:00
tld.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
2021-06-29 12:00:59 +00:00
tld.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
2021-06-29 12:00:59 +00:00
const { shapes } = tld.getPage(data)
2021-05-19 21:24:41 +00:00
if (isCreating) {
2021-06-29 12:00:59 +00:00
tld.setSelectedIds(data, [])
delete shapes[id]
2021-05-19 21:24:41 +00:00
} else {
2021-06-29 12:00:59 +00:00
const page = tld.getPage(data)
2021-06-05 19:36:46 +00:00
page.shapes[id] = initialShape
2021-06-29 12:00:59 +00:00
tld.updateParents(data, [id])
tld.setSelectedIds(data, [id])
2021-05-19 21:24:41 +00:00
}
2021-05-19 09:35:00 +00:00
},
})
)
}