summaryrefslogtreecommitdiff
path: root/js/undo.js
diff options
context:
space:
mode:
authortimb <opuscule@gmail.com>2015-07-22 00:25:07 -0700
committertimb <opuscule@gmail.com>2015-07-22 00:25:07 -0700
commita76225dc7f8ec707fc2dc563a796c65620b1c5a4 (patch)
tree313932c110e782565224b9ae6be53f39a6325677 /js/undo.js
parent1a009fd877364f5c6616a381d39e33902201a298 (diff)
finally hook up undo
Diffstat (limited to 'js/undo.js')
-rw-r--r--js/undo.js76
1 files changed, 64 insertions, 12 deletions
diff --git a/js/undo.js b/js/undo.js
index 3bdc0a9..cc7f2a5 100644
--- a/js/undo.js
+++ b/js/undo.js
@@ -78,16 +78,44 @@ var save_focused_lex = function(state){
save_lex(x, y, canvas.aa[y][x], state)
}
var save_rect = function(xpos, ypos, w, h, state){
+ if (w === 0 || h === 0) return;
state = state || current_undo;
+ state.rects = state.rects || []
var aa = canvas.aa;
+ var rect = {x: xpos, y: ypos, w: w, h: h, lexs: []}
+ var lexs = rect.lexs
+
var xlen = xpos + w
var ylen = ypos + h
- for (var x = xpos; x < xlen; x++){
- for (var y = ypos; y < ylen; y++){
- save_lex(x, y, aa[y][x], state)
+ for (var y = ypos; y < ylen; y++){
+ var aay = aa[y]
+ for (var x = xpos; x < xlen; x++){
+ lexs.push(new LexState(aay[x]))
+ }
+ }
+ state.rects.push(rect)
+}
+var save_resize = function(w, h, old_w, old_h, state){
+ state = state || current_undo
+ save_size(old_w, old_h, state)
+ if (old_w > w){
+ // .----.
+ // | XX
+ // |___XX
+ save_rect(w, 0, old_w - w, old_h, state)
+ if (old_h > h){
+ // .----.
+ // | |
+ // |XXX_|
+ save_rect(0, h, w, old_h - h, state)
}
+ } else if (old_h > h){
+ // .----.
+ // | |
+ // |XXXX|
+ save_rect(0, h, old_w, old_h - h, state)
}
}
@@ -97,26 +125,46 @@ var restore_state = function(state){
// if it doesn't have one, generate one
var make_redo = ! ('redo' in state || 'undo' in state);
var aa = canvas.aa
- var lexs = state.lexs
+ var lex, lexs; // = state.lexs
if (make_redo){
state.redo = new_redo()
}
+ // copy saved rects that intersect with current canvas size
+ // important to do this before resizing canvas
+ if (make_redo && 'rects' in state){
+ for (var ri=0, rect; rect=state.rects[ri]; ri++){
+ if (rect.x >= canvas.w ||
+ rect.y >= canvas.h) continue;
+ var w = Math.min(rect.w, canvas.w - rect.x)
+ var h = Math.min(rect.h, canvas.h - rect.y)
+ save_rect(rect.x, rect.y, w, h, state.redo)
+ }
+ }
+
if ('size' in state){
if (make_redo){
- save_size(canvas.w, canvas.h, state.redo)
- // note that resizing canvas redo saves the whole canvas instead of
- // just the changed part which is inefficient...
- save_rect(0,0, canvas.w, canvas.h, state.redo)
- make_redo = false // already saved whole canvas, skip lexs below
+ save_resize(state.size.w, state.size.h, canvas.w, canvas.h, state.redo)
+ }
+ canvas.resize(state.size.w, state.size.h, true);
+ }
+
+ if ('rects' in state){
+ for (var ri=0, rect; rect=state.rects[ri]; ri++){
+ lexs = rect.lexs
+ for (var li=0; lex=lexs[li]; li++){
+ var x = (li % rect.w) + rect.x
+ var y = ((li / rect.w)|0) + rect.y
+ aa[y][x].assign(lex)
+ }
}
- canvas.resize(state.size.w, state.size.h);
}
+ lexs = state.lexs
for (var key in lexs){
var xy = key.split(',');
- var lex = aa[xy[1]][xy[0]]
+ lex = aa[xy[1]][xy[0]]
if (make_redo)
save_lex(xy[0], xy[1], lex, state.redo)
lex.assign(lexs[key])
@@ -125,7 +173,10 @@ var restore_state = function(state){
if ('focus' in state){
canvas.focus_x = state.focus.x
canvas.focus_y = state.focus.y
- if (focused_input === canvas) canvas.focus()
+ if (current_canvas === canvas){ // &&)
+ canvas.focus()
+ }
+ // if (focused_input === canvas) canvas.focus()
}
}
@@ -166,6 +217,7 @@ return {
save_lex: save_lex,
save_focused_lex: save_focused_lex,
save_rect: save_rect,
+ save_resize: save_resize,
undo: undo,
redo: redo
}