~ knotless Search/
↑↓ move↵ open⇧↵ all results

grain.social/grain

photography-focused photo sharing platform

grain.social
1 collaborator
0
2

next: Create opens its form where you are, and a form opens once

Merged chadtmiller.com asked to merge into feature/hud from grain.social/grain:next/create

Only what this pull adds. It is built on The next grain.social and grain-ios, drawn by HUD.

vendor/hud/web/renderer/src/tree/compose/PlaceField.svelte+47−7
@@ -6,15 +6,18 @@
import { Lookup } from './lookup.svelte';
// A `Compose` form's place: a search (the `find_place` tool, a while
// after the typing stops) whose places list under it, the arrow keys
// going through them, Enter or a click picking one; picked, its name
// and where it is, with a × that clears it. With `from`, a photos
// after the typing stops) whose places drop down over the form, the
// arrow keys going through them, Enter or a click picking one; picked,
// its name and where it is, with a × that clears it. The places float
// in the top layer (a popover) anchored to the search, so the form's
// scrolling and its buttons never cut them off: under it, or over it
// where the window has more room above. With `from`, a photos
// field, its first photo that says where it was taken fills it while
// none is picked, as Grain suggests a photo's place: `find_place` at
// that point, once for each photo, so one cleared stays clear. `chip`
// (over a photo's review, a story's) draws it as a pill: picked, its
// name and ×; else Add <its label>, which opens the search, its places
// listed over it.
// listed over it where they fit.
let {
field,
values,
@@ -65,6 +68,37 @@
}
const pin = ['M20 10c0 4.99-5.54 10.19-7.4 11.8a1 1 0 0 1-1.2 0C9.54 20.19 4 14.99 4 10a8 8 0 0 1 16 0', 'M9 10a3 3 0 1 0 6 0a3 3 0 1 0-6 0'];
/** The places shown over everything, against the search: under it, or over it (a chip's first) where there is more room there; placed again as they change, and as the window scrolls or resizes. */
function float(list: HTMLElement) {
void places.items.length;
const at = () => {
if (!box) return;
const r = box.getBoundingClientRect();
const gap = 4;
const edge = 8;
const most = 16 * parseFloat(getComputedStyle(document.documentElement).fontSize);
const below = innerHeight - r.bottom - gap - edge;
const above = r.top - gap - edge;
const want = Math.min(list.scrollHeight, most);
const up = chip ? !(above < want && below > above) : below < want && above > below;
list.style.left = `${r.left}px`;
list.style.width = `${r.width}px`;
list.style.maxHeight = `${Math.min(most, up ? above : below)}px`;
list.style.top = up ? 'auto' : `${r.bottom + gap}px`;
list.style.bottom = up ? `${innerHeight - r.top + gap}px` : 'auto';
};
if (!list.matches(':popover-open')) list.showPopover();
at();
addEventListener('scroll', at, true);
addEventListener('resize', at);
visualViewport?.addEventListener('resize', at);
return () => {
removeEventListener('scroll', at, true);
removeEventListener('resize', at);
visualViewport?.removeEventListener('resize', at);
};
}
</script>
{#snippet icon(paths: readonly string[])}
@@ -119,7 +153,7 @@
}}
/>
{#if places.open}
<ul class="places" id="{id}-places" role="listbox" aria-label="Places">
<ul class="places" id="{id}-places" role="listbox" aria-label="Places" popover="manual" {@attach float}>
{#each places.items as place, i (i)}
<li
id="{id}-place-{i}"
@@ -244,14 +278,21 @@
color: var(--color-text-primary);
}
/* In the top layer (a popover), placed against the search (`float`). */
.places {
position: fixed;
inset: auto;
box-sizing: border-box;
max-height: 16rem;
margin: var(--hud-space-1) 0 0;
margin: 0;
padding: var(--hud-space-1);
overflow-y: auto;
border: var(--border-width-regular) solid var(--color-border-primary);
border-radius: var(--border-radius-md);
background: var(--color-background-primary);
color: var(--color-text-primary);
box-shadow: var(--shadow-lg);
font: inherit;
list-style: none;
}
@@ -320,7 +361,6 @@
width: min(20rem, 100%);
}
.chip .places,
.chip .note {
box-sizing: border-box;
width: min(20rem, 100%);
vendor/hud/web/renderer/src/tree/Compose.svelte+26−18
@@ -45,7 +45,9 @@
// no form that needs one. With `open` (the screen was opened at it), the
// form opens by itself once the tree is first drawn in the browser, once
// in that tree, so a server's drawing is the browser's first; a photo
// review's field then goes to the camera where it can (`shoot`).
// review's field then goes to the camera where it can (`shoot`). Drawn
// as its tree's form alone (`Tree`'s `form`), it has no button, and
// tells the tree as its form closes, sent (and read again) or not.
let { node }: { node: UiNode } = $props();
const tree = getTreeContext();
@@ -151,12 +153,14 @@
open = false;
error = null;
const reload = tree.onreload;
if (!reload) return;
try {
await reload();
} catch (failure) {
error = `Sent, but it could not be read again: ${failure instanceof Error ? failure.message : String(failure)}`;
if (reload) {
try {
await reload();
} catch (failure) {
error = `Sent, but it could not be read again: ${failure instanceof Error ? failure.message : String(failure)}`;
}
}
tree.form?.();
}
</script>
@@ -167,18 +171,21 @@
{#if offered}
<span class="compose">
<ActionButton
{icon}
text={p.label}
shown={count}
filled={p.style === 'button'}
aria-haspopup="dialog"
aria-expanded={open}
aria-label={name}
title={name}
onclick={() => show(false)}
/>
{#if error}<span class="error" role="alert">{error}</span>{/if}
<!-- Its form alone (`Tree`'s `form`): no button, the form opened over what the app shows. -->
{#if !tree.form}
<ActionButton
{icon}
text={p.label}
shown={count}
filled={p.style === 'button'}
aria-haspopup="dialog"
aria-expanded={open}
aria-label={name}
title={name}
onclick={() => show(false)}
/>
{#if error}<span class="error" role="alert">{error}</span>{/if}
{/if}
{#if open}
<Composer
title={p.title ?? p.label}
@@ -193,6 +200,7 @@
onclose={() => {
open = false;
confirming = null;
tree.form?.();
}}
>
{#each fields as field (field.name)}
vendor/hud/web/renderer/src/tree/opened.tsnew+36−0
@@ -0,0 +1,36 @@
// The form a screen was opened at (an `open` query's `compose`): the one
// `Compose` HUD marked `open` (hud_core::ui::open_compose), found as HUD
// marks it, through each node's children (`k`) in order, never a Menu's
// rows. An app opening that form over the screen it is on draws its tree
// with the form alone (`Tree`'s `form`); one keeping a screen's tree to
// draw again (a page's memory of its screens) keeps it unmarked
// (`unopened`), so drawn again it opens nothing by itself.
import type { UiNode, UiTree } from './types';
/** The `Compose` marked `open` in a tree; null where none is. */
export function formOf(node: UiNode): UiNode | null {
if (node.c === 'Compose') return node.p?.open === true ? node : null;
if (node.c === 'Menu') return null;
for (const child of node.k ?? []) {
const found = formOf(child);
if (found) return found;
}
return null;
}
/** A tree as it is but for the `Compose` marked `open`, unmarked: the same tree where none is. */
export function unopened(ui: UiTree): UiTree {
const unmark = (node: UiNode): UiNode => {
if (node.c === 'Compose') {
if (node.p?.open !== true) return node;
const { open: _, ...p } = node.p;
return { ...node, p };
}
if (node.c === 'Menu' || !node.k) return node;
const k = node.k.map(unmark);
return k.some((child, i) => child !== node.k![i]) ? { ...node, k } : node;
};
const root = unmark(ui.root);
return root === ui.root ? ui : { ...ui, root };
}
vendor/hud/web/renderer/src/tree/Tree.svelte+18−2
@@ -8,6 +8,7 @@
import { barOf } from './bar';
import { setTreeContext } from './context';
import Node from './Node.svelte';
import { formOf } from './opened';
import type { ActionHandler, UiBarPlace, UiCall, UiCallHandler, UiEndHandler, UiHosted, UiInteractionHandler, UiPaging, UiReplaceHandler, UiReport, UiShown, UiToolHandler, UiTree, UiUploadHandler } from './types';
// A view the server resolved against its data (`_meta.ui`), drawn with
@@ -37,6 +38,12 @@
// each picture's sizes is made from it (../slot.ts), a grid's tiles
// sharing it, so a page loads the size of a photo that fills its box;
// the app preloads the hero at the same (`heroPreload`).
// With `form`, it draws the form its screen was opened at alone (an
// `open` query's `compose`, the `Compose` marked `open`): no button,
// the form open at once over whatever the app shows, its writes and
// `onreload` as the screen's; `form` is told as it closes, sent or
// not. An app opens its primary action's form so, over the screen it
// is on (the frame's `action`, docs/views.md).
// An app with a top bar of its own draws the tree's `Bar` there: the tree
// less its Bar (`bar: 'apart'`) where the screen is, and the same tree,
// the same props, with its Bar alone (`bar: 'only'`) in the top bar.
@@ -66,7 +73,8 @@
pending = false,
now,
hrefOf,
area = SCREEN_WIDE
area = SCREEN_WIDE,
form
}: {
ui: UiTree;
oncall?: UiCallHandler;
@@ -107,6 +115,8 @@
hrefOf?: (call: UiCall) => string | null;
/** How wide the host draws the tree's main area, as an `<img>`'s sizes says (`min(1200px, 100vw - 245px)`); without it, the screen's width. */
area?: string;
/** Draw only the form the tree's screen was opened at, opened at once; told as it closes, sent or not. */
form?: () => void;
} = $props();
// Mounted, the page's dates and counts are the browser's (../view/clock.svelte.ts).
@@ -221,11 +231,17 @@
},
get hrefOf() {
return hrefOf;
},
get form() {
return form;
}
});
</script>
{#if bar !== 'only'}
{#if form}
{@const opened = formOf(ui.root)}
{#if opened}<Node node={opened} />{/if}
{:else if bar !== 'only'}
<Node node={ui.root} />
{:else if lifted}
<Node node={lifted} />
vendor/hud/web/renderer/src/tree/types.ts+2−0
@@ -528,4 +528,6 @@ export type TreeContext = {
readonly now?: number;
/** The address of the page a call draws, where the app has one: what opens the call is drawn as a link to it. */
readonly hrefOf?: (call: UiCall) => string | null;
/** The tree drawn as the form its screen was opened at alone (`Tree`'s `form`): told as that form closes, sent or not. */
readonly form?: () => void;
};
vendor/hud/web/renderer/src/index.ts+1−0
@@ -16,6 +16,7 @@ export { default as Tree } from './tree/Tree.svelte';
export { default as Skeleton } from './tree/Skeleton.svelte';
export { recallSkeleton, rememberSkeleton, screenKey, skeletonOf, SKELETON_DELAY } from './tree/skeleton';
export { barOf } from './tree/bar';
export { formOf, unopened } from './tree/opened';
export { heroPreload } from './hero';
export { formatDateTime } from './format';
export type * from './tree/types';
vendor/hud/web/shell/src/frame.ts+1−1
@@ -30,7 +30,7 @@ export type AccountItem = HostItem | CallItem;
export type Frame = {
/** The destinations: the app's tabs. */
destinations?: 'tabs';
/** The app's one primary action: a call's screen opened on the tab on show (a query's `compose` opening its form at once). */
/** The app's one primary action: a call whose screen opens at a form (a query's `compose`), that form alone opened over the screen on show; else its screen, opened on the tab on show. */
action?: { label: string; icon: IconName; call: Call };
account?: {
/** The account's picture, on the tab `tab` in place of its icon: a query's answer (`call`), the picture's URL at `at` in it. */
vendor/hud/web/shell/src/Nav.svelte+5−1
@@ -35,6 +35,7 @@
onsearch,
onitem,
onaction,
onactionahead,
onsignout
}: {
wordmark: string;
@@ -63,7 +64,10 @@
onsearch: () => void;
/** An account item's link clicked: the app opens its screen, unless the browser opens it elsewhere. */
onitem: (event: MouseEvent, item: CallItem) => void;
/** The action pressed: its form opened over the screen on show. */
onaction: () => void;
/** A pointer over the action, or pressing it: its call read ahead. */
onactionahead: () => void;
onsignout: () => void;
} = $props();
@@ -129,7 +133,7 @@
</div>
{#if action}
<button type="button" class="action" onclick={onaction}>
<button type="button" class="action" onclick={onaction} onpointerenter={onactionahead} onpointerdown={onactionahead}>
<span class="icon">{@render icon(action.icon)}</span>
<span class="label">{action.label}</span>
</button>
vendor/hud/web/shell/src/routes.test.tsnew+33−0
@@ -0,0 +1,33 @@
// `npm run unit` in web/ (`just check` runs it).
import { strict as assert } from 'node:assert';
import { test } from 'node:test';
import { Names, pathOf, plain, recordAt, type Route } from './routes.ts';
const FOR_YOU = { name: 'query', args: { method: 'social.grain.feed.getFeed', params: { feed: 'at://did:web:grain.social/social.grain.feed.generator/foryou' } } };
const ROUTES: Route[] = [
{ path: '/feeds/for-you', call: FOR_YOU },
{ path: '/profile/{actor}/gallery/{rkey}', call: { name: 'query', args: { method: 'social.grain.gallery.getGallery', params: { gallery: 'at://{actor}/social.grain.gallery/{rkey}' } } } },
{ path: '/profile/{actor}/story/{rkey}', call: { name: 'query', args: { method: 'social.grain.story.getArchive', params: { actor: '{actor:did}' }, at: 'at://{actor:did}/social.grain.story/{rkey}' } } },
{ path: '/profile/{actor}', call: { name: 'describe_app', args: { domain: 'grain.social', actor: '{actor}' } } }
];
test("a call less its compose is the screen it opens, at the screen's URL", () => {
const opening = { ...FOR_YOU, args: { ...FOR_YOU.args, compose: 'New gallery' } };
assert.deepEqual(plain(opening), FOR_YOU);
assert.equal(pathOf(opening, ROUTES, new Names()), '/feeds/for-you');
assert.equal(plain(FOR_YOU), FOR_YOU, 'a call with none is itself');
});
test("a record's screen is its route's, by the record its call names", () => {
const names = new Names([['did:plc:me', 'me.test']]);
const gallery = recordAt('at://did:plc:me/social.grain.gallery/3k2a', ROUTES);
assert.deepEqual(gallery, { call: { name: 'query', args: { method: 'social.grain.gallery.getGallery', params: { gallery: 'at://did:plc:me/social.grain.gallery/3k2a' } } }, tab: undefined });
assert.equal(pathOf(gallery!.call, ROUTES, names), '/profile/me.test/gallery/3k2a');
const story = recordAt('at://did:plc:me/social.grain.story/3k2b', ROUTES);
assert.deepEqual(story?.call.args, { method: 'social.grain.story.getArchive', params: { actor: 'did:plc:me' }, at: 'at://did:plc:me/social.grain.story/3k2b' });
});
test('a record no route names has no screen: an account alone is no record, nor a fixed one another', () => {
assert.equal(recordAt('at://did:plc:me/social.grain.photo/3k2c', ROUTES), null);
assert.equal(recordAt('at://did:web:grain.social/social.grain.feed.generator/recent', ROUTES), null);
});
vendor/hud/web/shell/src/routes.ts+34−0
@@ -37,6 +37,19 @@ export function same(a: Call, b: Call): boolean {
return JSON.stringify(a) === JSON.stringify(b);
}
/**
* A call as the screen it opens: less its `compose`, which asks the screen
* to open its form once, as the call opens it. It is no part of where the
* app is: the stacks, history entries, URLs and the memory of screens hold
* a call so, and a screen gone back to, loaded again or shown again opens
* no form.
*/
export function plain(call: Call): Call {
if (call.args.compose === undefined) return call;
const { compose: _, ...args } = call.args;
return { ...call, args };
}
/** Bind a token, or check it is bound to the same. */
function bind(bound: Bound, name: string, value: string): boolean {
if (name in bound) return bound[name] === value;
@@ -204,3 +217,24 @@ function matchPath(pattern: string, pathname: string, bound: Bound): boolean {
return true;
});
}
/** The strings of a call's pattern naming a record (`at://…`). */
function uris(pattern: unknown, into: string[] = []): string[] {
if (typeof pattern === 'string' && pattern.startsWith('at://')) into.push(pattern);
if (Array.isArray(pattern)) for (const p of pattern) uris(p, into);
if (isObject(pattern)) for (const p of Object.values(pattern)) uris(p, into);
return into;
}
/** The screen of a record, by its at:// URI: the first route whose call names a record as it does (`at://{actor}/social.grain.gallery/{rkey}`), every token of its path bound, and the tab it opens under; null where none does. */
export function recordAt(uri: string, routes: Route[]): { call: Call; tab?: string } | null {
for (const route of routes) {
const { path } = parts(route);
for (const pattern of uris(route.call)) {
const bound: Bound = {};
if (!matchText(pattern, uri, bound)) continue;
if ([...path.matchAll(TOKEN)].every((token) => token[1] in bound)) return { call: fill(route.call, bound) as Call, tab: route.tab };
}
}
return null;
}
vendor/hud/web/shell/src/Shell.svelte+98−13
vendor/hud/web/shell/src/Stack.svelte+42−28
vendor/hud/COMMIT+1−1