@brimsites/brimjs 0.0.0-stage → 1.0.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.
Files changed (238) hide show
  1. package/LICENSE +110 -0
  2. package/README.md +22 -2
  3. package/bin/brim.js +5 -0
  4. package/dist/cli.js +3802 -0
  5. package/editor-dist/editor.css +1 -0
  6. package/editor-dist/editor.js +91 -0
  7. package/package.json +71 -3
  8. package/skills/brim-convert/SKILL.md +256 -0
  9. package/skills/brim-hosting/SKILL.md +221 -0
  10. package/src/astro-shim.d.ts +5 -0
  11. package/src/auth/accounts.ts +341 -0
  12. package/src/auth/password.ts +34 -0
  13. package/src/auth/permissions.ts +44 -0
  14. package/src/blocks/accordion/Accordion.astro +48 -0
  15. package/src/blocks/accordion/accordion.css +63 -0
  16. package/src/blocks/analytics/Analytics.astro +45 -0
  17. package/src/blocks/analytics/ConsentBanner.astro +41 -0
  18. package/src/blocks/analytics/analytics.client.ts +125 -0
  19. package/src/blocks/analytics/analytics.css +51 -0
  20. package/src/blocks/analytics/consent.ts +51 -0
  21. package/src/blocks/button/Button.astro +64 -0
  22. package/src/blocks/button/button.css +100 -0
  23. package/src/blocks/carousel/Carousel.astro +140 -0
  24. package/src/blocks/carousel/carousel.client.ts +153 -0
  25. package/src/blocks/carousel/carousel.css +181 -0
  26. package/src/blocks/data/icons.ts +285 -0
  27. package/src/blocks/data/image-shapes.ts +91 -0
  28. package/src/blocks/data/social-icons.ts +41 -0
  29. package/src/blocks/data/social-networks.ts +256 -0
  30. package/src/blocks/divider/Divider.astro +27 -0
  31. package/src/blocks/divider/divider.css +17 -0
  32. package/src/blocks/form/Captcha.astro +27 -0
  33. package/src/blocks/form/Form.astro +250 -0
  34. package/src/blocks/form/FormField.astro +138 -0
  35. package/src/blocks/form/Newsletter.astro +80 -0
  36. package/src/blocks/form/form.client.ts +339 -0
  37. package/src/blocks/form/form.css +271 -0
  38. package/src/blocks/gallery/ImageGallery.astro +126 -0
  39. package/src/blocks/gallery/gallery.css +43 -0
  40. package/src/blocks/gallery/lightbox.client.ts +178 -0
  41. package/src/blocks/gallery/lightbox.css +134 -0
  42. package/src/blocks/icon/Icon.astro +41 -0
  43. package/src/blocks/icon/icon.css +16 -0
  44. package/src/blocks/image/Image.astro +158 -0
  45. package/src/blocks/image/image.css +42 -0
  46. package/src/blocks/images.ts +64 -0
  47. package/src/blocks/in-editor.client.ts +8 -0
  48. package/src/blocks/listing/Listing.astro +288 -0
  49. package/src/blocks/listing/ListingCards.astro +48 -0
  50. package/src/blocks/listing/listing.css +136 -0
  51. package/src/blocks/listing/query.ts +363 -0
  52. package/src/blocks/map/Map.astro +75 -0
  53. package/src/blocks/map/map.css +32 -0
  54. package/src/blocks/schemas/accordion.ts +118 -0
  55. package/src/blocks/schemas/button.ts +78 -0
  56. package/src/blocks/schemas/carousel.ts +133 -0
  57. package/src/blocks/schemas/divider.ts +29 -0
  58. package/src/blocks/schemas/form-field-types.ts +29 -0
  59. package/src/blocks/schemas/form.ts +350 -0
  60. package/src/blocks/schemas/icon.ts +24 -0
  61. package/src/blocks/schemas/image-gallery.ts +54 -0
  62. package/src/blocks/schemas/image.ts +87 -0
  63. package/src/blocks/schemas/index.ts +33 -0
  64. package/src/blocks/schemas/listing.ts +14 -0
  65. package/src/blocks/schemas/map.ts +81 -0
  66. package/src/blocks/schemas/newsletter.ts +324 -0
  67. package/src/blocks/schemas/social-media.ts +348 -0
  68. package/src/blocks/schemas/text.ts +151 -0
  69. package/src/blocks/schemas/video.ts +57 -0
  70. package/src/blocks/search/Search.astro +147 -0
  71. package/src/blocks/search/search.css +69 -0
  72. package/src/blocks/search/state.ts +28 -0
  73. package/src/blocks/settings.ts +94 -0
  74. package/src/blocks/social/SocialMedia.astro +102 -0
  75. package/src/blocks/social/social-links.ts +16 -0
  76. package/src/blocks/social/social.css +31 -0
  77. package/src/blocks/theme.ts +35 -0
  78. package/src/blocks/types.ts +52 -0
  79. package/src/blocks/video/Video.astro +90 -0
  80. package/src/blocks/video/video-source.ts +77 -0
  81. package/src/blocks/video/video.client.ts +43 -0
  82. package/src/blocks/video/video.css +29 -0
  83. package/src/check.ts +1119 -0
  84. package/src/cli.ts +493 -0
  85. package/src/components/Blocks.astro +68 -0
  86. package/src/components/Field.astro +61 -0
  87. package/src/components/Footer.astro +36 -0
  88. package/src/components/Link.astro +18 -0
  89. package/src/components/MobileMenu.astro +77 -0
  90. package/src/components/Navbar.astro +42 -0
  91. package/src/components/RichText.astro +32 -0
  92. package/src/components/Seo.astro +202 -0
  93. package/src/components/Setting.astro +46 -0
  94. package/src/components/Shared.astro +17 -0
  95. package/src/components/Text.astro +91 -0
  96. package/src/components/blocks.css +86 -0
  97. package/src/components/index.ts +27 -0
  98. package/src/components/mobile-menu.client.ts +21 -0
  99. package/src/components/mobile-menu.css +52 -0
  100. package/src/components/text.css +48 -0
  101. package/src/config.ts +413 -0
  102. package/src/content/block-layout.ts +63 -0
  103. package/src/content/block-values.ts +48 -0
  104. package/src/content/content-check.ts +174 -0
  105. package/src/content/content-types.ts +408 -0
  106. package/src/content/drafts.ts +115 -0
  107. package/src/content/feed.ts +237 -0
  108. package/src/content/field-types.ts +107 -0
  109. package/src/content/history.ts +140 -0
  110. package/src/content/ids.ts +50 -0
  111. package/src/content/image-size.ts +212 -0
  112. package/src/content/image-sizes.ts +126 -0
  113. package/src/content/item-fields.ts +110 -0
  114. package/src/content/item-head.ts +126 -0
  115. package/src/content/item-image.ts +26 -0
  116. package/src/content/item-path.ts +122 -0
  117. package/src/content/items.ts +580 -0
  118. package/src/content/language-scopes.ts +40 -0
  119. package/src/content/media.ts +271 -0
  120. package/src/content/menus.ts +51 -0
  121. package/src/content/page-templates.ts +52 -0
  122. package/src/content/redirects.ts +112 -0
  123. package/src/content/references.ts +165 -0
  124. package/src/content/rich-text.ts +63 -0
  125. package/src/content/search.ts +167 -0
  126. package/src/content/seed.ts +229 -0
  127. package/src/content/site-settings.ts +111 -0
  128. package/src/content/sitemap.ts +143 -0
  129. package/src/content/store.ts +225 -0
  130. package/src/content/tags.ts +67 -0
  131. package/src/content/text-default.ts +139 -0
  132. package/src/content/types.ts +137 -0
  133. package/src/content/validate.ts +253 -0
  134. package/src/content/validation-error.ts +2 -0
  135. package/src/db/cached.ts +140 -0
  136. package/src/db/database.ts +15 -0
  137. package/src/db/libsql.ts +29 -0
  138. package/src/db/schema.ts +189 -0
  139. package/src/db/url.ts +44 -0
  140. package/src/editor/client/BlockSettings.tsx +1063 -0
  141. package/src/editor/client/Chrome.tsx +284 -0
  142. package/src/editor/client/ContentBrowser.tsx +1299 -0
  143. package/src/editor/client/Editor.tsx +1756 -0
  144. package/src/editor/client/HistoryPanel.tsx +114 -0
  145. package/src/editor/client/ImagesWidget.tsx +113 -0
  146. package/src/editor/client/Inspector.tsx +127 -0
  147. package/src/editor/client/ItemForm.tsx +535 -0
  148. package/src/editor/client/LinkWidget.tsx +440 -0
  149. package/src/editor/client/ListingPanel.tsx +99 -0
  150. package/src/editor/client/Login.tsx +338 -0
  151. package/src/editor/client/Menu.tsx +64 -0
  152. package/src/editor/client/PageSettings.tsx +270 -0
  153. package/src/editor/client/RedirectsSettings.tsx +130 -0
  154. package/src/editor/client/Resize.tsx +204 -0
  155. package/src/editor/client/Sidebar.tsx +51 -0
  156. package/src/editor/client/SiteMap.tsx +568 -0
  157. package/src/editor/client/SiteSettings.tsx +409 -0
  158. package/src/editor/client/TextTools.tsx +391 -0
  159. package/src/editor/client/Toolbar.tsx +449 -0
  160. package/src/editor/client/VideoUpload.tsx +251 -0
  161. package/src/editor/client/api.ts +417 -0
  162. package/src/editor/client/brand.ts +20 -0
  163. package/src/editor/client/connection.ts +98 -0
  164. package/src/editor/client/context.ts +19 -0
  165. package/src/editor/client/css.d.ts +2 -0
  166. package/src/editor/client/editor.css +4656 -0
  167. package/src/editor/client/fields.tsx +812 -0
  168. package/src/editor/client/icons.tsx +174 -0
  169. package/src/editor/client/keys.ts +15 -0
  170. package/src/editor/client/kinds.ts +289 -0
  171. package/src/editor/client/languages.ts +32 -0
  172. package/src/editor/client/main.tsx +100 -0
  173. package/src/editor/client/responses.ts +26 -0
  174. package/src/editor/client/rich-text.ts +326 -0
  175. package/src/editor/client/site-tree.ts +239 -0
  176. package/src/editor/client/theme.ts +55 -0
  177. package/src/editor/client/tsconfig.json +18 -0
  178. package/src/editor/client/tus.ts +137 -0
  179. package/src/editor/client/use-rect.ts +50 -0
  180. package/src/editor/client/widgets.tsx +1218 -0
  181. package/src/forms/captcha.ts +83 -0
  182. package/src/forms/define.ts +166 -0
  183. package/src/forms/fields.ts +197 -0
  184. package/src/forms/file-types.ts +108 -0
  185. package/src/forms/files.ts +216 -0
  186. package/src/forms/form-page.ts +9 -0
  187. package/src/forms/handler.ts +622 -0
  188. package/src/forms/spec.ts +89 -0
  189. package/src/forms/store.ts +96 -0
  190. package/src/forms/work.ts +50 -0
  191. package/src/icons/library.ts +144 -0
  192. package/src/icons/load.ts +63 -0
  193. package/src/index.ts +99 -0
  194. package/src/init.ts +257 -0
  195. package/src/integration.ts +469 -0
  196. package/src/mail/mailer.ts +197 -0
  197. package/src/mail/notify.ts +272 -0
  198. package/src/mail/settings.ts +173 -0
  199. package/src/mail/templates.ts +191 -0
  200. package/src/mcp/page-nodes.ts +209 -0
  201. package/src/mcp/server.ts +111 -0
  202. package/src/mcp/tools.ts +637 -0
  203. package/src/runtime/TemplatePage.astro +39 -0
  204. package/src/runtime/TemplatePageStatic.astro +13 -0
  205. package/src/runtime/admin-route.ts +80 -0
  206. package/src/runtime/connect.ts +53 -0
  207. package/src/runtime/content.ts +188 -0
  208. package/src/runtime/context.ts +131 -0
  209. package/src/runtime/form-files.ts +18 -0
  210. package/src/runtime/forms-route.ts +33 -0
  211. package/src/runtime/forms.ts +28 -0
  212. package/src/runtime/icons.ts +5 -0
  213. package/src/runtime/media.ts +175 -0
  214. package/src/runtime/middleware.ts +186 -0
  215. package/src/runtime/nav.ts +13 -0
  216. package/src/runtime/query-reads.ts +49 -0
  217. package/src/runtime/query-store.ts +16 -0
  218. package/src/runtime/request-state.ts +69 -0
  219. package/src/runtime/robots.ts +29 -0
  220. package/src/runtime/rss-static.ts +16 -0
  221. package/src/runtime/rss.ts +29 -0
  222. package/src/runtime/server.ts +29 -0
  223. package/src/runtime/sitemap.ts +34 -0
  224. package/src/runtime/template-paths.ts +34 -0
  225. package/src/runtime/uploads-route.ts +13 -0
  226. package/src/seed-cli.ts +253 -0
  227. package/src/server/auth.ts +31 -0
  228. package/src/server/fetch-image.ts +219 -0
  229. package/src/server/handler.ts +2106 -0
  230. package/src/server/http.ts +162 -0
  231. package/src/server/locales.ts +73 -0
  232. package/src/server/node.ts +57 -0
  233. package/src/server/query-params.ts +249 -0
  234. package/src/server/rate-limit.ts +72 -0
  235. package/src/server/video-host.ts +162 -0
  236. package/src/starter.ts +70 -0
  237. package/src/version.ts +4 -0
  238. package/src/virtual.d.ts +65 -0
@@ -0,0 +1,440 @@
1
+ import { type FormEvent, useEffect, useMemo, useRef, useState } from "react";
2
+ import { createPortal } from "react-dom";
3
+ import { itemPagePath } from "../../server/locales.ts";
4
+ import { type ContentType, type Item, type SitePage, api } from "./api.ts";
5
+ import { useEditor } from "./context.ts";
6
+ import { ArrowLeftIcon, ChevronRightIcon, CloseIcon, FolderIcon, LinkIcon, PageIcon, SearchIcon } from "./icons.tsx";
7
+ import { useSiteLocales } from "./languages.ts";
8
+ import { pageTitle } from "./Toolbar.tsx";
9
+ import { isComposing } from "./keys.ts";
10
+
11
+ /**
12
+ * The editor's one widget for a link, wherever a link is edited: block settings, the navbar and link primitives,
13
+ * the item form. Type or paste an address, or browse: the site's pages and items, a part of a page, a website, an
14
+ * email address or a phone number. Optionally with "Open in new tab".
15
+ */
16
+ export function LinkWidget({
17
+ label,
18
+ description,
19
+ value,
20
+ placeholder = "Paste a link or browse the site",
21
+ newTab,
22
+ autoFocus = false,
23
+ onChange,
24
+ }: {
25
+ label: string;
26
+ description?: string | undefined;
27
+ value: string;
28
+ placeholder?: string | undefined;
29
+ newTab?: { checked: boolean; onChange: (checked: boolean) => void } | undefined;
30
+ /** In a popover opened for it, such as the text toolbar's link. */
31
+ autoFocus?: boolean;
32
+ onChange: (value: string) => void;
33
+ }) {
34
+ const [draft, setDraft] = useState(value);
35
+ const [picking, setPicking] = useState(false);
36
+ const input = useRef<HTMLInputElement>(null);
37
+ useEffect(() => setDraft(value), [value]);
38
+
39
+ return (
40
+ <div className="field-group brim-url-widget">
41
+ <span className="field-label">{label}</span>
42
+ {description && <p className="field-description">{description}</p>}
43
+ <div className="url-input-box">
44
+ <LinkIcon />
45
+ <input
46
+ ref={input}
47
+ type="text"
48
+ aria-label={label}
49
+ placeholder={placeholder}
50
+ autoFocus={autoFocus}
51
+ value={draft}
52
+ onChange={(event) => setDraft(event.target.value)}
53
+ onBlur={() => draft !== value && onChange(draft.trim())}
54
+ onKeyDown={(event) => event.key === "Enter" && !isComposing(event) && event.currentTarget.blur()}
55
+ />
56
+ <button type="button" className="url-browse-btn" aria-haspopup="dialog" onClick={() => setPicking(true)}>
57
+ Browse
58
+ </button>
59
+ </div>
60
+ {value && (
61
+ <div className="url-actions">
62
+ <span className="url-actions__label">{describeLink(value)}</span>
63
+ <button type="button" className="url-action" onClick={() => input.current?.select()}>
64
+ Change
65
+ </button>
66
+ <a className="url-action" href={value} target="_blank" rel="noreferrer">
67
+ Open
68
+ </a>
69
+ <button type="button" className="url-action" onClick={() => onChange("")}>
70
+ Clear
71
+ </button>
72
+ </div>
73
+ )}
74
+ {newTab && (
75
+ <label className="url-newtab-row field-label-checkbox">
76
+ <input
77
+ className="field-checkbox"
78
+ type="checkbox"
79
+ checked={newTab.checked}
80
+ onChange={(event) => newTab.onChange(event.target.checked)}
81
+ />
82
+ <span className="checkbox-label-text">Open in new tab</span>
83
+ </label>
84
+ )}
85
+ {picking && (
86
+ <LinkPicker
87
+ current={value}
88
+ onPick={(link) => {
89
+ setPicking(false);
90
+ onChange(link);
91
+ }}
92
+ onClose={() => setPicking(false)}
93
+ />
94
+ )}
95
+ </div>
96
+ );
97
+ }
98
+
99
+ /** What a link points to, in words: "Email", "Phone", "Website", or the part of the site. */
100
+ function describeLink(link: string): string {
101
+ if (link.startsWith("mailto:")) return "Email";
102
+ if (link.startsWith("tel:")) return "Phone";
103
+ if (/^https?:\/\//.test(link)) return "Website";
104
+ if (link.includes("#")) return "Part of a page";
105
+ return "Page of the site";
106
+ }
107
+
108
+ type Tab = "site" | "external";
109
+ type ExternalKind = "url" | "email" | "phone";
110
+
111
+ const EXTERNAL_KINDS: { key: ExternalKind; label: string; placeholder: string; prefix: string }[] = [
112
+ { key: "url", label: "Website", placeholder: "https://example.com", prefix: "" },
113
+ { key: "email", label: "Email", placeholder: "name@example.com", prefix: "mailto:" },
114
+ { key: "phone", label: "Phone", placeholder: "+40 123 456 789", prefix: "tel:" },
115
+ ];
116
+
117
+ /** A page or item the site has, as the picker lists it. */
118
+ interface Destination {
119
+ path: string;
120
+ title: string;
121
+ /** "Page", or the item's type ("Project"). */
122
+ kind: string;
123
+ private: boolean;
124
+ }
125
+
126
+ /** A part of a page a link can open at: an element of the page with an `id`. */
127
+ interface Target {
128
+ id: string;
129
+ title: string;
130
+ kind: string;
131
+ }
132
+
133
+ /**
134
+ * The picker behind every link widget, and the text toolbar's link: the site's pages and items (search, private ones
135
+ * marked), then the whole page or a part of it; or a website, an email address, a phone number.
136
+ */
137
+ export function LinkPicker({
138
+ current,
139
+ onPick,
140
+ onClose,
141
+ }: {
142
+ /** The link being changed, to open on its tab and mark it. */
143
+ current: string;
144
+ onPick: (link: string) => void;
145
+ onClose: () => void;
146
+ }) {
147
+ const external = /^(https?:\/\/|mailto:|tel:)/.test(current);
148
+ const [tab, setTab] = useState<Tab>(external ? "external" : "site");
149
+
150
+ useEffect(() => {
151
+ const onKeyDown = (event: KeyboardEvent): void => {
152
+ if (event.key === "Escape" && !isComposing(event)) onClose();
153
+ };
154
+ document.addEventListener("keydown", onKeyDown);
155
+ return () => document.removeEventListener("keydown", onKeyDown);
156
+ }, [onClose]);
157
+
158
+ return createPortal(
159
+ <div className="cb-modal-overlay" onClick={onClose}>
160
+ <div
161
+ className="brim-link-picker"
162
+ role="dialog"
163
+ aria-modal="true"
164
+ aria-label="Choose a link"
165
+ onClick={(event) => event.stopPropagation()}
166
+ >
167
+ <header className="brim-link-picker__header">
168
+ <h3>Choose a link</h3>
169
+ <button type="button" className="brim-sidebar-close-btn" aria-label="Close" onClick={onClose}>
170
+ <CloseIcon />
171
+ </button>
172
+ </header>
173
+ <div className="brim-link-picker__tabs" role="tablist">
174
+ <button type="button" role="tab" aria-selected={tab === "site"} onClick={() => setTab("site")}>
175
+ <PageIcon />
176
+ Site
177
+ </button>
178
+ <button type="button" role="tab" aria-selected={tab === "external"} onClick={() => setTab("external")}>
179
+ <LinkIcon />
180
+ Website, email or phone
181
+ </button>
182
+ </div>
183
+ {tab === "site" ? (
184
+ <SiteTab current={current} onPick={onPick} />
185
+ ) : (
186
+ <ExternalTab current={external ? current : ""} onPick={onPick} />
187
+ )}
188
+ </div>
189
+ </div>,
190
+ document.body,
191
+ );
192
+ }
193
+
194
+ function ExternalTab({ current, onPick }: { current: string; onPick: (link: string) => void }) {
195
+ const initial = EXTERNAL_KINDS.find((kind) => kind.prefix && current.startsWith(kind.prefix)) ?? EXTERNAL_KINDS[0]!;
196
+ const [kind, setKind] = useState(initial);
197
+ const [value, setValue] = useState(current.slice(initial.prefix.length));
198
+
199
+ function apply(event: FormEvent): void {
200
+ event.preventDefault();
201
+ const typed = value.trim();
202
+ if (!typed) return;
203
+ if (kind.key === "url") return onPick(/^https?:\/\//.test(typed) ? typed : `https://${typed}`);
204
+ onPick(kind.prefix + (kind.key === "phone" ? typed.replace(/[^\d+]/g, "") : typed));
205
+ }
206
+
207
+ return (
208
+ <form className="brim-link-picker__body" onSubmit={apply}>
209
+ <div className="brim-link-picker__kinds" role="radiogroup" aria-label="Kind of link">
210
+ {EXTERNAL_KINDS.map((option) => (
211
+ <button
212
+ key={option.key}
213
+ type="button"
214
+ role="radio"
215
+ aria-checked={option.key === kind.key}
216
+ onClick={() => setKind(option)}
217
+ >
218
+ {option.label}
219
+ </button>
220
+ ))}
221
+ </div>
222
+ <input
223
+ className="field-input"
224
+ type={kind.key === "email" ? "email" : kind.key === "phone" ? "tel" : "text"}
225
+ aria-label={kind.label}
226
+ placeholder={kind.placeholder}
227
+ autoFocus
228
+ value={value}
229
+ onChange={(event) => setValue(event.target.value)}
230
+ />
231
+ <div className="cb-modal-actions">
232
+ <button type="submit" className="cb-new-btn" disabled={value.trim() === ""}>
233
+ Use this link
234
+ </button>
235
+ </div>
236
+ </form>
237
+ );
238
+ }
239
+
240
+ function SiteTab({ current, onPick }: { current: string; onPick: (link: string) => void }) {
241
+ const { pages } = useEditor();
242
+ const locales = useSiteLocales();
243
+ const [types, setTypes] = useState<ContentType[]>([]);
244
+ const [items, setItems] = useState<Record<string, Item[]>>({});
245
+ const [query, setQuery] = useState("");
246
+ const [chosen, setChosen] = useState<Destination | null>(null);
247
+
248
+ useEffect(() => {
249
+ void (async () => {
250
+ const { types: list } = await api.types();
251
+ const withPages = list.filter((type) => type.hasPage);
252
+ const lists = await Promise.all(withPages.map((type) => api.items(type.key)));
253
+ setTypes(withPages);
254
+ setItems(Object.fromEntries(withPages.map((type, index) => [type.key, lists[index]?.items ?? []])));
255
+ })().catch(() => undefined);
256
+ }, []);
257
+
258
+ const groups = useMemo(() => {
259
+ const pageDestinations = pages.map((page: SitePage): Destination => ({
260
+ path: page.path,
261
+ title: pageTitle(page.path, page.title),
262
+ kind: "Page",
263
+ private: page.visibility === "private",
264
+ }));
265
+ const all = [
266
+ { title: "Pages", destinations: pageDestinations },
267
+ ...types.map((type) => ({
268
+ title: type.label,
269
+ destinations: (items[type.key] ?? []).map((item): Destination => ({
270
+ path: itemPagePath(type, item, locales ?? undefined),
271
+ title: item.title,
272
+ kind: type.singular,
273
+ private: item.visibility === "private",
274
+ })),
275
+ })),
276
+ ];
277
+ const words = query.trim().toLowerCase();
278
+ return all
279
+ .map((group) => ({
280
+ ...group,
281
+ destinations: words
282
+ ? group.destinations.filter((one) => `${one.title} ${one.path}`.toLowerCase().includes(words))
283
+ : group.destinations,
284
+ }))
285
+ .filter((group) => group.destinations.length > 0);
286
+ }, [pages, types, items, locales, query]);
287
+
288
+ if (chosen) return <TargetStep destination={chosen} onBack={() => setChosen(null)} onPick={onPick} />;
289
+
290
+ const currentPath = current.split("#")[0];
291
+ return (
292
+ <div className="brim-link-picker__body">
293
+ <label className="brim-link-picker__search">
294
+ <SearchIcon />
295
+ <input
296
+ type="search"
297
+ aria-label="Search the site"
298
+ placeholder="Search pages and content…"
299
+ autoFocus
300
+ value={query}
301
+ onChange={(event) => setQuery(event.target.value)}
302
+ />
303
+ </label>
304
+ <div className="brim-link-picker__list">
305
+ {groups.length === 0 && <p className="brim-sidebar-note">Nothing matches "{query}".</p>}
306
+ {groups.map((group) => (
307
+ <section key={group.title} aria-label={group.title}>
308
+ <h4 className="brim-link-picker__group">{group.title}</h4>
309
+ {group.destinations.map((destination) => (
310
+ <div
311
+ key={destination.path}
312
+ className="brim-link-picker__row"
313
+ aria-current={destination.path === currentPath ? "true" : undefined}
314
+ >
315
+ <button type="button" className="brim-link-picker__choose" onClick={() => onPick(destination.path)}>
316
+ {destination.kind === "Page" ? <PageIcon /> : <FolderIcon />}
317
+ <span className="brim-link-picker__text">
318
+ <span className="brim-link-picker__title">{destination.title}</span>
319
+ <code>{destination.path}</code>
320
+ </span>
321
+ {destination.private && <span className="cb-badge cb-badge-status private">private</span>}
322
+ </button>
323
+ <button
324
+ type="button"
325
+ className="brim-link-picker__parts"
326
+ title="Link to a part of this page"
327
+ aria-label={`Link to a part of ${destination.title}`}
328
+ onClick={() => setChosen(destination)}
329
+ >
330
+ <ChevronRightIcon />
331
+ </button>
332
+ </div>
333
+ ))}
334
+ </section>
335
+ ))}
336
+ </div>
337
+ </div>
338
+ );
339
+ }
340
+
341
+ /** Elements that make sense to open a page at; a script's or an input's id does not. */
342
+ const TARGET_TAGS: Record<string, string> = {
343
+ SECTION: "Section",
344
+ HEADER: "Section",
345
+ FOOTER: "Section",
346
+ MAIN: "Section",
347
+ ARTICLE: "Section",
348
+ ASIDE: "Section",
349
+ NAV: "Section",
350
+ DIV: "Section",
351
+ FIGURE: "Section",
352
+ FORM: "Form",
353
+ H1: "Heading",
354
+ H2: "Heading",
355
+ H3: "Heading",
356
+ H4: "Heading",
357
+ H5: "Heading",
358
+ H6: "Heading",
359
+ };
360
+
361
+ /** The page as visitors get it, read for the elements with an `id`: the parts a link can open at. */
362
+ async function targetsOf(path: string): Promise<Target[]> {
363
+ const html = await (await fetch(path, { credentials: "same-origin" })).text();
364
+ const page = new DOMParser().parseFromString(html, "text/html");
365
+ const seen = new Set<string>();
366
+ return [...page.body.querySelectorAll<HTMLElement>("[id]")].flatMap((element) => {
367
+ const kind = TARGET_TAGS[element.tagName];
368
+ const id = element.id;
369
+ // A tab's panel or a hidden element only shows once something on the page opens it.
370
+ const hidden = element.closest('[hidden], [aria-hidden="true"], [role="tabpanel"], template');
371
+ if (!kind || !id || seen.has(id) || hidden || /^(astro|brim)/.test(id)) return [];
372
+ seen.add(id);
373
+ const heading = kind === "Heading" ? element : element.querySelector("h1, h2, h3, h4, h5, h6");
374
+ const words = (heading?.textContent ?? element.textContent ?? "").replace(/\s+/g, " ").trim();
375
+ return [{ id, kind, title: words.length > 70 ? `${words.slice(0, 69)}…` : words || id }];
376
+ });
377
+ }
378
+
379
+ function TargetStep({
380
+ destination,
381
+ onBack,
382
+ onPick,
383
+ }: {
384
+ destination: Destination;
385
+ onBack: () => void;
386
+ onPick: (link: string) => void;
387
+ }) {
388
+ const [targets, setTargets] = useState<Target[] | null>(null);
389
+ const [error, setError] = useState(false);
390
+ useEffect(() => {
391
+ targetsOf(destination.path).then(setTargets, () => setError(true));
392
+ }, [destination.path]);
393
+
394
+ return (
395
+ <div className="brim-link-picker__body">
396
+ <div className="brim-link-picker__step">
397
+ <button type="button" className="brim-sidebar-close-btn" aria-label="Back to the site" onClick={onBack}>
398
+ <ArrowLeftIcon />
399
+ </button>
400
+ <span>
401
+ <span className="brim-link-picker__kicker">Link to a part of</span>
402
+ <span className="brim-link-picker__title">{destination.title}</span>
403
+ </span>
404
+ </div>
405
+ <div className="brim-link-picker__list">
406
+ <div className="brim-link-picker__row">
407
+ <button type="button" className="brim-link-picker__choose" onClick={() => onPick(destination.path)}>
408
+ <PageIcon />
409
+ <span className="brim-link-picker__text">
410
+ <span className="brim-link-picker__title">Whole page</span>
411
+ <code>{destination.path}</code>
412
+ </span>
413
+ </button>
414
+ </div>
415
+ {error && <p className="field-error">This page could not be read.</p>}
416
+ {!error && targets === null && <p className="brim-sidebar-note">Reading the page…</p>}
417
+ {targets?.length === 0 && (
418
+ <p className="brim-sidebar-note">
419
+ This page has no parts to link to: its code gives none of its sections an id.
420
+ </p>
421
+ )}
422
+ {targets?.map((target) => (
423
+ <div key={target.id} className="brim-link-picker__row">
424
+ <button
425
+ type="button"
426
+ className="brim-link-picker__choose"
427
+ onClick={() => onPick(`${destination.path}#${target.id}`)}
428
+ >
429
+ <span className="brim-link-picker__kind">{target.kind}</span>
430
+ <span className="brim-link-picker__text">
431
+ <span className="brim-link-picker__title">{target.title}</span>
432
+ <code>#{target.id}</code>
433
+ </span>
434
+ </button>
435
+ </div>
436
+ ))}
437
+ </div>
438
+ </div>
439
+ );
440
+ }
@@ -0,0 +1,99 @@
1
+ import { useEffect, useState } from "react";
2
+ import { type ContentType, api } from "./api.ts";
3
+ import { FolderIcon, PlusIcon } from "./icons.tsx";
4
+ import { Sidebar } from "./Sidebar.tsx";
5
+
6
+ /** What `<Listing>` tells the editor: its type, how many items it shows, and what a new item starts with. */
7
+ interface ListingInfo {
8
+ type: string;
9
+ count: number;
10
+ prefill: Record<string, unknown>;
11
+ }
12
+
13
+ function readListing(element: HTMLElement): ListingInfo | undefined {
14
+ try {
15
+ const value = JSON.parse(element.dataset.brimValue ?? "") as Partial<ListingInfo>;
16
+ if (typeof value.type !== "string") return undefined;
17
+ return { type: value.type, count: Number(value.count) || 0, prefill: value.prefill ?? {} };
18
+ } catch {
19
+ return undefined;
20
+ }
21
+ }
22
+
23
+ /**
24
+ * The panel of a selected listing. What it shows is the site's code; the owner changes what is listed by editing
25
+ * the items, so the panel opens them, or adds one that lands in this listing (its filter fills the new item in).
26
+ */
27
+ export function ListingPanel({
28
+ element,
29
+ onOpen,
30
+ onNew,
31
+ onClose,
32
+ }: {
33
+ element: HTMLElement;
34
+ onOpen: (type: string) => void;
35
+ onNew: (type: string, prefill: Record<string, unknown>) => void;
36
+ onClose: () => void;
37
+ }) {
38
+ const listing = readListing(element);
39
+ const [type, setType] = useState<ContentType | undefined>();
40
+ useEffect(() => {
41
+ if (!listing) return;
42
+ void api.types().then(({ types }) => setType(types.find((candidate) => candidate.key === listing.type)));
43
+ }, [listing?.type]);
44
+
45
+ if (!listing) {
46
+ return (
47
+ <Sidebar title="Listing" onClose={onClose}>
48
+ <p className="brim-sidebar-note">This listing does not say which content it shows.</p>
49
+ </Sidebar>
50
+ );
51
+ }
52
+ const label = type?.label ?? listing.type;
53
+ const singular = type?.singular ?? "item";
54
+ const prefilled = Object.entries(listing.prefill);
55
+
56
+ return (
57
+ <Sidebar title={label} onClose={onClose}>
58
+ <div className="brim-listing-panel">
59
+ <p className="brim-listing-panel__count">
60
+ {listing.count} {listing.count === 1 ? "item" : "items"} in this listing
61
+ </p>
62
+ <p className="brim-sidebar-note">
63
+ Which {label.toLowerCase()} show here, and in what order, is set in the site's code. Change what it lists by
64
+ editing the {label.toLowerCase()}: a private one shows only here in the editor until it is published.
65
+ </p>
66
+ <div className="brim-listing-panel__actions">
67
+ <button type="button" className="url-browse-btn" onClick={() => onNew(listing.type, listing.prefill)}>
68
+ <PlusIcon />
69
+ New {singular.toLowerCase()}
70
+ </button>
71
+ <button
72
+ type="button"
73
+ className="brim-toolbar-btn brim-toolbar-btn--outline"
74
+ onClick={() => onOpen(listing.type)}
75
+ >
76
+ <FolderIcon />
77
+ Open {label.toLowerCase()}
78
+ </button>
79
+ </div>
80
+ {prefilled.length > 0 && (
81
+ <p className="field-description">
82
+ A new {singular.toLowerCase()} added here starts with{" "}
83
+ {prefilled.map(([field, value], index) => (
84
+ <span key={field}>
85
+ {index > 0 && ", "}
86
+ <strong>{fieldLabel(type, field)}</strong>: {String(value)}
87
+ </span>
88
+ ))}
89
+ , so it shows in this listing.
90
+ </p>
91
+ )}
92
+ </div>
93
+ </Sidebar>
94
+ );
95
+ }
96
+
97
+ function fieldLabel(type: ContentType | undefined, field: string): string {
98
+ return type?.fields.find((candidate) => candidate.name === field)?.label ?? field;
99
+ }