CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
CI / docker (push) Successful in 1h28m38s
CI / go (Media) (push) Successful in 17m4s
CI / go (Server) (push) Successful in 37m31s
CI / go (Web-App) (push) Successful in 15m14s
CI / mobile (push) Failing after 2m42s
CI / frontend (push) Successful in 17m25s
Docs / build (push) Canceled after 0s
Docs / deploy (push) Canceled after 0s
CI / desktop (ubuntu-latest) (push) Failing after 3h14m9s
The list, gallery and timeline commits ran Prettier from Packages/editor, which wrapped lines at 80 columns and reformatted code those changes did not touch. Restore the files they did not mean to change, and keep only the intended edits in the others. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
5.7 KiB
TypeScript
174 lines
5.7 KiB
TypeScript
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// Small DOM helpers for the database block, which (like the rest of the
|
|
// editor's blocks) draws plain DOM so it also runs in the mobile bridge.
|
|
|
|
import { autoUpdate, computePosition, flip, offset, shift, type Placement } from "@floating-ui/dom";
|
|
import type { IconName } from "@docwell/ui/icons";
|
|
import { svgIcon } from "../suggestion";
|
|
|
|
type Child = Node | string | null | undefined | false;
|
|
type Props = Record<string, unknown> & {
|
|
class?: string;
|
|
on?: Partial<{ [K in keyof HTMLElementEventMap]: (e: HTMLElementEventMap[K]) => void }>;
|
|
dataset?: Record<string, string>;
|
|
style?: Partial<CSSStyleDeclaration>;
|
|
};
|
|
|
|
/** Makes an element: h("button", { class: "x", on: { click } }, "Text"). */
|
|
export function h<K extends keyof HTMLElementTagNameMap>(
|
|
tag: K,
|
|
props: Props = {},
|
|
...children: (Child | Child[])[]
|
|
): HTMLElementTagNameMap[K] {
|
|
const el = document.createElement(tag);
|
|
for (const [k, v] of Object.entries(props)) {
|
|
if (v === undefined || v === null || v === false) continue;
|
|
if (k === "class") el.className = String(v);
|
|
else if (k === "on") {
|
|
for (const [ev, fn] of Object.entries(v as Record<string, EventListener>)) el.addEventListener(ev, fn);
|
|
} else if (k === "dataset") Object.assign(el.dataset, v);
|
|
else if (k === "style") Object.assign(el.style, v);
|
|
else if (k in el && typeof v !== "string") (el as unknown as Record<string, unknown>)[k] = v;
|
|
else el.setAttribute(k, v === true ? "" : String(v));
|
|
}
|
|
for (const c of children.flat()) {
|
|
if (c === null || c === undefined || c === false) continue;
|
|
el.append(c);
|
|
}
|
|
return el;
|
|
}
|
|
|
|
export function icon(name: IconName, size = 16): SVGSVGElement {
|
|
return svgIcon(name, size);
|
|
}
|
|
|
|
/** A small button with an icon and, optionally, text; label is its accessible name. */
|
|
export function iconButton(name: IconName, label: string, onClick: (e: MouseEvent) => void, text?: string) {
|
|
const b = h(
|
|
"button",
|
|
{ type: "button", class: text ? "dw-db__btn" : "dw-db__btn dw-db__btn--icon", title: label, "aria-label": label },
|
|
icon(name, 15),
|
|
text ? h("span", {}, text) : null,
|
|
);
|
|
b.addEventListener("click", (e) => {
|
|
e.preventDefault();
|
|
onClick(e);
|
|
});
|
|
return b;
|
|
}
|
|
|
|
export interface Popover {
|
|
box: HTMLDivElement;
|
|
close: () => void;
|
|
}
|
|
|
|
let current: Popover | null = null;
|
|
|
|
/**
|
|
* A floating panel next to anchor (one at a time). Escape or a click outside
|
|
* closes it; onClose runs once. The panel's content is up to the caller.
|
|
*/
|
|
export function popover(
|
|
anchor: Element,
|
|
fill: (box: HTMLDivElement, close: () => void) => void,
|
|
opts: { placement?: Placement; class?: string; onClose?: () => void; label?: string } = {},
|
|
): Popover {
|
|
current?.close();
|
|
const box = h("div", { class: `dw-db-pop ${opts.class ?? ""}`, role: "dialog", "aria-label": opts.label });
|
|
let closed = false;
|
|
const close = () => {
|
|
if (closed) return;
|
|
closed = true;
|
|
stop();
|
|
box.remove();
|
|
document.removeEventListener("mousedown", onDown, true);
|
|
document.removeEventListener("keydown", onKey, true);
|
|
if (current?.box === box) current = null;
|
|
opts.onClose?.();
|
|
};
|
|
const onDown = (e: MouseEvent) => {
|
|
const t = e.target as Node;
|
|
// Menus opened from this panel (block menus, other popovers) are siblings in body.
|
|
if (box.contains(t) || anchor.contains(t) || (t instanceof Element && t.closest(".dw-block-menu"))) return;
|
|
close();
|
|
};
|
|
const onKey = (e: KeyboardEvent) => {
|
|
if (e.key === "Escape" && !document.querySelector(".dw-block-menu")) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
close();
|
|
if (anchor instanceof HTMLElement) anchor.focus();
|
|
}
|
|
};
|
|
document.body.append(box);
|
|
const stop = autoUpdate(anchor, box, () => {
|
|
void computePosition(anchor, box, {
|
|
placement: opts.placement ?? "bottom-start",
|
|
strategy: "fixed",
|
|
middleware: [offset(4), flip(), shift({ padding: 8 })],
|
|
}).then(({ x, y }) => Object.assign(box.style, { left: `${x}px`, top: `${y}px` }));
|
|
});
|
|
document.addEventListener("mousedown", onDown, true);
|
|
document.addEventListener("keydown", onKey, true);
|
|
fill(box, close);
|
|
current = { box, close };
|
|
return current;
|
|
}
|
|
|
|
/** Closes the open popover, if any. */
|
|
export function closePopover() {
|
|
current?.close();
|
|
}
|
|
|
|
/** A text input that commits on Enter or blur and cancels on Escape. */
|
|
export function textInput(
|
|
value: string,
|
|
opts: { placeholder?: string; label?: string; class?: string; type?: string; onCommit: (v: string) => void; onCancel?: () => void },
|
|
): HTMLInputElement {
|
|
const input = h("input", {
|
|
type: opts.type ?? "text",
|
|
class: opts.class ?? "dw-db-input",
|
|
placeholder: opts.placeholder,
|
|
"aria-label": opts.label ?? opts.placeholder,
|
|
});
|
|
input.value = value;
|
|
let done = false;
|
|
const commit = () => {
|
|
if (done) return;
|
|
done = true;
|
|
opts.onCommit(input.value);
|
|
};
|
|
input.addEventListener("keydown", (e) => {
|
|
e.stopPropagation();
|
|
if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
commit();
|
|
} else if (e.key === "Escape") {
|
|
done = true;
|
|
opts.onCancel?.();
|
|
}
|
|
});
|
|
input.addEventListener("blur", commit);
|
|
return input;
|
|
}
|
|
|
|
/** Offers a file for download. */
|
|
export function download(name: string, text: string, mime: string) {
|
|
const url = URL.createObjectURL(new Blob([text], { type: mime }));
|
|
const a = h("a", { href: url, download: name });
|
|
document.body.append(a);
|
|
a.click();
|
|
a.remove();
|
|
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
}
|
|
|
|
/** Opens the system file picker for one file. */
|
|
export function pickFile(accept: string): Promise<File | null> {
|
|
return new Promise((resolve) => {
|
|
const input = h("input", { type: "file", accept });
|
|
input.onchange = () => resolve(input.files?.[0] ?? null);
|
|
input.click();
|
|
});
|
|
}
|