Clipboard!
This commit is contained in:
30
codetab.ts
30
codetab.ts
@ -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 = () => {
|
||||
|
Reference in New Issue
Block a user