@vedyua/cms 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -2
- package/dist/admin-bar.d.ts +12 -0
- package/dist/admin-bar.js +94 -0
- package/dist/admin-bar.js.map +1 -0
- package/dist/admin-entry.d.ts +23 -0
- package/dist/admin-entry.js +112 -0
- package/dist/admin-entry.js.map +1 -0
- package/dist/context.d.ts +50 -0
- package/dist/context.js +260 -0
- package/dist/context.js.map +1 -0
- package/dist/cookies.d.ts +8 -0
- package/dist/cookies.js +37 -0
- package/dist/cookies.js.map +1 -0
- package/dist/define.d.ts +32 -0
- package/dist/define.js +100 -0
- package/dist/define.js.map +1 -0
- package/dist/editable-text.d.ts +20 -0
- package/dist/editable-text.js +114 -0
- package/dist/editable-text.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +61 -0
- package/dist/index.js.map +1 -0
- package/dist/next/create-api.d.ts +39 -0
- package/dist/next/create-api.js +238 -0
- package/dist/next/create-api.js.map +1 -0
- package/dist/next/index.d.ts +7 -0
- package/dist/next/index.js +7 -0
- package/dist/next/index.js.map +1 -0
- package/dist/next/save-handler.d.ts +19 -0
- package/dist/next/save-handler.js +96 -0
- package/dist/next/save-handler.js.map +1 -0
- package/dist/page-settings.d.ts +12 -0
- package/dist/page-settings.js +144 -0
- package/dist/page-settings.js.map +1 -0
- package/dist/path.d.ts +5 -0
- package/dist/path.js +38 -0
- package/dist/path.js.map +1 -0
- package/dist/server/auth-github.d.ts +24 -0
- package/dist/server/auth-github.js +112 -0
- package/dist/server/auth-github.js.map +1 -0
- package/dist/server/auth-password.d.ts +20 -0
- package/dist/server/auth-password.js +41 -0
- package/dist/server/auth-password.js.map +1 -0
- package/dist/server/auth.d.ts +45 -0
- package/dist/server/auth.js +1 -0
- package/dist/server/auth.js.map +1 -0
- package/dist/server/index.d.ts +7 -0
- package/dist/server/index.js +18 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/publish-command.d.ts +37 -0
- package/dist/server/publish-command.js +170 -0
- package/dist/server/publish-command.js.map +1 -0
- package/dist/server/publish-status.d.ts +20 -0
- package/dist/server/publish-status.js +131 -0
- package/dist/server/publish-status.js.map +1 -0
- package/dist/server/publish.d.ts +25 -0
- package/dist/server/publish.js +1 -0
- package/dist/server/publish.js.map +1 -0
- package/dist/server/session.d.ts +21 -0
- package/dist/server/session.js +95 -0
- package/dist/server/session.js.map +1 -0
- package/dist/styles.css +348 -0
- package/dist/types.d.ts +85 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/package.json +62 -4
package/README.md
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@vedyua/cms`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Universal inline editor for static JSON content.
|
|
4
|
+
|
|
5
|
+
- Mark fields in markup with `<EditableText />`
|
|
6
|
+
- Auth via pluggable adapters (`passwordAuth`, `githubAuth`, `devAuth`, or your own)
|
|
7
|
+
- Save drafts to JSON files
|
|
8
|
+
- Publish via pluggable adapters (safe background rebuild)
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm i @vedyua/cms
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Package exports
|
|
17
|
+
|
|
18
|
+
| Import | Purpose |
|
|
19
|
+
| --- | --- |
|
|
20
|
+
| `@vedyua/cms` | React UI + schema helpers (client-safe) |
|
|
21
|
+
| `@vedyua/cms/server` | `passwordAuth`, `commandPublish`, session store |
|
|
22
|
+
| `@vedyua/cms/next` | `createContentApi` for App Router |
|
|
23
|
+
| `@vedyua/cms/styles.css` | Admin UI styles |
|
|
24
|
+
|
|
25
|
+
## Develop
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install
|
|
29
|
+
npm run build
|
|
30
|
+
npm run dev
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Publish (maintainers)
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm login
|
|
37
|
+
npm publish --access public
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Minimal wiring
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createContentApi } from "@vedyua/cms/next";
|
|
44
|
+
import { commandPublish, passwordAuth } from "@vedyua/cms/server";
|
|
45
|
+
|
|
46
|
+
export const api = createContentApi({
|
|
47
|
+
config,
|
|
48
|
+
sessionSecret: process.env.CONTENT_CMS_SESSION_SECRET!,
|
|
49
|
+
auth: passwordAuth({
|
|
50
|
+
password: process.env.CONTENT_CMS_PASSWORD!,
|
|
51
|
+
}),
|
|
52
|
+
publish: commandPublish({
|
|
53
|
+
command: process.env.CONTENT_CMS_PUBLISH_COMMAND!,
|
|
54
|
+
}),
|
|
55
|
+
});
|
|
56
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
type AdminBarProps = {
|
|
4
|
+
/** Optional brand label shown on the left. */
|
|
5
|
+
title?: string;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Floating toolbar for edit mode. Hides itself when `editMode` is false.
|
|
9
|
+
*/
|
|
10
|
+
declare function AdminBar({ title }: AdminBarProps): react.JSX.Element | null;
|
|
11
|
+
|
|
12
|
+
export { AdminBar, type AdminBarProps };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { useContentCms } from "./context";
|
|
5
|
+
import { getDocument, listTextFields } from "./define";
|
|
6
|
+
import { PageSettingsPanel } from "./page-settings";
|
|
7
|
+
function AdminBar({ title = "Content CMS" }) {
|
|
8
|
+
const {
|
|
9
|
+
config,
|
|
10
|
+
editMode,
|
|
11
|
+
session,
|
|
12
|
+
publishEnabled,
|
|
13
|
+
dirtyCount,
|
|
14
|
+
saving,
|
|
15
|
+
publishing,
|
|
16
|
+
lastError,
|
|
17
|
+
lastPublishSha,
|
|
18
|
+
saveDrafts,
|
|
19
|
+
publish,
|
|
20
|
+
discardDrafts
|
|
21
|
+
} = useContentCms();
|
|
22
|
+
const [pageSettingsOpen, setPageSettingsOpen] = useState(false);
|
|
23
|
+
if (!editMode) return null;
|
|
24
|
+
const fields = listTextFields(config);
|
|
25
|
+
const busy = saving || publishing;
|
|
26
|
+
const logoutPath = config.logoutPath ?? "/api/content/auth/logout";
|
|
27
|
+
const seoDocumentName = config.seoDocument ?? "seo";
|
|
28
|
+
const hasPageSettings = Boolean(getDocument(config, seoDocumentName)) && (config.pages?.length ?? 0) > 0;
|
|
29
|
+
return /* @__PURE__ */ jsxs("div", { className: "scms-admin-bar", role: "region", "aria-label": "Content editor toolbar", children: [
|
|
30
|
+
/* @__PURE__ */ jsxs("div", { className: "scms-admin-bar__main", children: [
|
|
31
|
+
/* @__PURE__ */ jsxs("div", { className: "scms-admin-bar__brand", children: [
|
|
32
|
+
/* @__PURE__ */ jsx("strong", { children: title }),
|
|
33
|
+
/* @__PURE__ */ jsxs("span", { className: "scms-admin-bar__meta", children: [
|
|
34
|
+
session ? `${session.name} \xB7 ` : "",
|
|
35
|
+
fields.length,
|
|
36
|
+
" text fields \xB7 ",
|
|
37
|
+
dirtyCount > 0 ? `${dirtyCount} unsaved` : "clean",
|
|
38
|
+
lastPublishSha ? ` \xB7 published ${lastPublishSha.slice(0, 7)}` : ""
|
|
39
|
+
] })
|
|
40
|
+
] }),
|
|
41
|
+
/* @__PURE__ */ jsxs("div", { className: "scms-admin-bar__actions", children: [
|
|
42
|
+
hasPageSettings ? /* @__PURE__ */ jsx(
|
|
43
|
+
"button",
|
|
44
|
+
{
|
|
45
|
+
type: "button",
|
|
46
|
+
className: `scms-btn ${pageSettingsOpen ? "scms-btn--primary" : "scms-btn--ghost"}`,
|
|
47
|
+
onClick: () => setPageSettingsOpen((open) => !open),
|
|
48
|
+
title: "Edit title, description, favicon and SEO for pages",
|
|
49
|
+
children: "Page settings"
|
|
50
|
+
}
|
|
51
|
+
) : null,
|
|
52
|
+
/* @__PURE__ */ jsx(
|
|
53
|
+
"button",
|
|
54
|
+
{
|
|
55
|
+
type: "button",
|
|
56
|
+
className: "scms-btn scms-btn--ghost",
|
|
57
|
+
disabled: dirtyCount === 0 || busy,
|
|
58
|
+
onClick: () => discardDrafts(),
|
|
59
|
+
children: "Discard"
|
|
60
|
+
}
|
|
61
|
+
),
|
|
62
|
+
/* @__PURE__ */ jsx(
|
|
63
|
+
"button",
|
|
64
|
+
{
|
|
65
|
+
type: "button",
|
|
66
|
+
className: "scms-btn scms-btn--ghost",
|
|
67
|
+
disabled: dirtyCount === 0 || busy,
|
|
68
|
+
onClick: () => void saveDrafts(),
|
|
69
|
+
children: saving ? "Saving\u2026" : "Save draft"
|
|
70
|
+
}
|
|
71
|
+
),
|
|
72
|
+
/* @__PURE__ */ jsx(
|
|
73
|
+
"button",
|
|
74
|
+
{
|
|
75
|
+
type: "button",
|
|
76
|
+
className: "scms-btn scms-btn--primary",
|
|
77
|
+
disabled: !publishEnabled || busy,
|
|
78
|
+
onClick: () => void publish(),
|
|
79
|
+
title: !publishEnabled ? "Publish adapter is not configured" : publishing ? "Rebuild already in progress" : "Save drafts, then rebuild the site safely in the background",
|
|
80
|
+
children: publishing ? "Building\u2026" : "Publish"
|
|
81
|
+
}
|
|
82
|
+
),
|
|
83
|
+
/* @__PURE__ */ jsx("a", { className: "scms-btn scms-btn--ghost", href: logoutPath, children: "Log out" })
|
|
84
|
+
] })
|
|
85
|
+
] }),
|
|
86
|
+
pageSettingsOpen ? /* @__PURE__ */ jsx(PageSettingsPanel, { open: pageSettingsOpen, onClose: () => setPageSettingsOpen(false) }) : null,
|
|
87
|
+
lastError ? /* @__PURE__ */ jsx("p", { className: "scms-admin-bar__error", children: lastError }) : null,
|
|
88
|
+
/* @__PURE__ */ jsx("p", { className: "scms-admin-bar__hint", children: "Save writes JSON on disk. Publish rebuilds the site in the background \u2014 the live site stays up until the new build is ready; a failed build rolls back." })
|
|
89
|
+
] });
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
AdminBar
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=admin-bar.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/admin-bar.tsx"],"sourcesContent":["\"use client\";\n\nimport { useState } from \"react\";\n\nimport { useContentCms } from \"./context\";\nimport { getDocument, listTextFields } from \"./define\";\nimport { PageSettingsPanel } from \"./page-settings\";\n\nexport type AdminBarProps = {\n /** Optional brand label shown on the left. */\n title?: string;\n};\n\n/**\n * Floating toolbar for edit mode. Hides itself when `editMode` is false.\n */\nexport function AdminBar({ title = \"Content CMS\" }: AdminBarProps) {\n const {\n config,\n editMode,\n session,\n publishEnabled,\n dirtyCount,\n saving,\n publishing,\n lastError,\n lastPublishSha,\n saveDrafts,\n publish,\n discardDrafts,\n } = useContentCms();\n const [pageSettingsOpen, setPageSettingsOpen] = useState(false);\n\n if (!editMode) return null;\n\n const fields = listTextFields(config);\n const busy = saving || publishing;\n const logoutPath = config.logoutPath ?? \"/api/content/auth/logout\";\n const seoDocumentName = config.seoDocument ?? \"seo\";\n const hasPageSettings =\n Boolean(getDocument(config, seoDocumentName)) && (config.pages?.length ?? 0) > 0;\n\n return (\n <div className=\"scms-admin-bar\" role=\"region\" aria-label=\"Content editor toolbar\">\n <div className=\"scms-admin-bar__main\">\n <div className=\"scms-admin-bar__brand\">\n <strong>{title}</strong>\n <span className=\"scms-admin-bar__meta\">\n {session ? `${session.name} · ` : \"\"}\n {fields.length} text fields · {dirtyCount > 0 ? `${dirtyCount} unsaved` : \"clean\"}\n {lastPublishSha ? ` · published ${lastPublishSha.slice(0, 7)}` : \"\"}\n </span>\n </div>\n <div className=\"scms-admin-bar__actions\">\n {hasPageSettings ? (\n <button\n type=\"button\"\n className={`scms-btn ${pageSettingsOpen ? \"scms-btn--primary\" : \"scms-btn--ghost\"}`}\n onClick={() => setPageSettingsOpen((open) => !open)}\n title=\"Edit title, description, favicon and SEO for pages\"\n >\n Page settings\n </button>\n ) : null}\n <button\n type=\"button\"\n className=\"scms-btn scms-btn--ghost\"\n disabled={dirtyCount === 0 || busy}\n onClick={() => discardDrafts()}\n >\n Discard\n </button>\n <button\n type=\"button\"\n className=\"scms-btn scms-btn--ghost\"\n disabled={dirtyCount === 0 || busy}\n onClick={() => void saveDrafts()}\n >\n {saving ? \"Saving…\" : \"Save draft\"}\n </button>\n <button\n type=\"button\"\n className=\"scms-btn scms-btn--primary\"\n disabled={!publishEnabled || busy}\n onClick={() => void publish()}\n title={\n !publishEnabled\n ? \"Publish adapter is not configured\"\n : publishing\n ? \"Rebuild already in progress\"\n : \"Save drafts, then rebuild the site safely in the background\"\n }\n >\n {publishing ? \"Building…\" : \"Publish\"}\n </button>\n <a className=\"scms-btn scms-btn--ghost\" href={logoutPath}>\n Log out\n </a>\n </div>\n </div>\n {pageSettingsOpen ? (\n <PageSettingsPanel open={pageSettingsOpen} onClose={() => setPageSettingsOpen(false)} />\n ) : null}\n {lastError ? <p className=\"scms-admin-bar__error\">{lastError}</p> : null}\n <p className=\"scms-admin-bar__hint\">\n Save writes JSON on disk. Publish rebuilds the site in the background — the live site stays\n up until the new build is ready; a failed build rolls back.\n </p>\n </div>\n );\n}\n"],"mappings":";AA8CU,cACA,YADA;AA5CV,SAAS,gBAAgB;AAEzB,SAAS,qBAAqB;AAC9B,SAAS,aAAa,sBAAsB;AAC5C,SAAS,yBAAyB;AAU3B,SAAS,SAAS,EAAE,QAAQ,cAAc,GAAkB;AACjE,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,cAAc;AAClB,QAAM,CAAC,kBAAkB,mBAAmB,IAAI,SAAS,KAAK;AAE9D,MAAI,CAAC,SAAU,QAAO;AAEtB,QAAM,SAAS,eAAe,MAAM;AACpC,QAAM,OAAO,UAAU;AACvB,QAAM,aAAa,OAAO,cAAc;AACxC,QAAM,kBAAkB,OAAO,eAAe;AAC9C,QAAM,kBACJ,QAAQ,YAAY,QAAQ,eAAe,CAAC,MAAM,OAAO,OAAO,UAAU,KAAK;AAEjF,SACE,qBAAC,SAAI,WAAU,kBAAiB,MAAK,UAAS,cAAW,0BACvD;AAAA,yBAAC,SAAI,WAAU,wBACb;AAAA,2BAAC,SAAI,WAAU,yBACb;AAAA,4BAAC,YAAQ,iBAAM;AAAA,QACf,qBAAC,UAAK,WAAU,wBACb;AAAA,oBAAU,GAAG,QAAQ,IAAI,WAAQ;AAAA,UACjC,OAAO;AAAA,UAAO;AAAA,UAAgB,aAAa,IAAI,GAAG,UAAU,aAAa;AAAA,UACzE,iBAAiB,mBAAgB,eAAe,MAAM,GAAG,CAAC,CAAC,KAAK;AAAA,WACnE;AAAA,SACF;AAAA,MACA,qBAAC,SAAI,WAAU,2BACZ;AAAA,0BACC;AAAA,UAAC;AAAA;AAAA,YACC,MAAK;AAAA,YACL,WAAW,YAAY,mBAAmB,sBAAsB,iBAAiB;AAAA,YACjF,SAAS,MAAM,oBAAoB,CAAC,SAAS,CAAC,IAAI;AAAA,YAClD,OAAM;AAAA,YACP;AAAA;AAAA,QAED,IACE;AAAA,QACJ;AAAA,UAAC;AAAA;AAAA,YACC,MAAK;AAAA,YACL,WAAU;AAAA,YACV,UAAU,eAAe,KAAK;AAAA,YAC9B,SAAS,MAAM,cAAc;AAAA,YAC9B;AAAA;AAAA,QAED;AAAA,QACA;AAAA,UAAC;AAAA;AAAA,YACC,MAAK;AAAA,YACL,WAAU;AAAA,YACV,UAAU,eAAe,KAAK;AAAA,YAC9B,SAAS,MAAM,KAAK,WAAW;AAAA,YAE9B,mBAAS,iBAAY;AAAA;AAAA,QACxB;AAAA,QACA;AAAA,UAAC;AAAA;AAAA,YACC,MAAK;AAAA,YACL,WAAU;AAAA,YACV,UAAU,CAAC,kBAAkB;AAAA,YAC7B,SAAS,MAAM,KAAK,QAAQ;AAAA,YAC5B,OACE,CAAC,iBACG,sCACA,aACE,gCACA;AAAA,YAGP,uBAAa,mBAAc;AAAA;AAAA,QAC9B;AAAA,QACA,oBAAC,OAAE,WAAU,4BAA2B,MAAM,YAAY,qBAE1D;AAAA,SACF;AAAA,OACF;AAAA,IACC,mBACC,oBAAC,qBAAkB,MAAM,kBAAkB,SAAS,MAAM,oBAAoB,KAAK,GAAG,IACpF;AAAA,IACH,YAAY,oBAAC,OAAE,WAAU,yBAAyB,qBAAU,IAAO;AAAA,IACpE,oBAAC,OAAE,WAAU,wBAAuB,0KAGpC;AAAA,KACF;AAEJ;","names":[]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ContentEditorSession } from './context.js';
|
|
3
|
+
import { ContentConfig } from './types.js';
|
|
4
|
+
|
|
5
|
+
type AdminEntryProps = {
|
|
6
|
+
config: ContentConfig;
|
|
7
|
+
session?: ContentEditorSession | null;
|
|
8
|
+
authLabel?: string;
|
|
9
|
+
/** @default "password" */
|
|
10
|
+
authKind?: "password" | "oauth" | "instant";
|
|
11
|
+
publishEnabled?: boolean;
|
|
12
|
+
/** When false, login is disabled and a setup hint is shown. @default true */
|
|
13
|
+
configured?: boolean;
|
|
14
|
+
/** Site URL opened after enabling edit mode. @default "/" */
|
|
15
|
+
siteHref?: string;
|
|
16
|
+
title?: string;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* `/admin` landing page: login (if needed), then enter visual edit mode.
|
|
20
|
+
*/
|
|
21
|
+
declare function AdminEntry({ config, session, authLabel, authKind, publishEnabled, configured, siteHref, title, }: AdminEntryProps): react.JSX.Element;
|
|
22
|
+
|
|
23
|
+
export { AdminEntry, type AdminEntryProps };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { writeBrowserCookie } from "./cookies";
|
|
5
|
+
function AdminEntry({
|
|
6
|
+
config,
|
|
7
|
+
session = null,
|
|
8
|
+
authLabel = "Sign in",
|
|
9
|
+
authKind = "password",
|
|
10
|
+
publishEnabled = false,
|
|
11
|
+
configured = true,
|
|
12
|
+
siteHref = "/",
|
|
13
|
+
title = "Content editor"
|
|
14
|
+
}) {
|
|
15
|
+
const cookieName = config.editCookie ?? "scms_edit";
|
|
16
|
+
const loginPath = config.loginPath ?? "/api/content/auth/login";
|
|
17
|
+
const logoutPath = config.logoutPath ?? "/api/content/auth/logout";
|
|
18
|
+
const [username, setUsername] = useState("");
|
|
19
|
+
const [password, setPassword] = useState("");
|
|
20
|
+
const [busy, setBusy] = useState(false);
|
|
21
|
+
const [error, setError] = useState(null);
|
|
22
|
+
async function onPasswordLogin(event) {
|
|
23
|
+
event.preventDefault();
|
|
24
|
+
setBusy(true);
|
|
25
|
+
setError(null);
|
|
26
|
+
try {
|
|
27
|
+
const response = await fetch(loginPath, {
|
|
28
|
+
method: "POST",
|
|
29
|
+
headers: { "Content-Type": "application/json" },
|
|
30
|
+
body: JSON.stringify({ username, password, returnTo: "/admin" })
|
|
31
|
+
});
|
|
32
|
+
const payload = await response.json().catch(() => null);
|
|
33
|
+
if (!response.ok || !payload?.ok) {
|
|
34
|
+
throw new Error(payload?.error ?? `Login failed (${response.status})`);
|
|
35
|
+
}
|
|
36
|
+
writeBrowserCookie(cookieName, "1");
|
|
37
|
+
window.location.href = "/admin";
|
|
38
|
+
} catch (err) {
|
|
39
|
+
setError(err instanceof Error ? err.message : "Login failed");
|
|
40
|
+
} finally {
|
|
41
|
+
setBusy(false);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return /* @__PURE__ */ jsx("main", { className: "scms-admin-entry", children: /* @__PURE__ */ jsxs("div", { className: "scms-admin-entry__card", children: [
|
|
45
|
+
/* @__PURE__ */ jsx("p", { className: "scms-admin-entry__eyebrow", children: "Content CMS" }),
|
|
46
|
+
/* @__PURE__ */ jsx("h1", { children: title }),
|
|
47
|
+
/* @__PURE__ */ jsx("div", { className: "scms-admin-entry__actions", children: !configured ? null : session ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
48
|
+
/* @__PURE__ */ jsx(
|
|
49
|
+
"a",
|
|
50
|
+
{
|
|
51
|
+
className: "scms-btn scms-btn--primary",
|
|
52
|
+
href: siteHref,
|
|
53
|
+
onClick: () => writeBrowserCookie(cookieName, "1"),
|
|
54
|
+
children: "Open site for editing"
|
|
55
|
+
}
|
|
56
|
+
),
|
|
57
|
+
/* @__PURE__ */ jsx("a", { className: "scms-btn scms-btn--ghost", href: logoutPath, children: "Log out" })
|
|
58
|
+
] }) : authKind === "password" ? /* @__PURE__ */ jsxs("form", { className: "scms-login-form", onSubmit: (event) => void onPasswordLogin(event), children: [
|
|
59
|
+
/* @__PURE__ */ jsxs("label", { children: [
|
|
60
|
+
/* @__PURE__ */ jsx("span", { children: "Username" }),
|
|
61
|
+
/* @__PURE__ */ jsx(
|
|
62
|
+
"input",
|
|
63
|
+
{
|
|
64
|
+
name: "username",
|
|
65
|
+
autoComplete: "username",
|
|
66
|
+
value: username,
|
|
67
|
+
onChange: (event) => setUsername(event.target.value),
|
|
68
|
+
required: true
|
|
69
|
+
}
|
|
70
|
+
)
|
|
71
|
+
] }),
|
|
72
|
+
/* @__PURE__ */ jsxs("label", { children: [
|
|
73
|
+
/* @__PURE__ */ jsx("span", { children: "Password" }),
|
|
74
|
+
/* @__PURE__ */ jsx(
|
|
75
|
+
"input",
|
|
76
|
+
{
|
|
77
|
+
name: "password",
|
|
78
|
+
type: "password",
|
|
79
|
+
autoComplete: "current-password",
|
|
80
|
+
value: password,
|
|
81
|
+
onChange: (event) => setPassword(event.target.value),
|
|
82
|
+
required: true
|
|
83
|
+
}
|
|
84
|
+
)
|
|
85
|
+
] }),
|
|
86
|
+
error ? /* @__PURE__ */ jsx("p", { className: "scms-login-form__error", children: error }) : null,
|
|
87
|
+
/* @__PURE__ */ jsx("button", { className: "scms-btn scms-btn--primary", type: "submit", disabled: busy, children: busy ? "Signing in\u2026" : authLabel })
|
|
88
|
+
] }) : /* @__PURE__ */ jsx(
|
|
89
|
+
"a",
|
|
90
|
+
{
|
|
91
|
+
className: "scms-btn scms-btn--primary",
|
|
92
|
+
href: `${loginPath}?returnTo=${encodeURIComponent("/admin")}`,
|
|
93
|
+
children: authLabel
|
|
94
|
+
}
|
|
95
|
+
) }),
|
|
96
|
+
!configured ? /* @__PURE__ */ jsxs("p", { className: "scms-admin-entry__session", children: [
|
|
97
|
+
"CMS env is not configured on this server yet. Public site still works; set",
|
|
98
|
+
" ",
|
|
99
|
+
/* @__PURE__ */ jsx("code", { children: "CONTENT_CMS_SESSION_SECRET" }),
|
|
100
|
+
" and editor password to enable editing."
|
|
101
|
+
] }) : session ? /* @__PURE__ */ jsxs("p", { className: "scms-admin-entry__session", children: [
|
|
102
|
+
"Signed in as ",
|
|
103
|
+
/* @__PURE__ */ jsx("strong", { children: session.name }),
|
|
104
|
+
session.email ? ` (${session.email})` : "",
|
|
105
|
+
publishEnabled ? " \xB7 publish enabled" : " \xB7 publish adapter not configured"
|
|
106
|
+
] }) : null
|
|
107
|
+
] }) });
|
|
108
|
+
}
|
|
109
|
+
export {
|
|
110
|
+
AdminEntry
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=admin-entry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/admin-entry.tsx"],"sourcesContent":["\"use client\";\n\nimport { useState, type FormEvent } from \"react\";\n\nimport { writeBrowserCookie } from \"./cookies\";\nimport type { ContentEditorSession } from \"./context\";\nimport type { ContentConfig } from \"./types\";\n\nexport type AdminEntryProps = {\n config: ContentConfig;\n session?: ContentEditorSession | null;\n authLabel?: string;\n /** @default \"password\" */\n authKind?: \"password\" | \"oauth\" | \"instant\";\n publishEnabled?: boolean;\n /** When false, login is disabled and a setup hint is shown. @default true */\n configured?: boolean;\n /** Site URL opened after enabling edit mode. @default \"/\" */\n siteHref?: string;\n title?: string;\n};\n\n/**\n * `/admin` landing page: login (if needed), then enter visual edit mode.\n */\nexport function AdminEntry({\n config,\n session = null,\n authLabel = \"Sign in\",\n authKind = \"password\",\n publishEnabled = false,\n configured = true,\n siteHref = \"/\",\n title = \"Content editor\",\n}: AdminEntryProps) {\n const cookieName = config.editCookie ?? \"scms_edit\";\n const loginPath = config.loginPath ?? \"/api/content/auth/login\";\n const logoutPath = config.logoutPath ?? \"/api/content/auth/logout\";\n\n const [username, setUsername] = useState(\"\");\n const [password, setPassword] = useState(\"\");\n const [busy, setBusy] = useState(false);\n const [error, setError] = useState<string | null>(null);\n\n async function onPasswordLogin(event: FormEvent) {\n event.preventDefault();\n setBusy(true);\n setError(null);\n try {\n const response = await fetch(loginPath, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({ username, password, returnTo: \"/admin\" }),\n });\n const payload = (await response.json().catch(() => null)) as\n | { ok?: boolean; error?: string }\n | null;\n if (!response.ok || !payload?.ok) {\n throw new Error(payload?.error ?? `Login failed (${response.status})`);\n }\n writeBrowserCookie(cookieName, \"1\");\n window.location.href = \"/admin\";\n } catch (err) {\n setError(err instanceof Error ? err.message : \"Login failed\");\n } finally {\n setBusy(false);\n }\n }\n\n return (\n <main className=\"scms-admin-entry\">\n <div className=\"scms-admin-entry__card\">\n <p className=\"scms-admin-entry__eyebrow\">Content CMS</p>\n <h1>{title}</h1>\n\n <div className=\"scms-admin-entry__actions\">\n {!configured ? null : session ? (\n <>\n <a\n className=\"scms-btn scms-btn--primary\"\n href={siteHref}\n onClick={() => writeBrowserCookie(cookieName, \"1\")}\n >\n Open site for editing\n </a>\n <a className=\"scms-btn scms-btn--ghost\" href={logoutPath}>\n Log out\n </a>\n </>\n ) : authKind === \"password\" ? (\n <form className=\"scms-login-form\" onSubmit={(event) => void onPasswordLogin(event)}>\n <label>\n <span>Username</span>\n <input\n name=\"username\"\n autoComplete=\"username\"\n value={username}\n onChange={(event) => setUsername(event.target.value)}\n required\n />\n </label>\n <label>\n <span>Password</span>\n <input\n name=\"password\"\n type=\"password\"\n autoComplete=\"current-password\"\n value={password}\n onChange={(event) => setPassword(event.target.value)}\n required\n />\n </label>\n {error ? <p className=\"scms-login-form__error\">{error}</p> : null}\n <button className=\"scms-btn scms-btn--primary\" type=\"submit\" disabled={busy}>\n {busy ? \"Signing in…\" : authLabel}\n </button>\n </form>\n ) : (\n <a\n className=\"scms-btn scms-btn--primary\"\n href={`${loginPath}?returnTo=${encodeURIComponent(\"/admin\")}`}\n >\n {authLabel}\n </a>\n )}\n </div>\n\n {!configured ? (\n <p className=\"scms-admin-entry__session\">\n CMS env is not configured on this server yet. Public site still works; set{\" \"}\n <code>CONTENT_CMS_SESSION_SECRET</code> and editor password to enable editing.\n </p>\n ) : session ? (\n <p className=\"scms-admin-entry__session\">\n Signed in as <strong>{session.name}</strong>\n {session.email ? ` (${session.email})` : \"\"}\n {publishEnabled ? \" · publish enabled\" : \" · publish adapter not configured\"}\n </p>\n ) : null}\n </div>\n </main>\n );\n}\n"],"mappings":"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","names":[]}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ContentConfig, ContentDocuments, TextFieldType } from './types.js';
|
|
2
|
+
import * as react from 'react';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
|
|
5
|
+
type ContentEditorSession = {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string;
|
|
8
|
+
email: string;
|
|
9
|
+
username?: string;
|
|
10
|
+
avatarUrl?: string;
|
|
11
|
+
provider: string;
|
|
12
|
+
};
|
|
13
|
+
type ContentCmsContextValue = {
|
|
14
|
+
config: ContentConfig;
|
|
15
|
+
editMode: boolean;
|
|
16
|
+
session: ContentEditorSession | null;
|
|
17
|
+
publishEnabled: boolean;
|
|
18
|
+
documents: ContentDocuments;
|
|
19
|
+
dirtyCount: number;
|
|
20
|
+
saving: boolean;
|
|
21
|
+
publishing: boolean;
|
|
22
|
+
lastError: string | null;
|
|
23
|
+
lastPublishSha: string | null;
|
|
24
|
+
getFieldValue: (document: string, field: string) => string;
|
|
25
|
+
setFieldValue: (document: string, field: string, value: string) => void;
|
|
26
|
+
saveDrafts: () => Promise<boolean>;
|
|
27
|
+
publish: (message?: string) => Promise<boolean>;
|
|
28
|
+
discardDrafts: () => void;
|
|
29
|
+
};
|
|
30
|
+
type ContentCmsProviderProps = {
|
|
31
|
+
config: ContentConfig;
|
|
32
|
+
documents: ContentDocuments;
|
|
33
|
+
editMode?: boolean;
|
|
34
|
+
session?: ContentEditorSession | null;
|
|
35
|
+
publishEnabled?: boolean;
|
|
36
|
+
children: ReactNode;
|
|
37
|
+
};
|
|
38
|
+
declare function ContentCmsProvider({ config, documents: initialDocuments, editMode, session, publishEnabled, children, }: ContentCmsProviderProps): react.JSX.Element;
|
|
39
|
+
declare function useContentCms(): ContentCmsContextValue;
|
|
40
|
+
declare function useContentCmsOptional(): ContentCmsContextValue | null;
|
|
41
|
+
declare function useEditableField(document: string, field: string): {
|
|
42
|
+
enabled: boolean;
|
|
43
|
+
editMode: boolean;
|
|
44
|
+
label: string;
|
|
45
|
+
type: TextFieldType;
|
|
46
|
+
value: string;
|
|
47
|
+
setValue: (next: string) => void | undefined;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export { ContentCmsProvider, type ContentCmsProviderProps, type ContentEditorSession, useContentCms, useContentCmsOptional, useEditableField };
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useCallback,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef,
|
|
10
|
+
useState
|
|
11
|
+
} from "react";
|
|
12
|
+
import { DEFAULT_EDIT_COOKIE } from "./cookies";
|
|
13
|
+
import { getDocument } from "./define";
|
|
14
|
+
import { asString, getPathValue, setPathValue } from "./path";
|
|
15
|
+
const ContentCmsContext = createContext(null);
|
|
16
|
+
function ContentCmsProvider({
|
|
17
|
+
config,
|
|
18
|
+
documents: initialDocuments,
|
|
19
|
+
editMode = false,
|
|
20
|
+
session = null,
|
|
21
|
+
publishEnabled = false,
|
|
22
|
+
children
|
|
23
|
+
}) {
|
|
24
|
+
const [documents, setDocuments] = useState(initialDocuments);
|
|
25
|
+
const [baseline, setBaseline] = useState(initialDocuments);
|
|
26
|
+
const [saving, setSaving] = useState(false);
|
|
27
|
+
const [publishing, setPublishing] = useState(false);
|
|
28
|
+
const [lastError, setLastError] = useState(null);
|
|
29
|
+
const [lastPublishSha, setLastPublishSha] = useState(null);
|
|
30
|
+
const prevInitialJson = useRef(JSON.stringify(initialDocuments));
|
|
31
|
+
const publishingRef = useRef(false);
|
|
32
|
+
const publishPath = config.publishPath ?? "/api/content/publish";
|
|
33
|
+
const dirtyCount = useMemo(() => {
|
|
34
|
+
let count = 0;
|
|
35
|
+
for (const document of config.documents) {
|
|
36
|
+
for (const field of Object.keys(document.fields)) {
|
|
37
|
+
const current = asString(getPathValue(documents[document.name], field));
|
|
38
|
+
const original = asString(getPathValue(baseline[document.name], field));
|
|
39
|
+
if (current !== original) count += 1;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return count;
|
|
43
|
+
}, [baseline, config.documents, documents]);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
const nextJson = JSON.stringify(initialDocuments);
|
|
46
|
+
if (nextJson === prevInitialJson.current) return;
|
|
47
|
+
prevInitialJson.current = nextJson;
|
|
48
|
+
if (dirtyCount > 0) return;
|
|
49
|
+
setDocuments(initialDocuments);
|
|
50
|
+
setBaseline(initialDocuments);
|
|
51
|
+
}, [initialDocuments, dirtyCount]);
|
|
52
|
+
const fetchPublishStatus = useCallback(async () => {
|
|
53
|
+
try {
|
|
54
|
+
const response = await fetch(publishPath, { method: "GET", cache: "no-store" });
|
|
55
|
+
const payload = await response.json().catch(() => null);
|
|
56
|
+
if (!response.ok) return payload;
|
|
57
|
+
return payload;
|
|
58
|
+
} catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}, [publishPath]);
|
|
62
|
+
const syncPublishingFromStatus = useCallback(async () => {
|
|
63
|
+
const status = await fetchPublishStatus();
|
|
64
|
+
if (!status) return;
|
|
65
|
+
const running = status.state === "running";
|
|
66
|
+
publishingRef.current = running;
|
|
67
|
+
setPublishing(running);
|
|
68
|
+
if (status.state === "failed" && status.message) {
|
|
69
|
+
setLastError(status.message);
|
|
70
|
+
}
|
|
71
|
+
}, [fetchPublishStatus]);
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
if (!editMode || !publishEnabled) return;
|
|
74
|
+
void syncPublishingFromStatus();
|
|
75
|
+
const timer = window.setInterval(() => {
|
|
76
|
+
void syncPublishingFromStatus();
|
|
77
|
+
}, 2e3);
|
|
78
|
+
return () => window.clearInterval(timer);
|
|
79
|
+
}, [editMode, publishEnabled, syncPublishingFromStatus]);
|
|
80
|
+
const getFieldValue = useCallback(
|
|
81
|
+
(document, field) => asString(getPathValue(documents[document], field)),
|
|
82
|
+
[documents]
|
|
83
|
+
);
|
|
84
|
+
const setFieldValue = useCallback((document, field, value2) => {
|
|
85
|
+
setDocuments((prev) => {
|
|
86
|
+
const current = prev[document] ?? {};
|
|
87
|
+
return {
|
|
88
|
+
...prev,
|
|
89
|
+
[document]: setPathValue(current, field, value2)
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
setLastError(null);
|
|
93
|
+
}, []);
|
|
94
|
+
const discardDrafts = useCallback(() => {
|
|
95
|
+
setDocuments(baseline);
|
|
96
|
+
setLastError(null);
|
|
97
|
+
}, [baseline]);
|
|
98
|
+
const saveDrafts = useCallback(async () => {
|
|
99
|
+
const patches = [];
|
|
100
|
+
for (const document of config.documents) {
|
|
101
|
+
for (const field of Object.keys(document.fields)) {
|
|
102
|
+
const current = asString(getPathValue(documents[document.name], field));
|
|
103
|
+
const original = asString(getPathValue(baseline[document.name], field));
|
|
104
|
+
if (current !== original) {
|
|
105
|
+
patches.push({ document: document.name, field, value: current });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (patches.length === 0) return true;
|
|
110
|
+
setSaving(true);
|
|
111
|
+
setLastError(null);
|
|
112
|
+
try {
|
|
113
|
+
const response = await fetch(config.savePath ?? "/api/content/save", {
|
|
114
|
+
method: "POST",
|
|
115
|
+
headers: { "Content-Type": "application/json" },
|
|
116
|
+
body: JSON.stringify({ patches })
|
|
117
|
+
});
|
|
118
|
+
const payload = await response.json().catch(() => null);
|
|
119
|
+
if (!response.ok || !payload?.ok) {
|
|
120
|
+
throw new Error(payload?.error ?? `Save failed (${response.status})`);
|
|
121
|
+
}
|
|
122
|
+
setBaseline(documents);
|
|
123
|
+
return true;
|
|
124
|
+
} catch (error) {
|
|
125
|
+
const message = error instanceof Error ? error.message : "Save failed";
|
|
126
|
+
setLastError(message);
|
|
127
|
+
return false;
|
|
128
|
+
} finally {
|
|
129
|
+
setSaving(false);
|
|
130
|
+
}
|
|
131
|
+
}, [baseline, config.documents, config.savePath, documents]);
|
|
132
|
+
const publish = useCallback(
|
|
133
|
+
async (message) => {
|
|
134
|
+
if (publishingRef.current) {
|
|
135
|
+
setLastError("Publish already in progress");
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
publishingRef.current = true;
|
|
139
|
+
setPublishing(true);
|
|
140
|
+
setLastError(null);
|
|
141
|
+
try {
|
|
142
|
+
if (dirtyCount > 0) {
|
|
143
|
+
const saved = await saveDrafts();
|
|
144
|
+
if (!saved) {
|
|
145
|
+
publishingRef.current = false;
|
|
146
|
+
setPublishing(false);
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const response = await fetch(publishPath, {
|
|
151
|
+
method: "POST",
|
|
152
|
+
headers: { "Content-Type": "application/json" },
|
|
153
|
+
body: JSON.stringify({ message })
|
|
154
|
+
});
|
|
155
|
+
const payload = await response.json().catch(() => null);
|
|
156
|
+
if (response.status === 409 || payload?.state === "running") {
|
|
157
|
+
setLastError(payload?.error ?? "Publish already in progress");
|
|
158
|
+
publishingRef.current = true;
|
|
159
|
+
setPublishing(true);
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
if (!response.ok || !payload?.ok || !payload.commitSha) {
|
|
163
|
+
throw new Error(payload?.error ?? `Publish failed (${response.status})`);
|
|
164
|
+
}
|
|
165
|
+
setLastPublishSha(payload.commitSha);
|
|
166
|
+
if (payload.pending) {
|
|
167
|
+
publishingRef.current = true;
|
|
168
|
+
setPublishing(true);
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
publishingRef.current = false;
|
|
172
|
+
setPublishing(false);
|
|
173
|
+
return true;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
const text = error instanceof Error ? error.message : "Publish failed";
|
|
176
|
+
setLastError(text);
|
|
177
|
+
publishingRef.current = false;
|
|
178
|
+
setPublishing(false);
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
[dirtyCount, publishPath, saveDrafts]
|
|
183
|
+
);
|
|
184
|
+
const value = useMemo(
|
|
185
|
+
() => ({
|
|
186
|
+
config: {
|
|
187
|
+
...config,
|
|
188
|
+
editCookie: config.editCookie ?? DEFAULT_EDIT_COOKIE,
|
|
189
|
+
savePath: config.savePath ?? "/api/content/save",
|
|
190
|
+
publishPath,
|
|
191
|
+
loginPath: config.loginPath ?? "/api/content/auth/login",
|
|
192
|
+
logoutPath: config.logoutPath ?? "/api/content/auth/logout",
|
|
193
|
+
sessionPath: config.sessionPath ?? "/api/content/auth/session"
|
|
194
|
+
},
|
|
195
|
+
editMode,
|
|
196
|
+
session,
|
|
197
|
+
publishEnabled,
|
|
198
|
+
documents,
|
|
199
|
+
dirtyCount,
|
|
200
|
+
saving,
|
|
201
|
+
publishing,
|
|
202
|
+
lastError,
|
|
203
|
+
lastPublishSha,
|
|
204
|
+
getFieldValue,
|
|
205
|
+
setFieldValue,
|
|
206
|
+
saveDrafts,
|
|
207
|
+
publish,
|
|
208
|
+
discardDrafts
|
|
209
|
+
}),
|
|
210
|
+
[
|
|
211
|
+
config,
|
|
212
|
+
discardDrafts,
|
|
213
|
+
dirtyCount,
|
|
214
|
+
documents,
|
|
215
|
+
editMode,
|
|
216
|
+
getFieldValue,
|
|
217
|
+
lastError,
|
|
218
|
+
lastPublishSha,
|
|
219
|
+
publish,
|
|
220
|
+
publishEnabled,
|
|
221
|
+
publishPath,
|
|
222
|
+
saveDrafts,
|
|
223
|
+
saving,
|
|
224
|
+
publishing,
|
|
225
|
+
session,
|
|
226
|
+
setFieldValue
|
|
227
|
+
]
|
|
228
|
+
);
|
|
229
|
+
return /* @__PURE__ */ jsx(ContentCmsContext.Provider, { value, children });
|
|
230
|
+
}
|
|
231
|
+
function useContentCms() {
|
|
232
|
+
const value = useContext(ContentCmsContext);
|
|
233
|
+
if (!value) {
|
|
234
|
+
throw new Error("useContentCms must be used within ContentCmsProvider");
|
|
235
|
+
}
|
|
236
|
+
return value;
|
|
237
|
+
}
|
|
238
|
+
function useContentCmsOptional() {
|
|
239
|
+
return useContext(ContentCmsContext);
|
|
240
|
+
}
|
|
241
|
+
function useEditableField(document, field) {
|
|
242
|
+
const cms = useContentCmsOptional();
|
|
243
|
+
const def = cms ? getDocument(cms.config, document)?.fields[field] : void 0;
|
|
244
|
+
const value = cms ? cms.getFieldValue(document, field) : "";
|
|
245
|
+
return {
|
|
246
|
+
enabled: Boolean(cms?.editMode && def),
|
|
247
|
+
editMode: Boolean(cms?.editMode),
|
|
248
|
+
label: def?.label ?? field,
|
|
249
|
+
type: def?.type ?? "text",
|
|
250
|
+
value,
|
|
251
|
+
setValue: (next) => cms?.setFieldValue(document, field, next)
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
export {
|
|
255
|
+
ContentCmsProvider,
|
|
256
|
+
useContentCms,
|
|
257
|
+
useContentCmsOptional,
|
|
258
|
+
useEditableField
|
|
259
|
+
};
|
|
260
|
+
//# sourceMappingURL=context.js.map
|