fix: panel keys reach the panel, restore block height, sync field after replace

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 11:55:20 +01:00
1 parent 7116af9434
commit 6678cd3dc8
1 file changed
+85 -10
+85 -10
View File
@@ -28,6 +28,7 @@ import {
exec,
hc,
home,
run,
shared,
setShared,
notify,
@@ -92,6 +93,12 @@ const SIZE_ERROR = "Nie można odczytać rozmiaru pliku";
/** Folders are not carried - only files. */
const DIR_NOTICE = "Foldery nie są obsługiwane — dodaj pliki.";
/** A block lower than this (px) is made taller while the panel is open... */
const PANEL_MIN_BLOCK = 320;
/** ...to this. */
const PANEL_BLOCK_PX = 360;
/** How many rows the menu of the suggestions shows at most. */
const MENU_LIMIT = 20;
@@ -174,6 +181,10 @@ export default class Composer extends View {
// is picked, where the caret was when it opened, and the history as it was
// read then. Each tab has a search field of its own.
this.panel = null;
// The height of the composer block (reported by its root) and the height
// to give back when a panel that made the block taller closes.
this.blockHeight = null;
this.panelRestorePx = null;
this.panelInputs = {
history: InputState.new({ placeholder: "Szukaj w historii…" }),
favorites: InputState.new({ placeholder: "Szukaj w ulubionych…" }),
@@ -567,6 +578,18 @@ export default class Composer extends View {
const now = Date.now();
if (this.lastAction && this.lastAction.command === command && now - this.lastAction.at < 40) return;
this.lastAction = { command, at: now };
// While the panel is open the keys are the panel's - the keyboard is in
// its search field, not in the composer's, and the bindings of the
// composer (`ovis-composer > Input`) catch these keys as actions first.
if (this.panel) {
if (command === "up") this.panelCommand("up", cx);
else if (command === "down") this.panelCommand("down", cx);
else if (command === "accept") this.panelCommand("tab", cx);
else if (command === "escape" || command === "panel") this.panelCommand("escape", cx);
cx.notify();
return;
}
if (command === "up") this.arrow(-1, cx);
if (command === "down") this.arrow(1, cx);
if (command === "accept") this.acceptFromKey(cx);
@@ -594,6 +617,14 @@ export default class Composer extends View {
// The caret is unknown - the text goes in at the start.
}
// A low block leaves no room for a list: it is made taller while the
// panel is open, and goes back to its height when the panel closes.
this.panelRestorePx = null;
if (Number.isFinite(this.blockHeight) && this.blockHeight < PANEL_MIN_BLOCK) {
this.resizeBlock(PANEL_BLOCK_PX);
this.panelRestorePx = Math.round(this.blockHeight);
}
this.favorites = storage.loadFavorites(this.store);
this.panel = { tab, index: 0, caret, history: storage.loadHistory(this.store) };
for (const input of Object.values(this.panelInputs)) input.set_value("");
@@ -606,10 +637,23 @@ export default class Composer extends View {
if (!this.panel) return;
const { caret } = this.panel;
this.panel = null;
if (this.panelRestorePx !== null) {
this.resizeBlock(this.panelRestorePx);
this.panelRestorePx = null;
}
this.textarea.focus();
if (restore) this.restoreSelection(caret);
}
/** The composer block gets another height (`layout.resize`); a refusal is said, not worked around. */
resizeBlock(px) {
try {
run("layout.resize", { id: "composer", px });
} catch (err) {
this.notice(`Nie udało się zmienić wysokości composera: ${errorText(err)}`, "error");
}
}
/** The selection (or the caret) of the field. */
restoreSelection(range) {
try {
@@ -653,13 +697,15 @@ export default class Composer extends View {
const view = this.panelView();
if (key === "escape") {
this.closePanel();
this.panelCommand("escape", cx);
} else if (key === "arrowdown" || key === "down") {
this.movePanel(1, view);
this.panelCommand("down", cx);
} else if (key === "arrowup" || key === "up") {
this.movePanel(-1, view);
this.panelCommand("up", cx);
} else if (key === "tab") {
this.panelTab(otherTab(this.panel.tab), cx);
this.panelCommand("tab", cx);
} else if (key === "r" && m.control === true) {
this.panelCommand("escape", cx);
} else if (key === "enter") {
this.panelPick(view.index, cx);
} else if (key === "d" && m.control === true) {
@@ -672,6 +718,22 @@ export default class Composer extends View {
cx.notify();
}
/**
* A key of the panel: up, down, tab, escape. The same press can arrive twice
* (as an action of the composer and as a key of the panel), so the second
* delivery within 40 ms is dropped.
*/
panelCommand(command, cx) {
if (!this.panel) return;
const now = Date.now();
if (this.lastPanelCommand && this.lastPanelCommand.command === command && now - this.lastPanelCommand.at < 40) return;
this.lastPanelCommand = { command, at: now };
if (command === "escape") this.closePanel();
else if (command === "tab") this.panelTab(otherTab(this.panel.tab), cx);
else this.movePanel(command === "down" ? 1 : -1, this.panelView());
}
movePanel(step, view) {
this.panel.index = moveIndex(view.index, step, view.state.hits.length);
this.scrollPanel(this.panel.index);
@@ -689,6 +751,15 @@ export default class Composer extends View {
cx.notify();
}
/**
* `replace` may or may not report a `change`: what the field says is read
* back, and the draft (state and disk) follows it.
*/
syncFieldAfterReplace() {
this.value = this.textarea.value();
this.saveDraftNow();
}
/** The search is cleared. */
panelClear(cx) {
if (!this.panel) return;
@@ -715,12 +786,9 @@ export default class Composer extends View {
const hit = this.panelView().state.hits[index];
if (!hit) return;
const { caret } = this.panel;
this.panel = null;
this.textarea.focus();
this.restoreSelection(caret);
this.closePanel();
this.textarea.replace(hit.item.text);
this.scheduleDraftSave(cx);
this.syncFieldAfterReplace();
this.notice("Wstawiono w miejscu kursora — nic nie zostało wysłane.");
cx.notify();
}
@@ -1228,6 +1296,10 @@ export default class Composer extends View {
return;
}
// Only an entry that waits (or that the person chose to try again) goes.
const waiting = (this.state.queue[key] ?? []).find((entry) => entry.id === id);
if (!waiting || !["held", "uncertain", "queued"].includes(waiting.state)) return;
// A held entry goes back to the queue first, and the turn gate of the
// agent steps aside - this is a send outside a turn.
this.state = reduce(this.state, { type: "requeue", agent: key, id });
@@ -1286,8 +1358,8 @@ export default class Composer extends View {
this.textarea.focus();
this.textarea.replace(item.text);
this.syncFieldAfterReplace();
this.discard(agent, id, cx);
this.scheduleDraftSave(cx);
this.notice("Wiadomość wróciła do edytora — nic nie zostało wysłane.", undefined, cx);
}
@@ -1861,6 +1933,9 @@ export default class Composer extends View {
return v_flex()
.id("ovis-composer")
.relative()
.on_size_change((size) => {
this.blockHeight = size.height;
})
.track_focus(this.handle)
.key_context(CONTEXT)
.on_key_down((event, cx) => this.key(event, cx))