2021-06-21 21:35:28 +00:00
|
|
|
import { Data, Shape } from 'types'
|
2021-06-15 11:58:51 +00:00
|
|
|
import BaseSession from './base-session'
|
|
|
|
import commands from 'state/commands'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { getShapeUtils } from 'state/shape-utils'
|
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-06-15 11:58:51 +00:00
|
|
|
|
|
|
|
export default class EditSession extends BaseSession {
|
|
|
|
snapshot: EditSnapshot
|
|
|
|
|
|
|
|
constructor(data: Data) {
|
|
|
|
super(data)
|
|
|
|
this.snapshot = getEditSnapshot(data)
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
update(data: Data, change: Partial<Shape>): void {
|
2021-06-15 11:58:51 +00:00
|
|
|
const initialShape = this.snapshot.initialShape
|
2021-06-29 12:00:59 +00:00
|
|
|
const shape = tld.getShape(data, initialShape.id)
|
2021-06-15 11:58:51 +00:00
|
|
|
const utils = getShapeUtils(shape)
|
2021-07-02 10:48:49 +00:00
|
|
|
|
2021-06-15 11:58:51 +00:00
|
|
|
Object.entries(change).forEach(([key, value]) => {
|
|
|
|
utils.setProperty(shape, key as keyof Shape, value as Shape[keyof Shape])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
cancel(data: Data): void {
|
2021-06-15 11:58:51 +00:00
|
|
|
const initialShape = this.snapshot.initialShape
|
2021-06-29 12:00:59 +00:00
|
|
|
const page = tld.getPage(data)
|
2021-06-15 11:58:51 +00:00
|
|
|
page.shapes[initialShape.id] = initialShape
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
complete(data: Data): void {
|
2021-06-15 11:58:51 +00:00
|
|
|
commands.edit(data, this.snapshot, getEditSnapshot(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-06-15 11:58:51 +00:00
|
|
|
export function getEditSnapshot(data: Data) {
|
2021-06-29 14:54:46 +00:00
|
|
|
const initialShape = deepClone(tld.getSelectedShapes(data)[0])
|
2021-06-15 11:58:51 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
currentPageId: data.currentPageId,
|
|
|
|
initialShape,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type EditSnapshot = ReturnType<typeof getEditSnapshot>
|