Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions sideboard/src/sideboard/document-list-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,31 @@ export function DocumentListPanel(props: {
// Gated on folderReady: the walk recursively finds folder docs across the
// tree, so kicking it off before the root has loaded would flood the repo and
// keep the whole widget blank. Let the top level paint first, then reveal.
createEffect(() => {
if (!folderReady()) return;
//
// A filter is a temporary view (see folder-open.ts), so nothing is revealed
// into the tree while one is up. Clearing it reveals whatever is selected
// then, mounted or not: a row you clicked in the results was mounted only
// by the search, and goes away with it unless its folders open.
createEffect((wasFiltering: boolean) => {
const filtering = !!filter();
if (!folderReady() || filtering) return filtering || wasFiltering;
const selected = selectedDocUrls();
if (!selected.length) {
setAutoExpandedFolders(new Set());
return;
return false;
}
if (!wasFiltering && selected.every((url) => isMounted(props.element, url))) {
return false;
}
if (selected.every((url) => isMounted(props.element, url))) return;
let cancelled = false;
foldersToReveal(props.repo, props.folderUrl, selected).then((folders) => {
if (!cancelled) setAutoExpandedFolders(folders);
});
onCleanup(() => {
cancelled = true;
});
});
return false;
}, false);

function open(detail: OpenDocumentEventDetail) {
props.element.dispatchEvent(createOpenEvent(detail));
Expand Down
72 changes: 72 additions & 0 deletions sideboard/src/sideboard/document-list/folder-open.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { createRoot, createSignal } from "solid-js";
import { describe, expect, it } from "vitest";
import { createFolderOpenState } from "./folder-open.ts";

// Effects in a root run synchronously when signals are set outside a batch,
// so every assertion below sees the settled state.
function setup(expandedBefore: boolean) {
return createRoot((dispose) => {
const [filter, setFilter] = createSignal("");
const state = createFolderOpenState(filter);
if (expandedBefore) state.toggle();
return { state, setFilter, dispose };
});
}

describe("createFolderOpenState", () => {
it("opens every folder for a search", () => {
for (const before of [false, true]) {
const { state, setFilter, dispose } = setup(before);
setFilter("notes");
expect(state.open()).toBe(true);
dispose();
}
});

// The whole claim: for each starting state, and for whatever is done in the
// search view (nothing, toggle, toggle twice, expand), clearing the filter
// gives back the starting state.
it("returns to the state before the search, whatever happened in it", () => {
const actions: Record<string, (s: ReturnType<typeof setup>["state"]) => void> = {
nothing: () => {},
toggle: (s) => s.toggle(),
"toggle twice": (s) => (s.toggle(), s.toggle()),
"toggle, expand": (s) => (s.toggle(), s.expand()),
expand: (s) => s.expand(),
};
for (const before of [false, true]) {
for (const [name, act] of Object.entries(actions)) {
const { state, setFilter, dispose } = setup(before);
setFilter("a");
act(state);
setFilter("ab");
setFilter("");
expect(state.open(), `${before} / ${name}`).toBe(before);
expect(state.expanded(), `${before} / ${name}`).toBe(before);
dispose();
}
}
});

it("toggles only the search view while searching", () => {
const { state, setFilter, dispose } = setup(false);
setFilter("a");
state.toggle();
expect(state.open()).toBe(false);
setFilter("ab"); // still the same search view
expect(state.open()).toBe(false);
setFilter("");
setFilter("a"); // a fresh search starts open again
expect(state.open()).toBe(true);
dispose();
});

it("lets a reveal open the tree itself, even mid-search", () => {
const { state, setFilter, dispose } = setup(false);
setFilter("a");
state.reveal();
setFilter("");
expect(state.open()).toBe(true);
dispose();
});
});
45 changes: 45 additions & 0 deletions sideboard/src/sideboard/document-list/folder-open.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { createEffect, createSignal } from "solid-js";

/**
* Whether a folder row is open, in two layers.
*
* `expanded` is the tree as you left it: your clicks, the hold-to-open on a
* drag, the reveal of a selection. A filter puts a temporary view over it in
* which every folder starts open (so matches inside show) and a toggle only
* closes or reopens it in that view. Clearing the filter drops the view, and
* the tree is exactly as it was before the search: nothing a search did, or
* that you did during one, leaks into `expanded`.
*/
export function createFolderOpenState(filter: () => string) {
const [expanded, setExpanded] = createSignal(false);
const [closedInSearch, setClosedInSearch] = createSignal(false);

// A fresh search starts with everything open again.
createEffect(() => {
if (!filter()) setClosedInSearch(false);
});

const searching = () => !!filter();
const open = () => (searching() ? !closedInSearch() : expanded());

return {
/** what the row shows now */
open,
/** the tree's own state, under any search view */
expanded,
/** open it in whichever layer is showing */
expand() {
if (searching()) setClosedInSearch(false);
else setExpanded(true);
},
/** flip it in whichever layer is showing */
toggle() {
if (searching()) setClosedInSearch((closed) => !closed);
else setExpanded((yn) => !yn);
},
/** open it in the tree itself, search or not (reveal, create-inside) */
reveal() {
setExpanded(true);
},
};
}
54 changes: 18 additions & 36 deletions sideboard/src/sideboard/document-list/folder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@ import type {
PatchworkViewElement,
} from "@inkandswitch/patchwork-elements";
import type { FolderDoc } from "@inkandswitch/patchwork-filesystem";
import {
createEffect,
createSignal,
onCleanup,
Show,
Suspense,
} from "solid-js";
import {
filterMatches,
setRenaming,
autoExpandedFolders,
} from "../state.ts";
import { createEffect, onCleanup, Show, Suspense } from "solid-js";
import { setRenaming, autoExpandedFolders } from "../state.ts";
import { DocumentList } from "./document-list.tsx";
import { watchFolder } from "./auto-expand.ts";
import { createFolderOpenState } from "./folder-open.ts";
import Item from "./item.tsx";
import { ItemName } from "./name.tsx";
import { LoadingRow } from "./loading-row.tsx";
Expand Down Expand Up @@ -58,7 +49,9 @@ export default function Folder(props: {
filter: string;
clearFilter(): void;
}) {
const [expanded, setExpanded] = createSignal(false);
// A filter is a temporary view over the tree; see folder-open.ts.
const folderOpen = createFolderOpenState(() => props.filter);
const { open: isOpen, expand, toggle, reveal } = folderOpen;

const [folder, handle] = useDocument<FolderDoc>(() => props.url, props);
createEffect(() => {
Expand All @@ -73,29 +66,18 @@ export default function Folder(props: {
const depthStyle = () => ({ "--depth": depth() + 1 });
const folderDepthStyle = () => ({ "--depth": depth() });

createEffect((last) => {
if (
!last &&
props.filter &&
filterMatches(props.filter, folder()!?.title ?? props.name)
) {
setExpanded(true);
}
return props.filter;
});

// Auto-expand when this folder lies on the path to a selected doc. The set is
// computed from folder data by the panel (see auto-expand.ts), so it reveals
// deeply nested selections without us having to eagerly mount the subtree:
// as each ancestor expands it mounts the next level, which re-reads the set.
createEffect(() => {
if (autoExpandedFolders().has(props.url)) setExpanded(true);
if (autoExpandedFolders().has(props.url)) reveal();
});

// Hold a drag over this folder's row for a second and it opens, so you can
// drop inside it. It only ever opens: closing a folder you're aiming into
// would take the target away. Re-arms when the drag leaves.
const dragOpen = createHoverTimer(1000, () => setExpanded(true));
const dragOpen = createHoverTimer(1000, expand);
onCleanup(dragOpen.stop);

// Rename shows in two places: the folder's own title (what the row shows)
Expand All @@ -118,7 +100,7 @@ export default function Folder(props: {
folder.docs.unshift(freshy);
});
props.clearFilter();
setExpanded(true);
reveal();
props.open(freshy);
setRenaming(h.url + "/0");
}
Expand Down Expand Up @@ -152,7 +134,7 @@ export default function Folder(props: {
)
);
}
if (overRow?.getAttribute("data-dnd-item") === props.url && !expanded()) {
if (overRow?.getAttribute("data-dnd-item") === props.url && !isOpen()) {
dragOpen.start();
}
}}
Expand All @@ -171,7 +153,7 @@ export default function Folder(props: {
repo: props.repo,
rootFolderHandle: props.rootFolderHandle,
element: props.element,
expand: () => setExpanded(true),
expand,
});
}}
>
Expand All @@ -191,9 +173,9 @@ export default function Folder(props: {
rootFolderHandle={props.rootFolderHandle}
parentFolderHandle={props.parentFolderHandle}
itemIndex={props.itemIndex}
isExpanded={expanded()}
onToggleExpand={() => setExpanded((yn) => !yn)}
expand={() => setExpanded(true)}
isExpanded={isOpen()}
onToggleExpand={toggle}
expand={expand}
createInside={createInside}
openWith={(toolId) => {
props.open({
Expand All @@ -212,10 +194,10 @@ export default function Folder(props: {
class="document-list-folder__toggle"
on:click={(event: MouseEvent) => {
event.stopImmediatePropagation();
setExpanded((yn) => !yn);
toggle();
}}
>
<Show when={expanded()} fallback={<Chevron />}>
<Show when={isOpen()} fallback={<Chevron />}>
<Chevron style={{ rotate: "90deg" }} />
</Show>
</span>
Expand All @@ -229,7 +211,7 @@ export default function Folder(props: {
<div
role="group"
class="document-list-folder__contents"
classList={{ "document-list-folder__contents--hidden": !expanded() && !props.filter }}
classList={{ "document-list-folder__contents--hidden": !isOpen() }}
data-depth={depth()}
style={depthStyle()}
>
Expand All @@ -238,7 +220,7 @@ export default function Folder(props: {
find() its entire subtree on mount, loading the whole tree at once
and making the folder appear to suspend on its descendants rather
than just its own handle. */}
<Show when={expanded() || !!props.filter}>
<Show when={folderOpen.expanded() || !!props.filter}>
<Suspense fallback={<LoadingRow depth={depth() + 1} />}>
<DocumentList
docs={folder()?.docs}
Expand Down
Loading