Clipboard!

This commit is contained in:
dylan
2023-05-06 10:54:27 -07:00
parent b02d5155bd
commit 9685568f90
5 changed files with 49 additions and 15 deletions

View File

@ -3,8 +3,8 @@ import { fontWidth, fontHeight } from "./font.ts";
import { drawText } from "./builtins.ts";
import { COLOR } from "./colors.ts";
import {getSheet, setSheet} from "./sheet.ts";
import { K, getKeyboardString, keyPressed, shiftKeyDown } from "./keyboard.ts";
import { tokenize } from "./deps.ts";
import { K, ctrlKeyDown, getKeyboardString, keyPressed, shiftKeyDown } from "./keyboard.ts";
import { clipboard, tokenize } from "./deps.ts";
const state = {
scrollX: 0,
@ -91,6 +91,19 @@ const state = {
this.insertText("");
}
},
async copy() {
const {code, anchor, focus} = this;
const selected = code.slice(Math.min(anchor,focus), Math.max(anchor,focus));
await clipboard.writeText(selected);
},
async cut() {
await this.copy();
this.insertText("");
},
async paste() {
this.insertText(await clipboard.readText());
},
scrollToCursor() {
const {focusY, focusX, scrollY, scrollX} = this;
const fh = fontHeight + 1;
@ -332,14 +345,14 @@ const drawCodeField = (code: string, x: number, y: number, w: number, h: number)
})
}
const update = () => {
const update = async () => {
const { focus, focusX, focusY} = state;
const keyboardString = getKeyboardString();
if (keyboardString) {
state.insertText(keyboardString);
state.scrollToCursor();
}
// TODO: Handle ctrl-C, ctrl-V, ctrl-X, ctrl-Z
// TODO: Handle ctrl-Z
// TODO: Make ctrl-/ do commenting out (take inspiration from tab)
if (keyPressed(K.ENTER)) {
@ -399,6 +412,15 @@ const update = () => {
}
state.scrollToCursor();
}
if (keyPressed("C") && ctrlKeyDown()) {
await state.copy();
}
if (keyPressed("X") && ctrlKeyDown()) {
await state.cut();
}
if (keyPressed("V") && ctrlKeyDown()) {
await state.paste();
}
}
const draw = () => {