317 lines
9.2 KiB
JavaScript
317 lines
9.2 KiB
JavaScript
// panel.js - the view of the History | Favourites panel and of the image
|
|
// preview. GPUI only: no logic, no state of its own. main.js owns the state and
|
|
// hands in what to draw (`view`) and what to do (`on`); what a row says and
|
|
// which parts of it are lit is decided in history.js.
|
|
//
|
|
// The look follows docs/mockups/herdr-unified-prompt.html (`.pop.hist`,
|
|
// `.hrow`, `.hfoot`, `.empty`). The colors come from `theme()`.
|
|
//
|
|
// Limit of the list: rows of a `v_virtual_list` cannot carry their own click
|
|
// handlers (the host refuses a handler inside the item renderer) - one
|
|
// `on_item_click` serves the whole list and names the row by its key. So a row
|
|
// click inserts the text; the star of a row is a sign, not a button (the
|
|
// favourites are toggled with Ctrl+D or a right click - `on_item_secondary_click`).
|
|
|
|
import { div, image } from "gpui-kit";
|
|
import { v_flex, h_flex, Input, Scrollbar, v_virtual_list } from "gpui-base";
|
|
import { segments, entryMeta } from "./history.js";
|
|
|
|
/** The height of one row - the list is virtual, so every row has the same. */
|
|
export const ROW_HEIGHT = 56;
|
|
|
|
/** The text lines of a row (the rest is cut). */
|
|
const TEXT_HEIGHT = 34;
|
|
|
|
/**
|
|
* The panel.
|
|
*
|
|
* @param {Record<string,string>} t - theme()
|
|
* @param {{
|
|
* tab: "history"|"favorites",
|
|
* state: {counts:{history:number, favorites:number}, hits:{item:any, ranges:[number,number][]}[], empty:null|string},
|
|
* query: string,
|
|
* index: number,
|
|
* input: object,
|
|
* searchFocused: boolean,
|
|
* fieldBackground: string,
|
|
* scroll: object,
|
|
* isFavorite: (text:string) => boolean,
|
|
* }} view
|
|
* @param {{
|
|
* key: (event:any, cx:any) => void,
|
|
* tab: (tab:string, cx:any) => void,
|
|
* pick: (key:string, cx:any) => void,
|
|
* toggle: (key:string, cx:any) => void,
|
|
* clear: (cx:any) => void,
|
|
* }} on
|
|
*/
|
|
export function renderPanel(t, view, on) {
|
|
const { state } = view;
|
|
|
|
return v_flex()
|
|
.id("composer-panel")
|
|
.key_context("ovis-composer-panel")
|
|
.on_key_down((event, cx) => on.key(event, cx))
|
|
.size_full()
|
|
.rounded(12)
|
|
.border(1)
|
|
.border_color(t.border)
|
|
.bg(t.popover)
|
|
.overflow_hidden()
|
|
.children([
|
|
renderTop(t, view, on),
|
|
v_flex()
|
|
.id("composer-panel-body")
|
|
.flex_1()
|
|
.min_h(0)
|
|
.relative()
|
|
.child(state.empty ? renderEmpty(t, view, on) : renderList(t, view, on)),
|
|
renderFoot(t),
|
|
]);
|
|
}
|
|
|
|
/** The tabs and the search field. */
|
|
function renderTop(t, view, on) {
|
|
const { counts } = view.state;
|
|
|
|
return h_flex()
|
|
.id("composer-panel-top")
|
|
.gap(6)
|
|
.items_center()
|
|
.px(8)
|
|
.py(8)
|
|
.border_b(1)
|
|
.border_color(t.border)
|
|
.children([
|
|
renderTab(t, "history", `Historia ${counts.history}`, view.tab === "history", on),
|
|
renderTab(t, "favorites", `Ulubione ${counts.favorites}`, view.tab === "favorites", on),
|
|
// The search field: a darker recess with a border that lights up while
|
|
// the field has the keyboard (the `.hsearch` of the mockup).
|
|
h_flex()
|
|
.id("composer-panel-search")
|
|
.flex_1()
|
|
.min_w(0)
|
|
.h(30)
|
|
.px(10)
|
|
.items_center()
|
|
.gap(6)
|
|
.rounded(8)
|
|
.bg(view.fieldBackground)
|
|
.border_1()
|
|
.border_color(view.searchFocused ? t.accent : t.input)
|
|
.child(div().flex_none().text_size(13).text_color(t.dim).child("⌕"))
|
|
.child(Input.new(view.input).flex_1().min_w(0).text_size(12.5))
|
|
.when(view.query !== "", (el) =>
|
|
el.child(
|
|
div()
|
|
.id("composer-panel-clear")
|
|
.px(6)
|
|
.text_size(12)
|
|
.text_color(t.dim)
|
|
.cursor_pointer()
|
|
.hover((s) => s.text_color(t.foreground))
|
|
.on_click((event, cx) => on.clear(cx))
|
|
.child("✕"),
|
|
),
|
|
),
|
|
]);
|
|
}
|
|
|
|
/** One tab. */
|
|
function renderTab(t, name, label, on, handlers) {
|
|
return div()
|
|
.id(`composer-panel-tab-${name}`)
|
|
.px(10)
|
|
.h(26)
|
|
.flex()
|
|
.items_center()
|
|
.flex_none()
|
|
.rounded(6)
|
|
.text_size(12.5)
|
|
.text_color(on ? t.foreground : t.dim)
|
|
.when(on, (el) => el.bg(t.hover))
|
|
.cursor_pointer()
|
|
.on_click((event, cx) => handlers.tab(name, cx))
|
|
.child(label);
|
|
}
|
|
|
|
/** The list - virtual, so five hundred rows cost what twenty do. */
|
|
function renderList(t, view, on) {
|
|
const hits = view.state.hits;
|
|
|
|
return v_flex()
|
|
.size_full()
|
|
.relative()
|
|
.child(
|
|
v_virtual_list(
|
|
"composer-panel-list",
|
|
hits.length,
|
|
ROW_HEIGHT,
|
|
(index) => String(hits[index].item.id),
|
|
(range) => hits.slice(range.start, range.end).map((hit, i) => renderRow(t, view, hit, range.start + i)),
|
|
)
|
|
.track_scroll(view.scroll)
|
|
.size_full()
|
|
.on_item_click((key, cx) => on.pick(key, cx))
|
|
.on_item_secondary_click((key, event, cx) => on.toggle(key, cx)),
|
|
)
|
|
.child(Scrollbar.vertical("composer-panel-list").absolute().inset_0());
|
|
}
|
|
|
|
/** One row: the text with the match lit, the line under it, the star. */
|
|
function renderRow(t, view, hit, index) {
|
|
const on = index === view.index;
|
|
const fav = view.isFavorite(hit.item.text);
|
|
|
|
return h_flex()
|
|
.id(`composer-panel-row-${hit.item.id}`)
|
|
.h(ROW_HEIGHT)
|
|
.gap(10)
|
|
.px(10)
|
|
.py(7)
|
|
.items_start()
|
|
.rounded(7)
|
|
.when(on, (el) => el.bg(t.hover))
|
|
.child(
|
|
v_flex()
|
|
.flex_1()
|
|
.min_w(0)
|
|
.gap(2)
|
|
.child(renderText(t, hit))
|
|
.child(
|
|
div()
|
|
.text_size(11.5)
|
|
.text_color(t.dim)
|
|
.truncate()
|
|
.child(entryMeta(hit.item, view.tab)),
|
|
),
|
|
)
|
|
.child(
|
|
div()
|
|
.flex_none()
|
|
.size(24)
|
|
.flex()
|
|
.items_center()
|
|
.justify_center()
|
|
.text_size(14)
|
|
.text_color(fav ? t.working : t.dim)
|
|
.child(fav ? "★" : "☆"),
|
|
);
|
|
}
|
|
|
|
/** The text of a row: two lines, the parts that match the query lit. */
|
|
function renderText(t, hit) {
|
|
const parts = [];
|
|
for (const part of segments(hit.item.text, hit.ranges)) {
|
|
// Words are separate children so the line breaks between words; a newline
|
|
// in the message is one space here.
|
|
for (const word of part.text.replace(/\s+/g, " ").match(/\S+\s*|\s+/g) ?? []) {
|
|
parts.push(
|
|
div()
|
|
.flex_none()
|
|
.text_size(12.8)
|
|
.text_color(t.foreground)
|
|
.when(part.hit, (el) => el.bg(t.selected).rounded(2))
|
|
.child(word),
|
|
);
|
|
}
|
|
}
|
|
return h_flex().flex_wrap().h(TEXT_HEIGHT).overflow_hidden().children(parts);
|
|
}
|
|
|
|
/** What is said when there is nothing to list. */
|
|
function renderEmpty(t, view, on) {
|
|
const { empty } = view.state;
|
|
const [title, body] =
|
|
empty === "history"
|
|
? ["Historia jest pusta", "Wysłane wiadomości pojawią się tutaj — z każdej przestrzeni i od każdego agenta."]
|
|
: empty === "favorites"
|
|
? ["Brak ulubionych", "Kliknij ☆ przy edytorze, aby zapisać prompt na stałe."]
|
|
: [`Brak wyników dla „${view.query.trim()}”`, "Spróbuj krótszego fragmentu albo"];
|
|
|
|
return v_flex()
|
|
.id("composer-panel-empty")
|
|
.size_full()
|
|
.items_center()
|
|
.justify_center()
|
|
.gap(4)
|
|
.px(16)
|
|
.child(div().text_size(13).text_color(t.foreground).child(title))
|
|
.child(div().text_size(12).text_color(t.dim).child(body))
|
|
.when(empty === "query", (el) =>
|
|
el.child(
|
|
div()
|
|
.id("composer-panel-clear-query")
|
|
.mt(4)
|
|
.px(10)
|
|
.h(26)
|
|
.flex()
|
|
.items_center()
|
|
.rounded(6)
|
|
.bg(t.chrome)
|
|
.text_size(12)
|
|
.text_color(t.muted)
|
|
.cursor_pointer()
|
|
.hover((s) => s.bg(t.hover).text_color(t.foreground))
|
|
.on_click((event, cx) => on.clear(cx))
|
|
.child("wyczyść wyszukiwanie"),
|
|
),
|
|
);
|
|
}
|
|
|
|
/** The line of keys at the bottom. */
|
|
function renderFoot(t) {
|
|
const keys = [
|
|
"↑↓ wybór",
|
|
"Enter wstaw w miejscu kursora (nie wysyła)",
|
|
"Tab Historia/Ulubione",
|
|
"Ctrl+D ulubiony",
|
|
"Esc wróć do edytora",
|
|
];
|
|
|
|
return h_flex()
|
|
.id("composer-panel-foot")
|
|
.gap(14)
|
|
.flex_wrap()
|
|
.px(12)
|
|
.py(6)
|
|
.border_t(1)
|
|
.border_color(t.border)
|
|
.text_size(11.5)
|
|
.text_color(t.dim)
|
|
.children(keys.map((text) => div().child(text)));
|
|
}
|
|
|
|
/**
|
|
* The preview of an image: the whole image on a dark scrim, its name and size
|
|
* under it. A click or Esc closes it (main.js).
|
|
*
|
|
* @param {Record<string,string>} t
|
|
* @param {{path:string, caption:string}} preview
|
|
* @param {object} handle - FocusHandle of the overlay
|
|
* @param {{close: (cx:any) => void}} on
|
|
*/
|
|
export function renderPreview(t, preview, handle, on) {
|
|
return v_flex()
|
|
.id("composer-preview")
|
|
.track_focus(handle)
|
|
.key_context("ovis-composer-preview")
|
|
.on_key_down((event, cx) => {
|
|
if (String(event.key).toLowerCase() === "escape") {
|
|
on.close(cx);
|
|
cx.stop_propagation();
|
|
}
|
|
})
|
|
.absolute()
|
|
.inset_0()
|
|
.items_center()
|
|
.justify_center()
|
|
.gap(8)
|
|
.p(16)
|
|
.bg("#000000cc")
|
|
.cursor_pointer()
|
|
.on_click((event, cx) => on.close(cx))
|
|
.child(image(preview.path).max_w("90%").max_h("80%").rounded(8))
|
|
.child(div().text_size(12).text_color(t.muted).child(preview.caption));
|
|
}
|