fix(tui): honor keybindings for submit fallback

This commit is contained in:
Mario Zechner 2026-02-03 01:10:06 +01:00
parent d2139d8ed2
commit c64e228b76
2 changed files with 43 additions and 25 deletions

View file

@ -621,15 +621,13 @@ export class Editor implements Component, Focusable {
return;
}
// New line (Shift+Enter, Alt+Enter, etc.)
if (
kb.matches(data, "newLine") ||
(data.charCodeAt(0) === 10 && data.length > 1) ||
data === "\x1b\r" ||
data === "\x1b[13;2~" ||
(data.length > 1 && data.includes("\x1b") && data.includes("\r")) ||
(data === "\n" && data.length === 1)
) {
// New line
if (kb.matches(data, "newLine")) {
if (this.shouldSubmitOnBackslashEnter(data, kb)) {
this.handleBackspace();
this.submitValue();
return;
}
this.addNewLine();
return;
}
@ -647,22 +645,7 @@ export class Editor implements Component, Focusable {
return;
}
let result = this.state.lines.join("\n").trim();
for (const [pasteId, pasteContent] of this.pastes) {
const markerRegex = new RegExp(`\\[paste #${pasteId}( (\\+\\d+ lines|\\d+ chars))?\\]`, "g");
result = result.replace(markerRegex, pasteContent);
}
this.state = { lines: [""], cursorLine: 0, cursorCol: 0 };
this.pastes.clear();
this.pasteCounter = 0;
this.historyIndex = -1;
this.scrollOffset = 0;
this.undoStack.length = 0;
this.lastAction = null;
if (this.onChange) this.onChange("");
if (this.onSubmit) this.onSubmit(result);
this.submitValue();
return;
}
@ -1068,6 +1051,36 @@ export class Editor implements Component, Focusable {
}
}
private shouldSubmitOnBackslashEnter(data: string, kb: ReturnType<typeof getEditorKeybindings>): boolean {
if (this.disableSubmit) return false;
if (!matchesKey(data, "enter")) return false;
const submitKeys = kb.getKeys("submit");
const hasShiftEnter = submitKeys.includes("shift+enter") || submitKeys.includes("shift+return");
if (!hasShiftEnter) return false;
const currentLine = this.state.lines[this.state.cursorLine] || "";
return this.state.cursorCol > 0 && currentLine[this.state.cursorCol - 1] === "\\";
}
private submitValue(): void {
let result = this.state.lines.join("\n").trim();
for (const [pasteId, pasteContent] of this.pastes) {
const markerRegex = new RegExp(`\\[paste #${pasteId}( (\\+\\d+ lines|\\d+ chars))?\\]`, "g");
result = result.replace(markerRegex, pasteContent);
}
this.state = { lines: [""], cursorLine: 0, cursorCol: 0 };
this.pastes.clear();
this.pasteCounter = 0;
this.historyIndex = -1;
this.scrollOffset = 0;
this.undoStack.length = 0;
this.lastAction = null;
if (this.onChange) this.onChange("");
if (this.onSubmit) this.onSubmit(result);
}
private handleBackspace(): void {
this.historyIndex = -1; // Exit history browsing mode
this.lastAction = null;