Ctrl+backspace, register clipboard

This commit is contained in:
b1ek 2023-03-17 16:08:49 +10:00
parent 747b94f103
commit 34aaa678b1
Signed by: blek
GPG Key ID: 14546221E3595D0C
3 changed files with 48 additions and 15 deletions

View File

@ -20,6 +20,7 @@
}, },
"dependencies": { "dependencies": {
"@parcel/fs": "^2.8.3", "@parcel/fs": "^2.8.3",
"copy-to-clipboard": "^3.3.3",
"memfs": "^3.4.13", "memfs": "^3.4.13",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",

View File

@ -3,7 +3,9 @@ module.exports = (dom) => {
terminal.writeln('Welcome to my online resume!') terminal.writeln('Welcome to my online resume!')
terminal.writeln('Type \033[1;32mhelp\033[0m for list of commands') terminal.writeln('Type \033[1;32mhelp\033[0m for list of commands')
terminal.writeln('Use \033[1;33mAlt+C/V\033[0m to copy or paste');
terminal.writeln(''); terminal.writeln('');
require('./zsh')(terminal, dom); require('./zsh')(terminal, dom);
require('./pastebuffer')(terminal, dom);
} }

View File

@ -24,7 +24,14 @@ function text_prompt() {
return prompt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ''); return prompt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
} }
function pr_char(char) { /**
*
* @param { string } char
* @param { KeyboardEvent } dom
*/
function pr_char(char, dom) {
if (dom.key.length != 1) return;
cmd += char; cmd += char;
terminal.write(char); terminal.write(char);
} }
@ -99,20 +106,41 @@ function reset_cmd() {
terminal.writeln(''); terminal.writeln('');
} }
/** @param { KeyboardEvent } dom */ function cbackspace() {
function control_char(id, dom) { let exploded = cmd.split(' ');
if (exploded.length >= 1) {
reprint_prompt();
cmd = '';
return;
}
exploded.pop();
const backspace = () => { cmd = exploded.join(' ') + ' ';
reprint_prompt();
terminal.write(cmd);
return;
}
function backspace(isCtrl) {
if (terminal.buffer.active.cursorX <= text_prompt().length) return; if (terminal.buffer.active.cursorX <= text_prompt().length) return;
if (isCtrl) {
return cbackspace();
}
terminal.write('\b \b'); terminal.write('\b \b');
cmd = cmd.substring(0, cmd.length - 1); cmd = cmd.substring(0, cmd.length - 1);
} }
/** @param { KeyboardEvent } dom */
function control_char(id, dom) {
switch (id) { switch (id) {
// backspace // backspace
case 8: case 8:
backspace(); backspace(dom.ctrlKey);
break; break;
// enter // enter
@ -136,10 +164,8 @@ function control_char(id, dom) {
break; break;
} }
case 37: case 86:
backspace(); if (dom.altKey) break;
break;
default: default:
if (dom.ctrlKey && (dom.key.length == 1)) { if (dom.ctrlKey && (dom.key.length == 1)) {
@ -160,16 +186,20 @@ function key(e) {
/** @type {KeyboardEvent} */ /** @type {KeyboardEvent} */
const dom = e.domEvent; const dom = e.domEvent;
if (dom.key.length == 1 && !(dom.ctrlKey || dom.altKey)) { if (dom.key.length == 1 && !(dom.ctrlKey || dom.altKey)) {
pr_char(e.domEvent.key); pr_char(e.domEvent.key, dom);
} else { } else {
control_char(e.domEvent.keyCode, dom) control_char(e.domEvent.keyCode, dom)
} }
} }
module.exports = (t, d) => { function register(t, d) {
terminal = t; terminal = t;
dom = d; dom = d;
terminal.onKey(key); terminal.onKey(key);
terminal.write(prompt); terminal.write(prompt);
} }
register.pr_char = pr_char;
module.exports = register;