Implement scrolling in code tab

This commit is contained in:
dylan 2023-05-05 20:08:54 -07:00
parent 5e4b76ebb3
commit 99eb6b82f2

View File

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