@perkyzz/emvb 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -3
- package/package.json +4 -3
- package/src/admin/agents-api.ts +39 -0
- package/src/admin/editor/CommandPalette.tsx +43 -2
- package/src/admin/editor/Editor.tsx +27 -0
- package/src/admin/editor/Timeline.tsx +108 -6
- package/src/admin/editor/editor-css.ts +4 -0
- package/src/admin/editor/feature-css.ts +22 -3
- package/src/admin/editor/palette.ts +6 -2
- package/src/admin/editor/panels/ColorControl.tsx +19 -8
- package/src/admin/editor/panels/ElementPanel.tsx +1 -1
- package/src/admin/editor/panels/TokenScales.tsx +4 -2
- package/src/admin/editor/panels/color-space.ts +12 -1
- package/src/admin/editor/panels/settings/AdvancedFields.tsx +84 -28
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +42 -4
- package/src/admin/editor/panels/settings/DraftTextField.tsx +38 -9
- package/src/admin/editor/panels/settings/FieldControl.tsx +6 -0
- package/src/admin/editor/panels/settings/IconPicker.tsx +21 -1
- package/src/admin/editor/panels/settings/ImageSourcesField.tsx +111 -0
- package/src/admin/editor/panels/settings/MediaPicker.tsx +2 -3
- package/src/admin/editor/panels/settings/QueryStyleRows.tsx +178 -0
- package/src/admin/editor/panels/settings/advanced-rules.ts +3 -0
- package/src/admin/editor/panels/settings/link-rules.ts +19 -0
- package/src/admin/editor/panels/settings/number-input.ts +11 -0
- package/src/admin/editor/panels/settings/query-style.ts +65 -0
- package/src/admin/editor/panels/variable-values.ts +4 -2
- package/src/admin/editor/remote-change.ts +67 -0
- package/src/admin/index.tsx +3 -1
- package/src/admin/media-api.ts +20 -0
- package/src/admin/pages/AgentsPage.tsx +274 -0
- package/src/admin/ui.ts +28 -0
- package/src/astro/floats/runtime.ts +10 -0
- package/src/astro/popups/runtime.ts +15 -0
- package/src/astro/tabs/runtime.ts +60 -1
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +202 -26
- package/src/core/arrange.ts +11 -3
- package/src/core/css/generate.ts +9 -1
- package/src/core/elements/form-elements.ts +19 -3
- package/src/core/elements/index.ts +39 -11
- package/src/core/render/dynamic-post.ts +21 -5
- package/src/core/render/form.ts +12 -5
- package/src/core/render/index.ts +50 -11
- package/src/core/render/pagination.ts +21 -2
- package/src/core/sanitize/embed-url.ts +7 -1
- package/src/core/sanitize/href.ts +13 -1
- package/src/core/schema/descriptors.ts +4 -1
- package/src/core/schema/layout.ts +10 -3
- package/src/core/share/link.ts +4 -1
- package/src/core/theme/dynamic.ts +12 -7
- package/src/mcp/errors.ts +44 -0
- package/src/mcp/index.ts +4 -0
- package/src/mcp/ports.ts +78 -0
- package/src/mcp/prompts.ts +76 -0
- package/src/mcp/publish.ts +60 -0
- package/src/mcp/queue.ts +19 -0
- package/src/mcp/rate-limit.ts +80 -0
- package/src/mcp/read.ts +419 -0
- package/src/mcp/refs.ts +84 -0
- package/src/mcp/settings.ts +18 -0
- package/src/mcp/tools.ts +265 -0
- package/src/mcp/write.ts +582 -0
- package/src/server/agent-settings.ts +134 -0
- package/src/server/design-routes.ts +71 -17
- package/src/server/icon-routes.ts +5 -1
- package/src/server/mcp-routes.ts +265 -0
- package/src/server/plugin.ts +32 -2
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { Banner, ClipboardText, Empty, Loader, Switch, Table } from "@cloudflare/kumo";
|
|
2
|
+
import {
|
|
3
|
+
ArrowSquareOutIcon,
|
|
4
|
+
GlobeIcon,
|
|
5
|
+
LockKeyIcon,
|
|
6
|
+
PlugsConnectedIcon,
|
|
7
|
+
RobotIcon,
|
|
8
|
+
ShieldCheckIcon,
|
|
9
|
+
TerminalWindowIcon,
|
|
10
|
+
UploadSimpleIcon,
|
|
11
|
+
WarningCircleIcon,
|
|
12
|
+
type Icon,
|
|
13
|
+
} from "@phosphor-icons/react";
|
|
14
|
+
import type { ReactNode } from "react";
|
|
15
|
+
import { useEffect, useState } from "react";
|
|
16
|
+
import { defaultFetcher, type Fetcher } from "../api.ts";
|
|
17
|
+
import { loadAgents, saveAgentSettings, type AgentsState } from "../agents-api.ts";
|
|
18
|
+
import { useRole } from "../access/role.ts";
|
|
19
|
+
import { editorUrl } from "../editor/exit.ts";
|
|
20
|
+
import type { AgentSettings } from "../../mcp/settings.ts";
|
|
21
|
+
import { UI_CSS } from "../ui.ts";
|
|
22
|
+
|
|
23
|
+
const ADMIN_ROLE = 50;
|
|
24
|
+
|
|
25
|
+
/** The stdio bridge config a person pastes into their MCP client (plan § 4). */
|
|
26
|
+
const stdioSnippet = (site: string) =>
|
|
27
|
+
JSON.stringify(
|
|
28
|
+
{
|
|
29
|
+
mcpServers: {
|
|
30
|
+
emvb: {
|
|
31
|
+
command: "npx",
|
|
32
|
+
args: ["-y", "@perkyzz/emvb-mcp", "--site", site],
|
|
33
|
+
env: { EMVB_TOKEN: "<your EmDash API token>" },
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
null,
|
|
38
|
+
2,
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const remoteSnippet = (endpoint: string) =>
|
|
42
|
+
JSON.stringify({ mcpServers: { emvb: { url: endpoint } } }, null, 2);
|
|
43
|
+
|
|
44
|
+
/** A card's heading row: an icon tile, the title and one line on what the card is for (W-477). */
|
|
45
|
+
function SectionHead({
|
|
46
|
+
id,
|
|
47
|
+
icon: SectionIcon,
|
|
48
|
+
title,
|
|
49
|
+
children,
|
|
50
|
+
}: {
|
|
51
|
+
id: string;
|
|
52
|
+
icon: Icon;
|
|
53
|
+
title: string;
|
|
54
|
+
children: ReactNode;
|
|
55
|
+
}) {
|
|
56
|
+
return (
|
|
57
|
+
<div className="emvb-agents-head">
|
|
58
|
+
<span className="emvb-agents-icon" aria-hidden="true">
|
|
59
|
+
<SectionIcon size={18} />
|
|
60
|
+
</span>
|
|
61
|
+
<div>
|
|
62
|
+
<h2 id={id}>{title}</h2>
|
|
63
|
+
<p>{children}</p>
|
|
64
|
+
</div>
|
|
65
|
+
</div>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** AI agents (`/agents`, W-463): the master switch, publishing, how to connect, recent activity. */
|
|
70
|
+
export function AgentsPage({ fetcher = defaultFetcher }: { fetcher?: Fetcher }) {
|
|
71
|
+
const role = useRole(fetcher);
|
|
72
|
+
const [state, setState] = useState<AgentsState | null>(null);
|
|
73
|
+
const [error, setError] = useState<string | null>(null);
|
|
74
|
+
const [saving, setSaving] = useState(false);
|
|
75
|
+
const isAdmin = role.state === "ready" && role.role >= ADMIN_ROLE;
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
if (!isAdmin) return;
|
|
79
|
+
loadAgents(fetcher)
|
|
80
|
+
.then(setState)
|
|
81
|
+
.catch((e: unknown) => setError(e instanceof Error ? e.message : String(e)));
|
|
82
|
+
}, [fetcher, isAdmin]);
|
|
83
|
+
|
|
84
|
+
if (role.state === "loading") return <Loader />;
|
|
85
|
+
if (!isAdmin) {
|
|
86
|
+
return (
|
|
87
|
+
<section data-emvb-access="denied">
|
|
88
|
+
<Empty
|
|
89
|
+
icon={<LockKeyIcon aria-hidden="true" />}
|
|
90
|
+
title="Only administrators can manage AI agents"
|
|
91
|
+
description="Ask an administrator to change these settings."
|
|
92
|
+
/>
|
|
93
|
+
</section>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
if (error) {
|
|
97
|
+
return (
|
|
98
|
+
<Banner
|
|
99
|
+
variant="error"
|
|
100
|
+
icon={<WarningCircleIcon aria-hidden="true" />}
|
|
101
|
+
title="Couldn't load the AI agent settings"
|
|
102
|
+
description={error}
|
|
103
|
+
/>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
if (!state) return <Loader />;
|
|
107
|
+
|
|
108
|
+
const change = async (patch: Partial<AgentSettings>) => {
|
|
109
|
+
const settings = { ...state.settings, ...patch };
|
|
110
|
+
setSaving(true);
|
|
111
|
+
try {
|
|
112
|
+
const revision = await saveAgentSettings(fetcher, settings, state.revision);
|
|
113
|
+
setState({ ...state, settings, revision });
|
|
114
|
+
} catch (e) {
|
|
115
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
116
|
+
} finally {
|
|
117
|
+
setSaving(false);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const site = new URL(state.endpoint).origin;
|
|
121
|
+
const { settings } = state;
|
|
122
|
+
|
|
123
|
+
return (
|
|
124
|
+
<div className="emvb-pages emvb-agents" data-emvb-agents="ready">
|
|
125
|
+
<style>{UI_CSS}</style>
|
|
126
|
+
<header className="emvb-agents-header">
|
|
127
|
+
<div>
|
|
128
|
+
<h1 className="emvb-page-title">AI agents</h1>
|
|
129
|
+
<p>Let AI assistants build and edit EmVB pages through MCP. They only change drafts.</p>
|
|
130
|
+
</div>
|
|
131
|
+
<span className="emvb-agents-status" data-on={settings.mcpEnabled ? "true" : "false"}>
|
|
132
|
+
<span aria-hidden="true" />
|
|
133
|
+
{settings.mcpEnabled ? "Agents on" : "Agents off"}
|
|
134
|
+
</span>
|
|
135
|
+
</header>
|
|
136
|
+
<section
|
|
137
|
+
className="emvb-surface-card emvb-agents-section"
|
|
138
|
+
aria-labelledby="emvb-agents-access"
|
|
139
|
+
>
|
|
140
|
+
<SectionHead id="emvb-agents-access" icon={ShieldCheckIcon} title="Access">
|
|
141
|
+
The master switch. Turning it off refuses every agent call at once.
|
|
142
|
+
</SectionHead>
|
|
143
|
+
<div className="emvb-agents-option">
|
|
144
|
+
<Switch
|
|
145
|
+
label="Let AI agents edit EmVB pages"
|
|
146
|
+
checked={settings.mcpEnabled}
|
|
147
|
+
disabled={saving}
|
|
148
|
+
onCheckedChange={(checked: boolean) => void change({ mcpEnabled: checked })}
|
|
149
|
+
/>
|
|
150
|
+
<p>
|
|
151
|
+
Agents only change drafts. Every change shows in the page's version timeline and can be
|
|
152
|
+
restored.
|
|
153
|
+
</p>
|
|
154
|
+
</div>
|
|
155
|
+
<div className="emvb-agents-field">
|
|
156
|
+
<h3>MCP endpoint</h3>
|
|
157
|
+
<ClipboardText text={state.endpoint} />
|
|
158
|
+
</div>
|
|
159
|
+
</section>
|
|
160
|
+
<section
|
|
161
|
+
className="emvb-surface-card emvb-agents-section"
|
|
162
|
+
aria-labelledby="emvb-agents-publish"
|
|
163
|
+
>
|
|
164
|
+
<SectionHead id="emvb-agents-publish" icon={UploadSimpleIcon} title="Publishing">
|
|
165
|
+
Published changes go live immediately. Each one is still in the version timeline.
|
|
166
|
+
</SectionHead>
|
|
167
|
+
<div className="emvb-agents-option">
|
|
168
|
+
<Switch
|
|
169
|
+
label="Agents may publish pages"
|
|
170
|
+
checked={settings.publishPages}
|
|
171
|
+
disabled={saving || !settings.mcpEnabled}
|
|
172
|
+
onCheckedChange={(checked: boolean) => void change({ publishPages: checked })}
|
|
173
|
+
/>
|
|
174
|
+
<p>Off: agents save drafts and a person publishes them.</p>
|
|
175
|
+
</div>
|
|
176
|
+
<div className="emvb-agents-option">
|
|
177
|
+
<Switch
|
|
178
|
+
label="Agents may publish site styles"
|
|
179
|
+
checked={settings.publishStyles}
|
|
180
|
+
disabled={saving || !settings.mcpEnabled}
|
|
181
|
+
onCheckedChange={(checked: boolean) => void change({ publishStyles: checked })}
|
|
182
|
+
/>
|
|
183
|
+
<p>Site styles change every page at once, so leave this off unless you need it.</p>
|
|
184
|
+
</div>
|
|
185
|
+
</section>
|
|
186
|
+
<section
|
|
187
|
+
className="emvb-surface-card emvb-agents-section"
|
|
188
|
+
aria-labelledby="emvb-agents-connect"
|
|
189
|
+
>
|
|
190
|
+
<SectionHead id="emvb-agents-connect" icon={PlugsConnectedIcon} title="How to connect">
|
|
191
|
+
Create a token in EmDash → API tokens with the scope <code>mcp:tools:emvb</code> for a
|
|
192
|
+
user who is an editor or above. Never give an agent an admin token.
|
|
193
|
+
</SectionHead>
|
|
194
|
+
<div className="emvb-agents-snippets">
|
|
195
|
+
<div className="emvb-agents-snippet">
|
|
196
|
+
<h3>
|
|
197
|
+
<GlobeIcon aria-hidden="true" size={16} />
|
|
198
|
+
Remote (EmDash MCP, sign in with OAuth)
|
|
199
|
+
</h3>
|
|
200
|
+
<pre data-emvb-snippet="remote">{remoteSnippet(state.endpoint)}</pre>
|
|
201
|
+
</div>
|
|
202
|
+
<div className="emvb-agents-snippet">
|
|
203
|
+
<h3>
|
|
204
|
+
<TerminalWindowIcon aria-hidden="true" size={16} />
|
|
205
|
+
Local (stdio)
|
|
206
|
+
</h3>
|
|
207
|
+
<pre data-emvb-snippet="stdio">{stdioSnippet(site)}</pre>
|
|
208
|
+
</div>
|
|
209
|
+
</div>
|
|
210
|
+
</section>
|
|
211
|
+
<section
|
|
212
|
+
className="emvb-surface-card emvb-agents-section"
|
|
213
|
+
aria-labelledby="emvb-agents-activity"
|
|
214
|
+
>
|
|
215
|
+
<SectionHead id="emvb-agents-activity" icon={RobotIcon} title="Recent agent activity">
|
|
216
|
+
The latest changes agents made, newest first.
|
|
217
|
+
</SectionHead>
|
|
218
|
+
{state.activity.length === 0 ? (
|
|
219
|
+
<div data-emvb-agents-empty="">
|
|
220
|
+
<Empty
|
|
221
|
+
icon={<RobotIcon aria-hidden="true" />}
|
|
222
|
+
title="No agent changes yet"
|
|
223
|
+
description="When an agent edits a page, it shows up here with a link to the editor."
|
|
224
|
+
/>
|
|
225
|
+
</div>
|
|
226
|
+
) : (
|
|
227
|
+
<div className="emvb-agents-table">
|
|
228
|
+
<Table>
|
|
229
|
+
<Table.Header>
|
|
230
|
+
<Table.Row>
|
|
231
|
+
<Table.Head>When</Table.Head>
|
|
232
|
+
<Table.Head>Who</Table.Head>
|
|
233
|
+
<Table.Head>Change</Table.Head>
|
|
234
|
+
<Table.Head>Page</Table.Head>
|
|
235
|
+
</Table.Row>
|
|
236
|
+
</Table.Header>
|
|
237
|
+
<Table.Body>
|
|
238
|
+
{state.activity.map((entry) => (
|
|
239
|
+
<Table.Row key={entry.id}>
|
|
240
|
+
<Table.Cell>
|
|
241
|
+
<span className="emvb-agents-when">
|
|
242
|
+
{new Date(entry.at).toLocaleString()}
|
|
243
|
+
</span>
|
|
244
|
+
</Table.Cell>
|
|
245
|
+
<Table.Cell>
|
|
246
|
+
<span className="emvb-agents-who">
|
|
247
|
+
<span className="emvb-timeline-agent">
|
|
248
|
+
<RobotIcon aria-hidden="true" size={12} />
|
|
249
|
+
{entry.client ?? "Agent"}
|
|
250
|
+
</span>
|
|
251
|
+
{entry.userId ?? "unknown"}
|
|
252
|
+
</span>
|
|
253
|
+
</Table.Cell>
|
|
254
|
+
<Table.Cell>{entry.summary}</Table.Cell>
|
|
255
|
+
<Table.Cell>
|
|
256
|
+
{entry.pageId ? (
|
|
257
|
+
<a className="emvb-agents-link" href={editorUrl(entry.pageId)}>
|
|
258
|
+
Open in editor
|
|
259
|
+
<ArrowSquareOutIcon aria-hidden="true" size={14} />
|
|
260
|
+
</a>
|
|
261
|
+
) : (
|
|
262
|
+
"—"
|
|
263
|
+
)}
|
|
264
|
+
</Table.Cell>
|
|
265
|
+
</Table.Row>
|
|
266
|
+
))}
|
|
267
|
+
</Table.Body>
|
|
268
|
+
</Table>
|
|
269
|
+
</div>
|
|
270
|
+
)}
|
|
271
|
+
</section>
|
|
272
|
+
</div>
|
|
273
|
+
);
|
|
274
|
+
}
|
package/src/admin/ui.ts
CHANGED
|
@@ -91,6 +91,34 @@ export const UI_CSS = `
|
|
|
91
91
|
backdrop-filter: blur(var(--emvb-glass-blur, 6px));
|
|
92
92
|
box-shadow: var(--emvb-elevation-s);
|
|
93
93
|
}
|
|
94
|
+
/* Live MCP test: the AI agents page reads as cards with clear headings, not bare browser defaults. */
|
|
95
|
+
.emvb-agents { max-width: 960px; }
|
|
96
|
+
.emvb-agents-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
|
|
97
|
+
.emvb-agents-header p { margin: 4px 0 0; font-size: 14px; line-height: 20px; color: var(--text-color-kumo-subtle); }
|
|
98
|
+
.emvb-agents-status { display: inline-flex; flex: none; align-items: center; gap: 6px; margin-top: 6px; padding: 2px 10px; border: 1px solid var(--color-kumo-line); border-radius: 999px; background: var(--color-kumo-base); color: var(--text-color-kumo-default); font-size: 12px; line-height: 20px; font-weight: 600; }
|
|
99
|
+
.emvb-agents-status > span { width: 8px; height: 8px; border-radius: 999px; background: var(--text-color-kumo-subtle); }
|
|
100
|
+
.emvb-agents-status[data-on="true"] > span { background: var(--text-color-kumo-success); }
|
|
101
|
+
.emvb-agents-section { gap: 16px; padding: 20px; }
|
|
102
|
+
.emvb-agents-head { display: flex; align-items: flex-start; gap: 12px; }
|
|
103
|
+
.emvb-agents-icon { display: grid; flex: none; place-items: center; width: 36px; height: 36px; border-radius: 10px; background: var(--color-kumo-tint); color: var(--text-color-kumo-default); }
|
|
104
|
+
.emvb-agents-section h2 { margin: 0; font-size: 16px; line-height: 24px; font-weight: 600; color: var(--text-color-kumo-default); }
|
|
105
|
+
.emvb-agents-section h3 { display: flex; align-items: center; gap: 6px; margin: 0; font-size: 13px; line-height: 20px; font-weight: 600; color: var(--text-color-kumo-default); }
|
|
106
|
+
.emvb-agents-section p { margin: 0; font-size: 14px; line-height: 20px; color: var(--text-color-kumo-subtle); }
|
|
107
|
+
.emvb-agents-section code { padding: 1px 5px; border-radius: 4px; background: var(--color-kumo-tint); font-family: var(--font-mono); font-size: 12px; }
|
|
108
|
+
.emvb-agents-option { display: grid; gap: 4px; padding-top: 16px; border-top: 1px solid var(--color-kumo-line); }
|
|
109
|
+
.emvb-agents-option > p { padding-inline-start: 44px; font-size: 13px; }
|
|
110
|
+
.emvb-agents-field { display: grid; gap: 6px; }
|
|
111
|
+
.emvb-agents-snippets { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; }
|
|
112
|
+
.emvb-agents-snippet { display: grid; align-content: start; gap: 8px; min-width: 0; padding: 12px; border: 1px solid var(--color-kumo-line); border-radius: 8px; }
|
|
113
|
+
.emvb-agents-section pre { margin: 0; padding: 12px; overflow-x: auto; border-radius: 6px; background: var(--color-kumo-recessed, rgba(127,127,127,.08)); font-family: var(--font-mono); font-size: 12px; line-height: 18px; }
|
|
114
|
+
.emvb-agents-table { overflow-x: auto; margin-inline: -20px; margin-bottom: -20px; border-top: 1px solid var(--color-kumo-line); }
|
|
115
|
+
.emvb-agents-table table { width: 100%; }
|
|
116
|
+
.emvb-agents-when { color: var(--text-color-kumo-subtle); font-variant-numeric: tabular-nums; white-space: nowrap; }
|
|
117
|
+
.emvb-agents-who { display: inline-flex; align-items: center; gap: 8px; white-space: nowrap; }
|
|
118
|
+
.emvb-agents-link { display: inline-flex; align-items: center; gap: 4px; color: var(--text-color-kumo-link, var(--text-color-kumo-brand)); font-weight: 500; white-space: nowrap; }
|
|
119
|
+
.emvb-agents-link:hover { text-decoration: underline; }
|
|
120
|
+
.emvb-agents-link:focus-visible { outline: 2px solid var(--color-kumo-brand); outline-offset: 2px; border-radius: 4px; }
|
|
121
|
+
.emvb-timeline-agent { display: inline-flex; flex: none; align-items: center; gap: 3px; padding: 1px 7px 1px 5px; border: 1px solid var(--color-kumo-line); border-radius: 999px; background: var(--color-kumo-base); color: var(--text-color-kumo-default); font-size: 11px; line-height: 16px; font-weight: 600; }
|
|
94
122
|
.emvb-surface-card[data-emvb-list] { gap: 0; padding: 0; overflow: hidden; }
|
|
95
123
|
.emvb-surface-card[data-emvb-list] table { width: 100%; }
|
|
96
124
|
.emvb-surface-card .emvb-empty-shell,
|
|
@@ -59,6 +59,10 @@ function hide(el: HTMLElement): void {
|
|
|
59
59
|
|
|
60
60
|
/** Hide floats already closed this visit, then keep bars stacked. */
|
|
61
61
|
export function initFloats(): void {
|
|
62
|
+
// W-407: a second call (a host loading the script twice) added a second click handler.
|
|
63
|
+
const marked = document.documentElement;
|
|
64
|
+
if (marked.dataset["emvbFloatsReady"] === "1") return;
|
|
65
|
+
marked.dataset["emvbFloatsReady"] = "1";
|
|
62
66
|
for (const el of bars()) {
|
|
63
67
|
const id = el.getAttribute("data-emvb-float");
|
|
64
68
|
if (id && closed(id)) el.hidden = true;
|
|
@@ -75,4 +79,10 @@ export function initFloats(): void {
|
|
|
75
79
|
hide(float);
|
|
76
80
|
});
|
|
77
81
|
window.addEventListener("resize", placeFloats);
|
|
82
|
+
// W-407: a bar's height also changes without a window resize (web fonts arriving, text
|
|
83
|
+
// wrapping once styles load), which left the reserved padding short and the bar over content.
|
|
84
|
+
if (typeof ResizeObserver === "function") {
|
|
85
|
+
const observer = new ResizeObserver(() => placeFloats());
|
|
86
|
+
for (const el of bars()) observer.observe(el);
|
|
87
|
+
}
|
|
78
88
|
}
|
|
@@ -177,6 +177,21 @@ function bindA11y(ctrl: Controller): void {
|
|
|
177
177
|
ctrl.close();
|
|
178
178
|
}
|
|
179
179
|
});
|
|
180
|
+
// W-406: when the focused control disappears (a form swapped for its thank-you message), focus
|
|
181
|
+
// falls to <body>; Escape and Tab then never reached the dialog, so the popup couldn't be closed
|
|
182
|
+
// from the keyboard and Tab walked the page behind it.
|
|
183
|
+
document.addEventListener("keydown", (event) => {
|
|
184
|
+
if (!ctrl.opened || event.defaultPrevented || !ctrl.root.isConnected) return;
|
|
185
|
+
const active = document.activeElement;
|
|
186
|
+
if (active && active !== document.body && active !== document.documentElement) return;
|
|
187
|
+
if (event.key === "Tab") {
|
|
188
|
+
event.preventDefault();
|
|
189
|
+
(focusables(ctrl.dialog)[0] ?? ctrl.dialog).focus();
|
|
190
|
+
} else if (event.key === "Escape") {
|
|
191
|
+
event.preventDefault();
|
|
192
|
+
ctrl.close();
|
|
193
|
+
}
|
|
194
|
+
});
|
|
180
195
|
ctrl.dialog.addEventListener("keydown", (event) => {
|
|
181
196
|
if (event.key === "Escape") {
|
|
182
197
|
event.preventDefault();
|
|
@@ -90,7 +90,15 @@ function enhance(root: HTMLElement): void {
|
|
|
90
90
|
activate(root, selectedIndex(root));
|
|
91
91
|
|
|
92
92
|
list.addEventListener("keydown", (event) => {
|
|
93
|
-
|
|
93
|
+
// W-405: in right-to-left text the first tab is on the right, so the arrows swap.
|
|
94
|
+
const rtl = list.closest("[dir]")?.getAttribute("dir")?.toLowerCase() === "rtl";
|
|
95
|
+
const key =
|
|
96
|
+
rtl && event.key === "ArrowRight"
|
|
97
|
+
? "ArrowLeft"
|
|
98
|
+
: rtl && event.key === "ArrowLeft"
|
|
99
|
+
? "ArrowRight"
|
|
100
|
+
: event.key;
|
|
101
|
+
const target = KEY_TARGETS[key];
|
|
94
102
|
if (!target) return;
|
|
95
103
|
event.preventDefault();
|
|
96
104
|
if (labels.length === 0) return;
|
|
@@ -105,9 +113,60 @@ function enhance(root: HTMLElement): void {
|
|
|
105
113
|
});
|
|
106
114
|
}
|
|
107
115
|
|
|
116
|
+
/**
|
|
117
|
+
* W-414: opens every closed tab around `target` (outermost first) so a link to something inside a
|
|
118
|
+
* tab, a table of contents entry most often, lands on it. True when a tab was opened.
|
|
119
|
+
*/
|
|
120
|
+
function revealInTabs(target: Element): boolean {
|
|
121
|
+
const steps: { root: HTMLElement; index: number }[] = [];
|
|
122
|
+
let panel = target.closest<HTMLElement>(".emvb-tab-panel");
|
|
123
|
+
while (panel) {
|
|
124
|
+
const root = panel.parentElement?.parentElement;
|
|
125
|
+
if (root?.classList.contains("emvb-tabs")) {
|
|
126
|
+
const index = parts(root).panels.indexOf(panel);
|
|
127
|
+
if (index >= 0) steps.unshift({ root, index });
|
|
128
|
+
}
|
|
129
|
+
panel = root?.parentElement?.closest<HTMLElement>(".emvb-tab-panel") ?? null;
|
|
130
|
+
}
|
|
131
|
+
let opened = false;
|
|
132
|
+
for (const { root, index } of steps) {
|
|
133
|
+
if (selectedIndex(root) === index) continue;
|
|
134
|
+
activate(root, index);
|
|
135
|
+
opened = true;
|
|
136
|
+
}
|
|
137
|
+
return opened;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function targetOf(hash: string): Element | null {
|
|
141
|
+
if (!hash || hash === "#") return null;
|
|
142
|
+
try {
|
|
143
|
+
return document.getElementById(decodeURIComponent(hash.slice(1)));
|
|
144
|
+
} catch {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** After opening a tab, the browser's own jump already happened, so scroll again. */
|
|
150
|
+
function revealHash(hash: string): void {
|
|
151
|
+
const target = targetOf(hash);
|
|
152
|
+
if (!target || !revealInTabs(target)) return;
|
|
153
|
+
target.scrollIntoView?.({ block: "start" });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
let hashWatch = false;
|
|
157
|
+
|
|
108
158
|
export function initTabs(root: ParentNode = document): void {
|
|
109
159
|
const nodes = root.querySelectorAll<HTMLElement>(".emvb-tabs");
|
|
110
160
|
for (const node of nodes) enhance(node);
|
|
161
|
+
if (hashWatch || typeof window === "undefined") return;
|
|
162
|
+
hashWatch = true;
|
|
163
|
+
// A link to the page's own anchors, then the URL's hash on load and on back/forward.
|
|
164
|
+
document.addEventListener("click", (event) => {
|
|
165
|
+
const link = (event.target as Element | null)?.closest?.<HTMLAnchorElement>('a[href^="#"]');
|
|
166
|
+
if (link) revealHash(link.getAttribute("href") ?? "");
|
|
167
|
+
});
|
|
168
|
+
window.addEventListener("hashchange", () => revealHash(window.location.hash));
|
|
169
|
+
revealHash(window.location.hash);
|
|
111
170
|
}
|
|
112
171
|
|
|
113
172
|
/** Test helper: select tab by index after enhance. */
|
package/src/constants.ts
CHANGED