Undo and redo!

master
Dustin Swan 21 hours ago
parent 63f599b850
commit b0726bc0fb
Signed by: dustinswan
GPG Key ID: 30D46587E2100467

@ -12,6 +12,33 @@ textEditor = name \
{ state = state.{ lines = newLines, cursorCol = state.cursorCol + 1 }, emit = [] }; { state = state.{ lines = newLines, cursorCol = state.cursorCol + 1 }, emit = [] };
insertMode = state \
stateSnapshot = { lines = state.lines, cursorRow = state.cursorRow, cursorCol = state.cursorCol };
newUndoStack = [stateSnapshot, ...state.undoStack];
{
state = state.{
undoStack = newUndoStack,
mode = Insert
},
emit = []
};
escape = state \ { state = state.{ mode = Normal }, emit = [] };
undo = state \ state.undoStack
| [] \ { state = state, emit = [] }
| [prev, ...rest] \ (
stateSnapshot = { lines = state.lines, cursorRow = state.cursorRow, cursorCol = state.cursorCol };
{ state = state.{ lines = prev.lines, cursorCol = prev.cursorCol, cursorRow = prev.cursorRow, undoStack = rest, redoStack = [stateSnapshot, ...state.redoStack] }, emit = [] });
redo = state \ state.redoStack
| [] \ { state = state, emit = [] }
| [prev, ...rest] \ (
stateSnapshot = { lines = state.lines, cursorRow = state.cursorRow, cursorCol = state.cursorCol };
{ state = state.{ lines = prev.lines, cursorCol = prev.cursorCol, cursorRow = prev.cursorRow, undoStack = [stateSnapshot, ...state.undoStack], redoStack = rest }, emit = [] });
backspace = state \ backspace = state \
state.cursorCol == 0 state.cursorCol == 0
| True \ ( | True \ (
@ -84,6 +111,8 @@ textEditor = name \
cursorCol = 0, cursorCol = 0,
scrollX = 0, scrollX = 0,
scrollY = 0, scrollY = 0,
undoStack = [],
redoStack = [],
mode = Normal # Normal | Insert | Visual mode = Normal # Normal | Insert | Visual
}, },
@ -110,9 +139,17 @@ textEditor = name \
| Key { key = "i" } \ (state.mode | Key { key = "i" } \ (state.mode
| Insert \ insertChar "i" state | Insert \ insertChar "i" state
| Normal \ { state = state.{ mode = state.mode | Normal \ Insert | _ \ Insert } }) | Normal \ insertMode state)
| Key { key = "u" } \ (state.mode
| Insert \ insertChar "u" state
| Normal \ undo state)
| Key { key = "Escape" } \ { state = state.{ mode = state.mode | Insert \ Normal | _ \ Normal } } | Key { key = "r", ctrl = True } \ (state.mode
| Insert \ insertChar "R" state
| Normal \ redo state)
| Key { key = "Escape" } \ escape state
| Key { key = "Backspace" } \ (state.mode | Key { key = "Backspace" } \ (state.mode
| Insert \ backspace state | Insert \ backspace state
@ -130,8 +167,6 @@ textEditor = name \
| _ \ { state = state, emit = [] }, | _ \ { state = state, emit = [] },
view = state emit \ view = state emit \
_ = debug! "view" state;
buffer = map (l \ text l) state.lines; buffer = map (l \ text l) state.lines;
scale = 2; scale = 2;

Loading…
Cancel
Save