Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit b89ed52

Browse files
Add an action for moving a shape; add the execution of that action to selected shape
1 parent 53a7334 commit b89ed52

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/core/actions.coffee

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ class ClearAction
1313
@lc.repaintLayer('main')
1414

1515

16+
class MoveAction
17+
18+
constructor: (@lc, @selectedShape, @previousPosition, @newPosition) ->
19+
20+
do: ->
21+
@selectedShape.setUpperLeft {
22+
x: @newPosition.x,
23+
y: @newPosition.y
24+
}
25+
@lc.repaintLayer('main')
26+
27+
undo: ->
28+
@selectedShape.setUpperLeft {
29+
x: @previousPosition.x,
30+
y: @previousPosition.y
31+
}
32+
@lc.repaintLayer('main')
33+
34+
1635
class AddShapeAction
1736

1837
constructor: (@lc, @shape, @previousShapeId=null) ->
@@ -51,4 +70,4 @@ class AddShapeAction
5170
@lc.repaintLayer('main')
5271

5372

54-
module.exports = {ClearAction, AddShapeAction}
73+
module.exports = {ClearAction, MoveAction, AddShapeAction}

src/tools/SelectShape.coffee

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
actions = require '../core/actions'
12
{Tool} = require './base'
23
{createShape} = require '../core/shapes'
34

@@ -38,6 +39,10 @@ module.exports = class SelectShape extends Tool
3839
x: x - br.x,
3940
y: y - br.y
4041
}
42+
@initialPosition = {
43+
x: br.x,
44+
y: br.y
45+
}
4146

4247
onDrag = ({ x, y }) =>
4348
if @selectedShape?
@@ -56,6 +61,18 @@ module.exports = class SelectShape extends Tool
5661
onUp = ({ x, y }) =>
5762
if @didDrag
5863
@didDrag = false
64+
65+
# get the current position
66+
br = @selectedShape.getBoundingRect()
67+
68+
newPosition = {
69+
x: br.x,
70+
y: br.y
71+
}
72+
73+
# and add a move action
74+
lc.execute(new actions.MoveAction(lc, @selectedShape, @initialPosition, newPosition))
75+
5976
lc.trigger('shapeMoved', { shape: @selectedShape })
6077
lc.trigger('drawingChange', {})
6178
lc.repaintLayer('main')

0 commit comments

Comments
 (0)