@basicbenframework/cms 0.7.3 → 0.9.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/db/migrations/043_add_private_content.js +32 -0
- package/db/migrations/044_add_post_autosave_key.js +20 -0
- package/lib/client/components/admin/MediaPicker.js +3 -1
- package/lib/client/components/admin/SnippetPicker.js +3 -1
- package/lib/client/components/admin/SocialImageField.d.ts +24 -0
- package/lib/client/components/admin/SocialImageField.js +70 -0
- package/lib/client/i18n/locales/es/content.js +2 -0
- package/lib/client/i18n/locales/es/editor.js +18 -1
- package/lib/client/pages/Authentication.js +1 -1
- package/lib/client/pages/Database.js +15 -10
- package/lib/client/pages/Testing.js +1 -1
- package/lib/client/pages/admin/Pages.js +2 -2
- package/lib/client/pages/admin/PostEditor.js +103 -9
- package/lib/client/pages/admin/Posts.js +2 -2
- package/lib/controllers/FeedController.js +2 -2
- package/lib/controllers/MediaController.js +18 -1
- package/lib/controllers/PageController.js +5 -1
- package/lib/controllers/PostController.d.ts +14 -0
- package/lib/controllers/PostController.js +83 -1
- package/lib/features/members/Member.js +3 -2
- package/lib/helpers/autosave.d.ts +13 -0
- package/lib/helpers/autosave.js +19 -0
- package/lib/helpers/comment-mail.js +8 -3
- package/lib/helpers/insert-row.d.ts +18 -0
- package/lib/helpers/insert-row.js +18 -0
- package/lib/helpers/redirects.d.ts +7 -1
- package/lib/helpers/redirects.js +7 -1
- package/lib/helpers/seo-fields.d.ts +7 -0
- package/lib/helpers/seo-fields.js +9 -0
- package/lib/hooks.js +21 -0
- package/lib/models/Category.js +3 -2
- package/lib/models/Comment.js +3 -2
- package/lib/models/ContentType.js +3 -2
- package/lib/models/Credential.js +5 -1
- package/lib/models/Media.d.ts +4 -1
- package/lib/models/Media.js +21 -3
- package/lib/models/Menu.js +7 -6
- package/lib/models/Page.d.ts +2 -0
- package/lib/models/Page.js +9 -5
- package/lib/models/Post.d.ts +16 -0
- package/lib/models/Post.js +24 -3
- package/lib/models/PostRevision.js +4 -3
- package/lib/models/PublicContent.d.ts +1 -1
- package/lib/models/PublicContent.js +5 -5
- package/lib/models/Snippet.js +3 -2
- package/lib/models/Tag.js +3 -2
- package/lib/models/Taxonomy.js +5 -2
- package/lib/models/User.js +5 -1
- package/lib/models/visibility.d.ts +9 -0
- package/lib/models/visibility.js +15 -1
- package/lib/routes/api/posts.js +3 -0
- package/lib/types/index.d.ts +4 -0
- package/package.json +3 -3
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Private posts and pages, the WordPress way.
|
|
3
|
+
*
|
|
4
|
+
* A private item is published but not public. Signed-in people who can edit it
|
|
5
|
+
* (editors and admins, and a post's own author) see it in the admin; nobody
|
|
6
|
+
* else sees it anywhere — not the content API, the theme, the feeds, the
|
|
7
|
+
* sitemap, oEmbed, search, menus, comments or newsletters.
|
|
8
|
+
*
|
|
9
|
+
* A flag rather than a fourth `status` or a `visibility` level, on purpose:
|
|
10
|
+
*
|
|
11
|
+
* - `status` is the editorial workflow (draft, pending, published) and is kept
|
|
12
|
+
* in step with the `published` flag every query reads. Private is not a
|
|
13
|
+
* step in that workflow: a private post can still be scheduled, and making
|
|
14
|
+
* one public again must not mean republishing it.
|
|
15
|
+
* - `visibility` (migration 034) says who may read the *body* of a post that
|
|
16
|
+
* is still listed, and its levels belong to the access policy a feature
|
|
17
|
+
* registers. Private hides the whole item, and must hold with no feature
|
|
18
|
+
* installed at all.
|
|
19
|
+
*
|
|
20
|
+
* Enforced in one place: `livePost()` and `livePage()` in
|
|
21
|
+
* `src/models/visibility.ts`, which every public read goes through.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export const up = async (db) => {
|
|
25
|
+
await db.exec('ALTER TABLE posts ADD COLUMN is_private INTEGER NOT NULL DEFAULT 0')
|
|
26
|
+
await db.exec('ALTER TABLE pages ADD COLUMN is_private INTEGER NOT NULL DEFAULT 0')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const down = async (db) => {
|
|
30
|
+
await db.exec('ALTER TABLE pages DROP COLUMN is_private')
|
|
31
|
+
await db.exec('ALTER TABLE posts DROP COLUMN is_private')
|
|
32
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Makes the editor's first autosave of a new post idempotent.
|
|
3
|
+
*
|
|
4
|
+
* The editor turns a new post into a draft with one request
|
|
5
|
+
* (`POST /api/posts/autosave`). If the row is written but the response is lost,
|
|
6
|
+
* the editor cannot know, and its next try would make a second draft. It now
|
|
7
|
+
* sends a key it generated once per screen; the server stores it here and
|
|
8
|
+
* answers a repeat with the draft it already made. Unique per author, and
|
|
9
|
+
* null on every post written any other way.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export const up = async (db) => {
|
|
13
|
+
await db.exec('ALTER TABLE posts ADD COLUMN autosave_key TEXT')
|
|
14
|
+
await db.exec('CREATE UNIQUE INDEX idx_posts_autosave_key ON posts(user_id, autosave_key)')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const down = async (db) => {
|
|
18
|
+
await db.exec('DROP INDEX IF EXISTS idx_posts_autosave_key')
|
|
19
|
+
await db.exec('ALTER TABLE posts DROP COLUMN autosave_key')
|
|
20
|
+
}
|
|
@@ -22,7 +22,9 @@ export default function MediaPicker({ onSelect, onClose }) {
|
|
|
22
22
|
display: 'flex',
|
|
23
23
|
alignItems: 'center',
|
|
24
24
|
justifyContent: 'center',
|
|
25
|
-
|
|
25
|
+
// Above the admin sidebar (z-index 100), which is fixed at every width
|
|
26
|
+
// and otherwise covers the left of the dialog — and takes its clicks.
|
|
27
|
+
zIndex: 200,
|
|
26
28
|
padding: '1rem'
|
|
27
29
|
}, children: _jsxs("div", { onClick: event => event.stopPropagation(), onDragOver: event => {
|
|
28
30
|
event.preventDefault();
|
|
@@ -41,7 +41,9 @@ export default function SnippetPicker({ onInsert, disabled }) {
|
|
|
41
41
|
display: 'flex',
|
|
42
42
|
alignItems: 'center',
|
|
43
43
|
justifyContent: 'center',
|
|
44
|
-
|
|
44
|
+
// Above the admin sidebar (z-index 100), which is fixed at every width
|
|
45
|
+
// and otherwise covers the left of the dialog — and takes its clicks.
|
|
46
|
+
zIndex: 200,
|
|
45
47
|
padding: '1rem'
|
|
46
48
|
}, children: _jsxs("div", { onClick: event => event.stopPropagation(), style: {
|
|
47
49
|
background: 'var(--surface)',
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The image on a social card — Open Graph or Twitter / X — for posts and pages.
|
|
3
|
+
*
|
|
4
|
+
* The same media library picker as the featured image, because the column is
|
|
5
|
+
* the same kind of thing: a media id, which the server turns into a URL
|
|
6
|
+
* through the storage adapter. A pasted URL is accepted too, and resolved to
|
|
7
|
+
* the library item it serves (`GET /api/media?url=`): the column cannot hold a
|
|
8
|
+
* URL from somewhere else, and storing one would put an address on every
|
|
9
|
+
* shared link that nothing here can check or move.
|
|
10
|
+
*
|
|
11
|
+
* Empty is the common case and means "fall back" — to the Open Graph image,
|
|
12
|
+
* then the featured image — so the empty state says which.
|
|
13
|
+
*/
|
|
14
|
+
interface SocialImageFieldProps {
|
|
15
|
+
id: string;
|
|
16
|
+
label: string;
|
|
17
|
+
/** What a blank field falls back to, said in the empty state. */
|
|
18
|
+
fallback: string;
|
|
19
|
+
value: number | null;
|
|
20
|
+
onChange: (id: number | null) => void;
|
|
21
|
+
disabled?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export default function SocialImageField({ id, label, fallback, value, onChange, disabled }: SocialImageFieldProps): import("react").JSX.Element;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import MediaPicker from "./MediaPicker.js";
|
|
4
|
+
import { isImage, mediaUrl } from "../../hooks/useMediaLibrary.js";
|
|
5
|
+
import { api } from "../../../helpers/api.js";
|
|
6
|
+
import { useT } from "../../i18n/index.js";
|
|
7
|
+
export default function SocialImageField({ id, label, fallback, value, onChange, disabled }) {
|
|
8
|
+
const { t } = useT();
|
|
9
|
+
const [picking, setPicking] = useState(false);
|
|
10
|
+
const [url, setUrl] = useState(null);
|
|
11
|
+
const [pasted, setPasted] = useState('');
|
|
12
|
+
const [error, setError] = useState(null);
|
|
13
|
+
const [resolving, setResolving] = useState(false);
|
|
14
|
+
// A saved post carries only the id, so the preview asks for the rest.
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (!value) {
|
|
17
|
+
setUrl(null);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
let current = true;
|
|
21
|
+
api.get(`/api/media/${value}`)
|
|
22
|
+
.then(res => { if (current && res?.media)
|
|
23
|
+
setUrl(mediaUrl(res.media)); })
|
|
24
|
+
.catch(() => { if (current)
|
|
25
|
+
setUrl(null); });
|
|
26
|
+
return () => { current = false; };
|
|
27
|
+
}, [value]);
|
|
28
|
+
const choose = (item) => {
|
|
29
|
+
if (!isImage(item.mime_type)) {
|
|
30
|
+
setError(t('A social card image has to be an image.'));
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
setError(null);
|
|
34
|
+
setUrl(mediaUrl(item));
|
|
35
|
+
onChange(item.id);
|
|
36
|
+
setPicking(false);
|
|
37
|
+
};
|
|
38
|
+
const applyPasted = async () => {
|
|
39
|
+
const address = pasted.trim();
|
|
40
|
+
if (!address || resolving)
|
|
41
|
+
return;
|
|
42
|
+
setResolving(true);
|
|
43
|
+
setError(null);
|
|
44
|
+
try {
|
|
45
|
+
const res = await api.get(`/api/media?${new URLSearchParams({ url: address, per_page: '5' })}`);
|
|
46
|
+
const match = (res?.media ?? []).find(item => isImage(item.mime_type));
|
|
47
|
+
if (match) {
|
|
48
|
+
choose(match);
|
|
49
|
+
setPasted('');
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
setError(t('That URL is not an image in the media library. Upload it first, then choose it here.'));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
setError(t('Could not look that URL up.'));
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
setResolving(false);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
return (_jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", htmlFor: `${id}-url`, children: label }), value ? (_jsxs("div", { children: [url && (_jsx("img", { src: url, alt: "", style: { width: '100%', maxWidth: '320px', borderRadius: '0.375rem', display: 'block', marginBottom: '0.5rem' } })), _jsxs("div", { style: { display: 'flex', gap: '0.5rem' }, children: [_jsx("button", { type: "button", className: "admin-btn admin-btn-secondary", disabled: disabled, onClick: () => setPicking(true), children: t('Replace') }), _jsx("button", { type: "button", className: "admin-btn admin-btn-danger", disabled: disabled, onClick: () => { setUrl(null); onChange(null); }, children: t('Remove') })] })] })) : (_jsxs("div", { style: { display: 'grid', gap: '0.5rem' }, children: [_jsx("small", { style: { color: 'var(--fg-muted)' }, children: fallback }), _jsxs("div", { style: { display: 'flex', gap: '0.5rem', flexWrap: 'wrap' }, children: [_jsx("button", { type: "button", className: "admin-btn admin-btn-secondary", disabled: disabled, onClick: () => setPicking(true), children: t('Choose image') }), _jsx("input", { id: `${id}-url`, type: "url", value: pasted, disabled: disabled, onChange: event => { setPasted(event.target.value); setError(null); },
|
|
63
|
+
// Enter here would submit the whole post.
|
|
64
|
+
onKeyDown: event => {
|
|
65
|
+
if (event.key === 'Enter') {
|
|
66
|
+
event.preventDefault();
|
|
67
|
+
void applyPasted();
|
|
68
|
+
}
|
|
69
|
+
}, className: "admin-input", style: { flex: '1 1 12rem' }, placeholder: t('or paste the URL of a library image') }), pasted.trim() && (_jsx("button", { type: "button", className: "admin-btn admin-btn-secondary", disabled: disabled || resolving, onClick: () => void applyPasted(), children: resolving ? t('Looking…') : t('Use URL') }))] })] })), error && _jsx("small", { role: "alert", style: { color: 'var(--danger)', display: 'block', marginTop: '0.25rem' }, children: error }), picking && _jsx(MediaPicker, { onSelect: choose, onClose: () => setPicking(false) })] }));
|
|
70
|
+
}
|
|
@@ -22,6 +22,8 @@ const strings = {
|
|
|
22
22
|
'Key': 'Clave',
|
|
23
23
|
'Published': 'Publicada',
|
|
24
24
|
'Draft': 'Borrador',
|
|
25
|
+
'(no title)': '(sin título)',
|
|
26
|
+
'Only people who can edit it can see it': 'Solo la ven quienes pueden editarla',
|
|
25
27
|
'Edit {name}': 'Editar {name}',
|
|
26
28
|
'New {name}': 'Añadir {name}',
|
|
27
29
|
'Select {name}': 'Seleccionar {name}',
|
|
@@ -139,6 +139,23 @@ const strings = {
|
|
|
139
139
|
'Write': 'Escribir',
|
|
140
140
|
'Preview failed: {message}': 'Error en la vista previa: {message}',
|
|
141
141
|
'Nothing to preview yet.': 'Aún no hay nada que previsualizar.',
|
|
142
|
-
'Markdown supported — headings, lists, links, tables, and fenced code. HTML is shown as text rather than rendered.': 'Se admite Markdown: encabezados, listas, enlaces, tablas y bloques de código. El HTML se muestra como texto, no se interpreta.'
|
|
142
|
+
'Markdown supported — headings, lists, links, tables, and fenced code. HTML is shown as text rather than rendered.': 'Se admite Markdown: encabezados, listas, enlaces, tablas y bloques de código. El HTML se muestra como texto, no se interpreta.',
|
|
143
|
+
// Visibility: public or private (migration 043)
|
|
144
|
+
'Visibility': 'Visibilidad',
|
|
145
|
+
'Public': 'Público',
|
|
146
|
+
'Only its author, editors and admins see it, here in the admin. It is left out of the site, the API, feeds, the sitemap and search.': 'Solo la ven su autor, los editores y los administradores, aquí en el panel. Queda fuera del sitio, la API, los feeds, el mapa del sitio y la búsqueda.',
|
|
147
|
+
'Only people who can edit pages see it, here in the admin. It is left out of the site, the API, feeds, the sitemap and search.': 'Solo la ven quienes pueden editar páginas, aquí en el panel. Queda fuera del sitio, la API, los feeds, el mapa del sitio y la búsqueda.',
|
|
148
|
+
// Social card images
|
|
149
|
+
'Open Graph image': 'Imagen de Open Graph',
|
|
150
|
+
'Twitter / X image': 'Imagen de Twitter / X',
|
|
151
|
+
'Falls back to the featured image.': 'Si se deja vacía, se usa la imagen destacada.',
|
|
152
|
+
'Falls back to the Open Graph image.': 'Si se deja vacía, se usa la imagen de Open Graph.',
|
|
153
|
+
'A social card image has to be an image.': 'La imagen de una tarjeta social tiene que ser una imagen.',
|
|
154
|
+
'Choose image': 'Elegir imagen',
|
|
155
|
+
'or paste the URL of a library image': 'o pega la URL de una imagen de la biblioteca',
|
|
156
|
+
'Use URL': 'Usar URL',
|
|
157
|
+
'Looking…': 'Buscando…',
|
|
158
|
+
'That URL is not an image in the media library. Upload it first, then choose it here.': 'Esa URL no es una imagen de la biblioteca de medios. Súbela primero y luego elígela aquí.',
|
|
159
|
+
'Could not look that URL up.': 'No se pudo buscar esa URL.'
|
|
143
160
|
};
|
|
144
161
|
export default strings;
|
|
@@ -66,7 +66,7 @@ export const AuthController = {
|
|
|
66
66
|
const isFirstUser = (await (await query('users')).count()) === 0
|
|
67
67
|
const role = isFirstUser ? ROLES.ADMIN : DEFAULT_ROLE
|
|
68
68
|
|
|
69
|
-
const { lastInsertRowid } = await (await query('users')).insert({
|
|
69
|
+
const { lastInsertRowid } = await (await query('users')).only('name', 'email', 'password', 'role').insert({
|
|
70
70
|
name,
|
|
71
71
|
email,
|
|
72
72
|
password: await hashPassword(password),
|
|
@@ -45,7 +45,7 @@ import { ROLES } from '@basicbenframework/core/auth/permissions'
|
|
|
45
45
|
export async function seed() {
|
|
46
46
|
const password = await hashPassword('password123')
|
|
47
47
|
|
|
48
|
-
await (await db.table('users')).insert({
|
|
48
|
+
await (await db.table('users')).only('name', 'email', 'password', 'role').insert({
|
|
49
49
|
name: 'Admin User',
|
|
50
50
|
email: 'admin@example.com',
|
|
51
51
|
password,
|
|
@@ -58,7 +58,7 @@ export async function seed() {
|
|
|
58
58
|
export async function seed() {
|
|
59
59
|
const user = await (await db.table('users')).first()
|
|
60
60
|
|
|
61
|
-
await (await db.table('posts')).insert({
|
|
61
|
+
await (await db.table('posts')).only('user_id', 'title', 'content', 'published').insert({
|
|
62
62
|
user_id: user.id,
|
|
63
63
|
title: 'Welcome to BasicBen',
|
|
64
64
|
content: 'Your first post.',
|
|
@@ -91,7 +91,8 @@ const latest = await (await query('posts'))
|
|
|
91
91
|
.first()
|
|
92
92
|
|
|
93
93
|
// db.table() is the same builder under a different name
|
|
94
|
-
const drafts = await (await db.table('posts')).where('published', 0).get()` }), _jsx(CodeBlock, { title: "Writing", children:
|
|
94
|
+
const drafts = await (await db.table('posts')).where('published', 0).get()` }), _jsx(CodeBlock, { title: "Writing", children: `// Every write names the columns it may touch, or it throws.
|
|
95
|
+
const result = await (await query('posts')).only('user_id', 'title', 'content').insert({
|
|
95
96
|
user_id: 1,
|
|
96
97
|
title: 'My Post',
|
|
97
98
|
content: 'Hello world'
|
|
@@ -101,6 +102,7 @@ result.lastInsertRowid // id of the new row
|
|
|
101
102
|
result.changes // 1
|
|
102
103
|
|
|
103
104
|
await (await query('posts'))
|
|
105
|
+
.only('title')
|
|
104
106
|
.where('id', postId)
|
|
105
107
|
.update({ title: 'Updated Title' })
|
|
106
108
|
|
|
@@ -135,24 +137,27 @@ const page = await (await query('posts'))
|
|
|
135
137
|
{ name: 'only(...) / except(...)', desc: 'Mass assignment rules, below' },
|
|
136
138
|
{ name: 'toSql()', desc: 'The SELECT this builder would run' },
|
|
137
139
|
].map(({ name, desc }) => (_jsxs("div", { className: `rounded-lg p-3 ${t.card} border ${t.border}`, children: [_jsx("code", { className: "text-sm font-semibold", children: name }), _jsx("p", { className: `text-xs mt-1 ${t.muted}`, children: desc })] }, name))) }), _jsxs("p", { className: `text-sm ${t.muted} mt-4`, children: ["Multiple ", _jsx("code", { children: "where" }), " calls are joined with ", _jsx("code", { children: "AND" }), ". Table and column names are validated against ", _jsx("code", { children: "[a-zA-Z_][a-zA-Z0-9_]*" }), " and quoted, and every value goes in as a bound parameter, so user input cannot become SQL."] })] }), _jsxs(Card, { children: [_jsx("h2", { className: "text-lg font-semibold mb-2", children: "Booleans on SQLite" }), _jsxs("p", { className: `text-sm ${t.muted} mb-4`, children: [_jsx("code", { children: "node:sqlite" }), " refuses to bind JavaScript booleans. Passing", _jsx("code", { children: "true" }), " or ", _jsx("code", { children: "false" }), " as a value throws", _jsx("code", { children: "Provided value cannot be bound to SQLite parameter" }), ", whether it came from an insert or a where clause. Store flags as integers and convert at the edge."] }), _jsx(CodeBlock, { title: "Use 1 and 0", children: `// Throws on SQLite
|
|
138
|
-
await (await query('posts')).insert({ title: 'Draft', published: false })
|
|
140
|
+
await (await query('posts')).only('title', 'published').insert({ title: 'Draft', published: false })
|
|
139
141
|
await (await query('posts')).where('published', true).get()
|
|
140
142
|
|
|
141
143
|
// Works
|
|
142
|
-
await (await query('posts')).insert({ title: 'Draft', published: 0 })
|
|
144
|
+
await (await query('posts')).only('title', 'published').insert({ title: 'Draft', published: 0 })
|
|
143
145
|
await (await query('posts')).where('published', 1).get()
|
|
144
146
|
|
|
145
147
|
// Convert where the value arrives, not everywhere it is used
|
|
146
|
-
await (await query('posts')).insert({
|
|
148
|
+
await (await query('posts')).only('title', 'published').insert({
|
|
147
149
|
title: req.body.title,
|
|
148
150
|
published: req.body.published ? 1 : 0
|
|
149
|
-
})` })] }), _jsxs(Card, { children: [_jsx("h2", { className: "text-lg font-semibold mb-2", children: "Mass Assignment Protection" }), _jsxs("p", { className: `text-sm ${t.muted} mb-4`, children: [_jsx("code", { children: "
|
|
151
|
+
})` })] }), _jsxs(Card, { children: [_jsx("h2", { className: "text-lg font-semibold mb-2", children: "Mass Assignment Protection" }), _jsxs("p", { className: `text-sm ${t.muted} mb-4`, children: ["Every ", _jsx("code", { children: "insert()" }), " and ", _jsx("code", { children: "update()" }), " names the columns it may touch with ", _jsx("code", { children: "only()" }), ", or it throws (", _jsx("code", { children: "MASS_ASSIGNMENT" }), "). Anything not listed is dropped silently rather than raising, so a request body with extra fields still succeeds \u2014 minus the fields it should not have set. ", _jsx("code", { children: "except()" }), " then removes columns from that list; on its own it no longer permits a write.", _jsx("code", { children: "unguarded()" }), " writes every key, for the rare row whose keys you built yourself and have a reason not to list."] }), _jsx(CodeBlock, { title: "Whitelist with only()", children: `await (await query('users'))
|
|
150
152
|
.only('name', 'email', 'bio')
|
|
151
153
|
.insert(req.body)
|
|
152
154
|
|
|
153
155
|
// Given { name: 'Bob', email: 'bob@test.com', is_admin: 1 }
|
|
154
|
-
// the row is written without is_admin` }), _jsx(CodeBlock, { title: "
|
|
155
|
-
|
|
156
|
+
// the row is written without is_admin` }), _jsx(CodeBlock, { title: "Narrow a list with except()", children: `const PROFILE = ['name', 'email', 'bio', 'avatar_url']
|
|
157
|
+
|
|
158
|
+
await (await query('users'))
|
|
159
|
+
.only(...PROFILE)
|
|
160
|
+
.except('email') // this form cannot change the address
|
|
156
161
|
.where('id', userId)
|
|
157
162
|
.update(req.body)` }), _jsxs("p", { className: `text-sm ${t.muted} mt-4`, children: ["Prefer ", _jsx("code", { children: "only()" }), ". A whitelist stays correct when someone adds a column later; a blacklist quietly stops covering everything."] })] }), _jsxs(Card, { children: [_jsx("h2", { className: "text-lg font-semibold mb-2", children: "What the Builder Does Not Do" }), _jsxs("p", { className: `text-sm ${t.muted} mb-4`, children: ["The list above is all of it. There is no ", _jsx("code", { children: "join" }), ", no", _jsx("code", { children: "whereIn" }), ", no ", _jsx("code", { children: "orWhere" }), ", no ", _jsx("code", { children: "groupBy" }), ", and no aggregate beyond ", _jsx("code", { children: "count()" }), ". This is deliberate: a builder that covers every query ends up as large as the SQL it hides. When you need more, write SQL."] }), _jsx(CodeBlock, { title: "A join is a raw query", children: `import { db } from '@basicbenframework/core/db'
|
|
158
163
|
|
|
@@ -229,7 +234,7 @@ export const Post = {
|
|
|
229
234
|
// Through the builder so Postgres gets RETURNING id and the new
|
|
230
235
|
// id comes back.
|
|
231
236
|
const posts = await query('posts')
|
|
232
|
-
const result = await posts.insert({
|
|
237
|
+
const result = await posts.only('user_id', 'title', 'content', 'published').insert({
|
|
233
238
|
user_id: data.user_id,
|
|
234
239
|
title: data.title,
|
|
235
240
|
content: data.content,
|
|
@@ -79,7 +79,7 @@ afterAll(async () => {
|
|
|
79
79
|
|
|
80
80
|
describe('posts', () => {
|
|
81
81
|
it('inserts a row and reads it back', async () => {
|
|
82
|
-
const result = await (await query('posts')).insert({ title: 'Hello', published: 1 })
|
|
82
|
+
const result = await (await query('posts')).only('title', 'published').insert({ title: 'Hello', published: 1 })
|
|
83
83
|
const post = await (await query('posts')).find(result.lastInsertRowid)
|
|
84
84
|
|
|
85
85
|
expect(post.title).toBe('Hello')
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import { useState, useEffect } from 'react';
|
|
3
3
|
import { api } from "../../../helpers/api.js";
|
|
4
4
|
import AdminLayout from "../../layouts/AdminLayout.js";
|
|
@@ -49,5 +49,5 @@ export default function AdminPages() {
|
|
|
49
49
|
if (loading) {
|
|
50
50
|
return (_jsx(AdminLayout, { title: t('Pages'), children: _jsx("div", { className: "admin-loading", children: t('Loading...') }) }));
|
|
51
51
|
}
|
|
52
|
-
return (_jsx(AdminLayout, { title: t('Pages'), actions: _jsx(Link, { href: "/admin/pages/new", className: "admin-btn admin-btn-primary", children: t('+ New Page') }), children: _jsxs("div", { className: "admin-card", children: [pages.length === 0 ? (_jsx("p", { style: { color: 'var(--fg-muted)', textAlign: 'center', padding: '2rem' }, children: t('No pages yet. Create your first page!') })) : (_jsxs("table", { className: "admin-table", children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { children: t('Title') }), _jsx("th", { children: t('Slug') }), _jsx("th", { children: t('Template') }), _jsx("th", { children: t('Status') }), _jsx("th", { children: t('Actions') })] }) }), _jsx("tbody", { children: pages.map(page => (_jsxs("tr", { children: [_jsx("td", { children: _jsx(Link, { href: `/admin/pages/${page.id}/edit`, style: { color: 'var(--accent)', fontWeight: 500 }, children: page.title }) }), _jsx("td", { children: _jsxs("code", { children: ["/", page.slug] }) }), _jsx("td", { children: page.template }),
|
|
52
|
+
return (_jsx(AdminLayout, { title: t('Pages'), actions: _jsx(Link, { href: "/admin/pages/new", className: "admin-btn admin-btn-primary", children: t('+ New Page') }), children: _jsxs("div", { className: "admin-card", children: [pages.length === 0 ? (_jsx("p", { style: { color: 'var(--fg-muted)', textAlign: 'center', padding: '2rem' }, children: t('No pages yet. Create your first page!') })) : (_jsxs("table", { className: "admin-table", children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { children: t('Title') }), _jsx("th", { children: t('Slug') }), _jsx("th", { children: t('Template') }), _jsx("th", { children: t('Status') }), _jsx("th", { children: t('Actions') })] }) }), _jsx("tbody", { children: pages.map(page => (_jsxs("tr", { children: [_jsx("td", { children: _jsx(Link, { href: `/admin/pages/${page.id}/edit`, style: { color: 'var(--accent)', fontWeight: 500 }, children: page.title }) }), _jsx("td", { children: _jsxs("code", { children: ["/", page.slug] }) }), _jsx("td", { children: page.template }), _jsxs("td", { children: [_jsx("span", { className: `admin-badge ${page.published ? 'admin-badge-success' : 'admin-badge-warning'}`, children: page.published ? t('Published') : t('Draft') }), Number(page.is_private) ? (_jsxs(_Fragment, { children: [' ', _jsx("span", { className: "admin-badge admin-badge-info", title: t('Only people who can edit it can see it'), children: t('Private') })] })) : null] }), _jsx("td", { children: _jsxs("div", { style: { display: 'flex', gap: '0.5rem' }, children: [_jsx(Link, { href: `/admin/pages/${page.id}/edit`, className: "admin-btn admin-btn-secondary", children: t('Edit') }), _jsx("button", { onClick: () => handleDelete(page.id), className: "admin-btn admin-btn-danger", children: t('Delete') })] }) })] }, page.id))) })] })), meta && _jsx(Pagination, { meta: meta, onChange: setPage, noun: t('pages') })] }) }));
|
|
53
53
|
}
|
|
@@ -6,6 +6,7 @@ import AdminLayout from "../../layouts/AdminLayout.js";
|
|
|
6
6
|
import MarkdownEditor from "../../components/MarkdownEditor.js";
|
|
7
7
|
import { excerpt, slugify } from '@basicbenframework/core/content';
|
|
8
8
|
import FeaturedImageBox from "../../components/admin/FeaturedImageBox.js";
|
|
9
|
+
import SocialImageField from "../../components/admin/SocialImageField.js";
|
|
9
10
|
import { Slot, hasFills } from "../../host/index.js";
|
|
10
11
|
import TermPicker from "../../components/admin/TermPicker.js";
|
|
11
12
|
import SnippetPicker from "../../components/admin/SnippetPicker.js";
|
|
@@ -15,8 +16,15 @@ import { CustomFieldsPanel, CustomTermsPanel } from "../../components/admin/Cont
|
|
|
15
16
|
import { Link } from "../../components/Link.js";
|
|
16
17
|
import { can } from '@basicbenframework/core/auth/permissions';
|
|
17
18
|
import { useT, slots } from "../../i18n/index.js";
|
|
19
|
+
import { worthAutosaving } from "../../../helpers/autosave.js";
|
|
18
20
|
/** How often an unsaved change is autosaved, in milliseconds. */
|
|
19
21
|
const AUTOSAVE_EVERY = 30_000;
|
|
22
|
+
/**
|
|
23
|
+
* How long after the first words of a new post it is saved as a draft. Short,
|
|
24
|
+
* because until then a closed tab loses it; not instant, so the draft starts
|
|
25
|
+
* with more than its first letter.
|
|
26
|
+
*/
|
|
27
|
+
const FIRST_AUTOSAVE_AFTER = 3_000;
|
|
20
28
|
/** A stored UTC timestamp as the value a datetime-local input wants. */
|
|
21
29
|
function toLocalInput(value) {
|
|
22
30
|
if (!value)
|
|
@@ -112,12 +120,16 @@ export default function AdminPostEditor() {
|
|
|
112
120
|
publish_at: '',
|
|
113
121
|
og_title: '',
|
|
114
122
|
og_description: '',
|
|
123
|
+
// Media ids, like the featured image; SocialImageField shows them.
|
|
115
124
|
og_image: null,
|
|
116
|
-
og_image_url: null,
|
|
117
125
|
twitter_title: '',
|
|
118
126
|
twitter_description: '',
|
|
127
|
+
twitter_image: null,
|
|
119
128
|
canonical_url: '',
|
|
120
129
|
noindex: false,
|
|
130
|
+
// Private: published, but only for people who can edit it, in the admin.
|
|
131
|
+
// Posts and pages both; the server leaves it out of every public read.
|
|
132
|
+
is_private: false,
|
|
121
133
|
// Posts only: who may read the body. The levels beyond public, and what
|
|
122
134
|
// they mean, belong to a feature (see the post.editor.sidebar slot below).
|
|
123
135
|
visibility: 'public',
|
|
@@ -144,11 +156,76 @@ export default function AdminPostEditor() {
|
|
|
144
156
|
// The post as loaded, and the fields the sidebar's feature panels set.
|
|
145
157
|
const [loaded, setLoaded] = useState(null);
|
|
146
158
|
const [extra, setExtra] = useState({});
|
|
159
|
+
// The latest form, for timers that outlive the render that set them.
|
|
160
|
+
const latest = useRef(formData);
|
|
161
|
+
latest.current = formData;
|
|
162
|
+
// The draft this editor created for a new post (see createDraft). Its id is
|
|
163
|
+
// in the URL now, but what is on screen is newer than what was saved, so it
|
|
164
|
+
// is not loaded back over the top of the typing.
|
|
165
|
+
const adopted = useRef(null);
|
|
166
|
+
const creatingDraft = useRef(null);
|
|
167
|
+
// One per screen, so a retry after a lost response returns the draft the
|
|
168
|
+
// first try made instead of making another. randomUUID needs a secure
|
|
169
|
+
// context; an admin served over plain http elsewhere gets the fallback.
|
|
170
|
+
const autosaveKey = useRef(typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function'
|
|
171
|
+
? crypto.randomUUID()
|
|
172
|
+
: `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 12)}`);
|
|
147
173
|
useEffect(() => {
|
|
174
|
+
if (postId && postId === adopted.current)
|
|
175
|
+
return;
|
|
148
176
|
loadData();
|
|
149
177
|
}, [postId]);
|
|
150
|
-
|
|
151
|
-
|
|
178
|
+
/**
|
|
179
|
+
* Save a new post as a draft, so autosave has something to save against.
|
|
180
|
+
*
|
|
181
|
+
* Once, on the first title or body: an untouched New post screen creates
|
|
182
|
+
* nothing. The editor then becomes the editor for that post — the URL is
|
|
183
|
+
* replaced with its edit URL in place, the way WordPress does it, so the
|
|
184
|
+
* component, the focus and whatever is being typed stay exactly where they
|
|
185
|
+
* are. Saving waits for this if it is still in flight, so a quick Save does
|
|
186
|
+
* not create the post twice.
|
|
187
|
+
*/
|
|
188
|
+
const createDraft = () => {
|
|
189
|
+
if (creatingDraft.current)
|
|
190
|
+
return creatingDraft.current;
|
|
191
|
+
const sent = { title: latest.current.title, content: latest.current.content, excerpt: latest.current.excerpt };
|
|
192
|
+
creatingDraft.current = api
|
|
193
|
+
.post('/api/posts/autosave', { ...sent, post_type: postType, autosave_key: autosaveKey.current })
|
|
194
|
+
.then(res => {
|
|
195
|
+
const id = res?.post?.id;
|
|
196
|
+
if (!id)
|
|
197
|
+
throw new Error('No id');
|
|
198
|
+
adopted.current = id;
|
|
199
|
+
setLastAutosaved(new Date().toISOString());
|
|
200
|
+
// Only typing since the request left needs the next autosave.
|
|
201
|
+
const now = latest.current;
|
|
202
|
+
dirty.current = now.title !== sent.title || now.content !== sent.content || now.excerpt !== sent.excerpt;
|
|
203
|
+
// navigate() scrolls to the top, which is not where the writer is.
|
|
204
|
+
const scrolled = window.scrollY;
|
|
205
|
+
navigateWithType(to => navigate(to, { replace: true }), `/admin/posts/${id}/edit`, postType);
|
|
206
|
+
window.scrollTo(0, scrolled);
|
|
207
|
+
return id;
|
|
208
|
+
})
|
|
209
|
+
.catch(error => {
|
|
210
|
+
console.error('Failed to autosave the new post:', error);
|
|
211
|
+
// Try again on the next edit rather than never.
|
|
212
|
+
creatingDraft.current = null;
|
|
213
|
+
return null;
|
|
214
|
+
});
|
|
215
|
+
return creatingDraft.current;
|
|
216
|
+
};
|
|
217
|
+
// A new post's first autosave: a few seconds after there is a title or a
|
|
218
|
+
// body worth keeping (helpers/autosave.ts — a stray keystroke is not),
|
|
219
|
+
// timed from that point rather than reset by every keystroke.
|
|
220
|
+
const hasText = worthAutosaving(formData.title, formData.content);
|
|
221
|
+
useEffect(() => {
|
|
222
|
+
if (isPage || postId || !hasText)
|
|
223
|
+
return;
|
|
224
|
+
const timer = setTimeout(() => void createDraft(), FIRST_AUTOSAVE_AFTER);
|
|
225
|
+
return () => clearTimeout(timer);
|
|
226
|
+
}, [isPage, postId, hasText]);
|
|
227
|
+
// Autosave an existing post while it is being edited — including a new one
|
|
228
|
+
// once createDraft has given it an id.
|
|
152
229
|
useEffect(() => {
|
|
153
230
|
if (isPage || !postId)
|
|
154
231
|
return;
|
|
@@ -237,11 +314,12 @@ export default function AdminPostEditor() {
|
|
|
237
314
|
og_title: record.og_title || '',
|
|
238
315
|
og_description: record.og_description || '',
|
|
239
316
|
og_image: record.og_image ?? null,
|
|
240
|
-
og_image_url: null,
|
|
241
317
|
twitter_title: record.twitter_title || '',
|
|
242
318
|
twitter_description: record.twitter_description || '',
|
|
319
|
+
twitter_image: record.twitter_image ?? null,
|
|
243
320
|
canonical_url: record.canonical_url || '',
|
|
244
321
|
noindex: Boolean(Number(record.noindex ?? 0)),
|
|
322
|
+
is_private: Boolean(Number(record.is_private ?? 0)),
|
|
245
323
|
visibility: String(record.visibility || 'public'),
|
|
246
324
|
featured_image: record.featured_image ?? null,
|
|
247
325
|
featured_image_url: record.featured_image_url ?? null,
|
|
@@ -330,7 +408,7 @@ export default function AdminPostEditor() {
|
|
|
330
408
|
// `featured_image_url` is the server's answer, not the editor's input —
|
|
331
409
|
// sending it back would be asking the API to store a URL in a column that
|
|
332
410
|
// holds a media id.
|
|
333
|
-
const { featured_image_url,
|
|
411
|
+
const { featured_image_url, user_id, category_ids, tags, excerpt, status, publish_at, published, ...common } = formData;
|
|
334
412
|
const finalStatus = submitStatus ?? status;
|
|
335
413
|
// Posts only: a page has no visibility. `extra` is what the sidebar's
|
|
336
414
|
// feature panels set; see PostEditorSidebarProps.
|
|
@@ -361,8 +439,10 @@ export default function AdminPostEditor() {
|
|
|
361
439
|
...(contentType?.supports.custom_fields && contentType.fields.length ? { fields: customFields } : {}),
|
|
362
440
|
...(termsReady ? { term_ids: termIds } : {})
|
|
363
441
|
};
|
|
364
|
-
|
|
365
|
-
|
|
442
|
+
// A new post whose first autosave is on its way is that post now.
|
|
443
|
+
const id = postId ?? (creatingDraft.current ? await creatingDraft.current : null);
|
|
444
|
+
if (id) {
|
|
445
|
+
await api.put(`/api/${resource}/${id}`, payload);
|
|
366
446
|
}
|
|
367
447
|
else {
|
|
368
448
|
await api.post(`/api/${resource}`, payload);
|
|
@@ -394,6 +474,12 @@ export default function AdminPostEditor() {
|
|
|
394
474
|
[name]: type === 'checkbox' ? checked : value
|
|
395
475
|
}));
|
|
396
476
|
};
|
|
477
|
+
const setImage = (key, id) => {
|
|
478
|
+
if (lockedOut)
|
|
479
|
+
return;
|
|
480
|
+
dirty.current = true;
|
|
481
|
+
setFormData(prev => ({ ...prev, [key]: id }));
|
|
482
|
+
};
|
|
397
483
|
// What the main button will do, so its label can say so.
|
|
398
484
|
const scheduledFor = formData.publish_at ? new Date(formData.publish_at) : null;
|
|
399
485
|
const isFuture = Boolean(scheduledFor && scheduledFor.getTime() > Date.now());
|
|
@@ -507,11 +593,19 @@ export default function AdminPostEditor() {
|
|
|
507
593
|
: slots(t('{name} is editing this post. You can read it, but saving is paused so neither of you overwrites the other.'), { name: _jsx("strong", { children: lock.holder }) }) }), _jsxs("span", { style: { display: 'flex', gap: '0.5rem' }, children: [_jsx("button", { type: "button", className: "admin-btn admin-btn-primary", onClick: takeOver, children: t('Take over') }), _jsx(Link, { href: "/admin/posts", className: "admin-btn admin-btn-secondary", children: t('Back to posts') })] })] })), lock.status === 'free' && (_jsxs("div", { className: "admin-card", role: "status", style: { display: 'flex', gap: '0.75rem', alignItems: 'center', justifyContent: 'space-between' }, children: [_jsx("span", { children: t('{name} has finished editing. Reload to see their changes and start editing.', { name: lock.holder || t('The other editor') }) }), _jsx("button", { type: "button", className: "admin-btn admin-btn-primary", onClick: reloadAndEdit, children: t('Reload and edit') })] })), autosave && (_jsxs("div", { className: "admin-card", role: "status", style: { display: 'flex', gap: '0.75rem', alignItems: 'center', justifyContent: 'space-between' }, children: [_jsx("span", { children: t('There is an autosave of this post from {date} that is newer than the saved version.', { date: new Date(autosave.created_at).toLocaleString() }) }), _jsxs("span", { style: { display: 'flex', gap: '0.5rem' }, children: [_jsx("button", { type: "button", className: "admin-btn admin-btn-primary", onClick: restoreAutosave, children: t('Restore it') }), _jsx("button", { type: "button", className: "admin-btn admin-btn-secondary", onClick: () => setAutosave(null), children: t('Dismiss') })] })] })), _jsxs("div", { className: "admin-grid admin-grid-2", style: { gridTemplateColumns: '2fr 1fr' }, children: [_jsxs("div", { children: [_jsxs("div", { className: "admin-card", children: [_jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Title') }), _jsx("input", { type: "text", name: "title", value: formData.title, onChange: handleChange, className: "admin-input", placeholder: pick(t('Enter page title'), t('Enter post title'), t('Enter {type} title', { type: customType?.toLowerCase() ?? '' })), required: true }), _jsxs("div", { className: "admin-slug", children: [_jsx("label", { htmlFor: "slug", children: t('Slug') }), _jsx("input", { id: "slug", type: "text", name: "slug", value: formData.slug, onChange: handleChange, className: "admin-slug-input", placeholder: slugify(formData.title) || pick(t('page-url-slug'), t('post-url-slug'), t('{type}-url-slug', { type: slugify(customType ?? '') })) }), !formData.slug.trim() && _jsx("span", { className: "admin-slug-note", children: t('from the title') })] })] }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Content') }), _jsx(MarkdownEditor, { name: "content", value: formData.content, onChange: handleChange, minHeight: "400px", placeholder: t('Write your content here. Markdown is supported.'), required: true, readOnly: lockedOut, tools: (insert, disabled) => _jsx(SnippetPicker, { onInsert: insert, disabled: disabled }) })] }), !isPage && supports('excerpt') && (_jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Excerpt') }), _jsx("textarea", { name: "excerpt", value: formData.excerpt, onChange: handleChange, className: "admin-textarea", style: { minHeight: '100px' }, placeholder: summaryOf(formData.content) || t('Written for you from the content if you leave this blank') })] }))] }), !isPage && (_jsx(CustomFieldsPanel, { type: contentType, values: customFields, disabled: lockedOut, onChange: values => {
|
|
508
594
|
dirty.current = true;
|
|
509
595
|
setCustomFields(values);
|
|
510
|
-
} })), _jsxs("div", { className: "admin-card", children: [_jsx("h3", { className: "admin-card-title", children: t('SEO Settings') }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Meta Title') }), _jsx("input", { type: "text", name: "meta_title", value: formData.meta_title, onChange: handleChange, className: "admin-input", placeholder: t('SEO title') })] }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Meta Description') }), _jsx("textarea", { name: "meta_description", value: formData.meta_description, onChange: handleChange, className: "admin-textarea", style: { minHeight: '80px' }, placeholder: t('SEO description') })] }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Canonical URL') }), _jsx("input", { type: "url", name: "canonical_url", value: formData.canonical_url, onChange: handleChange, className: "admin-input", placeholder: t('Only if this was first published somewhere else') })] }), _jsx("div", { className: "admin-form-group", children: _jsxs("label", { style: { display: 'flex', alignItems: 'center', gap: '0.5rem' }, children: [_jsx("input", { type: "checkbox", name: "noindex", checked: formData.noindex, onChange: handleChange }), pick(t('Ask search engines not to index this page'), t('Ask search engines not to index this post'), t('Ask search engines not to index this {type}', { type: customType?.toLowerCase() ?? '' }))] }) })] }), _jsxs("div", { className: "admin-card", children: [_jsx("h3", { className: "admin-card-title", children: t('Social cards') }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Open Graph title') }), _jsx("input", { type: "text", name: "og_title", value: formData.og_title, onChange: handleChange, className: "admin-input", placeholder: formData.meta_title || formData.title || t('Falls back to the SEO title') })] }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Open Graph description') }), _jsx("textarea", { name: "og_description", value: formData.og_description, onChange: handleChange, className: "admin-textarea", style: { minHeight: '60px' }, placeholder: formData.meta_description || t('Falls back to the SEO description') })] }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Twitter / X title') }), _jsx("input", { type: "text", name: "twitter_title", value: formData.twitter_title, onChange: handleChange, className: "admin-input", placeholder: t('Falls back to the Open Graph title') })] }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Twitter / X description') }), _jsx("textarea", { name: "twitter_description", value: formData.twitter_description, onChange: handleChange, className: "admin-textarea", style: { minHeight: '60px' }, placeholder: t('Falls back to the Open Graph description') })] })] }), !isPage && isEditing && revisions.length > 0 && (_jsxs("div", { className: "admin-card", children: [_jsx("h3", { className: "admin-card-title", children: t('Revisions') }), _jsx("ul", { style: { listStyle: 'none', padding: 0, margin: 0, display: 'grid', gap: '0.5rem' }, children: revisions.slice(0, 20).map(revision => (_jsxs("li", { style: { display: 'flex', justifyContent: 'space-between', gap: '0.5rem', alignItems: 'center' }, children: [_jsxs("span", { children: [new Date(revision.created_at).toLocaleString(), revision.author_name ? ` — ${revision.author_name}` : '', _jsx("br", {}), _jsx("small", { style: { color: 'var(--fg-muted)' }, children: revision.title })] }), _jsx("button", { type: "button", className: "admin-btn admin-btn-secondary", onClick: () => restoreRevision(revision), children: t('Restore') })] }, revision.id))) })] }))] }), _jsxs("div", { children: [_jsxs("div", { className: "admin-card", children: [_jsx("h3", { className: "admin-card-title", children: t('Publish') }), isPage ? (_jsx("div", { className: "admin-form-group", children: _jsxs("label", { style: { display: 'flex', alignItems: 'center', gap: '0.5rem' }, children: [_jsx("input", { type: "checkbox", name: "published", checked: formData.published, onChange: handleChange }), t('Published')] }) })) : (_jsxs(_Fragment, { children: [_jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", htmlFor: "status", children: t('Status') }), _jsxs("select", { id: "status", name: "status", value: formData.status, onChange: handleChange, className: "admin-input", children: [_jsx("option", { value: "draft", children: t('Draft') }), _jsx("option", { value: "pending", children: t('Pending review') }), _jsx("option", { value: "published", disabled: !mayPublish && formData.status !== 'published', children: mayPublish ? t('Published') : t('Published (an editor publishes)') })] })] }), formData.status === 'published' && (_jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", htmlFor: "publish_at", children: t('Publish on') }), _jsx("input", { id: "publish_at", type: "datetime-local", name: "publish_at", value: formData.publish_at, onChange: handleChange, className: "admin-input" }), _jsx("small", { style: { color: 'var(--fg-muted)' }, children: isFuture
|
|
596
|
+
} })), _jsxs("div", { className: "admin-card", children: [_jsx("h3", { className: "admin-card-title", children: t('SEO Settings') }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Meta Title') }), _jsx("input", { type: "text", name: "meta_title", value: formData.meta_title, onChange: handleChange, className: "admin-input", placeholder: t('SEO title') })] }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Meta Description') }), _jsx("textarea", { name: "meta_description", value: formData.meta_description, onChange: handleChange, className: "admin-textarea", style: { minHeight: '80px' }, placeholder: t('SEO description') })] }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Canonical URL') }), _jsx("input", { type: "url", name: "canonical_url", value: formData.canonical_url, onChange: handleChange, className: "admin-input", placeholder: t('Only if this was first published somewhere else') })] }), _jsx("div", { className: "admin-form-group", children: _jsxs("label", { style: { display: 'flex', alignItems: 'center', gap: '0.5rem' }, children: [_jsx("input", { type: "checkbox", name: "noindex", checked: formData.noindex, onChange: handleChange }), pick(t('Ask search engines not to index this page'), t('Ask search engines not to index this post'), t('Ask search engines not to index this {type}', { type: customType?.toLowerCase() ?? '' }))] }) })] }), _jsxs("div", { className: "admin-card", children: [_jsx("h3", { className: "admin-card-title", children: t('Social cards') }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Open Graph title') }), _jsx("input", { type: "text", name: "og_title", value: formData.og_title, onChange: handleChange, className: "admin-input", placeholder: formData.meta_title || formData.title || t('Falls back to the SEO title') })] }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Open Graph description') }), _jsx("textarea", { name: "og_description", value: formData.og_description, onChange: handleChange, className: "admin-textarea", style: { minHeight: '60px' }, placeholder: formData.meta_description || t('Falls back to the SEO description') })] }), _jsx(SocialImageField, { id: "og_image", label: t('Open Graph image'), fallback: t('Falls back to the featured image.'), value: formData.og_image, disabled: lockedOut, onChange: id => setImage('og_image', id) }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Twitter / X title') }), _jsx("input", { type: "text", name: "twitter_title", value: formData.twitter_title, onChange: handleChange, className: "admin-input", placeholder: t('Falls back to the Open Graph title') })] }), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", children: t('Twitter / X description') }), _jsx("textarea", { name: "twitter_description", value: formData.twitter_description, onChange: handleChange, className: "admin-textarea", style: { minHeight: '60px' }, placeholder: t('Falls back to the Open Graph description') })] }), _jsx(SocialImageField, { id: "twitter_image", label: t('Twitter / X image'), fallback: t('Falls back to the Open Graph image.'), value: formData.twitter_image, disabled: lockedOut, onChange: id => setImage('twitter_image', id) })] }), !isPage && isEditing && revisions.length > 0 && (_jsxs("div", { className: "admin-card", children: [_jsx("h3", { className: "admin-card-title", children: t('Revisions') }), _jsx("ul", { style: { listStyle: 'none', padding: 0, margin: 0, display: 'grid', gap: '0.5rem' }, children: revisions.slice(0, 20).map(revision => (_jsxs("li", { style: { display: 'flex', justifyContent: 'space-between', gap: '0.5rem', alignItems: 'center' }, children: [_jsxs("span", { children: [new Date(revision.created_at).toLocaleString(), revision.author_name ? ` — ${revision.author_name}` : '', _jsx("br", {}), _jsx("small", { style: { color: 'var(--fg-muted)' }, children: revision.title })] }), _jsx("button", { type: "button", className: "admin-btn admin-btn-secondary", onClick: () => restoreRevision(revision), children: t('Restore') })] }, revision.id))) })] }))] }), _jsxs("div", { children: [_jsxs("div", { className: "admin-card", children: [_jsx("h3", { className: "admin-card-title", children: t('Publish') }), isPage ? (_jsx("div", { className: "admin-form-group", children: _jsxs("label", { style: { display: 'flex', alignItems: 'center', gap: '0.5rem' }, children: [_jsx("input", { type: "checkbox", name: "published", checked: formData.published, onChange: handleChange }), t('Published')] }) })) : (_jsxs(_Fragment, { children: [_jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", htmlFor: "status", children: t('Status') }), _jsxs("select", { id: "status", name: "status", value: formData.status, onChange: handleChange, className: "admin-input", children: [_jsx("option", { value: "draft", children: t('Draft') }), _jsx("option", { value: "pending", children: t('Pending review') }), _jsx("option", { value: "published", disabled: !mayPublish && formData.status !== 'published', children: mayPublish ? t('Published') : t('Published (an editor publishes)') })] })] }), formData.status === 'published' && (_jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", htmlFor: "publish_at", children: t('Publish on') }), _jsx("input", { id: "publish_at", type: "datetime-local", name: "publish_at", value: formData.publish_at, onChange: handleChange, className: "admin-input" }), _jsx("small", { style: { color: 'var(--fg-muted)' }, children: isFuture
|
|
511
597
|
? t('Scheduled — goes live {date}.', { date: scheduledFor.toLocaleString() })
|
|
512
598
|
: formData.publish_at
|
|
513
599
|
? t('Already live.')
|
|
514
|
-
: t('Blank publishes now. A future date schedules it.') })] })), lastAutosaved && (_jsx("p", { style: { color: 'var(--fg-muted)', fontSize: '0.8rem' }, children: t('Autosaved {time}', { time: new Date(lastAutosaved).toLocaleTimeString() }) }))] })), _jsxs("div", {
|
|
600
|
+
: t('Blank publishes now. A future date schedules it.') })] })), lastAutosaved && (_jsx("p", { style: { color: 'var(--fg-muted)', fontSize: '0.8rem' }, children: t('Autosaved {time}', { time: new Date(lastAutosaved).toLocaleTimeString() }) }))] })), _jsxs("div", { className: "admin-form-group", children: [_jsx("label", { className: "admin-label", htmlFor: "is_private", children: t('Visibility') }), _jsxs("select", { id: "is_private", value: formData.is_private ? 'private' : 'public', onChange: event => {
|
|
601
|
+
if (lockedOut)
|
|
602
|
+
return;
|
|
603
|
+
dirty.current = true;
|
|
604
|
+
const isPrivate = event.target.value === 'private';
|
|
605
|
+
setFormData(prev => ({ ...prev, is_private: isPrivate }));
|
|
606
|
+
}, className: "admin-input", children: [_jsx("option", { value: "public", children: t('Public') }), _jsx("option", { value: "private", children: t('Private') })] }), formData.is_private && (_jsx("small", { style: { color: 'var(--fg-muted)' }, children: isPage
|
|
607
|
+
? t('Only people who can edit pages see it, here in the admin. It is left out of the site, the API, feeds, the sitemap and search.')
|
|
608
|
+
: t('Only its author, editors and admins see it, here in the admin. It is left out of the site, the API, feeds, the sitemap and search.') }))] }), _jsxs("div", { style: { display: 'flex', gap: '0.5rem' }, children: [_jsx("button", { type: "submit", className: "admin-btn admin-btn-primary", disabled: saving || lockedOut, title: lockedOut ? t('Someone else is editing this post') : undefined, style: { flex: 1 }, children: submitLabel }), _jsx(Link, { href: `/admin/${resource}`, className: "admin-btn admin-btn-secondary", children: t('Cancel') })] })] }), !isPage && (_jsx(Slot, { name: "post.editor.sidebar", kind: "post", postId: postId, record: loaded, values: { visibility: formData.visibility, ...extra }, disabled: lockedOut, onChange: (changes) => {
|
|
515
609
|
dirty.current = true;
|
|
516
610
|
const { visibility, ...rest } = changes;
|
|
517
611
|
if (typeof visibility === 'string')
|
|
@@ -145,8 +145,8 @@ export default function AdminPosts() {
|
|
|
145
145
|
: t('Nothing here.') })) : (_jsxs("table", { className: "admin-table", children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { style: { width: '2rem' }, children: _jsx("input", { type: "checkbox", "aria-label": t('Select all posts on this page'), checked: allSelected, onChange: () => setSelected(allSelected ? [] : posts.map(p => p.id)) }) }), _jsx("th", { children: t('Title') }), _jsx("th", { children: t('Author') }), _jsx("th", { children: t('Categories') }), _jsx("th", { children: t('Status') }), _jsx("th", { children: t('Date') }), _jsx("th", { children: t('Actions') })] }) }), _jsx("tbody", { children: posts.map(post => {
|
|
146
146
|
const badge = badgeFor(post);
|
|
147
147
|
const date = parseUtc(post.publish_at) ?? parseUtc(post.created_at);
|
|
148
|
-
return (_jsxs("tr", { children: [_jsx("td", { children: _jsx("input", { type: "checkbox", "aria-label": t('Select {name}', { name: post.title }), checked: selected.includes(post.id), onChange: () => toggle(post.id) }) }), _jsx("td", { children: inTrash ? (_jsx("span", { style: { fontWeight: 500 }, children: post.title })) : (_jsx(Link, { href: `/admin/posts/${post.id}/edit`, style: { color: 'var(--accent)', fontWeight: 500 }, children: post.title })) }), _jsx("td", { children: post.author_name || '—' }), _jsx("td", { children: post.categories?.length
|
|
148
|
+
return (_jsxs("tr", { children: [_jsx("td", { children: _jsx("input", { type: "checkbox", "aria-label": t('Select {name}', { name: post.title }), checked: selected.includes(post.id), onChange: () => toggle(post.id) }) }), _jsx("td", { children: inTrash ? (_jsx("span", { style: { fontWeight: 500 }, children: post.title || t('(no title)') })) : (_jsx(Link, { href: `/admin/posts/${post.id}/edit`, style: { color: 'var(--accent)', fontWeight: 500 }, children: post.title || t('(no title)') })) }), _jsx("td", { children: post.author_name || '—' }), _jsx("td", { children: post.categories?.length
|
|
149
149
|
? post.categories.map(c => c.name).join(', ')
|
|
150
|
-
: '—' }),
|
|
150
|
+
: '—' }), _jsxs("td", { children: [_jsx("span", { className: `admin-badge ${badge.tone}`, children: badge.label }), Number(post.is_private) ? (_jsxs(_Fragment, { children: [' ', _jsx("span", { className: "admin-badge admin-badge-info", title: t('Only people who can edit it can see it'), children: t('Private') })] })) : null] }), _jsx("td", { children: date ? date.toLocaleDateString() : '—' }), _jsx("td", { children: _jsx("div", { style: { display: 'flex', gap: '0.5rem' }, children: inTrash ? (_jsxs(_Fragment, { children: [_jsx("button", { onClick: () => runBulk('restore', [post.id]), className: "admin-btn admin-btn-secondary", children: t('Restore') }), _jsx("button", { onClick: () => runBulk('delete', [post.id]), className: "admin-btn admin-btn-danger", children: t('Delete permanently') })] })) : (_jsxs(_Fragment, { children: [_jsx(Link, { href: `/admin/posts/${post.id}/edit`, className: "admin-btn admin-btn-secondary", children: t('Edit') }), _jsx("button", { onClick: () => trash(post.id), className: "admin-btn admin-btn-danger", children: t('Trash') })] })) }) })] }, post.id));
|
|
151
151
|
}) })] })), meta && _jsx(Pagination, { meta: meta, onChange: setPage, noun: t('posts') })] }) }));
|
|
152
152
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getDb } from '@basicbenframework/core/db';
|
|
2
2
|
import { Settings } from "../models/Settings.js";
|
|
3
|
-
import { livePost } from "../models/visibility.js";
|
|
3
|
+
import { livePost, livePage } from "../models/visibility.js";
|
|
4
4
|
import { siteUrl } from "../helpers/site.js";
|
|
5
5
|
import { isGated, gatedFeedHtml, publicExcerpt } from "../helpers/gating.js";
|
|
6
6
|
import { excerpt as plainText } from '@basicbenframework/core/content';
|
|
@@ -178,7 +178,7 @@ export const FeedController = {
|
|
|
178
178
|
const baseOf = (key) => customTypes.find((type) => type.key === key)?.route_base ?? null;
|
|
179
179
|
const pages = await db.all(`
|
|
180
180
|
SELECT id, slug, updated_at FROM pages
|
|
181
|
-
WHERE
|
|
181
|
+
WHERE ${livePage('pages')} AND (noindex IS NULL OR noindex = 0)
|
|
182
182
|
`);
|
|
183
183
|
const categories = await db.all('SELECT id, slug FROM categories');
|
|
184
184
|
const url = (loc, extra = '') => ` <url>
|