2021-05-15 13:02:13 +00:00
|
|
|
import Command from "./command"
|
|
|
|
import history from "../history"
|
2021-05-17 10:01:11 +00:00
|
|
|
import { CodeControl, Data, Shape } from "types"
|
2021-05-15 13:02:13 +00:00
|
|
|
import { current } from "immer"
|
|
|
|
|
|
|
|
export default function setGeneratedShapes(
|
|
|
|
data: Data,
|
|
|
|
currentPageId: string,
|
|
|
|
generatedShapes: Shape[]
|
|
|
|
) {
|
2021-05-17 10:01:11 +00:00
|
|
|
const cData = current(data)
|
|
|
|
|
2021-05-15 13:02:13 +00:00
|
|
|
const prevGeneratedShapes = Object.values(
|
2021-05-17 10:01:11 +00:00
|
|
|
cData.document.pages[currentPageId].shapes
|
2021-05-15 13:02:13 +00:00
|
|
|
).filter((shape) => shape.isGenerated)
|
|
|
|
|
2021-05-17 10:01:11 +00:00
|
|
|
const currentShapes = data.document.pages[currentPageId].shapes
|
|
|
|
|
|
|
|
// Remove previous generated shapes
|
|
|
|
for (let id in currentShapes) {
|
|
|
|
if (currentShapes[id].isGenerated) {
|
|
|
|
delete currentShapes[id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add new ones
|
2021-05-15 13:02:13 +00:00
|
|
|
for (let shape of generatedShapes) {
|
2021-05-17 10:01:11 +00:00
|
|
|
currentShapes[shape.id] = shape
|
2021-05-15 13:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
history.execute(
|
|
|
|
data,
|
|
|
|
new Command({
|
|
|
|
name: "translate_shapes",
|
|
|
|
category: "canvas",
|
|
|
|
do(data) {
|
|
|
|
const { shapes } = data.document.pages[currentPageId]
|
|
|
|
|
|
|
|
data.selectedIds.clear()
|
|
|
|
|
|
|
|
// Remove previous generated shapes
|
|
|
|
for (let id in shapes) {
|
|
|
|
if (shapes[id].isGenerated) {
|
|
|
|
delete shapes[id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add new generated shapes
|
|
|
|
for (let shape of generatedShapes) {
|
|
|
|
shapes[shape.id] = shape
|
|
|
|
}
|
|
|
|
},
|
|
|
|
undo(data) {
|
|
|
|
const { shapes } = data.document.pages[currentPageId]
|
|
|
|
|
|
|
|
// Remove generated shapes
|
|
|
|
for (let id in shapes) {
|
|
|
|
if (shapes[id].isGenerated) {
|
|
|
|
delete shapes[id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore previous generated shapes
|
|
|
|
for (let shape of prevGeneratedShapes) {
|
|
|
|
shapes[shape.id] = shape
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|