дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 01:01:17 +02:00
feat(bookmarks): start the Bookmarks tree collapsed and reveal new bookmarks (#7532)
The Bookmarks tool window tree starts collapsed and expands just enough to reveal a newly added bookmark. Expansion state is kept per tree, and a bookmark replaced by an edit is not treated as new. Co-authored-by: solhovoy <[email protected]>
Цей коміт міститься в:
1 джерело
0303a513cd
коміт
a7b9529109
5 files changed
+113
-35
No files matched your search
@@ -36,6 +36,7 @@ test("line bookmarks keep their code or description readable in the dock", async
|
||||
);
|
||||
|
||||
const tree = page.getByRole("tree", { name: "Bookmarks" });
|
||||
await page.getByRole("button", { name: "Expand All", exact: true }).click();
|
||||
await expect(tree.getByText(name, { exact: true })).toBeVisible();
|
||||
const snippet = tree.getByText(code, { exact: true });
|
||||
await expect.poll(() => snippet.evaluate((el) => el.clientWidth)).toBeGreaterThanOrEqual(105);
|
||||
@@ -78,6 +79,7 @@ test("selecting a bookmark shows its code in the preview below the side-docked B
|
||||
const previewButton = page.getByRole("button", { name: "Editor Preview" });
|
||||
if ((await previewButton.getAttribute("aria-pressed")) !== "true") await previewButton.click();
|
||||
const tree = page.getByRole("tree", { name: "Bookmarks" });
|
||||
await page.getByRole("button", { name: "Expand All", exact: true }).click();
|
||||
await tree.getByRole("treeitem", { name: /const previewBookmark/ }).click();
|
||||
const preview = page.locator("#bookmarks-preview");
|
||||
await expect(preview).toBeVisible();
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||
import * as actions from "@/platform/actions";
|
||||
import * as open from "../common/open";
|
||||
import * as messages from "@/workbench/messages";
|
||||
import { BookmarkPopupsOverlay } from "../bookmarks/BookmarkPopups";
|
||||
import { BookmarksActions, BookmarksView } from "../bookmarks/BookmarksView";
|
||||
import { useBookmarkPopups, useBookmarks } from "../bookmarks/store";
|
||||
import { loadBookmarks, updateBookmarkData, useBookmarkPopups, useBookmarks } from "../bookmarks/store";
|
||||
|
||||
vi.mock("../find/parts", () => ({
|
||||
CodePreview: ({ path, line }: { path: string | null; line: number }) => (
|
||||
@@ -65,6 +65,47 @@ describe("Bookmarks tool window actions", () => {
|
||||
expect(screen.queryByTestId("bookmark-preview")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("reveals a newly added bookmark in a collapsed tree", () => {
|
||||
setup();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Collapse All" }));
|
||||
expect(screen.queryByRole("treeitem", { name: /hello/ })).not.toBeInTheDocument();
|
||||
act(() =>
|
||||
updateBookmarkData((d) => ({
|
||||
...d,
|
||||
items: [...d.items, { path: "/p/src/b.ts", line: 4, text: "added", list: "default", created: 3 }],
|
||||
})),
|
||||
);
|
||||
expect(screen.getByRole("treeitem", { name: /added/ })).toBeInTheDocument();
|
||||
expect(screen.queryByRole("treeitem", { name: /hello/ })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("keeps the list collapsed when an existing bookmark changes", () => {
|
||||
setup();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Collapse All" }));
|
||||
act(() =>
|
||||
updateBookmarkData((d) => ({
|
||||
...d,
|
||||
items: d.items.map((b) => ({ ...b, line: b.line + 1, text: "edited", description: "Existing bookmark" })),
|
||||
})),
|
||||
);
|
||||
expect(screen.getByRole("treeitem", { name: /Bookmarks/ })).toHaveAttribute("aria-expanded", "false");
|
||||
});
|
||||
|
||||
it("does not carry expansion into another project's bookmark tree", () => {
|
||||
setup();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Expand All" }));
|
||||
localStorage.setItem(
|
||||
"openidea.bookmarks.v1:/other",
|
||||
JSON.stringify([{ path: "/other/b.ts", line: 0, text: "another project", created: 4 }]),
|
||||
);
|
||||
try {
|
||||
act(() => loadBookmarks("/other"));
|
||||
expect(screen.getByRole("treeitem", { name: /^other/ })).toHaveAttribute("aria-expanded", "false");
|
||||
} finally {
|
||||
localStorage.removeItem("openidea.bookmarks.v1:/other");
|
||||
}
|
||||
});
|
||||
|
||||
it("expands and collapses the bookmark tree from the header", () => {
|
||||
setup();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Expand All" }));
|
||||
|
||||
@@ -85,6 +85,9 @@ describe.each([false, true])("path scope, replace=%s", (replace) => {
|
||||
);
|
||||
const tree = screen.getByRole("tree", { name: "Bookmarks" });
|
||||
tree.focus();
|
||||
// The list starts collapsed.
|
||||
fireEvent.keyDown(tree, { key: "Home" });
|
||||
fireEvent.keyDown(tree, { key: "ArrowRight" });
|
||||
for (const [key, directory] of [
|
||||
["Home", undefined],
|
||||
["ArrowDown", "/p/documents"],
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { useMemo, useRef, type KeyboardEvent } from "react";
|
||||
import { useMemo, type KeyboardEvent } from "react";
|
||||
import clsx from "clsx";
|
||||
import { create } from "zustand";
|
||||
import { executeAction } from "@/platform/actions";
|
||||
import { dirname } from "@/platform/project";
|
||||
import { shortcutLabel } from "@/platform/keymap";
|
||||
@@ -19,15 +18,17 @@ import {
|
||||
showContextMenu,
|
||||
type MenuItem,
|
||||
} from "@/ui";
|
||||
import { basename, loadJson, openLocation, relPath, saveJson } from "../common/open";
|
||||
import { basename, openLocation, relPath, saveJson } from "../common/open";
|
||||
import { CodePreview } from "../find/parts";
|
||||
import {
|
||||
BOOKMARK_PREVIEW_KEY,
|
||||
bookmarkLabel,
|
||||
deleteList,
|
||||
moveBookmarks,
|
||||
setDefaultList,
|
||||
updateBookmarkData,
|
||||
useBookmarkPopups,
|
||||
useBookmarkTreeState,
|
||||
useBookmarks,
|
||||
type Bookmark,
|
||||
type BookmarkData,
|
||||
@@ -53,14 +54,7 @@ type Node =
|
||||
| { kind: "folder"; key: string; list: string; path: string; name: string; dir: string }
|
||||
| { kind: "mark"; key: string; bookmark: Bookmark };
|
||||
|
||||
const PREVIEW_KEY = "openidea.bookmarks.previewEnabled.v1";
|
||||
|
||||
/** The tree's selection and expansion, shared with the toolbar; Editor Preview is remembered. */
|
||||
const useBookmarksViewState = create<{
|
||||
selected: ReadonlySet<string>;
|
||||
collapsed: ReadonlySet<string>;
|
||||
showPreview: boolean;
|
||||
}>(() => ({ selected: new Set(), collapsed: new Set(), showPreview: loadJson(PREVIEW_KEY, false) }));
|
||||
const markKey = (b: Bookmark) => `b:${b.path}:${b.line}`;
|
||||
|
||||
/** The bookmark tree, its nodes by key, and the keys of the nodes that expand. */
|
||||
function useBookmarkNodes() {
|
||||
@@ -89,8 +83,6 @@ function useBookmarkNodes() {
|
||||
return { roots, byKey, allKeys };
|
||||
}
|
||||
|
||||
const markKey = (b: Bookmark) => `b:${b.path}:${b.line}`;
|
||||
|
||||
function location(path: string): { name: string; dir: string } {
|
||||
const rel = relPath(path);
|
||||
const name = basename(rel);
|
||||
@@ -228,12 +220,10 @@ export function BookmarksView() {
|
||||
const items = useBookmarks((s) => s.items);
|
||||
const paths = useBookmarks((s) => s.paths);
|
||||
const lists = useBookmarks((s) => s.lists);
|
||||
const { roots, byKey, allKeys } = useBookmarkNodes();
|
||||
const collapsed = useBookmarksViewState((s) => s.collapsed);
|
||||
const selected = useBookmarksViewState((s) => s.selected);
|
||||
const showPreview = useBookmarksViewState((s) => s.showPreview);
|
||||
const expanded = useMemo(() => new Set(allKeys.filter((k) => !collapsed.has(k))), [allKeys, collapsed]);
|
||||
const selection = useRef<Set<string>>(new Set());
|
||||
const { roots, byKey } = useBookmarkNodes();
|
||||
const expanded = useBookmarkTreeState((s) => s.expanded);
|
||||
const selected = useBookmarkTreeState((s) => s.selected);
|
||||
const showPreview = useBookmarkTreeState((s) => s.showPreview);
|
||||
const previewTarget = useMemo(() => {
|
||||
if (selected.size !== 1) return null;
|
||||
const node = byKey.get([...selected][0]!);
|
||||
@@ -265,7 +255,7 @@ export function BookmarksView() {
|
||||
const renameList = (l: BookmarkList) => useBookmarkPopups.setState({ naming: { listId: l.id } });
|
||||
|
||||
const onKeyDown = (e: KeyboardEvent) => {
|
||||
const nodes = [...selection.current].map((k) => byKey.get(k)).filter((n): n is Node => !!n);
|
||||
const nodes = [...useBookmarkTreeState.getState().selected].map((k) => byKey.get(k)).filter((n): n is Node => !!n);
|
||||
if (e.key === "F2" && !e.metaKey && !e.ctrlKey && !e.altKey && !e.shiftKey) {
|
||||
const only = nodes.length === 1 ? nodes[0]! : null;
|
||||
if (only?.kind === "mark") rename(only.bookmark);
|
||||
@@ -320,13 +310,8 @@ export function BookmarksView() {
|
||||
isExpandable={(n) => n.kind === "list" || (n.kind === "file" && n.children.length > 0)}
|
||||
expanded={expanded}
|
||||
selected={selected}
|
||||
onExpandedChange={(next) =>
|
||||
useBookmarksViewState.setState({ collapsed: new Set(allKeys.filter((k) => !next.has(k))) })
|
||||
}
|
||||
onSelectionChange={(next) => {
|
||||
selection.current = next;
|
||||
useBookmarksViewState.setState({ selected: new Set(next) });
|
||||
}}
|
||||
onExpandedChange={(next) => useBookmarkTreeState.setState({ expanded: new Set(next) })}
|
||||
onSelectionChange={(next) => useBookmarkTreeState.setState({ selected: new Set(next) })}
|
||||
onActivate={open}
|
||||
onContextMenu={(node, sel, e) => {
|
||||
const targets = sel.size
|
||||
@@ -452,8 +437,8 @@ export function BookmarksView() {
|
||||
|
||||
export function BookmarksActions() {
|
||||
const count = useBookmarks((s) => s.items.length + s.paths.length);
|
||||
const selected = useBookmarksViewState((s) => s.selected);
|
||||
const showPreview = useBookmarksViewState((s) => s.showPreview);
|
||||
const selected = useBookmarkTreeState((s) => s.selected);
|
||||
const showPreview = useBookmarkTreeState((s) => s.showPreview);
|
||||
const { byKey, allKeys } = useBookmarkNodes();
|
||||
const targets = [...selected].map((key) => byKey.get(key)).filter((n): n is Node => !!n);
|
||||
const only = targets.length === 1 ? targets[0] : null;
|
||||
@@ -479,13 +464,13 @@ export function BookmarksActions() {
|
||||
icon="expand-all"
|
||||
title="Expand All"
|
||||
size="small"
|
||||
onClick={() => useBookmarksViewState.setState({ collapsed: new Set() })}
|
||||
onClick={() => useBookmarkTreeState.setState({ expanded: new Set(allKeys) })}
|
||||
/>
|
||||
<IconButton
|
||||
icon="collapse-all"
|
||||
title="Collapse All"
|
||||
size="small"
|
||||
onClick={() => useBookmarksViewState.setState({ collapsed: new Set(allKeys) })}
|
||||
onClick={() => useBookmarkTreeState.setState({ expanded: new Set() })}
|
||||
/>
|
||||
<IconButton
|
||||
icon="editor-preview"
|
||||
@@ -494,8 +479,8 @@ export function BookmarksActions() {
|
||||
pressed={showPreview}
|
||||
onClick={() => {
|
||||
const enabled = !showPreview;
|
||||
useBookmarksViewState.setState({ showPreview: enabled });
|
||||
saveJson(PREVIEW_KEY, enabled);
|
||||
useBookmarkTreeState.setState({ showPreview: enabled });
|
||||
saveJson(BOOKMARK_PREVIEW_KEY, enabled);
|
||||
}}
|
||||
/>
|
||||
{count > 0 && (
|
||||
|
||||
@@ -44,6 +44,7 @@ export function isMnemonic(c: unknown): c is string {
|
||||
const KEY_PREFIX = "openidea.bookmarks.v1:";
|
||||
/** Lists and file / folder bookmarks; line bookmarks stay under `KEY_PREFIX` (readable by older versions). */
|
||||
const LISTS_PREFIX = "openidea.bookmarkLists.v1:";
|
||||
export const BOOKMARK_PREVIEW_KEY = "openidea.bookmarks.previewEnabled.v1";
|
||||
export const DEFAULT_LIST_ID = "default";
|
||||
|
||||
// --- pure helpers -----------------------------------------------------------
|
||||
@@ -340,6 +341,52 @@ const EMPTY: BookmarkData = {
|
||||
|
||||
export const useBookmarks = create<BookmarksState>(() => ({ scope: null, ...EMPTY }));
|
||||
|
||||
/** The tree's selection and expansion, shared with the toolbar; Editor Preview is remembered. */
|
||||
export const useBookmarkTreeState = create<{
|
||||
selected: ReadonlySet<string>;
|
||||
expanded: ReadonlySet<string>;
|
||||
showPreview: boolean;
|
||||
}>(() => ({
|
||||
selected: new Set(),
|
||||
expanded: new Set(),
|
||||
showPreview: loadJson(BOOKMARK_PREVIEW_KEY, false),
|
||||
}));
|
||||
|
||||
useBookmarks.subscribe((s, prev) => {
|
||||
if (s.scope !== prev.scope) {
|
||||
useBookmarkTreeState.setState({ selected: new Set(), expanded: new Set() });
|
||||
return;
|
||||
}
|
||||
const keys: string[] = [];
|
||||
if (s.items !== prev.items)
|
||||
for (const b of addedBookmarks(prev.items, s.items, (b) => `${b.path}:${b.line}`))
|
||||
keys.push(`l:${b.list}`, `f:${b.list}:${b.path}`);
|
||||
if (s.paths !== prev.paths)
|
||||
for (const p of addedBookmarks(prev.paths, s.paths, (b) => b.path)) keys.push(`l:${p.list}`);
|
||||
if (!keys.length) return;
|
||||
const { expanded } = useBookmarkTreeState.getState();
|
||||
if (keys.every((k) => expanded.has(k))) return;
|
||||
useBookmarkTreeState.setState({ expanded: new Set([...expanded, ...keys]) });
|
||||
});
|
||||
|
||||
function addedBookmarks<T extends { created: number }>(previous: T[], current: T[], key: (b: T) => string): T[] {
|
||||
const previousAt = new Map(previous.map((b) => [key(b), b.created]));
|
||||
const currentAt = new Map(current.map((b) => [key(b), b.created]));
|
||||
// A rename or line edit preserves the creation time. Match unchanged locations first since two bookmarks can
|
||||
// share a timestamp.
|
||||
const relocated = new Map<number, number>();
|
||||
for (const b of previous) {
|
||||
if (currentAt.get(key(b)) !== b.created) relocated.set(b.created, (relocated.get(b.created) ?? 0) + 1);
|
||||
}
|
||||
return current.filter((b) => {
|
||||
if (previousAt.get(key(b)) === b.created) return false;
|
||||
const remaining = relocated.get(b.created) ?? 0;
|
||||
if (remaining === 0) return true;
|
||||
relocated.set(b.created, remaining - 1);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
export function loadBookmarks(scope: string | null) {
|
||||
if (!scope) return useBookmarks.setState({ scope, ...EMPTY });
|
||||
const items = sanitize(loadJson<unknown>(KEY_PREFIX + scope, []));
|
||||
|
||||
Reference in new issue
Заблокувати користувача