about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--drawing.lua7
-rw-r--r--main.lua8
-rw-r--r--undo.lua6
3 files changed, 10 insertions, 11 deletions
diff --git a/drawing.lua b/drawing.lua
index acec83b..25fde99 100644
--- a/drawing.lua
+++ b/drawing.lua
@@ -206,7 +206,6 @@ function Drawing.in_drawing(drawing, x,y)
 end
 
 function Drawing.mouse_pressed(drawing, x,y, button)
-  Drawing.before = snapshot()
   if Current_drawing_mode == 'freehand' then
     drawing.pending = {mode=Current_drawing_mode, points={{x=Drawing.coord(x-16), y=Drawing.coord(y-drawing.y)}}}
   elseif Current_drawing_mode == 'line' or Current_drawing_mode == 'manhattan' then
@@ -226,7 +225,6 @@ function Drawing.mouse_pressed(drawing, x,y, button)
     print(Current_drawing_mode)
     assert(false)
   end
-  Lines.current_drawing = drawing
 end
 
 -- a couple of operations on drawings need to constantly check the state of the mouse
@@ -348,11 +346,6 @@ function Drawing.mouse_released(x,y, button)
       Lines.current_drawing = nil
     end
   end
-  save_to_disk(Lines, Filename)
-  if Drawing.before then
-    record_undo_event({before=Drawing.before, after=snapshot()})
-    Drawing.before = nil
-  end
 end
 
 function Drawing.keychord_pressed(chord)
diff --git a/main.lua b/main.lua
index 53751bb..ddde651 100644
--- a/main.lua
+++ b/main.lua
@@ -290,6 +290,9 @@ function App.mousepressed(x,y, mouse_button)
       end
     elseif line.mode == 'drawing' then
       if Drawing.in_drawing(line, x, y) then
+        Lines.current_drawing_index = line_index
+        Lines.current_drawing = line
+        Drawing.before = snapshot(line_index)
         Drawing.mouse_pressed(line, x,y, button)
       end
     end
@@ -300,6 +303,11 @@ function App.mousereleased(x,y, button)
   if Search_term then return end
   if Lines.current_drawing then
     Drawing.mouse_released(x,y, button)
+    save_to_disk(Lines, Filename)
+    if Drawing.before then
+      record_undo_event({before=Drawing.before, after=snapshot(Lines.current_drawing_index)})
+      Drawing.before = nil
+    end
   else
     for line_index,line in ipairs(Lines) do
       if line.mode == 'text' then
diff --git a/undo.lua b/undo.lua
index 3007070..3504577 100644
--- a/undo.lua
+++ b/undo.lua
@@ -36,10 +36,8 @@ end
 -- Make copies of objects; the rest of the app may mutate them in place, but undo requires immutable histories.
 function snapshot(s,e)
   -- Snapshot everything by default, but subset if requested.
-  if s == nil and e == nil then
-    s = 1
-    e = #Lines
-  elseif e == nil then
+  assert(s)
+  if e == nil then
     e = s
   end
   assert(#Lines > 0)