@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,134 @@
1
+ /* A gallery's pictures over the whole screen (lightbox.client.ts). */
2
+ /* In the `brim` layer: any CSS of the site wins over it, whatever its selectors. */
3
+ @layer brim {
4
+ [data-lightbox][role="button"] {
5
+ cursor: zoom-in;
6
+ }
7
+
8
+ html:has(.brim-lightbox[open]) {
9
+ overflow: hidden;
10
+ }
11
+
12
+ .brim-lightbox {
13
+ position: fixed;
14
+ inset: 0;
15
+ width: 100%;
16
+ max-width: none;
17
+ height: 100%;
18
+ max-height: none;
19
+ margin: 0;
20
+ padding: 0;
21
+ border: 0;
22
+ background: var(--brim-lightbox-background, rgb(16 16 16 / 0.96));
23
+ color: var(--brim-lightbox-color, #fff);
24
+ overflow: hidden;
25
+ }
26
+
27
+ .brim-lightbox__track {
28
+ position: relative;
29
+ display: flex;
30
+ width: 100%;
31
+ height: 100%;
32
+ overflow: auto hidden;
33
+ overscroll-behavior: contain;
34
+ scroll-snap-type: x mandatory;
35
+ scrollbar-width: none;
36
+ }
37
+
38
+ .brim-lightbox__slide {
39
+ display: flex;
40
+ flex: 0 0 100%;
41
+ flex-direction: column;
42
+ align-items: center;
43
+ justify-content: center;
44
+ gap: 0.75rem;
45
+ min-width: 0;
46
+ height: 100%;
47
+ margin: 0;
48
+ padding: 4rem 5rem;
49
+ scroll-snap-align: start;
50
+ scroll-snap-stop: always;
51
+ }
52
+
53
+ /* Fits whatever a site's own `img` rules set (`max-width: 100%; height: auto`), and is never stretched. */
54
+ .brim-lightbox__image {
55
+ width: auto;
56
+ min-height: 0;
57
+ max-width: 100%;
58
+ max-height: 100%;
59
+ object-fit: contain;
60
+ }
61
+
62
+ .brim-lightbox__caption {
63
+ flex: none;
64
+ max-width: 42rem;
65
+ text-align: center;
66
+ }
67
+
68
+ .brim-lightbox__count {
69
+ position: absolute;
70
+ inset-block-start: 1.25rem;
71
+ inset-inline-start: 1.25rem;
72
+ margin: 0;
73
+ font-variant-numeric: tabular-nums;
74
+ }
75
+
76
+ .brim-lightbox__button {
77
+ position: absolute;
78
+ display: grid;
79
+ place-items: center;
80
+ width: 3rem;
81
+ height: 3rem;
82
+ padding: 0;
83
+ border: 0;
84
+ border-radius: 50%;
85
+ background: rgb(128 128 128 / 0.35);
86
+ color: inherit;
87
+ cursor: pointer;
88
+ }
89
+
90
+ .brim-lightbox__close {
91
+ inset-block-start: 0.75rem;
92
+ inset-inline-end: 0.75rem;
93
+ }
94
+
95
+ .brim-lightbox__previous,
96
+ .brim-lightbox__next {
97
+ inset-block-start: calc(50% - 1.5rem);
98
+ }
99
+
100
+ .brim-lightbox__previous {
101
+ inset-inline-start: 0.75rem;
102
+ }
103
+
104
+ .brim-lightbox__next {
105
+ inset-inline-end: 0.75rem;
106
+ }
107
+
108
+ :dir(rtl) > .brim-lightbox__button svg {
109
+ transform: scaleX(-1);
110
+ }
111
+
112
+ @media (max-width: 700px) {
113
+ .brim-lightbox__slide {
114
+ padding: 4rem 0 5rem;
115
+ }
116
+
117
+ .brim-lightbox__caption {
118
+ padding-inline: 1rem;
119
+ }
120
+
121
+ .brim-lightbox__previous,
122
+ .brim-lightbox__next {
123
+ inset-block: auto calc(1rem + env(safe-area-inset-bottom));
124
+ }
125
+
126
+ .brim-lightbox__previous {
127
+ inset-inline-start: calc(50% - 3.5rem);
128
+ }
129
+
130
+ .brim-lightbox__next {
131
+ inset-inline-end: calc(50% - 3.5rem);
132
+ }
133
+ }
134
+ }
@@ -0,0 +1,41 @@
1
+ ---
2
+ import { defaultScope } from "../../runtime/request-state.ts";
3
+ import "./icon.css";
4
+ import type { NodeScope } from "../../runtime/context.ts";
5
+ import { icons } from "../../runtime/icons.ts";
6
+ import { icon as iconBlock } from "../schemas/icon.ts";
7
+
8
+ /**
9
+ * One icon, from Brim's own set or a pack the site added; the owner can pick another in the editor. It is drawn in
10
+ * the text's colour, `1em` wide; its size, colour and any shape behind it are the site's CSS. `class` goes on it.
11
+ */
12
+ interface Props {
13
+ nodeId: string;
14
+ scope?: NodeScope;
15
+ icon?: string;
16
+ class?: string;
17
+ }
18
+
19
+ const { nodeId, scope = defaultScope(), class: className, ...source } = Astro.props;
20
+ const settings = Astro.locals.brim.settings(iconBlock, nodeId, scope, source);
21
+ // An icon whose pack was taken out of the config falls back to the star, so the place is not left empty.
22
+ const drawn = icons.find(settings.icon) ?? icons.find("star")!;
23
+ // A pack's icon carries its own look; only Brim's outlines are drawn and tinted by the block.
24
+ const outline = drawn.outline && {
25
+ fill: "none",
26
+ stroke: "currentColor",
27
+ "stroke-width": "2",
28
+ "stroke-linecap": "round",
29
+ "stroke-linejoin": "round",
30
+ };
31
+ ---
32
+
33
+ <span class:list={["brim-icon", className]} {...Astro.locals.brim.marker("icon", nodeId, scope, settings)}>
34
+ <svg
35
+ xmlns="http://www.w3.org/2000/svg"
36
+ viewBox={drawn.viewBox}
37
+ {...outline}
38
+ aria-hidden="true"
39
+ set:html={drawn.body}
40
+ />
41
+ </span>
@@ -0,0 +1,16 @@
1
+ /* An icon in the text's colour; --brim-icon-size, or the site's CSS on `.brim-icon`, sets how big. */
2
+ /* In the `brim` layer: any CSS of the site wins over it, whatever its selectors. */
3
+ @layer brim {
4
+ .brim-icon {
5
+ display: inline-flex;
6
+ align-items: center;
7
+ justify-content: center;
8
+ width: var(--brim-icon-size, 2.5rem);
9
+ height: var(--brim-icon-size, 2.5rem);
10
+ }
11
+
12
+ .brim-icon svg {
13
+ width: 100%;
14
+ height: 100%;
15
+ }
16
+ }
@@ -0,0 +1,158 @@
1
+ ---
2
+ import { defaultScope } from "../../runtime/request-state.ts";
3
+ import "./image.css";
4
+ import { type ItemBinding, boundValue } from "../../content/item-fields.ts";
5
+ import type { Item } from "../../content/items.ts";
6
+ import type { ImageValue } from "../../content/types.ts";
7
+ import type { NodeScope } from "../../runtime/context.ts";
8
+ import { image } from "../schemas/image.ts";
9
+ import { imageSources } from "../../runtime/media.ts";
10
+
11
+ /**
12
+ * A picture the owner replaces, describes, captions and links, with the part that stays in sight when the site's
13
+ * CSS crops it (its focal point). Every prop is a starting value; `src` is the image's address. With `item` and
14
+ * `field` it shows an image field of a content item (`src` and `alt` are then used while the field is empty) and
15
+ * replacing it saves the item.
16
+ *
17
+ * Brim draws it in a `<figure>` as wide as it is given. `bare` draws the `<img>` alone, with every attribute the code
18
+ * gives it (`class`, `loading`, `sizes`…), for a site whose markup and CSS expect a plain image.
19
+ *
20
+ * A starting picture in public/ can come with smaller copies of its own: `srcset="/hero-1024.webp 1024w, …"` with
21
+ * `sizes`. They are drawn while that picture is shown; once the owner chose another, Brim gives the new one its own.
22
+ */
23
+ interface Props {
24
+ nodeId: string;
25
+ src: string;
26
+ alt: string;
27
+ item?: Item;
28
+ field?: string;
29
+ scope?: NodeScope;
30
+ width?: number | string;
31
+ height?: number | string;
32
+ showCaption?: boolean;
33
+ caption?: string;
34
+ link?: string;
35
+ openInNewTab?: boolean;
36
+ /** Only the `<img>`, with the attributes below, and no frame: see above. */
37
+ bare?: boolean;
38
+ /**
39
+ * The picture a visitor sees first (a hero): loaded at once and ahead of the page's other pictures, where every
40
+ * other image waits until it is scrolled near. One per page at most.
41
+ */
42
+ priority?: boolean;
43
+ /** Where the picture is drawn narrower than the page, how wide: `"(max-width: 768px) 100vw, 50vw"`. */
44
+ sizes?: string;
45
+ /** With `bare`, any other attribute goes on the `<img>` as it is. */
46
+ [attribute: string]: unknown;
47
+ }
48
+
49
+ /** The props that are the image's settings; with `bare`, every other prop is an attribute of the `<img>`. */
50
+ const SETTINGS = new Set([...Object.keys(image.schema.properties), "openInNewTab"]);
51
+ /** Settings an image had before its look became the site's: still accepted from older code, and left out. */
52
+ const FORMER = new Set([
53
+ "size",
54
+ "customWidth",
55
+ "rotation",
56
+ "objectFit",
57
+ "objectPosition",
58
+ "shape",
59
+ "stretchShape",
60
+ "opacity",
61
+ "overlay",
62
+ ]);
63
+
64
+ const {
65
+ nodeId,
66
+ scope = defaultScope(),
67
+ src,
68
+ width,
69
+ height,
70
+ item,
71
+ field,
72
+ bare = false,
73
+ priority = false,
74
+ ...rest
75
+ } = Astro.props;
76
+ const source = Object.fromEntries(Object.entries(rest).filter(([name]) => SETTINGS.has(name)));
77
+ const attributes = Object.fromEntries(
78
+ Object.entries(rest).filter(([name]) => !SETTINGS.has(name) && !FORMER.has(name)),
79
+ );
80
+ const binding: ItemBinding | undefined = item && field ? { item, field } : undefined;
81
+ const bound = binding ? (boundValue(binding) as ImageValue | undefined) : undefined;
82
+ const settings = Astro.locals.brim.settings(image, nodeId, scope, { ...source, url: src });
83
+ // The item's image wins over anything stored with the element: the item is where it is saved.
84
+ if (bound) Object.assign(settings, { url: bound.src, alt: bound.alt });
85
+
86
+ // The focal point only shows where the site crops the image (`object-fit: cover`); elsewhere it changes nothing.
87
+ const x = Number(settings.focalPointX ?? 50);
88
+ const y = Number(settings.focalPointY ?? 50);
89
+ const focus = x === 50 && y === 50 ? undefined : `object-position:${x}% ${y}%`;
90
+ const style = [typeof attributes.style === "string" ? attributes.style : undefined, focus].filter(Boolean).join(";");
91
+ const sizes = typeof attributes.sizes === "string" ? attributes.sizes : undefined;
92
+ // A `srcset` from the code lists copies of its starting picture (smaller files in public/): it goes with that picture
93
+ // only. Once the owner chose another one, it would make browsers load the old picture, so it is left out.
94
+ const startingSrcset = typeof attributes.srcset === "string" && settings.url === src ? attributes.srcset : undefined;
95
+ delete attributes.srcset;
96
+ // `sizes` goes with a srcset (in `sources` below), once: Astro writes an attribute of two spreads twice.
97
+ delete attributes.sizes;
98
+ const href = typeof settings.link === "string" && settings.link !== "" ? settings.link : undefined;
99
+ const Frame = href ? "a" : "span";
100
+ const marker = Astro.locals.brim.marker("image", nodeId, scope, settings, binding);
101
+ // Astro writes a spread attribute and the same one written after it both; the size is set once, below.
102
+ const { width: measuredWidth, height: measuredHeight, ...measured } = imageSources(String(settings.url ?? ""), sizes);
103
+ const sources =
104
+ measured.srcset || !startingSrcset ? measured : { ...measured, srcset: startingSrcset, ...(sizes && { sizes }) };
105
+ // A picture seen first is asked for at once and before the others; any other waits until it is scrolled near.
106
+ const loading = priority ? { loading: "eager", fetchpriority: "high" } : { loading: "lazy" };
107
+ // With `bare` the code's own attributes win; Astro writes an attribute of two spreads twice, so each goes in once.
108
+ const bareLoading = priority
109
+ ? Object.fromEntries(Object.entries(loading).filter(([name]) => !(name in attributes)))
110
+ : {};
111
+ // The code's width and height, when it gives either, are the image's; otherwise the size the media library measured,
112
+ // so the browser keeps its place. Both come from one or the other, so the shape stays the picture's.
113
+ const given = width !== undefined || height !== undefined;
114
+ const drawnWidth = given ? width : measuredWidth;
115
+ const drawnHeight = given ? height : measuredHeight;
116
+ ---
117
+
118
+ {bare ? (
119
+ settings.url || Astro.locals.brim.editing ? (
120
+ <img
121
+ {...bareLoading}
122
+ {...attributes}
123
+ {...sources}
124
+ alt={String(settings.alt ?? "")}
125
+ width={drawnWidth}
126
+ height={drawnHeight}
127
+ style={style || undefined}
128
+ data-brim-bare={Astro.locals.brim.editing ? "" : undefined}
129
+ {...marker}
130
+ />
131
+ ) : null
132
+ ) : (
133
+ <figure class:list={["brim-image", attributes.class as string | undefined]} {...marker}>
134
+ <Frame
135
+ class="brim-image__frame"
136
+ href={href}
137
+ target={href && settings.openInNewTab ? "_blank" : undefined}
138
+ rel={href && settings.openInNewTab ? "noopener noreferrer" : undefined}
139
+ >
140
+ {settings.url ? (
141
+ <img
142
+ {...sources}
143
+ alt={String(settings.alt ?? "")}
144
+ width={drawnWidth}
145
+ height={drawnHeight}
146
+ style={focus}
147
+ {...loading}
148
+ decoding="async"
149
+ />
150
+ ) : (
151
+ Astro.locals.brim.editing && <span class="brim-image__empty">No image yet. Select this block to add one.</span>
152
+ )}
153
+ </Frame>
154
+ {settings.showCaption && settings.caption && (
155
+ <figcaption class="brim-image__caption">{settings.caption}</figcaption>
156
+ )}
157
+ </figure>
158
+ )}
@@ -0,0 +1,42 @@
1
+ /* The frame Brim draws around an image: as wide as the picture, never wider than its place. */
2
+ /* In the `brim` layer: any CSS of the site wins over it, whatever its selectors. */
3
+ @layer brim {
4
+ .brim-image {
5
+ display: flex;
6
+ flex-direction: column;
7
+ gap: 0.5rem;
8
+ max-width: 100%;
9
+ margin: 0;
10
+ }
11
+
12
+ .brim-image__frame {
13
+ display: block;
14
+ max-width: 100%;
15
+ }
16
+
17
+ .brim-image__frame img {
18
+ display: block;
19
+ max-width: 100%;
20
+ height: auto;
21
+ }
22
+
23
+ .brim-image__caption {
24
+ font-size: 0.875em;
25
+ }
26
+
27
+ /* Only in the editor: where an image without a picture yet is, so it can be selected. */
28
+ .brim-image__empty {
29
+ display: flex;
30
+ align-items: center;
31
+ justify-content: center;
32
+ width: 32rem;
33
+ max-width: 100%;
34
+ aspect-ratio: 16 / 9;
35
+ padding: 1rem;
36
+ border: 1px dashed color-mix(in srgb, currentColor 35%, transparent);
37
+ background: color-mix(in srgb, currentColor 6%, transparent);
38
+ font-size: 0.875rem;
39
+ text-align: center;
40
+ opacity: 0.75;
41
+ }
42
+ }
@@ -0,0 +1,64 @@
1
+ import type { ImageSources } from "../runtime/media.ts";
2
+ /**
3
+ * The images of a `Gallery` or a `Carousel`: what the owner adds, orders and describes in the side panel. The
4
+ * primitive keeps them and the sizes visitors download; how they are laid out is the site's markup.
5
+ */
6
+ export interface ShownImage {
7
+ src: string;
8
+ alt: string;
9
+ caption?: string;
10
+ link?: string;
11
+ /**
12
+ * Everything an `<img>` needs, a `srcset` included when the site's media can be resized and its `width` and
13
+ * `height` when the media library measured it: `<img {...image.img} />`.
14
+ * In a `Gallery` whose pictures open over the whole screen, also what makes this one open (`data-lightbox`).
15
+ */
16
+ img: ImageSources & { alt: string; "data-lightbox"?: number; "data-caption"?: string | undefined };
17
+ }
18
+
19
+ /** What a `Gallery`'s function gets: `{({ images }) => …}`. */
20
+ export interface GalleryValue {
21
+ images: ShownImage[];
22
+ }
23
+
24
+ /** What a `Carousel`'s function gets: `{({ slides }) => …}`. */
25
+ export interface CarouselValue {
26
+ slides: ShownImage[];
27
+ }
28
+
29
+ /** An image as the code starts it: `src` (or `url`, as BrimCMS named it), with an optional caption and link. */
30
+ export interface StartingImage {
31
+ src?: string;
32
+ url?: string;
33
+ alt?: string;
34
+ caption?: string;
35
+ link?: string;
36
+ }
37
+
38
+ const SAFE_LINK = /^(https?:\/\/|\/(?!\/)|#|mailto:|tel:)/i;
39
+
40
+ /**
41
+ * Reads a list of images, whichever way it was saved: `{ url }` by the side panel, `{ src }` by an item's gallery
42
+ * field and the code; `title` was a gallery image's caption before. Entries without an address are left out.
43
+ * `sources` gives an image's `<img>` attributes: `imageSources` with the sizes it is drawn at.
44
+ */
45
+ export function readImages(value: unknown, sources: (src: string) => ImageSources): ShownImage[] {
46
+ if (!Array.isArray(value)) return [];
47
+ return value.flatMap((entry): ShownImage[] => {
48
+ if (typeof entry !== "object" || entry === null) return [];
49
+ const image = entry as Record<string, unknown>;
50
+ const src = typeof image.src === "string" ? image.src : typeof image.url === "string" ? image.url : "";
51
+ if (src === "") return [];
52
+ const alt = typeof image.alt === "string" ? image.alt : "";
53
+ const caption = [image.caption, image.title].find(
54
+ (text): text is string => typeof text === "string" && text !== "",
55
+ );
56
+ const link = typeof image.link === "string" && SAFE_LINK.test(image.link) ? image.link : undefined;
57
+ return [{ src, alt, ...(caption && { caption }), ...(link && { link }), img: { ...sources(src), alt } }];
58
+ });
59
+ }
60
+
61
+ /** Starting images from the code, in the shape the side panel saves (`url`), so the owner edits the same list. */
62
+ export function toSaved(images: StartingImage[] | undefined): Record<string, unknown>[] | undefined {
63
+ return images?.map(({ src, url, ...rest }) => ({ ...rest, url: src ?? url ?? "" }));
64
+ }
@@ -0,0 +1,8 @@
1
+ /** The page is drawn inside Brim's editor. */
2
+ export function inEditor(): boolean {
3
+ try {
4
+ return window.self !== window.top && (window.top?.location.pathname.startsWith("/admin/edit") ?? false);
5
+ } catch {
6
+ return false;
7
+ }
8
+ }