Update command ids, add undo/redo stack

This commit is contained in:
Steve Ruiz 2021-09-06 13:14:43 +01:00
parent 2362baefe7
commit e6eb277f49
17 changed files with 96 additions and 14 deletions

View file

@ -3,3 +3,66 @@
exports[` 1`] = `"[]"`; exports[` 1`] = `"[]"`;
exports[` 2`] = `undefined`; exports[` 2`] = `undefined`;
exports[`TLDrawState Exposes undo/redo stack: history 1`] = `
Array [
Object {
"after": Object {
"document": Object {
"pageStates": Object {
"page1": Object {
"selectedIds": Array [
"rect1",
],
},
},
"pages": Object {
"page1": Object {
"shapes": Object {
"rect1": Object {
"childIndex": 1,
"id": "rect1",
"name": "Rectangle",
"parentId": "page1",
"point": Array [
0,
0,
],
"rotation": 0,
"size": Array [
100,
200,
],
"style": Object {
"color": "Black",
"dash": "Draw",
"isFilled": false,
"size": "Medium",
},
"type": "rectangle",
},
},
},
},
},
},
"before": Object {
"document": Object {
"pageStates": Object {
"page1": Object {
"selectedIds": Array [],
},
},
"pages": Object {
"page1": Object {
"shapes": Object {
"rect1": undefined,
},
},
},
},
},
"id": "create",
},
]
`;

View file

@ -50,7 +50,7 @@ export function align(data: Data, ids: string[], type: AlignType): TLDrawCommand
) )
return { return {
id: 'align_shapes', id: 'align',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -14,7 +14,7 @@ export function create(data: Data, shapes: TLDrawShape[]): TLDrawCommand {
}) })
return { return {
id: 'toggle_shapes', id: 'create',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -12,7 +12,7 @@ export function deleteShapes(
const { before, after } = removeShapesFromPage(data, ids, pageId) const { before, after } = removeShapesFromPage(data, ids, pageId)
return { return {
id: 'delete_shapes', id: 'delete',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -18,7 +18,7 @@ export function distribute(data: Data, ids: string[], type: DistributeType): TLD
) )
return { return {
id: 'distribute_shapes', id: 'distribute',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -58,7 +58,7 @@ export function flip(data: Data, ids: string[], type: FlipType): TLDrawCommand {
) )
return { return {
id: 'flip_shapes', id: 'flip',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -188,7 +188,7 @@ export function group(
}) })
return { return {
id: 'group_shapes', id: 'group',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -217,7 +217,7 @@ export function move(data: Data, ids: string[], type: MoveType): TLDrawCommand {
}) })
return { return {
id: 'move_shapes', id: 'move',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -44,7 +44,7 @@ export function rotate(data: Data, ids: string[], delta = -PI2 / 4): TLDrawComma
) )
return { return {
id: 'toggle_shapes', id: 'rotate',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -57,7 +57,7 @@ export function stretch(data: Data, ids: string[], type: StretchType): TLDrawCom
) )
return { return {
id: 'stretch_shapes', id: 'stretch',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -14,7 +14,7 @@ export function style(data: Data, ids: string[], changes: Partial<ShapeStyles>):
) )
return { return {
id: 'style_shapes', id: 'style',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -16,7 +16,7 @@ export function toggle(data: Data, ids: string[], prop: keyof TLDrawShape): TLDr
) )
return { return {
id: 'toggle_shapes', id: 'toggle',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -63,7 +63,7 @@ export function translate(data: Data, ids: string[], delta: number[]): TLDrawCom
}) })
return { return {
id: 'translate_shapes', id: 'translate',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -99,7 +99,7 @@ export function ungroup(data: Data, groupId: string, pageId: string): TLDrawComm
}) })
return { return {
id: 'ungroup_shapes', id: 'ungroup',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -28,7 +28,7 @@ export function update(
after.shapes = change.after after.shapes = change.after
return { return {
id: 'translate_shapes', id: 'update',
before: { before: {
document: { document: {
pages: { pages: {

View file

@ -359,4 +359,16 @@ describe('TLDrawState', () => {
expect(tlstate.getShape('rect5').childIndex).toBe(3) expect(tlstate.getShape('rect5').childIndex).toBe(3)
}) })
}) })
it('Exposes undo/redo stack', () => {
const tlstate = new TLDrawState().loadDocument(mockDocument).createShapes({
id: 'rect1',
type: TLDrawShapeType.Rectangle,
point: [0, 0],
size: [100, 200],
})
expect(tlstate.history).toBeDefined()
expect(tlstate.history).toMatchSnapshot('history')
})
}) })

View file

@ -646,6 +646,13 @@ export class TLDrawState extends StateManager<Data> {
return Vec.sub(Vec.div(point, camera.zoom), camera.point) return Vec.sub(Vec.div(point, camera.zoom), camera.point)
} }
/**
* Get the current undo/redo stack.
*/
get history() {
return this.stack
}
/** /**
* The current document. * The current document.
*/ */