Package Anidev Skill and standalone API panel integration
This commit is contained in:
1 parent
8e4e8e6ef0
commit
61b05abe44
43 files changed
+1859
-57
No files matched your search
@@ -0,0 +1,40 @@
|
||||
/** Bind one .copy-input; never caches the input value. */
|
||||
export function mountCopyField(root, { notify, t = (ru) => ru } = {}) {
|
||||
const input = root.querySelector("input"),
|
||||
button = root.querySelector("[data-copy-input]");
|
||||
if (!input || !button)
|
||||
throw new TypeError("Copy field needs an input and copy button");
|
||||
const abort = new AbortController();
|
||||
let disposed = false,
|
||||
pending = false;
|
||||
button.addEventListener(
|
||||
"click",
|
||||
async () => {
|
||||
if (pending) return;
|
||||
pending = true;
|
||||
try {
|
||||
await navigator.clipboard.writeText(input.value);
|
||||
if (!disposed)
|
||||
notify?.(t("Значение поля скопировано.", "Field value copied."), {
|
||||
type: "success",
|
||||
});
|
||||
} catch {
|
||||
if (!disposed)
|
||||
notify?.(
|
||||
t(
|
||||
"Не удалось скопировать значение.",
|
||||
"Could not copy the field value.",
|
||||
),
|
||||
{ type: "error" },
|
||||
);
|
||||
} finally {
|
||||
pending = false;
|
||||
}
|
||||
},
|
||||
{ signal: abort.signal },
|
||||
);
|
||||
return () => {
|
||||
disposed = true;
|
||||
abort.abort();
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
/** One document-wide Dock shell. The host owns routes and active nav items. */
|
||||
export function mountDock({
|
||||
navigation,
|
||||
main,
|
||||
trigger,
|
||||
scrim,
|
||||
t = (ru) => ru,
|
||||
onOpenChange = () => {},
|
||||
}) {
|
||||
if (
|
||||
![navigation, main, trigger, scrim].every((e) => e instanceof HTMLElement)
|
||||
)
|
||||
throw new TypeError(
|
||||
"Dock needs navigation, main, trigger and scrim elements",
|
||||
);
|
||||
const abort = new AbortController(),
|
||||
{ signal } = abort;
|
||||
const initialInert = main.inert;
|
||||
let open = false,
|
||||
pinned = false,
|
||||
timer,
|
||||
disposed = false;
|
||||
const focusable = () =>
|
||||
[
|
||||
...navigation.querySelectorAll(
|
||||
'button:not(:disabled),a[href],input:not(:disabled),[tabindex="0"]',
|
||||
),
|
||||
].filter(
|
||||
(e) => e.getClientRects().length && !e.closest("[inert],[hidden]"),
|
||||
);
|
||||
function setOpen(next, restore = false) {
|
||||
if (disposed || open === next) return;
|
||||
clearTimeout(timer);
|
||||
open = next;
|
||||
document.body.classList.toggle("dock-open", open);
|
||||
trigger.setAttribute("aria-expanded", String(open));
|
||||
trigger.setAttribute(
|
||||
"aria-label",
|
||||
open
|
||||
? t("Свернуть меню", "Collapse navigation")
|
||||
: t("Развернуть меню", "Expand navigation"),
|
||||
);
|
||||
main.inert = open || initialInert;
|
||||
if (open) {
|
||||
document
|
||||
.querySelectorAll('[popover="auto"]:popover-open')
|
||||
.forEach((e) => e.hidePopover());
|
||||
navigation.setAttribute("role", "dialog");
|
||||
navigation.setAttribute("aria-modal", "true");
|
||||
} else {
|
||||
pinned = false;
|
||||
navigation.removeAttribute("role");
|
||||
navigation.removeAttribute("aria-modal");
|
||||
if (restore || navigation.contains(document.activeElement))
|
||||
trigger.focus({ preventScroll: true });
|
||||
}
|
||||
navigation.dispatchEvent(
|
||||
new CustomEvent("anidev:dockchange", { detail: { open } }),
|
||||
);
|
||||
onOpenChange(open);
|
||||
}
|
||||
trigger.setAttribute("aria-expanded", "false");
|
||||
trigger.addEventListener(
|
||||
"click",
|
||||
() => {
|
||||
if (open && pinned) setOpen(false, true);
|
||||
else {
|
||||
setOpen(true);
|
||||
pinned = true;
|
||||
}
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
navigation.addEventListener(
|
||||
"pointerenter",
|
||||
(e) => {
|
||||
clearTimeout(timer);
|
||||
if (
|
||||
e.pointerType === "mouse" &&
|
||||
matchMedia("(hover:hover) and (pointer:fine)").matches
|
||||
)
|
||||
setOpen(true);
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
navigation.addEventListener(
|
||||
"pointerleave",
|
||||
() => {
|
||||
if (!pinned && !navigation.contains(document.activeElement))
|
||||
timer = setTimeout(() => setOpen(false), 180);
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
navigation.addEventListener(
|
||||
"focusout",
|
||||
(e) => {
|
||||
if (
|
||||
!pinned &&
|
||||
!navigation.contains(e.relatedTarget) &&
|
||||
!navigation.matches(":hover")
|
||||
)
|
||||
timer = setTimeout(() => setOpen(false), 180);
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
scrim.addEventListener("click", () => setOpen(false, true), { signal });
|
||||
document.addEventListener(
|
||||
"keydown",
|
||||
(e) => {
|
||||
if (!open || e.defaultPrevented) return;
|
||||
if (e.key === "Escape") {
|
||||
const popup = document.querySelector('[popover="auto"]:popover-open');
|
||||
if (popup) return;
|
||||
e.preventDefault();
|
||||
setOpen(false, true);
|
||||
}
|
||||
if (e.key === "Tab") {
|
||||
const items = focusable(),
|
||||
first = items[0],
|
||||
last = items.at(-1);
|
||||
if (!navigation.contains(document.activeElement)) {
|
||||
e.preventDefault();
|
||||
(e.shiftKey ? last : first)?.focus();
|
||||
} else if (e.shiftKey && document.activeElement === first) {
|
||||
e.preventDefault();
|
||||
last?.focus();
|
||||
} else if (!e.shiftKey && document.activeElement === last) {
|
||||
e.preventDefault();
|
||||
first?.focus();
|
||||
}
|
||||
}
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
return {
|
||||
open() {
|
||||
setOpen(true);
|
||||
pinned = true;
|
||||
},
|
||||
close() {
|
||||
setOpen(false, true);
|
||||
},
|
||||
get isOpen() {
|
||||
return open;
|
||||
},
|
||||
dispose() {
|
||||
if (disposed) return;
|
||||
setOpen(false);
|
||||
clearTimeout(timer);
|
||||
abort.abort();
|
||||
disposed = true;
|
||||
main.inert = initialInert;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -106,6 +106,7 @@ export async function exportSystem(
|
||||
...iconNames.map((name) => `icons/svg/${name}.svg`),
|
||||
"components/system.css",
|
||||
"components/toast.js",
|
||||
"components/copy-field.js",
|
||||
"components/search-select.js",
|
||||
"components/workspaces.css",
|
||||
"components/ssh-console.js",
|
||||
@@ -161,7 +162,7 @@ export async function exportSystem(
|
||||
}
|
||||
files["selection.json"] = JSON.stringify(
|
||||
{
|
||||
version: "0.1.0",
|
||||
version: "0.2.0",
|
||||
status: "approved-design",
|
||||
concept: c.id,
|
||||
page: state.page,
|
||||
@@ -185,7 +186,7 @@ export async function exportSystem(
|
||||
files["index.html"] =
|
||||
`<!doctype html><meta charset="utf-8"><title>Anidev ${c.name}</title><script>location.replace('playground/frame.html?${exportParams}')<\/script>`;
|
||||
files["README.md"] =
|
||||
`# Anidev / ${c.name}\nApproved Dock design.\nServe this folder over HTTP (for example python -m http.server 8080).\nOpen index.html.\nUse tokens/light.css or tokens/dark.css and components/system.css in any framework.\nSemantic token JSON is the source of values.\nNo upstream implementation is bundled. Reusable Skill packaging is tracked separately.\n`;
|
||||
`# Anidev / ${c.name}\nApproved Dock design — gallery snapshot, version 0.2.0.\nServe this folder over HTTP (for example python -m http.server 8080).\nOpen index.html.\nUse tokens/light.css or tokens/dark.css and components/system.css in any framework.\nFor the separate integration package and installable Skill, run npm run build:package in the source repository; this snapshot does not contain its build tools.\nThird-party terminal licenses are in vendor/xterm.\n`;
|
||||
const url = URL.createObjectURL(zip(files)),
|
||||
a = document.createElement("a");
|
||||
a.href = url;
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
import { iconSvg, iconNames } from "../icons/icons.js";
|
||||
let sequence = 0;
|
||||
const escape = (value) =>
|
||||
String(value).replace(
|
||||
/[&<>"']/g,
|
||||
(c) =>
|
||||
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[
|
||||
c
|
||||
],
|
||||
);
|
||||
|
||||
/** Validate before replacing an existing menu; malformed API data leaves it intact. */
|
||||
export function validateNavigation(items) {
|
||||
if (!Array.isArray(items) || items.length > 100)
|
||||
throw new TypeError("Navigation must contain at most 100 items");
|
||||
const ids = new Set();
|
||||
function visit(nodes, level) {
|
||||
return nodes.map((item) => {
|
||||
if (
|
||||
!item ||
|
||||
typeof item.id !== "string" ||
|
||||
!/^[a-z][\w-]{0,63}$/i.test(item.id) ||
|
||||
ids.has(item.id)
|
||||
)
|
||||
throw new TypeError("Navigation ids must be unique and valid");
|
||||
ids.add(item.id);
|
||||
if (
|
||||
typeof item.label !== "string" ||
|
||||
!item.label.trim() ||
|
||||
item.label.length > 160
|
||||
)
|
||||
throw new TypeError("Navigation needs a label of 1–160 characters");
|
||||
if (!iconNames.includes(item.icon))
|
||||
throw new TypeError("Unknown navigation icon");
|
||||
if (
|
||||
item.children !== undefined &&
|
||||
(level || !Array.isArray(item.children) || item.children.length > 100)
|
||||
)
|
||||
throw new TypeError("Navigation supports one group level");
|
||||
return {
|
||||
id: item.id,
|
||||
label: item.label,
|
||||
icon: item.icon,
|
||||
disabled: Boolean(item.disabled),
|
||||
...(item.children
|
||||
? {
|
||||
children: visit(item.children, level + 1).map((child) => ({
|
||||
...child,
|
||||
disabled: Boolean(item.disabled) || child.disabled,
|
||||
})),
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
});
|
||||
}
|
||||
const result = visit(items, 0);
|
||||
if (ids.size > 100)
|
||||
throw new TypeError(
|
||||
"Navigation must contain at most 100 entries including groups",
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function mountNavigation(
|
||||
root,
|
||||
{
|
||||
items = [],
|
||||
activeId = null,
|
||||
onSelect = () => {},
|
||||
onActiveChange = () => {},
|
||||
t = (ru) => ru,
|
||||
} = {},
|
||||
) {
|
||||
const shell = root.closest(".navigation"),
|
||||
prefix = `anidev-nav-${++sequence}`;
|
||||
const abort = new AbortController(),
|
||||
{ signal } = abort;
|
||||
let model = [],
|
||||
active = null,
|
||||
expanded = document.body.classList.contains("dock-open"),
|
||||
disposed = false;
|
||||
const groups = new Set();
|
||||
const leaves = () =>
|
||||
model
|
||||
.flatMap((item) =>
|
||||
item.children ? (item.disabled ? [] : item.children) : [item],
|
||||
)
|
||||
.filter((item) => !item.disabled);
|
||||
function button(item) {
|
||||
return `<button type="button" data-nav-id="${item.id}" ${item.disabled ? "disabled" : ""} ${active === item.id ? 'aria-current="page"' : ""} aria-label="${escape(item.label)}" title="${escape(item.label)}">${iconSvg(item.icon, { className: "nav-icon" })}<span>${escape(item.label)}</span></button>`;
|
||||
}
|
||||
function sync() {
|
||||
root.querySelectorAll("[data-nav-group]").forEach((group) => {
|
||||
const open = expanded && groups.has(group.dataset.navGroup);
|
||||
group.classList.toggle("is-expanded", open);
|
||||
group
|
||||
.querySelector(".nav-group-toggle")
|
||||
.setAttribute("aria-expanded", String(open));
|
||||
const submenu = group.querySelector(".nav-submenu-container");
|
||||
submenu.inert = !open;
|
||||
submenu.setAttribute("aria-hidden", String(!open));
|
||||
});
|
||||
}
|
||||
function render() {
|
||||
const focused = root.contains(document.activeElement)
|
||||
? document.activeElement.dataset.navId ||
|
||||
document.activeElement.dataset.navToggle
|
||||
: null;
|
||||
root.innerHTML = model
|
||||
.map((item) => {
|
||||
if (!item.children) return button(item);
|
||||
return `<div class="nav-group" data-nav-group="${item.id}"><button type="button" class="nav-group-toggle" data-nav-toggle="${item.id}" ${item.disabled ? "disabled" : ""} data-active="${item.children.some((child) => child.id === active)}" aria-label="${escape(item.label)}" title="${escape(item.label)}" aria-expanded="false" aria-controls="${prefix}-${item.id}">${iconSvg(item.icon, { className: "nav-icon" })}<span class="nav-group-label">${escape(item.label)}${iconSvg("chevron-down", { size: 16, className: "nav-chevron" })}</span></button><div class="nav-submenu-container" id="${prefix}-${item.id}" aria-hidden="true" inert><div class="nav-submenu-clip"><div class="nav-submenu-list">${item.children.map(button).join("")}</div></div></div></div>`;
|
||||
})
|
||||
.join("");
|
||||
if (!model.length) {
|
||||
const message = document.createElement("p");
|
||||
message.className = "nav-empty muted";
|
||||
message.textContent = t(
|
||||
"Нет доступных разделов",
|
||||
"No available sections",
|
||||
);
|
||||
root.append(message);
|
||||
}
|
||||
sync();
|
||||
if (focused) {
|
||||
const candidate = [...root.querySelectorAll("button")].find(
|
||||
(e) =>
|
||||
(e.dataset.navId || e.dataset.navToggle) === focused &&
|
||||
!e.disabled &&
|
||||
!e.closest("[inert]"),
|
||||
);
|
||||
(
|
||||
candidate ||
|
||||
root.querySelector("button:not(:disabled)") ||
|
||||
shell?.querySelector(".dock-brand")
|
||||
)?.focus({ preventScroll: true });
|
||||
}
|
||||
}
|
||||
function setItems(next, { activeId: requested = active } = {}) {
|
||||
if (disposed) return;
|
||||
const valid = validateNavigation(next);
|
||||
const previous = active;
|
||||
model = valid;
|
||||
active = leaves().some((item) => item.id === requested)
|
||||
? requested
|
||||
: leaves()[0]?.id || null;
|
||||
for (const id of groups)
|
||||
if (!model.some((item) => item.id === id)) groups.delete(id);
|
||||
model
|
||||
.filter((item) => item.children?.some((child) => child.id === active))
|
||||
.forEach((item) => groups.add(item.id));
|
||||
render();
|
||||
if (previous !== active) onActiveChange(active);
|
||||
}
|
||||
function setActive(id) {
|
||||
if (disposed) return;
|
||||
if (!leaves().some((item) => item.id === id))
|
||||
throw new TypeError("Active section is not available");
|
||||
setItems(model, { activeId: id });
|
||||
}
|
||||
root.addEventListener(
|
||||
"click",
|
||||
(event) => {
|
||||
const toggle = event.target.closest("[data-nav-toggle]");
|
||||
if (toggle && !toggle.disabled) {
|
||||
groups.has(toggle.dataset.navToggle)
|
||||
? groups.delete(toggle.dataset.navToggle)
|
||||
: groups.add(toggle.dataset.navToggle);
|
||||
sync();
|
||||
return;
|
||||
}
|
||||
const target = event.target.closest("[data-nav-id]");
|
||||
if (target && !target.disabled && !target.closest("[inert]")) {
|
||||
setActive(target.dataset.navId);
|
||||
onSelect(active);
|
||||
}
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
root.addEventListener(
|
||||
"keydown",
|
||||
(event) => {
|
||||
const group = event.target.closest("[data-nav-group]");
|
||||
if (!group || !["ArrowRight", "ArrowLeft"].includes(event.key)) return;
|
||||
event.preventDefault();
|
||||
if (event.key === "ArrowRight") {
|
||||
groups.add(group.dataset.navGroup);
|
||||
sync();
|
||||
group.querySelector(".nav-submenu-list button:not(:disabled)")?.focus();
|
||||
} else {
|
||||
groups.delete(group.dataset.navGroup);
|
||||
sync();
|
||||
group.querySelector(".nav-group-toggle").focus();
|
||||
}
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
shell?.addEventListener(
|
||||
"anidev:dockchange",
|
||||
(event) => {
|
||||
expanded = event.detail.open;
|
||||
sync();
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
setItems(items, { activeId });
|
||||
return {
|
||||
setItems,
|
||||
setActive,
|
||||
get activeId() {
|
||||
return active;
|
||||
},
|
||||
dispose() {
|
||||
disposed = true;
|
||||
abort.abort();
|
||||
},
|
||||
};
|
||||
}
|
||||
+44
-23
@@ -7,6 +7,9 @@ const types = {
|
||||
};
|
||||
export function createNotifier(element, { t = (ru) => ru } = {}) {
|
||||
element.classList.add("anidev-toast");
|
||||
const abort = new AbortController(),
|
||||
signal = abort.signal;
|
||||
let disposed = false;
|
||||
let timer,
|
||||
remaining = 0,
|
||||
started = 0,
|
||||
@@ -29,28 +32,40 @@ export function createNotifier(element, { t = (ru) => ru } = {}) {
|
||||
clearTimeout(timer);
|
||||
remaining = Math.max(1, remaining - (performance.now() - started));
|
||||
}
|
||||
element.addEventListener("pointerenter", pause);
|
||||
element.addEventListener("pointerleave", () => {
|
||||
if (!element.contains(document.activeElement)) resume();
|
||||
});
|
||||
element.addEventListener("focusin", pause);
|
||||
element.addEventListener("focusout", (event) => {
|
||||
if (!element.contains(event.relatedTarget) && !element.matches(":hover"))
|
||||
resume();
|
||||
});
|
||||
element.addEventListener("click", (event) => {
|
||||
if (event.target.closest("[data-toast-dismiss]")) {
|
||||
const restore = element.contains(document.activeElement);
|
||||
hide();
|
||||
if (
|
||||
restore &&
|
||||
invoker?.isConnected &&
|
||||
!invoker.closest("[inert]") &&
|
||||
invoker.getClientRects().length
|
||||
)
|
||||
invoker.focus({ preventScroll: true });
|
||||
}
|
||||
});
|
||||
element.addEventListener("pointerenter", pause, { signal });
|
||||
element.addEventListener(
|
||||
"pointerleave",
|
||||
() => {
|
||||
if (!element.contains(document.activeElement)) resume();
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
element.addEventListener("focusin", pause, { signal });
|
||||
element.addEventListener(
|
||||
"focusout",
|
||||
(event) => {
|
||||
if (!element.contains(event.relatedTarget) && !element.matches(":hover"))
|
||||
resume();
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
element.addEventListener(
|
||||
"click",
|
||||
(event) => {
|
||||
if (event.target.closest("[data-toast-dismiss]")) {
|
||||
const restore = element.contains(document.activeElement);
|
||||
hide();
|
||||
if (
|
||||
restore &&
|
||||
invoker?.isConnected &&
|
||||
!invoker.closest("[inert]") &&
|
||||
invoker.getClientRects().length
|
||||
)
|
||||
invoker.focus({ preventScroll: true });
|
||||
}
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
// A modal makes outside elements inert: put the toast inside it, then use the top layer.
|
||||
document.addEventListener(
|
||||
"close",
|
||||
@@ -63,9 +78,10 @@ export function createNotifier(element, { t = (ru) => ru } = {}) {
|
||||
document.body.append(element);
|
||||
}
|
||||
},
|
||||
{ capture: true },
|
||||
{ capture: true, signal },
|
||||
);
|
||||
function notify(message, { type = "info", title, duration } = {}) {
|
||||
if (disposed) return;
|
||||
type = Object.hasOwn(types, type) ? type : "info";
|
||||
const config = types[type];
|
||||
if (!element.contains(document.activeElement))
|
||||
@@ -95,5 +111,10 @@ export function createNotifier(element, { t = (ru) => ru } = {}) {
|
||||
resume();
|
||||
}
|
||||
notify.dismiss = hide;
|
||||
notify.dispose = () => {
|
||||
hide();
|
||||
abort.abort();
|
||||
disposed = true;
|
||||
};
|
||||
return notify;
|
||||
}
|
||||
Reference in new issue
Block a user