mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
fix(templates): align and scroll the Surround With menu (#4796)
Surround With gets a separate shortcut column and uses the UI font for action labels. The list scrolls within the space beside the caret, and the title stays visible while the keyboard moves through it.
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import type { EditorView } from "@codemirror/view";
|
||||
import { expect, openFromTree, test } from "./fixtures";
|
||||
|
||||
test("Surround With keeps its title and keyboard-selected actions visible in a short window", async ({
|
||||
workbench: page,
|
||||
}) => {
|
||||
await page.setViewportSize({ width: 680, height: 320 });
|
||||
const editor = await openFromTree(page, "App.tsx");
|
||||
const content = editor.locator(".cm-content");
|
||||
const source = [
|
||||
"const fee = 5;",
|
||||
"",
|
||||
"function settle(amount: number) {",
|
||||
" const adjusted = amount - fee;",
|
||||
" const receipt = adjusted.toString();",
|
||||
" console.log(receipt);",
|
||||
" return adjusted;",
|
||||
"}",
|
||||
].join("\n");
|
||||
const selectedText = " console.log(receipt);";
|
||||
await content.evaluate(
|
||||
(node, { source, selectedText }) => {
|
||||
const view = (node as HTMLElement & { cmTile: { view: EditorView } }).cmTile.view;
|
||||
const start = source.indexOf(selectedText);
|
||||
view.dispatch({
|
||||
changes: { from: 0, to: view.state.doc.length, insert: source },
|
||||
selection: { anchor: start, head: start + selectedText.length },
|
||||
scrollIntoView: true,
|
||||
});
|
||||
view.focus();
|
||||
},
|
||||
{ source, selectedText },
|
||||
);
|
||||
const openSurround = () =>
|
||||
page.evaluate(async () => {
|
||||
const module = "/src/platform/actions.ts";
|
||||
const { executeAction } = await import(/* @vite-ignore */ module);
|
||||
await executeAction("code.surroundWith");
|
||||
});
|
||||
|
||||
await openSurround();
|
||||
const popup = page.getByRole("dialog", { name: "Surround With", exact: true });
|
||||
const title = popup.getByText("Surround With", { exact: true });
|
||||
const list = popup.getByRole("listbox");
|
||||
await expect(title).toBeInViewport({ ratio: 1 });
|
||||
await expect.poll(() => list.evaluate((node) => node.scrollHeight - node.clientHeight)).toBeGreaterThan(0);
|
||||
await expect.poll(() => popup.evaluate((node) => node.scrollHeight - node.clientHeight)).toBe(0);
|
||||
|
||||
const first = list.getByRole("option", { name: /^1\.?\s+if$/ });
|
||||
await expect(first).toHaveAttribute("aria-selected", "true");
|
||||
const action = first.getByText("if", { exact: true });
|
||||
const mnemonic = first.getByText(/^1\.?$/, { exact: true });
|
||||
expect((await mnemonic.boundingBox())!.x + (await mnemonic.boundingBox())!.width).toBeLessThan(
|
||||
(await action.boundingBox())!.x,
|
||||
);
|
||||
expect(await action.evaluate((node) => getComputedStyle(node).fontSize)).toBe("13px");
|
||||
await page.keyboard.press("End");
|
||||
const last = list.getByRole("option", { name: /!\(expr\)/ });
|
||||
await expect(last).toHaveAttribute("aria-selected", "true");
|
||||
await expect(last).toBeInViewport({ ratio: 1 });
|
||||
await expect(title).toBeInViewport({ ratio: 1 });
|
||||
|
||||
await page.keyboard.press("Escape");
|
||||
await expect(popup).toBeHidden();
|
||||
await expect(content).toBeFocused();
|
||||
await expect(content).toContainText("console.log(receipt);");
|
||||
|
||||
await openSurround();
|
||||
await expect(first).toBeVisible();
|
||||
await page.keyboard.press("1");
|
||||
await expect(popup).toBeHidden();
|
||||
await expect(content).toContainText(/if \([^)]*\) \{/);
|
||||
await expect(content).toContainText("console.log(receipt);");
|
||||
});
|
||||
@@ -82,3 +82,47 @@
|
||||
flex: 1;
|
||||
border-top: 1px solid var(--border-popup);
|
||||
}
|
||||
.actions {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
width: 320px;
|
||||
min-width: 0;
|
||||
}
|
||||
.actions .body {
|
||||
min-height: 0;
|
||||
}
|
||||
.actions .header {
|
||||
flex: none;
|
||||
padding-inline: 14px;
|
||||
color: var(--fg-strong);
|
||||
font-size: var(--fs-ui);
|
||||
font-weight: 600;
|
||||
}
|
||||
.actions .title {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.actions .list {
|
||||
max-height: none;
|
||||
}
|
||||
.actions .row {
|
||||
gap: var(--space-2);
|
||||
}
|
||||
.actions .label {
|
||||
flex-shrink: 1;
|
||||
min-width: 0;
|
||||
max-width: none;
|
||||
font-family: var(--font-ui);
|
||||
font-size: var(--fs-ui);
|
||||
}
|
||||
.actions .section {
|
||||
width: 100%;
|
||||
padding: 0;
|
||||
}
|
||||
.mnemonic {
|
||||
flex: 0 0 var(--space-4);
|
||||
color: var(--fg-muted);
|
||||
font-variant-numeric: tabular-nums;
|
||||
text-align: end;
|
||||
}
|
||||
|
||||
@@ -37,17 +37,10 @@ export function filterChooserItems(items: readonly ChooserItem[], filter: string
|
||||
|
||||
const isSection = (r: Row) => !!r.item.section;
|
||||
|
||||
/** The label with its mnemonic underlined (IDEA: `1. if`, `T. Surround with…`). */
|
||||
/** The abbreviation with its mnemonic underlined. */
|
||||
function MnemonicLabel({ item }: { item: ChooserItem }) {
|
||||
const m = item.mnemonic;
|
||||
if (!m) return <>{item.label}</>;
|
||||
const i = item.mnemonicIndex;
|
||||
if (i === undefined)
|
||||
return (
|
||||
<>
|
||||
<u>{m}</u>. {item.label}
|
||||
</>
|
||||
);
|
||||
if (i === undefined) return <>{item.label}</>;
|
||||
return (
|
||||
<>
|
||||
{item.label.slice(0, i)}
|
||||
@@ -58,7 +51,11 @@ function MnemonicLabel({ item }: { item: ChooserItem }) {
|
||||
}
|
||||
|
||||
function ChooserPopup({ request }: { request: ChooserRequest }) {
|
||||
const rowHeight = useRowHeight(22);
|
||||
const rowHeight = useRowHeight(request.mnemonics ? 24 : 22);
|
||||
// Keep the scroll viewport stable as the popup finds room above or below the caret.
|
||||
const maxHeight = request.mnemonics
|
||||
? `calc(max(${request.anchor.y}px, 100vh - ${request.anchor.y}px) - 8px)`
|
||||
: undefined;
|
||||
const [filter, setFilter] = useState(request.filter ?? "");
|
||||
const rows = useMemo(() => filterChooserItems(request.items, filter), [request.items, filter]);
|
||||
const [active, setActive] = useState(() =>
|
||||
@@ -113,11 +110,12 @@ function ChooserPopup({ request }: { request: ChooserRequest }) {
|
||||
onClose={close}
|
||||
role="dialog"
|
||||
aria-label={request.title}
|
||||
className={styles.popup}
|
||||
className={clsx(styles.popup, request.mnemonics && styles.actions)}
|
||||
style={maxHeight ? { maxHeight } : undefined}
|
||||
>
|
||||
<div className={styles.body} tabIndex={0} data-autofocus onKeyDown={onKeyDown}>
|
||||
<div className={styles.header}>
|
||||
<span className={styles.title}>{request.title}</span>
|
||||
<span className={styles.title}>{tx(request.title)}</span>
|
||||
{request.filterable && filter && <span className={styles.filter}>{filter}</span>}
|
||||
</div>
|
||||
<List
|
||||
@@ -128,7 +126,8 @@ function ChooserPopup({ request }: { request: ChooserRequest }) {
|
||||
onActivate={(r) => pick(r)}
|
||||
isSection={isSection}
|
||||
sectionHeight={22}
|
||||
renderSection={(r) => <div className={styles.section}>{r.item.label}</div>}
|
||||
sectionHeightOf={request.mnemonics ? (r) => (r.item.label ? undefined : 12) : undefined}
|
||||
renderSection={(r) => <div className={styles.section}>{tx(r.item.label)}</div>}
|
||||
focusable={false}
|
||||
hoverSelects
|
||||
rowHeight={rowHeight}
|
||||
@@ -137,14 +136,21 @@ function ChooserPopup({ request }: { request: ChooserRequest }) {
|
||||
empty={<div className={styles.empty}>{request.emptyText ?? tx("Nothing to show")}</div>}
|
||||
renderItem={(r, state) => (
|
||||
<div className={clsx(styles.row, state.active && styles.active)}>
|
||||
<span className={styles.label}>
|
||||
{r.item.mnemonic ? (
|
||||
{request.mnemonics && (
|
||||
<span className={styles.mnemonic}>{r.item.mnemonicIndex === undefined && r.item.mnemonic}</span>
|
||||
)}
|
||||
<span className={styles.label} title={request.mnemonics ? r.item.label : undefined}>
|
||||
{r.item.mnemonicIndex !== undefined ? (
|
||||
<MnemonicLabel item={r.item} />
|
||||
) : (
|
||||
<HighlightedText text={r.item.label} indices={r.matches ?? undefined} />
|
||||
)}
|
||||
</span>
|
||||
{r.item.detail && <span className={styles.detail}>{r.item.detail}</span>}
|
||||
{r.item.detail && (
|
||||
<span className={styles.detail} title={request.mnemonics ? r.item.detail : undefined}>
|
||||
{r.item.detail}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
|
||||
@@ -11,7 +11,7 @@ export interface ChooserItem {
|
||||
section?: boolean;
|
||||
/** Typing this character picks the item (see `ChooserRequest.mnemonics`). */
|
||||
mnemonic?: string;
|
||||
/** Where the mnemonic is underlined in the label; without it the label gets an `M. ` prefix. */
|
||||
/** Where the mnemonic is underlined in the label. Omit for a separate leading column. */
|
||||
mnemonicIndex?: number;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user