@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.
- package/LICENSE +110 -0
- package/README.md +22 -2
- package/bin/brim.js +5 -0
- package/dist/cli.js +3802 -0
- package/editor-dist/editor.css +1 -0
- package/editor-dist/editor.js +91 -0
- package/package.json +71 -3
- package/skills/brim-convert/SKILL.md +256 -0
- package/skills/brim-hosting/SKILL.md +221 -0
- package/src/astro-shim.d.ts +5 -0
- package/src/auth/accounts.ts +341 -0
- package/src/auth/password.ts +34 -0
- package/src/auth/permissions.ts +44 -0
- package/src/blocks/accordion/Accordion.astro +48 -0
- package/src/blocks/accordion/accordion.css +63 -0
- package/src/blocks/analytics/Analytics.astro +45 -0
- package/src/blocks/analytics/ConsentBanner.astro +41 -0
- package/src/blocks/analytics/analytics.client.ts +125 -0
- package/src/blocks/analytics/analytics.css +51 -0
- package/src/blocks/analytics/consent.ts +51 -0
- package/src/blocks/button/Button.astro +64 -0
- package/src/blocks/button/button.css +100 -0
- package/src/blocks/carousel/Carousel.astro +140 -0
- package/src/blocks/carousel/carousel.client.ts +153 -0
- package/src/blocks/carousel/carousel.css +181 -0
- package/src/blocks/data/icons.ts +285 -0
- package/src/blocks/data/image-shapes.ts +91 -0
- package/src/blocks/data/social-icons.ts +41 -0
- package/src/blocks/data/social-networks.ts +256 -0
- package/src/blocks/divider/Divider.astro +27 -0
- package/src/blocks/divider/divider.css +17 -0
- package/src/blocks/form/Captcha.astro +27 -0
- package/src/blocks/form/Form.astro +250 -0
- package/src/blocks/form/FormField.astro +138 -0
- package/src/blocks/form/Newsletter.astro +80 -0
- package/src/blocks/form/form.client.ts +339 -0
- package/src/blocks/form/form.css +271 -0
- package/src/blocks/gallery/ImageGallery.astro +126 -0
- package/src/blocks/gallery/gallery.css +43 -0
- package/src/blocks/gallery/lightbox.client.ts +178 -0
- package/src/blocks/gallery/lightbox.css +134 -0
- package/src/blocks/icon/Icon.astro +41 -0
- package/src/blocks/icon/icon.css +16 -0
- package/src/blocks/image/Image.astro +158 -0
- package/src/blocks/image/image.css +42 -0
- package/src/blocks/images.ts +64 -0
- package/src/blocks/in-editor.client.ts +8 -0
- package/src/blocks/listing/Listing.astro +288 -0
- package/src/blocks/listing/ListingCards.astro +48 -0
- package/src/blocks/listing/listing.css +136 -0
- package/src/blocks/listing/query.ts +363 -0
- package/src/blocks/map/Map.astro +75 -0
- package/src/blocks/map/map.css +32 -0
- package/src/blocks/schemas/accordion.ts +118 -0
- package/src/blocks/schemas/button.ts +78 -0
- package/src/blocks/schemas/carousel.ts +133 -0
- package/src/blocks/schemas/divider.ts +29 -0
- package/src/blocks/schemas/form-field-types.ts +29 -0
- package/src/blocks/schemas/form.ts +350 -0
- package/src/blocks/schemas/icon.ts +24 -0
- package/src/blocks/schemas/image-gallery.ts +54 -0
- package/src/blocks/schemas/image.ts +87 -0
- package/src/blocks/schemas/index.ts +33 -0
- package/src/blocks/schemas/listing.ts +14 -0
- package/src/blocks/schemas/map.ts +81 -0
- package/src/blocks/schemas/newsletter.ts +324 -0
- package/src/blocks/schemas/social-media.ts +348 -0
- package/src/blocks/schemas/text.ts +151 -0
- package/src/blocks/schemas/video.ts +57 -0
- package/src/blocks/search/Search.astro +147 -0
- package/src/blocks/search/search.css +69 -0
- package/src/blocks/search/state.ts +28 -0
- package/src/blocks/settings.ts +94 -0
- package/src/blocks/social/SocialMedia.astro +102 -0
- package/src/blocks/social/social-links.ts +16 -0
- package/src/blocks/social/social.css +31 -0
- package/src/blocks/theme.ts +35 -0
- package/src/blocks/types.ts +52 -0
- package/src/blocks/video/Video.astro +90 -0
- package/src/blocks/video/video-source.ts +77 -0
- package/src/blocks/video/video.client.ts +43 -0
- package/src/blocks/video/video.css +29 -0
- package/src/check.ts +1119 -0
- package/src/cli.ts +493 -0
- package/src/components/Blocks.astro +68 -0
- package/src/components/Field.astro +61 -0
- package/src/components/Footer.astro +36 -0
- package/src/components/Link.astro +18 -0
- package/src/components/MobileMenu.astro +77 -0
- package/src/components/Navbar.astro +42 -0
- package/src/components/RichText.astro +32 -0
- package/src/components/Seo.astro +202 -0
- package/src/components/Setting.astro +46 -0
- package/src/components/Shared.astro +17 -0
- package/src/components/Text.astro +91 -0
- package/src/components/blocks.css +86 -0
- package/src/components/index.ts +27 -0
- package/src/components/mobile-menu.client.ts +21 -0
- package/src/components/mobile-menu.css +52 -0
- package/src/components/text.css +48 -0
- package/src/config.ts +413 -0
- package/src/content/block-layout.ts +63 -0
- package/src/content/block-values.ts +48 -0
- package/src/content/content-check.ts +174 -0
- package/src/content/content-types.ts +408 -0
- package/src/content/drafts.ts +115 -0
- package/src/content/feed.ts +237 -0
- package/src/content/field-types.ts +107 -0
- package/src/content/history.ts +140 -0
- package/src/content/ids.ts +50 -0
- package/src/content/image-size.ts +212 -0
- package/src/content/image-sizes.ts +126 -0
- package/src/content/item-fields.ts +110 -0
- package/src/content/item-head.ts +126 -0
- package/src/content/item-image.ts +26 -0
- package/src/content/item-path.ts +122 -0
- package/src/content/items.ts +580 -0
- package/src/content/language-scopes.ts +40 -0
- package/src/content/media.ts +271 -0
- package/src/content/menus.ts +51 -0
- package/src/content/page-templates.ts +52 -0
- package/src/content/redirects.ts +112 -0
- package/src/content/references.ts +165 -0
- package/src/content/rich-text.ts +63 -0
- package/src/content/search.ts +167 -0
- package/src/content/seed.ts +229 -0
- package/src/content/site-settings.ts +111 -0
- package/src/content/sitemap.ts +143 -0
- package/src/content/store.ts +225 -0
- package/src/content/tags.ts +67 -0
- package/src/content/text-default.ts +139 -0
- package/src/content/types.ts +137 -0
- package/src/content/validate.ts +253 -0
- package/src/content/validation-error.ts +2 -0
- package/src/db/cached.ts +140 -0
- package/src/db/database.ts +15 -0
- package/src/db/libsql.ts +29 -0
- package/src/db/schema.ts +189 -0
- package/src/db/url.ts +44 -0
- package/src/editor/client/BlockSettings.tsx +1063 -0
- package/src/editor/client/Chrome.tsx +284 -0
- package/src/editor/client/ContentBrowser.tsx +1299 -0
- package/src/editor/client/Editor.tsx +1756 -0
- package/src/editor/client/HistoryPanel.tsx +114 -0
- package/src/editor/client/ImagesWidget.tsx +113 -0
- package/src/editor/client/Inspector.tsx +127 -0
- package/src/editor/client/ItemForm.tsx +535 -0
- package/src/editor/client/LinkWidget.tsx +440 -0
- package/src/editor/client/ListingPanel.tsx +99 -0
- package/src/editor/client/Login.tsx +338 -0
- package/src/editor/client/Menu.tsx +64 -0
- package/src/editor/client/PageSettings.tsx +270 -0
- package/src/editor/client/RedirectsSettings.tsx +130 -0
- package/src/editor/client/Resize.tsx +204 -0
- package/src/editor/client/Sidebar.tsx +51 -0
- package/src/editor/client/SiteMap.tsx +568 -0
- package/src/editor/client/SiteSettings.tsx +409 -0
- package/src/editor/client/TextTools.tsx +391 -0
- package/src/editor/client/Toolbar.tsx +449 -0
- package/src/editor/client/VideoUpload.tsx +251 -0
- package/src/editor/client/api.ts +417 -0
- package/src/editor/client/brand.ts +20 -0
- package/src/editor/client/connection.ts +98 -0
- package/src/editor/client/context.ts +19 -0
- package/src/editor/client/css.d.ts +2 -0
- package/src/editor/client/editor.css +4656 -0
- package/src/editor/client/fields.tsx +812 -0
- package/src/editor/client/icons.tsx +174 -0
- package/src/editor/client/keys.ts +15 -0
- package/src/editor/client/kinds.ts +289 -0
- package/src/editor/client/languages.ts +32 -0
- package/src/editor/client/main.tsx +100 -0
- package/src/editor/client/responses.ts +26 -0
- package/src/editor/client/rich-text.ts +326 -0
- package/src/editor/client/site-tree.ts +239 -0
- package/src/editor/client/theme.ts +55 -0
- package/src/editor/client/tsconfig.json +18 -0
- package/src/editor/client/tus.ts +137 -0
- package/src/editor/client/use-rect.ts +50 -0
- package/src/editor/client/widgets.tsx +1218 -0
- package/src/forms/captcha.ts +83 -0
- package/src/forms/define.ts +166 -0
- package/src/forms/fields.ts +197 -0
- package/src/forms/file-types.ts +108 -0
- package/src/forms/files.ts +216 -0
- package/src/forms/form-page.ts +9 -0
- package/src/forms/handler.ts +622 -0
- package/src/forms/spec.ts +89 -0
- package/src/forms/store.ts +96 -0
- package/src/forms/work.ts +50 -0
- package/src/icons/library.ts +144 -0
- package/src/icons/load.ts +63 -0
- package/src/index.ts +99 -0
- package/src/init.ts +257 -0
- package/src/integration.ts +469 -0
- package/src/mail/mailer.ts +197 -0
- package/src/mail/notify.ts +272 -0
- package/src/mail/settings.ts +173 -0
- package/src/mail/templates.ts +191 -0
- package/src/mcp/page-nodes.ts +209 -0
- package/src/mcp/server.ts +111 -0
- package/src/mcp/tools.ts +637 -0
- package/src/runtime/TemplatePage.astro +39 -0
- package/src/runtime/TemplatePageStatic.astro +13 -0
- package/src/runtime/admin-route.ts +80 -0
- package/src/runtime/connect.ts +53 -0
- package/src/runtime/content.ts +188 -0
- package/src/runtime/context.ts +131 -0
- package/src/runtime/form-files.ts +18 -0
- package/src/runtime/forms-route.ts +33 -0
- package/src/runtime/forms.ts +28 -0
- package/src/runtime/icons.ts +5 -0
- package/src/runtime/media.ts +175 -0
- package/src/runtime/middleware.ts +186 -0
- package/src/runtime/nav.ts +13 -0
- package/src/runtime/query-reads.ts +49 -0
- package/src/runtime/query-store.ts +16 -0
- package/src/runtime/request-state.ts +69 -0
- package/src/runtime/robots.ts +29 -0
- package/src/runtime/rss-static.ts +16 -0
- package/src/runtime/rss.ts +29 -0
- package/src/runtime/server.ts +29 -0
- package/src/runtime/sitemap.ts +34 -0
- package/src/runtime/template-paths.ts +34 -0
- package/src/runtime/uploads-route.ts +13 -0
- package/src/seed-cli.ts +253 -0
- package/src/server/auth.ts +31 -0
- package/src/server/fetch-image.ts +219 -0
- package/src/server/handler.ts +2106 -0
- package/src/server/http.ts +162 -0
- package/src/server/locales.ts +73 -0
- package/src/server/node.ts +57 -0
- package/src/server/query-params.ts +249 -0
- package/src/server/rate-limit.ts +72 -0
- package/src/server/video-host.ts +162 -0
- package/src/starter.ts +70 -0
- package/src/version.ts +4 -0
- package/src/virtual.d.ts +65 -0
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
import type { SitePage, User, Visibility } from "./api.ts";
|
|
2
|
+
import {
|
|
3
|
+
ChevronDownIcon,
|
|
4
|
+
DesktopIcon,
|
|
5
|
+
EyeIcon,
|
|
6
|
+
FolderIcon,
|
|
7
|
+
MobileIcon,
|
|
8
|
+
MoreIcon,
|
|
9
|
+
PageIcon,
|
|
10
|
+
RedoIcon,
|
|
11
|
+
SettingsIcon,
|
|
12
|
+
TabletIcon,
|
|
13
|
+
TemplateIcon,
|
|
14
|
+
UndoIcon,
|
|
15
|
+
} from "./icons.tsx";
|
|
16
|
+
import { Menu } from "./Menu.tsx";
|
|
17
|
+
import { type Theme, useTheme } from "./theme.ts";
|
|
18
|
+
|
|
19
|
+
export type Viewport = "desktop" | "tablet" | "mobile";
|
|
20
|
+
|
|
21
|
+
export interface LanguageChoice {
|
|
22
|
+
current: string;
|
|
23
|
+
options: { code: string; path: string; exists: boolean }[];
|
|
24
|
+
/** Whether this page's text can be copied into the other languages: a page's can, an item's is its own. */
|
|
25
|
+
copy: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SaveStatus {
|
|
29
|
+
state: "idle" | "saving" | "saved" | "error";
|
|
30
|
+
message?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const VIEWPORTS = [
|
|
34
|
+
{ value: "desktop", label: "Desktop · 992px and up", Icon: DesktopIcon },
|
|
35
|
+
{ value: "tablet", label: "Tablet · 480–991px", Icon: TabletIcon },
|
|
36
|
+
{ value: "mobile", label: "Mobile · 479px and below", Icon: MobileIcon },
|
|
37
|
+
] as const;
|
|
38
|
+
|
|
39
|
+
const STATUS_TEXT: Record<SaveStatus["state"], string> = {
|
|
40
|
+
idle: "",
|
|
41
|
+
saving: "",
|
|
42
|
+
saved: "All changes saved",
|
|
43
|
+
error: "Could not save",
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Changes wait in a draft only the editor shows until they are saved. `count` is how many there are on the whole
|
|
48
|
+
* site; undo and redo step through the ones made in this window.
|
|
49
|
+
*/
|
|
50
|
+
export interface DraftControls {
|
|
51
|
+
count: number;
|
|
52
|
+
/** Text typed on the page that is not in the drafts yet: Save saves it first. */
|
|
53
|
+
typing: boolean;
|
|
54
|
+
saving: boolean;
|
|
55
|
+
onSave: () => void;
|
|
56
|
+
onDiscard: () => void;
|
|
57
|
+
onUndo?: (() => void) | undefined;
|
|
58
|
+
onRedo?: (() => void) | undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const isMac = typeof navigator !== "undefined" && /Mac|iPhone|iPad/.test(navigator.platform);
|
|
62
|
+
const MOD = isMac ? "⌘" : "Ctrl+";
|
|
63
|
+
|
|
64
|
+
/** A page added from a template has its own title; otherwise `/about/team` → `Team`. */
|
|
65
|
+
export function pageTitle(path: string, title?: string): string {
|
|
66
|
+
if (title) return title;
|
|
67
|
+
const segment = path.split("/").filter(Boolean).at(-1);
|
|
68
|
+
if (!segment) return "Home";
|
|
69
|
+
return segment.charAt(0).toUpperCase() + segment.slice(1).replaceAll("-", " ");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The editor's top bar: where you are, who can see it, the preview size, and the page's own settings. */
|
|
73
|
+
export function Toolbar({
|
|
74
|
+
user,
|
|
75
|
+
page,
|
|
76
|
+
viewport,
|
|
77
|
+
status,
|
|
78
|
+
drafts,
|
|
79
|
+
siteMapOpen,
|
|
80
|
+
pageSettingsOpen,
|
|
81
|
+
onSiteMap,
|
|
82
|
+
onContent,
|
|
83
|
+
onViewport,
|
|
84
|
+
onVisibility,
|
|
85
|
+
onPageSettings,
|
|
86
|
+
onSiteSettings,
|
|
87
|
+
onChangePassword,
|
|
88
|
+
onHistory,
|
|
89
|
+
onSignOut,
|
|
90
|
+
languages,
|
|
91
|
+
onLanguage,
|
|
92
|
+
template,
|
|
93
|
+
onEditTemplate,
|
|
94
|
+
}: {
|
|
95
|
+
user: User;
|
|
96
|
+
page: SitePage;
|
|
97
|
+
viewport: Viewport;
|
|
98
|
+
status: SaveStatus;
|
|
99
|
+
drafts: DraftControls;
|
|
100
|
+
siteMapOpen: boolean;
|
|
101
|
+
pageSettingsOpen: boolean;
|
|
102
|
+
onSiteMap: () => void;
|
|
103
|
+
/** Opens the content browser: the site's content types and their items. */
|
|
104
|
+
onContent: () => void;
|
|
105
|
+
onViewport: (viewport: Viewport) => void;
|
|
106
|
+
onVisibility: (visibility: Visibility) => void;
|
|
107
|
+
onPageSettings: () => void;
|
|
108
|
+
/** Absent for editors: Site Settings are the owners'. */
|
|
109
|
+
onSiteSettings?: (() => void) | undefined;
|
|
110
|
+
/** Opens the form to change one's own password; absent when the user signed in another way (an access token). */
|
|
111
|
+
onChangePassword?: (() => void) | undefined;
|
|
112
|
+
/** Opens the site's history: what changed lately, and taking a change back. */
|
|
113
|
+
onHistory: () => void;
|
|
114
|
+
onSignOut: () => void;
|
|
115
|
+
/** The page's language and the same page in the site's other languages; absent on a site with one language. */
|
|
116
|
+
languages?: LanguageChoice | undefined;
|
|
117
|
+
/** `fill`: copy this page's text into that language where it has none, then open it. */
|
|
118
|
+
onLanguage?: (code: string, fill: boolean) => void;
|
|
119
|
+
/** Set while a content type's template is edited: the bar names it and offers to go back to the item. */
|
|
120
|
+
template?: { title: string; note: string; onDone: () => void } | undefined;
|
|
121
|
+
/** Set on the page of an item whose type's template can be edited. */
|
|
122
|
+
onEditTemplate?: (() => void) | undefined;
|
|
123
|
+
}) {
|
|
124
|
+
const isPublic = page.visibility === "published";
|
|
125
|
+
|
|
126
|
+
return (
|
|
127
|
+
<header className="brim-editor-toolbar">
|
|
128
|
+
<div className="toolbar-zone toolbar-zone-left">
|
|
129
|
+
{template ? (
|
|
130
|
+
<>
|
|
131
|
+
<span className="toolbar-template">
|
|
132
|
+
<strong>{template.title}</strong>
|
|
133
|
+
{template.note}
|
|
134
|
+
</span>
|
|
135
|
+
<button type="button" className="brim-toolbar-btn brim-toolbar-btn--outline" onClick={template.onDone}>
|
|
136
|
+
Done
|
|
137
|
+
</button>
|
|
138
|
+
</>
|
|
139
|
+
) : (
|
|
140
|
+
<>
|
|
141
|
+
<button
|
|
142
|
+
type="button"
|
|
143
|
+
className="breadcrumb-page-trigger"
|
|
144
|
+
aria-expanded={siteMapOpen}
|
|
145
|
+
aria-label="Site map"
|
|
146
|
+
title="Site map"
|
|
147
|
+
onClick={onSiteMap}
|
|
148
|
+
>
|
|
149
|
+
<span className="breadcrumb-page-icon">
|
|
150
|
+
<PageIcon />
|
|
151
|
+
</span>
|
|
152
|
+
<span className="breadcrumb-page-title">{pageTitle(page.path, page.title)}</span>
|
|
153
|
+
<ChevronDownIcon />
|
|
154
|
+
</button>
|
|
155
|
+
{languages && (
|
|
156
|
+
<Menu
|
|
157
|
+
label="Language of this page"
|
|
158
|
+
className="brim-toolbar-btn brim-toolbar-btn--outline toolbar-language"
|
|
159
|
+
trigger={
|
|
160
|
+
<>
|
|
161
|
+
{languages.current.toUpperCase()}
|
|
162
|
+
<ChevronDownIcon />
|
|
163
|
+
</>
|
|
164
|
+
}
|
|
165
|
+
>
|
|
166
|
+
{(close) => (
|
|
167
|
+
<>
|
|
168
|
+
{languages.options.map((option) => (
|
|
169
|
+
<button
|
|
170
|
+
key={option.code}
|
|
171
|
+
type="button"
|
|
172
|
+
role="menuitem"
|
|
173
|
+
className="brim-editor-menu__item"
|
|
174
|
+
aria-current={option.code === languages.current ? "true" : undefined}
|
|
175
|
+
onClick={() => {
|
|
176
|
+
close();
|
|
177
|
+
if (option.code !== languages.current) onLanguage?.(option.code, false);
|
|
178
|
+
}}
|
|
179
|
+
>
|
|
180
|
+
<span>{option.code.toUpperCase()}</span>
|
|
181
|
+
<code>
|
|
182
|
+
{option.code === languages.current
|
|
183
|
+
? "This page"
|
|
184
|
+
: option.exists
|
|
185
|
+
? option.path
|
|
186
|
+
: `Translate into ${option.code.toUpperCase()}`}
|
|
187
|
+
</code>
|
|
188
|
+
</button>
|
|
189
|
+
))}
|
|
190
|
+
{languages.copy &&
|
|
191
|
+
languages.options.some((option) => option.code !== languages.current && option.exists) && (
|
|
192
|
+
<div className="brim-editor-menu__separator" />
|
|
193
|
+
)}
|
|
194
|
+
{languages.options
|
|
195
|
+
.filter((option) => languages.copy && option.code !== languages.current && option.exists)
|
|
196
|
+
.map((option) => (
|
|
197
|
+
<button
|
|
198
|
+
key={`copy-${option.code}`}
|
|
199
|
+
type="button"
|
|
200
|
+
role="menuitem"
|
|
201
|
+
className="brim-editor-menu__item"
|
|
202
|
+
onClick={() => {
|
|
203
|
+
close();
|
|
204
|
+
onLanguage?.(option.code, true);
|
|
205
|
+
}}
|
|
206
|
+
>
|
|
207
|
+
<span>Copy this page's text to {option.code.toUpperCase()}</span>
|
|
208
|
+
<code>Only where {option.code.toUpperCase()} has none yet</code>
|
|
209
|
+
</button>
|
|
210
|
+
))}
|
|
211
|
+
</>
|
|
212
|
+
)}
|
|
213
|
+
</Menu>
|
|
214
|
+
)}
|
|
215
|
+
<button
|
|
216
|
+
type="button"
|
|
217
|
+
className="brim-toolbar-btn brim-toolbar-btn--outline"
|
|
218
|
+
title="Content: the site's content types and their items"
|
|
219
|
+
aria-label="Content"
|
|
220
|
+
onClick={onContent}
|
|
221
|
+
>
|
|
222
|
+
<FolderIcon />
|
|
223
|
+
<span className="brim-toolbar-label">Content</span>
|
|
224
|
+
</button>
|
|
225
|
+
<span className="toolbar-zone-divider" />
|
|
226
|
+
<Menu
|
|
227
|
+
label="Who can see this page"
|
|
228
|
+
className="toolbar-status-pill"
|
|
229
|
+
trigger={
|
|
230
|
+
<>
|
|
231
|
+
<span className="toolbar-status-dot" data-state={page.visibility} />
|
|
232
|
+
<span className="toolbar-status-text">{isPublic ? "Public" : "Private"}</span>
|
|
233
|
+
<ChevronDownIcon />
|
|
234
|
+
</>
|
|
235
|
+
}
|
|
236
|
+
>
|
|
237
|
+
{(close) => (
|
|
238
|
+
<button
|
|
239
|
+
type="button"
|
|
240
|
+
role="menuitem"
|
|
241
|
+
className="brim-editor-menu__item"
|
|
242
|
+
onClick={() => {
|
|
243
|
+
close();
|
|
244
|
+
onVisibility(isPublic ? "private" : "published");
|
|
245
|
+
}}
|
|
246
|
+
>
|
|
247
|
+
<span>{isPublic ? "Make Private" : "Make Public"}</span>
|
|
248
|
+
<code>{isPublic ? "Only in the editor" : "On the public site"}</code>
|
|
249
|
+
</button>
|
|
250
|
+
)}
|
|
251
|
+
</Menu>
|
|
252
|
+
{onEditTemplate && (
|
|
253
|
+
<button
|
|
254
|
+
type="button"
|
|
255
|
+
className="brim-toolbar-btn brim-toolbar-btn--outline"
|
|
256
|
+
title="Edit template: change what every item of this type shows"
|
|
257
|
+
aria-label="Edit template"
|
|
258
|
+
onClick={onEditTemplate}
|
|
259
|
+
>
|
|
260
|
+
<TemplateIcon />
|
|
261
|
+
<span className="brim-toolbar-label">Edit template</span>
|
|
262
|
+
</button>
|
|
263
|
+
)}
|
|
264
|
+
</>
|
|
265
|
+
)}
|
|
266
|
+
<span className="brim-editor-autosave" role="status" data-state={status.state} title={status.message}>
|
|
267
|
+
{status.state === "error"
|
|
268
|
+
? (status.message ?? STATUS_TEXT.error)
|
|
269
|
+
: drafts.count > 0
|
|
270
|
+
? `${drafts.count} unsaved ${drafts.count === 1 ? "change" : "changes"}`
|
|
271
|
+
: STATUS_TEXT[status.state]}
|
|
272
|
+
</span>
|
|
273
|
+
</div>
|
|
274
|
+
|
|
275
|
+
<div className="toolbar-zone toolbar-zone-center">
|
|
276
|
+
<div className="device-preview-pill" role="group" aria-label="Preview size">
|
|
277
|
+
{VIEWPORTS.map(({ value, label, Icon }) => (
|
|
278
|
+
<button
|
|
279
|
+
key={value}
|
|
280
|
+
type="button"
|
|
281
|
+
className="brim-toolbar-btn brim-toolbar-btn--icon"
|
|
282
|
+
aria-pressed={viewport === value}
|
|
283
|
+
aria-label={label}
|
|
284
|
+
title={label}
|
|
285
|
+
onClick={() => onViewport(value)}
|
|
286
|
+
>
|
|
287
|
+
<Icon />
|
|
288
|
+
</button>
|
|
289
|
+
))}
|
|
290
|
+
</div>
|
|
291
|
+
</div>
|
|
292
|
+
|
|
293
|
+
<div className="toolbar-zone toolbar-zone-right">
|
|
294
|
+
<button
|
|
295
|
+
type="button"
|
|
296
|
+
className="brim-toolbar-btn brim-toolbar-btn--icon"
|
|
297
|
+
aria-label="Undo"
|
|
298
|
+
title={`Undo (${MOD}Z)`}
|
|
299
|
+
disabled={!drafts.onUndo}
|
|
300
|
+
onClick={drafts.onUndo}
|
|
301
|
+
>
|
|
302
|
+
<UndoIcon />
|
|
303
|
+
</button>
|
|
304
|
+
<button
|
|
305
|
+
type="button"
|
|
306
|
+
className="brim-toolbar-btn brim-toolbar-btn--icon toolbar-redo"
|
|
307
|
+
aria-label="Redo"
|
|
308
|
+
title={`Redo (${isMac ? "⇧⌘Z" : "Ctrl+Y"})`}
|
|
309
|
+
disabled={!drafts.onRedo}
|
|
310
|
+
onClick={drafts.onRedo}
|
|
311
|
+
>
|
|
312
|
+
<RedoIcon />
|
|
313
|
+
</button>
|
|
314
|
+
{drafts.count > 0 && (
|
|
315
|
+
<button
|
|
316
|
+
type="button"
|
|
317
|
+
className="brim-toolbar-btn brim-toolbar-btn--outline"
|
|
318
|
+
title="Throw away every unsaved change"
|
|
319
|
+
disabled={drafts.saving}
|
|
320
|
+
onClick={drafts.onDiscard}
|
|
321
|
+
>
|
|
322
|
+
Discard
|
|
323
|
+
</button>
|
|
324
|
+
)}
|
|
325
|
+
<button
|
|
326
|
+
type="button"
|
|
327
|
+
className="brim-toolbar-btn brim-toolbar-btn--action brim-toolbar-save"
|
|
328
|
+
title={drafts.count > 0 || drafts.typing ? `Save, so visitors see the changes (${MOD}S)` : "Nothing to save"}
|
|
329
|
+
disabled={(drafts.count === 0 && !drafts.typing) || drafts.saving}
|
|
330
|
+
onClick={drafts.onSave}
|
|
331
|
+
>
|
|
332
|
+
{drafts.saving ? "Saving…" : "Save"}
|
|
333
|
+
</button>
|
|
334
|
+
<span className="toolbar-separator" />
|
|
335
|
+
<a
|
|
336
|
+
className="brim-toolbar-btn brim-toolbar-btn--icon toolbar-preview"
|
|
337
|
+
href={page.path}
|
|
338
|
+
target="_blank"
|
|
339
|
+
rel="noreferrer"
|
|
340
|
+
title="Preview page"
|
|
341
|
+
aria-label="Preview page"
|
|
342
|
+
>
|
|
343
|
+
<EyeIcon />
|
|
344
|
+
</a>
|
|
345
|
+
<span className="toolbar-separator" />
|
|
346
|
+
{!template && (
|
|
347
|
+
<button
|
|
348
|
+
type="button"
|
|
349
|
+
className="brim-toolbar-btn brim-toolbar-btn--icon"
|
|
350
|
+
aria-pressed={pageSettingsOpen}
|
|
351
|
+
aria-label="Page settings"
|
|
352
|
+
title="Page settings"
|
|
353
|
+
onClick={onPageSettings}
|
|
354
|
+
>
|
|
355
|
+
<SettingsIcon />
|
|
356
|
+
</button>
|
|
357
|
+
)}
|
|
358
|
+
<Menu label="Account" className="brim-toolbar-btn brim-toolbar-btn--icon" align="end" trigger={<MoreIcon />}>
|
|
359
|
+
{(close) => (
|
|
360
|
+
<>
|
|
361
|
+
<p className="brim-editor-menu__note">{user.email}</p>
|
|
362
|
+
<div className="brim-editor-menu__separator" />
|
|
363
|
+
<button
|
|
364
|
+
type="button"
|
|
365
|
+
role="menuitem"
|
|
366
|
+
className="brim-editor-menu__item"
|
|
367
|
+
onClick={() => {
|
|
368
|
+
close();
|
|
369
|
+
onHistory();
|
|
370
|
+
}}
|
|
371
|
+
>
|
|
372
|
+
History
|
|
373
|
+
</button>
|
|
374
|
+
{onSiteSettings && (
|
|
375
|
+
<button
|
|
376
|
+
type="button"
|
|
377
|
+
role="menuitem"
|
|
378
|
+
className="brim-editor-menu__item"
|
|
379
|
+
onClick={() => {
|
|
380
|
+
close();
|
|
381
|
+
onSiteSettings();
|
|
382
|
+
}}
|
|
383
|
+
>
|
|
384
|
+
Site settings
|
|
385
|
+
</button>
|
|
386
|
+
)}
|
|
387
|
+
<ThemeChoice />
|
|
388
|
+
<div className="brim-editor-menu__separator" />
|
|
389
|
+
{onChangePassword && (
|
|
390
|
+
<button
|
|
391
|
+
type="button"
|
|
392
|
+
role="menuitem"
|
|
393
|
+
className="brim-editor-menu__item"
|
|
394
|
+
onClick={() => {
|
|
395
|
+
close();
|
|
396
|
+
onChangePassword();
|
|
397
|
+
}}
|
|
398
|
+
>
|
|
399
|
+
Change password
|
|
400
|
+
</button>
|
|
401
|
+
)}
|
|
402
|
+
<button
|
|
403
|
+
type="button"
|
|
404
|
+
role="menuitem"
|
|
405
|
+
className="brim-editor-menu__item"
|
|
406
|
+
onClick={() => {
|
|
407
|
+
close();
|
|
408
|
+
onSignOut();
|
|
409
|
+
}}
|
|
410
|
+
>
|
|
411
|
+
Sign out
|
|
412
|
+
</button>
|
|
413
|
+
</>
|
|
414
|
+
)}
|
|
415
|
+
</Menu>
|
|
416
|
+
</div>
|
|
417
|
+
</header>
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const THEMES: { value: Theme; label: string }[] = [
|
|
422
|
+
{ value: "system", label: "System" },
|
|
423
|
+
{ value: "light", label: "Light" },
|
|
424
|
+
{ value: "dark", label: "Dark" },
|
|
425
|
+
];
|
|
426
|
+
|
|
427
|
+
/** Light, dark, or as the system is: the editor's own appearance, remembered in this browser. */
|
|
428
|
+
function ThemeChoice() {
|
|
429
|
+
const [theme, setTheme] = useTheme();
|
|
430
|
+
return (
|
|
431
|
+
<div className="brim-editor-menu__theme" role="group" aria-label="Appearance">
|
|
432
|
+
<span className="brim-editor-menu__theme-label">Appearance</span>
|
|
433
|
+
<div className="brim-editor-menu__theme-options">
|
|
434
|
+
{THEMES.map((option) => (
|
|
435
|
+
<button
|
|
436
|
+
key={option.value}
|
|
437
|
+
type="button"
|
|
438
|
+
role="menuitemradio"
|
|
439
|
+
aria-checked={theme === option.value}
|
|
440
|
+
className="brim-editor-menu__theme-option"
|
|
441
|
+
onClick={() => setTheme(option.value)}
|
|
442
|
+
>
|
|
443
|
+
{option.label}
|
|
444
|
+
</button>
|
|
445
|
+
))}
|
|
446
|
+
</div>
|
|
447
|
+
</div>
|
|
448
|
+
);
|
|
449
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { type UploadedVideo, type VideoStatus, api, loadSiteInfo } from "./api.ts";
|
|
3
|
+
import { tusUpload } from "./tus.ts";
|
|
4
|
+
|
|
5
|
+
type Stage =
|
|
6
|
+
| { name: "idle" }
|
|
7
|
+
| { name: "uploading"; file: string; percent: number }
|
|
8
|
+
| { name: "processing"; file: string; percent: number };
|
|
9
|
+
|
|
10
|
+
const POLL_MS = 2500;
|
|
11
|
+
/** A status that cannot be read is asked for again this many times before giving up. */
|
|
12
|
+
const POLL_FAILURES = 5;
|
|
13
|
+
|
|
14
|
+
/** Whether the site has a video host. Asked once per editor session. */
|
|
15
|
+
function useVideoUploads(): boolean {
|
|
16
|
+
const [enabled, setEnabled] = useState(false);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
let current = true;
|
|
19
|
+
void loadSiteInfo().then((info) => current && setEnabled((info.capabilities ?? []).includes("video-uploads")));
|
|
20
|
+
return () => {
|
|
21
|
+
current = false;
|
|
22
|
+
};
|
|
23
|
+
}, []);
|
|
24
|
+
return enabled;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A file's size as people read it: "3.4 MB". */
|
|
28
|
+
function readableSize(bytes: number): string {
|
|
29
|
+
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
30
|
+
let value = bytes;
|
|
31
|
+
let unit = 0;
|
|
32
|
+
while (value >= 1000 && unit < units.length - 1) {
|
|
33
|
+
value /= 1000;
|
|
34
|
+
unit++;
|
|
35
|
+
}
|
|
36
|
+
return `${unit === 0 || value >= 100 ? Math.round(value) : value.toFixed(1)} ${units[unit]}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const sleep = (ms: number, signal: AbortSignal): Promise<void> =>
|
|
40
|
+
new Promise((resolve, reject) => {
|
|
41
|
+
const timer = setTimeout(resolve, ms);
|
|
42
|
+
signal.addEventListener("abort", () => {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
reject(signal.reason);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Uploads a video file to the site's video host and hands back where it plays. The file goes from the browser
|
|
50
|
+
* straight to the host, piece by piece; the site only asks the host where to send it and when it is ready.
|
|
51
|
+
* Renders nothing on a site without a video host: there the address is pasted.
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* Uploads a video to the site's video host and waits until it can be played: the one way the editor uploads videos
|
|
55
|
+
* (a video block's settings). `onStage` tells the upload's progress, then the processing's.
|
|
56
|
+
*/
|
|
57
|
+
export async function uploadVideo(
|
|
58
|
+
file: File,
|
|
59
|
+
onStage: (stage: "uploading" | "processing", percent: number) => void,
|
|
60
|
+
signal: AbortSignal,
|
|
61
|
+
): Promise<{ url: string; posterUrl: string | null } | undefined> {
|
|
62
|
+
const ticket = await api.createVideo({ name: file.name, size: file.size, type: file.type });
|
|
63
|
+
if (ticket.upload.protocol !== "tus")
|
|
64
|
+
throw new Error("This site's video host uses an upload method Brim does not know.");
|
|
65
|
+
await tusUpload(file, ticket.upload, {
|
|
66
|
+
signal,
|
|
67
|
+
onProgress: (sent, total) => onStage("uploading", Math.floor((sent / total) * 100)),
|
|
68
|
+
});
|
|
69
|
+
let failures = 0;
|
|
70
|
+
let asked = false;
|
|
71
|
+
let status: VideoStatus | undefined;
|
|
72
|
+
while (status?.status !== "ready") {
|
|
73
|
+
onStage("processing", Math.round(status?.progress ?? 0));
|
|
74
|
+
await sleep(asked ? POLL_MS : 0, signal);
|
|
75
|
+
asked = true;
|
|
76
|
+
try {
|
|
77
|
+
status = await api.videoStatus(ticket.id);
|
|
78
|
+
failures = 0;
|
|
79
|
+
} catch (failure) {
|
|
80
|
+
if (++failures > POLL_FAILURES) throw failure;
|
|
81
|
+
}
|
|
82
|
+
if (signal.aborted) return undefined;
|
|
83
|
+
if (status?.status === "failed") throw new Error("The video could not be processed. Try another file.");
|
|
84
|
+
}
|
|
85
|
+
return { url: status.url || ticket.url, posterUrl: status.posterUrl ?? ticket.posterUrl ?? null };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function VideoUpload({ onUploaded }: { onUploaded: (url: string, posterUrl: string | null) => void }) {
|
|
89
|
+
const enabled = useVideoUploads();
|
|
90
|
+
const [stage, setStage] = useState<Stage>({ name: "idle" });
|
|
91
|
+
const [error, setError] = useState("");
|
|
92
|
+
const [over, setOver] = useState(false);
|
|
93
|
+
const fileInput = useRef<HTMLInputElement>(null);
|
|
94
|
+
const running = useRef<AbortController | null>(null);
|
|
95
|
+
const [videos, setVideos] = useState<UploadedVideo[]>([]);
|
|
96
|
+
const [deleting, setDeleting] = useState("");
|
|
97
|
+
const mounted = useRef(true);
|
|
98
|
+
|
|
99
|
+
// The videos already on the host. A host that does not list them, or a list that cannot be read, shows nothing:
|
|
100
|
+
// uploading works without it.
|
|
101
|
+
const refresh = useCallback(async (): Promise<void> => {
|
|
102
|
+
const list = await api.listVideos().catch(() => undefined);
|
|
103
|
+
if (mounted.current) setVideos(list?.supported ? list.videos : []);
|
|
104
|
+
}, []);
|
|
105
|
+
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
mounted.current = true;
|
|
108
|
+
if (enabled) void refresh();
|
|
109
|
+
return () => {
|
|
110
|
+
mounted.current = false;
|
|
111
|
+
};
|
|
112
|
+
}, [enabled, refresh]);
|
|
113
|
+
|
|
114
|
+
// Leaving the block's settings stops an upload that is still going.
|
|
115
|
+
useEffect(() => () => running.current?.abort(), []);
|
|
116
|
+
|
|
117
|
+
if (!enabled) return null;
|
|
118
|
+
|
|
119
|
+
async function start(file: File | undefined): Promise<void> {
|
|
120
|
+
if (!file || running.current) return;
|
|
121
|
+
if (!file.type.startsWith("video/")) return setError("Choose a video file.");
|
|
122
|
+
const controller = new AbortController();
|
|
123
|
+
running.current = controller;
|
|
124
|
+
setError("");
|
|
125
|
+
setStage({ name: "uploading", file: file.name, percent: 0 });
|
|
126
|
+
try {
|
|
127
|
+
const video = await uploadVideo(
|
|
128
|
+
file,
|
|
129
|
+
(name, percent) =>
|
|
130
|
+
setStage(name === "uploading" ? { name, file: file.name, percent } : { name, file: file.name, percent }),
|
|
131
|
+
controller.signal,
|
|
132
|
+
);
|
|
133
|
+
if (video) onUploaded(video.url, video.posterUrl);
|
|
134
|
+
} catch (failure) {
|
|
135
|
+
if (!controller.signal.aborted) {
|
|
136
|
+
setError(failure instanceof Error && failure.message ? failure.message : "Could not upload the video.");
|
|
137
|
+
}
|
|
138
|
+
} finally {
|
|
139
|
+
if (running.current === controller) running.current = null;
|
|
140
|
+
if (mounted.current) setStage({ name: "idle" });
|
|
141
|
+
// Also after a failure or a cancel: the host may already hold the video, and the list is where it can be deleted.
|
|
142
|
+
void refresh();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function remove(video: UploadedVideo): Promise<void> {
|
|
147
|
+
if (deleting || !window.confirm("Delete this video? Pages that show it will stop playing it.")) return;
|
|
148
|
+
setError("");
|
|
149
|
+
setDeleting(video.id);
|
|
150
|
+
try {
|
|
151
|
+
await api.deleteVideo(video.id);
|
|
152
|
+
} catch (failure) {
|
|
153
|
+
if (mounted.current) {
|
|
154
|
+
setError(failure instanceof Error && failure.message ? failure.message : "Could not delete the video.");
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (mounted.current) setDeleting("");
|
|
158
|
+
await refresh();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const busy = stage.name !== "idle";
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<div className="field-group">
|
|
165
|
+
<div
|
|
166
|
+
className="video-upload"
|
|
167
|
+
data-over={over}
|
|
168
|
+
onDragOver={(event) => {
|
|
169
|
+
event.preventDefault();
|
|
170
|
+
setOver(true);
|
|
171
|
+
}}
|
|
172
|
+
onDragLeave={() => setOver(false)}
|
|
173
|
+
onDrop={(event) => {
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
setOver(false);
|
|
176
|
+
void start(event.dataTransfer.files[0]);
|
|
177
|
+
}}
|
|
178
|
+
>
|
|
179
|
+
{busy ? (
|
|
180
|
+
<>
|
|
181
|
+
<p className="video-upload__status" role="status">
|
|
182
|
+
{stage.name === "uploading" ? `Uploading… ${stage.percent}%` : `Processing… ${stage.percent}%`}
|
|
183
|
+
<small>{stage.file}</small>
|
|
184
|
+
</p>
|
|
185
|
+
<progress className="video-upload__bar" max={100} value={stage.percent} />
|
|
186
|
+
<p className="field-description">Keep these settings open until the video is ready.</p>
|
|
187
|
+
<button type="button" className="btn-remove" onClick={() => running.current?.abort()}>
|
|
188
|
+
Cancel
|
|
189
|
+
</button>
|
|
190
|
+
</>
|
|
191
|
+
) : (
|
|
192
|
+
<>
|
|
193
|
+
<button type="button" className="btn-upload" onClick={() => fileInput.current?.click()}>
|
|
194
|
+
Upload video
|
|
195
|
+
</button>
|
|
196
|
+
<p className="field-description">Or drop a video file here.</p>
|
|
197
|
+
</>
|
|
198
|
+
)}
|
|
199
|
+
<input
|
|
200
|
+
ref={fileInput}
|
|
201
|
+
type="file"
|
|
202
|
+
accept="video/*"
|
|
203
|
+
hidden
|
|
204
|
+
onChange={(event) => {
|
|
205
|
+
void start(event.target.files?.[0]);
|
|
206
|
+
event.target.value = "";
|
|
207
|
+
}}
|
|
208
|
+
/>
|
|
209
|
+
</div>
|
|
210
|
+
{error && (
|
|
211
|
+
<p className="field-error" role="alert">
|
|
212
|
+
{error}
|
|
213
|
+
</p>
|
|
214
|
+
)}
|
|
215
|
+
{videos.length > 0 && (
|
|
216
|
+
<div className="video-list">
|
|
217
|
+
<p className="video-list__title">Uploaded videos</p>
|
|
218
|
+
<ul>
|
|
219
|
+
{videos.map((video) => (
|
|
220
|
+
<li key={video.id}>
|
|
221
|
+
<span className="video-list__name" title={video.name}>
|
|
222
|
+
{video.name || "Untitled video"}
|
|
223
|
+
<small>
|
|
224
|
+
{readableSize(video.size)}
|
|
225
|
+
{video.status === "processing" && " · Processing…"}
|
|
226
|
+
{video.status === "failed" && " · Could not be processed"}
|
|
227
|
+
</small>
|
|
228
|
+
</span>
|
|
229
|
+
<button
|
|
230
|
+
type="button"
|
|
231
|
+
disabled={video.status !== "ready"}
|
|
232
|
+
onClick={() => onUploaded(video.url, video.posterUrl)}
|
|
233
|
+
>
|
|
234
|
+
Use
|
|
235
|
+
</button>
|
|
236
|
+
<button
|
|
237
|
+
type="button"
|
|
238
|
+
className="btn-remove"
|
|
239
|
+
disabled={deleting !== ""}
|
|
240
|
+
onClick={() => void remove(video)}
|
|
241
|
+
>
|
|
242
|
+
{deleting === video.id ? "Deleting…" : "Delete"}
|
|
243
|
+
</button>
|
|
244
|
+
</li>
|
|
245
|
+
))}
|
|
246
|
+
</ul>
|
|
247
|
+
</div>
|
|
248
|
+
)}
|
|
249
|
+
</div>
|
|
250
|
+
);
|
|
251
|
+
}
|