diff --git a/codetab.ts b/codetab.ts index 0bb8df3..42553b8 100644 --- a/codetab.ts +++ b/codetab.ts @@ -91,6 +91,23 @@ const state = { this.insertText(""); } }, + scrollToCursor() { + const {focusY, focusX, scrollY, scrollX} = this; + const fh = fontHeight + 1; + const fw = fontWidth; + if (focusY*fh < scrollY) { + this.scrollY = focusY*fh; + } + if (focusY*fh > scrollY+112-fh) { + this.scrollY = focusY*fh-112+fh; + } + if (focusX*fw < scrollX) { + this.scrollX = focusX*fw; + } + if (focusX*fw > scrollX+128-fw) { + this.scrollX = focusX*fw-128+fw; + } + }, get code() { const {sheet_type, value} = getSheet(0); if (sheet_type !== "code") { @@ -156,6 +173,7 @@ const update = () => { const keyboardString = getKeyboardString(); if (keyboardString) { state.insertText(keyboardString); + state.scrollToCursor(); } // TODO: Handle ctrl-C, ctrl-V, ctrl-X, ctrl-Z // TODO: Make ctrl-/ do commenting out (take inspiration from tab) @@ -163,6 +181,7 @@ const update = () => { if (keyPressed(K.ENTER)) { // TODO: Make this play nicely with indentation state.insertText("\n"); + state.scrollToCursor(); } if (keyPressed(K.TAB)) { if (!shiftKeyDown()) { @@ -174,12 +193,15 @@ const update = () => { } else { state.outdent(/^(\t| )/); } + state.scrollToCursor(); } if (keyPressed(K.BACKSPACE)) { state.backspace(); + state.scrollToCursor(); } if (keyPressed(K.DELETE)) { state.delete(); + state.scrollToCursor(); } if (keyPressed(K.ARROW_RIGHT)) { if (shiftKeyDown()) { @@ -187,6 +209,7 @@ const update = () => { } else { state.setSelection(focus+1); } + state.scrollToCursor(); } if (keyPressed(K.ARROW_LEFT)) { if (shiftKeyDown()) { @@ -194,6 +217,7 @@ const update = () => { } else { state.setSelection(focus-1); } + state.scrollToCursor(); } if (keyPressed(K.ARROW_DOWN)) { if (shiftKeyDown()) { @@ -201,6 +225,7 @@ const update = () => { } else { state.setSelection({x: focusX, y: focusY+1}); } + state.scrollToCursor(); } if (keyPressed(K.ARROW_UP)) { if (shiftKeyDown()) { @@ -208,6 +233,7 @@ const update = () => { } else { state.setSelection({x: focusX, y: focusY-1}); } + state.scrollToCursor(); } }