Remove last word with ctrl+w

Fixes #80.
This commit is contained in:
Florine W. Dekker 2019-11-13 15:42:55 +01:00
parent fc62d8d12a
commit e672cb3eb7
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
4 changed files with 43 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{
"name": "fwdekker.com",
"version": "0.14.0",
"version": "0.15.0",
"description": "The source code of [my personal website](https://fwdekker.com/).",
"author": "Felix W. Dekker",
"repository": {

View File

@ -100,6 +100,7 @@ body {
max-width: 100%;
overflow-wrap: break-word;
white-space: pre;
outline: none;
border: none;

View File

@ -60,6 +60,23 @@ export function getFileExtension(filename: string): string {
return extension == null ? "" : extension[1];
}
/**
* Moves the caret to the given position in the given node.
*
* @param node the node to move the caret in
* @param position the position from the left to place the caret at
*/
export function moveCaretTo(node: Node, position: number): void {
const range = document.createRange();
range.setStart(node, position);
const selection = window.getSelection();
if (selection !== null) {
selection.removeAllRanges();
selection.addRange(range);
}
}
/**
* Moves the caret to the end of the given node.
*

View File

@ -1,6 +1,6 @@
import {InputHistory} from "./InputHistory";
import {Persistence} from "./Persistence";
import {escapeHtml, moveCaretToEndOf, parseCssPixels} from "./Shared";
import {escapeHtml, moveCaretTo, moveCaretToEndOf, parseCssPixels} from "./Shared";
import {Shell} from "./Shell";
import {Buffer, StreamSet} from "./Stream";
@ -306,6 +306,29 @@ export class Terminal {
event.preventDefault();
}
break;
case "w":
if (event.ctrlKey) {
let offset = this.inputText.length;
if (this.input === document.activeElement)
offset = document.getSelection()?.anchorOffset ?? offset;
const left = this.inputText.slice(0, offset);
const right = this.inputText.slice(offset);
const newLeft = left.includes(" ")
? left.slice(0, left.trimRight().lastIndexOf(" ") + 1)
: "";
const newOffset = offset - (left.length - newLeft.length);
this.inputText = newLeft + right;
const element = this.input.firstChild;
if (element !== null)
window.setTimeout(() => moveCaretTo(element, newOffset), 0);
event.preventDefault();
}
break;
}
}
}