@kurumera/editable 0.3.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/dist/chrome.d.ts +6 -0
- package/dist/chrome.js +10 -0
- package/dist/client.d.ts +19 -0
- package/dist/client.js +16 -0
- package/dist/context.d.ts +34 -0
- package/dist/context.js +63 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +33 -0
- package/dist/primitives/EditableBackgroundImage.d.ts +16 -0
- package/dist/primitives/EditableBackgroundImage.js +19 -0
- package/dist/primitives/EditableBackgroundImageClient.d.ts +9 -0
- package/dist/primitives/EditableBackgroundImageClient.js +32 -0
- package/dist/primitives/EditableButton.d.ts +13 -0
- package/dist/primitives/EditableButton.js +23 -0
- package/dist/primitives/EditableImage.d.ts +16 -0
- package/dist/primitives/EditableImage.js +21 -0
- package/dist/primitives/EditableImageClient.d.ts +10 -0
- package/dist/primitives/EditableImageClient.js +34 -0
- package/dist/primitives/EditableLink.d.ts +12 -0
- package/dist/primitives/EditableLink.js +18 -0
- package/dist/primitives/EditableRepeater.d.ts +34 -0
- package/dist/primitives/EditableRepeater.js +32 -0
- package/dist/primitives/EditableRepeaterClient.d.ts +24 -0
- package/dist/primitives/EditableRepeaterClient.js +42 -0
- package/dist/primitives/EditableRichText.d.ts +10 -0
- package/dist/primitives/EditableRichText.js +11 -0
- package/dist/primitives/EditableRichTextClient.d.ts +8 -0
- package/dist/primitives/EditableRichTextClient.js +36 -0
- package/dist/primitives/EditableSection.d.ts +18 -0
- package/dist/primitives/EditableSection.js +22 -0
- package/dist/primitives/EditableSectionClient.d.ts +10 -0
- package/dist/primitives/EditableSectionClient.js +12 -0
- package/dist/primitives/EditableText.d.ts +18 -0
- package/dist/primitives/EditableText.js +17 -0
- package/dist/primitives/EditableTextClient.d.ts +9 -0
- package/dist/primitives/EditableTextClient.js +15 -0
- package/dist/primitives/EditableVideo.d.ts +20 -0
- package/dist/primitives/EditableVideo.js +32 -0
- package/dist/primitives/EditableVideoClient.d.ts +11 -0
- package/dist/primitives/EditableVideoClient.js +48 -0
- package/dist/primitives/LinkFieldClient.d.ts +17 -0
- package/dist/primitives/LinkFieldClient.js +29 -0
- package/dist/primitives/videoField.d.ts +19 -0
- package/dist/primitives/videoField.js +15 -0
- package/dist/save-queue.d.ts +27 -0
- package/dist/save-queue.js +82 -0
- package/dist/server/resolve.d.ts +21 -0
- package/dist/server/resolve.js +47 -0
- package/dist/server.d.ts +11 -0
- package/dist/server.js +11 -0
- package/dist/store.d.ts +26 -0
- package/dist/store.js +50 -0
- package/dist/types.d.ts +27 -0
- package/dist/types.js +1 -0
- package/dist/useEditableField.d.ts +28 -0
- package/dist/useEditableField.js +38 -0
- package/package.json +33 -0
package/dist/chrome.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { FieldStatus } from "./types.js";
|
|
2
|
+
/** Shared hover-outline + status class names every client leaf applies —
|
|
3
|
+
* centralized so the visual language (and any future fix to it) is one
|
|
4
|
+
* change, not nine. Consuming themes can target `.kurumera-editable` /
|
|
5
|
+
* `.kurumera-editable--<status>` in their own CSS to restyle. */
|
|
6
|
+
export declare function editableClassName(className: string | undefined, status: FieldStatus): string;
|
package/dist/chrome.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Shared hover-outline + status class names every client leaf applies —
|
|
2
|
+
* centralized so the visual language (and any future fix to it) is one
|
|
3
|
+
* change, not nine. Consuming themes can target `.kurumera-editable` /
|
|
4
|
+
* `.kurumera-editable--<status>` in their own CSS to restyle. */
|
|
5
|
+
export function editableClassName(className, status) {
|
|
6
|
+
const parts = ["kurumera-editable", `kurumera-editable--${status}`];
|
|
7
|
+
if (className)
|
|
8
|
+
parts.push(className);
|
|
9
|
+
return parts.join(" ");
|
|
10
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kurumera/editable/client — the client-safe engine: the hook, the
|
|
3
|
+
* provider, and their types.
|
|
4
|
+
*
|
|
5
|
+
* Import from HERE — not the bare `.` entry — inside your OWN "use client"
|
|
6
|
+
* component that needs `useEditableField()` or `EditableProvider`. The bare
|
|
7
|
+
* `.` entry re-exports the 9 Server Component primitives, which
|
|
8
|
+
* transitively import `next/headers` (via `resolveEditableContent`); a
|
|
9
|
+
* "use client" file that reaches even ONE of them — regardless of which
|
|
10
|
+
* named export it actually uses — fails the Next.js build ("You're
|
|
11
|
+
* importing a component that needs next/headers"). The package's own
|
|
12
|
+
* primitives already follow this rule internally: their client leaves
|
|
13
|
+
* import the hook via a relative path, never through either public entry.
|
|
14
|
+
*/
|
|
15
|
+
export { EditableProvider, useEditableContext } from "./context.js";
|
|
16
|
+
export type { EditableProviderProps, EditableContextValue } from "./context.js";
|
|
17
|
+
export { useEditableField } from "./useEditableField.js";
|
|
18
|
+
export type { UseEditableFieldOptions, UseEditableFieldResult } from "./useEditableField.js";
|
|
19
|
+
export type { EditMode, FieldStatus, FieldRecord, EditableResolution } from "./types.js";
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kurumera/editable/client — the client-safe engine: the hook, the
|
|
3
|
+
* provider, and their types.
|
|
4
|
+
*
|
|
5
|
+
* Import from HERE — not the bare `.` entry — inside your OWN "use client"
|
|
6
|
+
* component that needs `useEditableField()` or `EditableProvider`. The bare
|
|
7
|
+
* `.` entry re-exports the 9 Server Component primitives, which
|
|
8
|
+
* transitively import `next/headers` (via `resolveEditableContent`); a
|
|
9
|
+
* "use client" file that reaches even ONE of them — regardless of which
|
|
10
|
+
* named export it actually uses — fails the Next.js build ("You're
|
|
11
|
+
* importing a component that needs next/headers"). The package's own
|
|
12
|
+
* primitives already follow this rule internally: their client leaves
|
|
13
|
+
* import the hook via a relative path, never through either public entry.
|
|
14
|
+
*/
|
|
15
|
+
export { EditableProvider, useEditableContext } from "./context.js";
|
|
16
|
+
export { useEditableField } from "./useEditableField.js";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import { type EditableMediaUpload } from "@kurumera/storefront";
|
|
3
|
+
import { EditableStore } from "./store.js";
|
|
4
|
+
import { type SaveQueue } from "./save-queue.js";
|
|
5
|
+
import type { EditMode } from "./types.js";
|
|
6
|
+
export interface EditableProviderProps {
|
|
7
|
+
mode: EditMode;
|
|
8
|
+
editToken: string | null;
|
|
9
|
+
tenant: string;
|
|
10
|
+
apiUrl?: string;
|
|
11
|
+
fields: Record<string, {
|
|
12
|
+
value: unknown;
|
|
13
|
+
type?: string;
|
|
14
|
+
}>;
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
}
|
|
17
|
+
export interface EditableContextValue {
|
|
18
|
+
mode: EditMode;
|
|
19
|
+
store: EditableStore;
|
|
20
|
+
/** null when not in edit mode — nothing should be saving. */
|
|
21
|
+
saveQueue: SaveQueue | null;
|
|
22
|
+
uploadMedia: (file: Blob, filename?: string, opts?: {
|
|
23
|
+
contentType?: "IMAGE" | "VIDEO";
|
|
24
|
+
}) => Promise<EditableMediaUpload>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Mounted ONCE, at the theme's root layout, seeded from the same
|
|
28
|
+
* request-scoped `resolveEditableContent()` fetch every `Editable*` Server
|
|
29
|
+
* Component also reads independently (see server/resolve.ts) — this is the
|
|
30
|
+
* client-tier half of that two-tier data flow (Server Components can't
|
|
31
|
+
* `useContext`, so they read the server tier directly instead).
|
|
32
|
+
*/
|
|
33
|
+
export declare function EditableProvider({ mode, editToken, tenant, apiUrl, fields, children }: EditableProviderProps): import("react").JSX.Element;
|
|
34
|
+
export declare function useEditableContext(): EditableContextValue;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useContext, useMemo, useRef } from "react";
|
|
4
|
+
import { createKurumeraClient } from "@kurumera/storefront";
|
|
5
|
+
import { EditableStore } from "./store.js";
|
|
6
|
+
import { createSaveQueue } from "./save-queue.js";
|
|
7
|
+
const EditableContext = createContext(null);
|
|
8
|
+
/**
|
|
9
|
+
* Mounted ONCE, at the theme's root layout, seeded from the same
|
|
10
|
+
* request-scoped `resolveEditableContent()` fetch every `Editable*` Server
|
|
11
|
+
* Component also reads independently (see server/resolve.ts) — this is the
|
|
12
|
+
* client-tier half of that two-tier data flow (Server Components can't
|
|
13
|
+
* `useContext`, so they read the server tier directly instead).
|
|
14
|
+
*/
|
|
15
|
+
export function EditableProvider({ mode, editToken, tenant, apiUrl, fields, children }) {
|
|
16
|
+
const storeRef = useRef(null);
|
|
17
|
+
if (!storeRef.current)
|
|
18
|
+
storeRef.current = new EditableStore(fields);
|
|
19
|
+
const value = useMemo(() => {
|
|
20
|
+
const store = storeRef.current;
|
|
21
|
+
if (mode !== "edit" || !editToken) {
|
|
22
|
+
return {
|
|
23
|
+
mode,
|
|
24
|
+
store,
|
|
25
|
+
saveQueue: null,
|
|
26
|
+
uploadMedia: async () => {
|
|
27
|
+
throw new Error("Not in edit mode — media upload is unavailable.");
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const client = createKurumeraClient({ tenant, apiUrl, editSessionToken: editToken });
|
|
32
|
+
const saveQueue = createSaveQueue({
|
|
33
|
+
store,
|
|
34
|
+
save: async (fieldKey, val, type) => {
|
|
35
|
+
await client.content.setDraft(fieldKey, val, { type });
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
mode,
|
|
40
|
+
store,
|
|
41
|
+
saveQueue,
|
|
42
|
+
uploadMedia: (file, filename, opts) => client.content.uploadMedia(file, filename, opts),
|
|
43
|
+
};
|
|
44
|
+
// storeRef.current is stable for the component's lifetime — intentionally omitted.
|
|
45
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
46
|
+
}, [mode, editToken, tenant, apiUrl]);
|
|
47
|
+
return _jsx(EditableContext.Provider, { value: value, children: children });
|
|
48
|
+
}
|
|
49
|
+
const FALLBACK_STORE = new EditableStore({});
|
|
50
|
+
/** Used outside an `<EditableProvider>` (e.g. the package isn't wired into
|
|
51
|
+
* this theme yet) — degrades to a safe, permanently-"off" context rather
|
|
52
|
+
* than crashing the page. */
|
|
53
|
+
const FALLBACK_CONTEXT = {
|
|
54
|
+
mode: "off",
|
|
55
|
+
store: FALLBACK_STORE,
|
|
56
|
+
saveQueue: null,
|
|
57
|
+
uploadMedia: async () => {
|
|
58
|
+
throw new Error("EditableProvider is not mounted in this theme.");
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
export function useEditableContext() {
|
|
62
|
+
return useContext(EditableContext) ?? FALLBACK_CONTEXT;
|
|
63
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kurumera/editable — inline, draft/publish, no-rebuild content editing for
|
|
3
|
+
* Kurumera code themes.
|
|
4
|
+
*
|
|
5
|
+
* import { EditableText, EditableImage, EditableRepeater } from "@kurumera/editable";
|
|
6
|
+
*
|
|
7
|
+
* <EditableText field="home.hero.heading" defaultValue="Welcome" as="h1" />
|
|
8
|
+
*
|
|
9
|
+
* A field is only ever merchant-editable if it's wrapped in one of these
|
|
10
|
+
* components (or `useEditableField()`) — that's the whole safety boundary:
|
|
11
|
+
* nothing here lets a merchant reach arbitrary layout or code, only content
|
|
12
|
+
* a theme developer explicitly marked.
|
|
13
|
+
*
|
|
14
|
+
* Every primitive here is a Server Component, for use in your theme's
|
|
15
|
+
* page/section files. Outside the dashboard's edit mode (i.e. for every
|
|
16
|
+
* real shopper) each renders plain output with ZERO extra client JS — the
|
|
17
|
+
* interactive client leaf is never even included in that page's bundle,
|
|
18
|
+
* not merely hidden.
|
|
19
|
+
*
|
|
20
|
+
* Need `useEditableField()` or `EditableProvider` inside your OWN "use
|
|
21
|
+
* client" component? Import those from `@kurumera/editable/client`
|
|
22
|
+
* instead — NOT from here (see that entry's docstring for why). The root
|
|
23
|
+
* layout that mounts `<EditableProvider>` uses that subpath too.
|
|
24
|
+
*/
|
|
25
|
+
export { EditableText } from "./primitives/EditableText.js";
|
|
26
|
+
export type { EditableTextProps } from "./primitives/EditableText.js";
|
|
27
|
+
export { EditableRichText } from "./primitives/EditableRichText.js";
|
|
28
|
+
export type { EditableRichTextProps } from "./primitives/EditableRichText.js";
|
|
29
|
+
export { EditableImage } from "./primitives/EditableImage.js";
|
|
30
|
+
export type { EditableImageProps, EditableImageValue } from "./primitives/EditableImage.js";
|
|
31
|
+
export { EditableSection } from "./primitives/EditableSection.js";
|
|
32
|
+
export type { EditableSectionProps } from "./primitives/EditableSection.js";
|
|
33
|
+
export { EditableRepeater } from "./primitives/EditableRepeater.js";
|
|
34
|
+
export type { EditableRepeaterProps, RepeaterItem } from "./primitives/EditableRepeater.js";
|
|
35
|
+
export { EditableButton } from "./primitives/EditableButton.js";
|
|
36
|
+
export type { EditableButtonProps } from "./primitives/EditableButton.js";
|
|
37
|
+
export { EditableLink } from "./primitives/EditableLink.js";
|
|
38
|
+
export type { EditableLinkProps } from "./primitives/EditableLink.js";
|
|
39
|
+
export type { LinkFieldValue } from "./primitives/LinkFieldClient.js";
|
|
40
|
+
export { EditableVideo } from "./primitives/EditableVideo.js";
|
|
41
|
+
export type { EditableVideoProps } from "./primitives/EditableVideo.js";
|
|
42
|
+
export type { EditableVideoValue, VideoProvider } from "./primitives/videoField.js";
|
|
43
|
+
export { EditableBackgroundImage } from "./primitives/EditableBackgroundImage.js";
|
|
44
|
+
export type { EditableBackgroundImageProps } from "./primitives/EditableBackgroundImage.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kurumera/editable — inline, draft/publish, no-rebuild content editing for
|
|
3
|
+
* Kurumera code themes.
|
|
4
|
+
*
|
|
5
|
+
* import { EditableText, EditableImage, EditableRepeater } from "@kurumera/editable";
|
|
6
|
+
*
|
|
7
|
+
* <EditableText field="home.hero.heading" defaultValue="Welcome" as="h1" />
|
|
8
|
+
*
|
|
9
|
+
* A field is only ever merchant-editable if it's wrapped in one of these
|
|
10
|
+
* components (or `useEditableField()`) — that's the whole safety boundary:
|
|
11
|
+
* nothing here lets a merchant reach arbitrary layout or code, only content
|
|
12
|
+
* a theme developer explicitly marked.
|
|
13
|
+
*
|
|
14
|
+
* Every primitive here is a Server Component, for use in your theme's
|
|
15
|
+
* page/section files. Outside the dashboard's edit mode (i.e. for every
|
|
16
|
+
* real shopper) each renders plain output with ZERO extra client JS — the
|
|
17
|
+
* interactive client leaf is never even included in that page's bundle,
|
|
18
|
+
* not merely hidden.
|
|
19
|
+
*
|
|
20
|
+
* Need `useEditableField()` or `EditableProvider` inside your OWN "use
|
|
21
|
+
* client" component? Import those from `@kurumera/editable/client`
|
|
22
|
+
* instead — NOT from here (see that entry's docstring for why). The root
|
|
23
|
+
* layout that mounts `<EditableProvider>` uses that subpath too.
|
|
24
|
+
*/
|
|
25
|
+
export { EditableText } from "./primitives/EditableText.js";
|
|
26
|
+
export { EditableRichText } from "./primitives/EditableRichText.js";
|
|
27
|
+
export { EditableImage } from "./primitives/EditableImage.js";
|
|
28
|
+
export { EditableSection } from "./primitives/EditableSection.js";
|
|
29
|
+
export { EditableRepeater } from "./primitives/EditableRepeater.js";
|
|
30
|
+
export { EditableButton } from "./primitives/EditableButton.js";
|
|
31
|
+
export { EditableLink } from "./primitives/EditableLink.js";
|
|
32
|
+
export { EditableVideo } from "./primitives/EditableVideo.js";
|
|
33
|
+
export { EditableBackgroundImage } from "./primitives/EditableBackgroundImage.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
export interface EditableBackgroundImageProps {
|
|
3
|
+
field: string;
|
|
4
|
+
defaultSrc: string;
|
|
5
|
+
as?: string;
|
|
6
|
+
className?: string;
|
|
7
|
+
children?: ReactNode;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Unlike EditableImage, this component IS the container the theme dev
|
|
11
|
+
* would otherwise have written by hand (`<div className="hero"
|
|
12
|
+
* style={{backgroundImage}}>...</div>`) — not an extra wrapper around it.
|
|
13
|
+
* `children` render exactly as given, live or edit; only the background
|
|
14
|
+
* image itself is ever merchant-editable.
|
|
15
|
+
*/
|
|
16
|
+
export declare function EditableBackgroundImage({ field, defaultSrc, as, className, children }: EditableBackgroundImageProps): Promise<import("react").JSX.Element>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { resolveEditableContent } from "../server/resolve.js";
|
|
3
|
+
import { EditableBackgroundImageClient } from "./EditableBackgroundImageClient.js";
|
|
4
|
+
/**
|
|
5
|
+
* Unlike EditableImage, this component IS the container the theme dev
|
|
6
|
+
* would otherwise have written by hand (`<div className="hero"
|
|
7
|
+
* style={{backgroundImage}}>...</div>`) — not an extra wrapper around it.
|
|
8
|
+
* `children` render exactly as given, live or edit; only the background
|
|
9
|
+
* image itself is ever merchant-editable.
|
|
10
|
+
*/
|
|
11
|
+
export async function EditableBackgroundImage({ field, defaultSrc, as = "div", className, children }) {
|
|
12
|
+
const { mode, fields } = await resolveEditableContent();
|
|
13
|
+
const src = fields[field]?.value || defaultSrc;
|
|
14
|
+
if (mode !== "edit") {
|
|
15
|
+
const As = as;
|
|
16
|
+
return (_jsx(As, { className: className, style: { backgroundImage: `url(${src})` }, children: children }));
|
|
17
|
+
}
|
|
18
|
+
return (_jsx(EditableBackgroundImageClient, { field: field, defaultSrc: defaultSrc, as: as, className: className, children: children }));
|
|
19
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface EditableBackgroundImageClientProps {
|
|
3
|
+
field: string;
|
|
4
|
+
defaultSrc: string;
|
|
5
|
+
as: string;
|
|
6
|
+
className?: string;
|
|
7
|
+
children?: ReactNode;
|
|
8
|
+
}
|
|
9
|
+
export declare function EditableBackgroundImageClient({ field, defaultSrc, as, className, children }: EditableBackgroundImageClientProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useRef, useState } from "react";
|
|
4
|
+
import { useEditableField } from "../useEditableField.js";
|
|
5
|
+
import { useEditableContext } from "../context.js";
|
|
6
|
+
import { editableClassName } from "../chrome.js";
|
|
7
|
+
export function EditableBackgroundImageClient({ field, defaultSrc, as, className, children }) {
|
|
8
|
+
const { value, setValue, status, retry } = useEditableField(field, defaultSrc, { type: "image" });
|
|
9
|
+
const { uploadMedia } = useEditableContext();
|
|
10
|
+
const inputRef = useRef(null);
|
|
11
|
+
const [uploading, setUploading] = useState(false);
|
|
12
|
+
const As = as;
|
|
13
|
+
async function handleFile(e) {
|
|
14
|
+
const file = e.target.files?.[0];
|
|
15
|
+
e.target.value = "";
|
|
16
|
+
if (!file)
|
|
17
|
+
return;
|
|
18
|
+
setUploading(true);
|
|
19
|
+
try {
|
|
20
|
+
const uploaded = await uploadMedia(file, file.name);
|
|
21
|
+
if (uploaded.url)
|
|
22
|
+
setValue(uploaded.url);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// nothing was set — the merchant can just try again
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
setUploading(false);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return (_jsxs(As, { className: editableClassName(className, status), style: { backgroundImage: `url(${value})`, position: "relative" }, "data-kurumera-field": field, title: status === "error" ? "Couldn't save — click to retry" : undefined, children: [children, _jsx("button", { type: "button", className: "kurumera-editable-bg__change", contentEditable: false, onClick: () => (status === "error" ? retry() : inputRef.current?.click()), disabled: uploading, children: uploading ? "Uploading…" : status === "error" ? "Retry save" : "Change background" }), _jsx("input", { ref: inputRef, type: "file", accept: "image/*", hidden: true, onChange: handleFile })] }));
|
|
32
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface EditableButtonProps {
|
|
2
|
+
/** Field stores `{ label, href }`. */
|
|
3
|
+
field: string;
|
|
4
|
+
defaultLabel: string;
|
|
5
|
+
defaultHref: string;
|
|
6
|
+
className?: string;
|
|
7
|
+
}
|
|
8
|
+
/** A styled CTA — same `{label, href}` shape as the hero CTAs already
|
|
9
|
+
* hand-written in the base-theme template (see app/page.tsx's
|
|
10
|
+
* hero.primaryCta). Reach for `EditableLink` instead for an inline text
|
|
11
|
+
* link where a "new tab" toggle (or other link-specific behavior) makes
|
|
12
|
+
* more sense than button styling. */
|
|
13
|
+
export declare function EditableButton({ field, defaultLabel, defaultHref, className }: EditableButtonProps): Promise<import("react").JSX.Element>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { resolveEditableContent } from "../server/resolve.js";
|
|
3
|
+
import { LinkFieldClient } from "./LinkFieldClient.js";
|
|
4
|
+
function resolveValue(raw, defaultLabel, defaultHref) {
|
|
5
|
+
if (raw && typeof raw === "object") {
|
|
6
|
+
const v = raw;
|
|
7
|
+
return { label: v.label || defaultLabel, href: v.href || defaultHref };
|
|
8
|
+
}
|
|
9
|
+
return { label: defaultLabel, href: defaultHref };
|
|
10
|
+
}
|
|
11
|
+
/** A styled CTA — same `{label, href}` shape as the hero CTAs already
|
|
12
|
+
* hand-written in the base-theme template (see app/page.tsx's
|
|
13
|
+
* hero.primaryCta). Reach for `EditableLink` instead for an inline text
|
|
14
|
+
* link where a "new tab" toggle (or other link-specific behavior) makes
|
|
15
|
+
* more sense than button styling. */
|
|
16
|
+
export async function EditableButton({ field, defaultLabel, defaultHref, className }) {
|
|
17
|
+
const { mode, fields } = await resolveEditableContent();
|
|
18
|
+
const { label, href } = resolveValue(fields[field]?.value, defaultLabel, defaultHref);
|
|
19
|
+
if (mode !== "edit") {
|
|
20
|
+
return (_jsx("a", { href: href, className: className, children: label }));
|
|
21
|
+
}
|
|
22
|
+
return _jsx(LinkFieldClient, { field: field, defaultValue: { label: defaultLabel, href: defaultHref }, fieldType: "button", className: className });
|
|
23
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface EditableImageValue {
|
|
2
|
+
src: string;
|
|
3
|
+
alt?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface EditableImageProps {
|
|
6
|
+
field: string;
|
|
7
|
+
defaultSrc: string;
|
|
8
|
+
defaultAlt?: string;
|
|
9
|
+
className?: string;
|
|
10
|
+
width?: number;
|
|
11
|
+
height?: number;
|
|
12
|
+
sizes?: string;
|
|
13
|
+
}
|
|
14
|
+
/** base-theme deliberately never uses next/image (server/CDN-agnostic
|
|
15
|
+
* merchant image hosts) — this primitive follows that same convention. */
|
|
16
|
+
export declare function EditableImage({ field, defaultSrc, defaultAlt, className, width, height, sizes }: EditableImageProps): Promise<import("react").JSX.Element>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { resolveEditableContent } from "../server/resolve.js";
|
|
3
|
+
import { EditableImageClient } from "./EditableImageClient.js";
|
|
4
|
+
function resolveValue(raw, defaultSrc, defaultAlt) {
|
|
5
|
+
if (raw && typeof raw === "object" && "src" in raw) {
|
|
6
|
+
const v = raw;
|
|
7
|
+
return { src: v.src || defaultSrc, alt: v.alt ?? defaultAlt ?? "" };
|
|
8
|
+
}
|
|
9
|
+
return { src: defaultSrc, alt: defaultAlt ?? "" };
|
|
10
|
+
}
|
|
11
|
+
/** base-theme deliberately never uses next/image (server/CDN-agnostic
|
|
12
|
+
* merchant image hosts) — this primitive follows that same convention. */
|
|
13
|
+
export async function EditableImage({ field, defaultSrc, defaultAlt, className, width, height, sizes }) {
|
|
14
|
+
const { mode, fields } = await resolveEditableContent();
|
|
15
|
+
const { src, alt } = resolveValue(fields[field]?.value, defaultSrc, defaultAlt);
|
|
16
|
+
if (mode !== "edit") {
|
|
17
|
+
// eslint-disable-next-line @next/next/no-img-element
|
|
18
|
+
return _jsx("img", { src: src, alt: alt, className: className, width: width, height: height, sizes: sizes });
|
|
19
|
+
}
|
|
20
|
+
return (_jsx(EditableImageClient, { field: field, defaultSrc: defaultSrc, defaultAlt: defaultAlt, className: className, width: width, height: height, sizes: sizes }));
|
|
21
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface EditableImageClientProps {
|
|
2
|
+
field: string;
|
|
3
|
+
defaultSrc: string;
|
|
4
|
+
defaultAlt?: string;
|
|
5
|
+
className?: string;
|
|
6
|
+
width?: number;
|
|
7
|
+
height?: number;
|
|
8
|
+
sizes?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function EditableImageClient({ field, defaultSrc, defaultAlt, className, width, height, sizes }: EditableImageClientProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useRef, useState } from "react";
|
|
4
|
+
import { useEditableField } from "../useEditableField.js";
|
|
5
|
+
import { useEditableContext } from "../context.js";
|
|
6
|
+
import { editableClassName } from "../chrome.js";
|
|
7
|
+
export function EditableImageClient({ field, defaultSrc, defaultAlt, className, width, height, sizes }) {
|
|
8
|
+
const defaultValue = { src: defaultSrc, alt: defaultAlt ?? "" };
|
|
9
|
+
const { value, setValue, status, retry } = useEditableField(field, defaultValue, { type: "image" });
|
|
10
|
+
const { uploadMedia } = useEditableContext();
|
|
11
|
+
const inputRef = useRef(null);
|
|
12
|
+
const [uploading, setUploading] = useState(false);
|
|
13
|
+
async function handleFile(e) {
|
|
14
|
+
const file = e.target.files?.[0];
|
|
15
|
+
e.target.value = "";
|
|
16
|
+
if (!file)
|
|
17
|
+
return;
|
|
18
|
+
setUploading(true);
|
|
19
|
+
try {
|
|
20
|
+
const uploaded = await uploadMedia(file, file.name);
|
|
21
|
+
if (uploaded.url)
|
|
22
|
+
setValue({ src: uploaded.url, alt: value.alt });
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// uploadMedia's own errors surface via a rejected promise only — the
|
|
26
|
+
// field's save status is untouched (nothing was set), so just let the
|
|
27
|
+
// merchant try again; no partial/broken state is left behind.
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
setUploading(false);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return (_jsxs("span", { className: "kurumera-editable-image", style: { position: "relative", display: "inline-block" }, children: [_jsx("img", { src: value.src, alt: value.alt, className: editableClassName(className, status), width: width, height: height, sizes: sizes, "data-kurumera-field": field, title: status === "error" ? "Couldn't save — click to retry" : undefined, onClick: status === "error" ? () => retry() : undefined }), _jsx("button", { type: "button", className: "kurumera-editable-image__change", onClick: () => inputRef.current?.click(), disabled: uploading, children: uploading ? "Uploading…" : "Change image" }), _jsx("input", { ref: inputRef, type: "file", accept: "image/*", hidden: true, onChange: handleFile })] }));
|
|
34
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface EditableLinkProps {
|
|
2
|
+
/** Field stores `{ label, href }` — same shape as EditableButton, kept as
|
|
3
|
+
* a distinct export since "inline text link" and "styled CTA button" are
|
|
4
|
+
* conceptually different reaches for a theme dev, and are likely to
|
|
5
|
+
* diverge further (e.g. a future "open in new tab" toggle only makes
|
|
6
|
+
* sense here). */
|
|
7
|
+
field: string;
|
|
8
|
+
defaultLabel: string;
|
|
9
|
+
defaultHref: string;
|
|
10
|
+
className?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function EditableLink({ field, defaultLabel, defaultHref, className }: EditableLinkProps): Promise<import("react").JSX.Element>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { resolveEditableContent } from "../server/resolve.js";
|
|
3
|
+
import { LinkFieldClient } from "./LinkFieldClient.js";
|
|
4
|
+
function resolveValue(raw, defaultLabel, defaultHref) {
|
|
5
|
+
if (raw && typeof raw === "object") {
|
|
6
|
+
const v = raw;
|
|
7
|
+
return { label: v.label || defaultLabel, href: v.href || defaultHref };
|
|
8
|
+
}
|
|
9
|
+
return { label: defaultLabel, href: defaultHref };
|
|
10
|
+
}
|
|
11
|
+
export async function EditableLink({ field, defaultLabel, defaultHref, className }) {
|
|
12
|
+
const { mode, fields } = await resolveEditableContent();
|
|
13
|
+
const { label, href } = resolveValue(fields[field]?.value, defaultLabel, defaultHref);
|
|
14
|
+
if (mode !== "edit") {
|
|
15
|
+
return (_jsx("a", { href: href, className: className, children: label }));
|
|
16
|
+
}
|
|
17
|
+
return _jsx(LinkFieldClient, { field: field, defaultValue: { label: defaultLabel, href: defaultHref }, fieldType: "link", className: className });
|
|
18
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
export interface RepeaterItem<Item> {
|
|
3
|
+
id: string;
|
|
4
|
+
/** Fallback data for this item — from `defaultItems` (by original seed
|
|
5
|
+
* position) for a theme-shipped item, or `itemDefaults` for one the
|
|
6
|
+
* merchant added. Individual sub-fields still resolve their OWN draft/live
|
|
7
|
+
* value independently (via `fieldFor`) once edited — this is only what a
|
|
8
|
+
* not-yet-edited sub-field falls back to. */
|
|
9
|
+
data: Item;
|
|
10
|
+
/** Qualifies a sub-field key for this item, e.g. fieldFor("quote") →
|
|
11
|
+
* "home.testimonials.item-0.quote" — pass straight to a nested Editable*'s
|
|
12
|
+
* `field` prop. */
|
|
13
|
+
fieldFor: (subfield: string) => string;
|
|
14
|
+
}
|
|
15
|
+
export interface EditableRepeaterProps<Item> {
|
|
16
|
+
field: string;
|
|
17
|
+
/** The theme's shipped seed content — unaffected until a merchant edits. */
|
|
18
|
+
defaultItems: Item[];
|
|
19
|
+
/** Fallback data for a brand-new merchant-added item. */
|
|
20
|
+
itemDefaults: Item;
|
|
21
|
+
min?: number;
|
|
22
|
+
max?: number;
|
|
23
|
+
as?: string;
|
|
24
|
+
itemAs?: string;
|
|
25
|
+
className?: string;
|
|
26
|
+
itemClassName?: string;
|
|
27
|
+
/** The theme dev's per-item template — called for EVERY item, live or
|
|
28
|
+
* edit, so a repeater full of EditableText children still ships zero
|
|
29
|
+
* editor JS to shoppers, recursively. The merchant's add/remove/reorder
|
|
30
|
+
* UI can only change WHICH ids exist and in what order — it never touches
|
|
31
|
+
* this function, so it can't alter the template/layout. */
|
|
32
|
+
children: (item: RepeaterItem<Item>, index: number) => ReactNode;
|
|
33
|
+
}
|
|
34
|
+
export declare function EditableRepeater<Item>({ field, defaultItems, itemDefaults, min, max, as, itemAs, className, itemClassName, children, }: EditableRepeaterProps<Item>): Promise<import("react").JSX.Element>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { resolveEditableContent } from "../server/resolve.js";
|
|
3
|
+
import { EditableRepeaterClient } from "./EditableRepeaterClient.js";
|
|
4
|
+
function resolveOrder(raw, seedCount) {
|
|
5
|
+
if (Array.isArray(raw) && raw.every((x) => typeof x === "string"))
|
|
6
|
+
return raw;
|
|
7
|
+
return Array.from({ length: seedCount }, (_, i) => `item-${i}`);
|
|
8
|
+
}
|
|
9
|
+
function defaultDataForId(id, defaultItems, itemDefaults) {
|
|
10
|
+
const match = /^item-(\d+)$/.exec(id);
|
|
11
|
+
if (match) {
|
|
12
|
+
const seed = defaultItems[Number(match[1])];
|
|
13
|
+
if (seed !== undefined)
|
|
14
|
+
return seed;
|
|
15
|
+
}
|
|
16
|
+
return itemDefaults;
|
|
17
|
+
}
|
|
18
|
+
export async function EditableRepeater({ field, defaultItems, itemDefaults, min, max, as = "div", itemAs = "div", className, itemClassName, children, }) {
|
|
19
|
+
const { mode, fields } = await resolveEditableContent();
|
|
20
|
+
const order = resolveOrder(fields[field]?.value, defaultItems.length);
|
|
21
|
+
const rendered = order.map((id, i) => {
|
|
22
|
+
const data = defaultDataForId(id, defaultItems, itemDefaults);
|
|
23
|
+
const node = children({ id, data, fieldFor: (sub) => `${field}.${id}.${sub}` }, i);
|
|
24
|
+
return { id, node };
|
|
25
|
+
});
|
|
26
|
+
const As = as;
|
|
27
|
+
const ItemAs = itemAs;
|
|
28
|
+
if (mode !== "edit") {
|
|
29
|
+
return (_jsx(As, { className: className, children: rendered.map(({ id, node }) => (_jsx(ItemAs, { className: itemClassName, children: node }, id))) }));
|
|
30
|
+
}
|
|
31
|
+
return (_jsx(EditableRepeaterClient, { field: field, as: as, itemAs: itemAs, className: className, itemClassName: itemClassName, order: order, min: min, max: max, items: rendered }));
|
|
32
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
interface RenderedItem {
|
|
3
|
+
id: string;
|
|
4
|
+
node: ReactNode;
|
|
5
|
+
}
|
|
6
|
+
export interface EditableRepeaterClientProps {
|
|
7
|
+
field: string;
|
|
8
|
+
as: string;
|
|
9
|
+
itemAs: string;
|
|
10
|
+
className?: string;
|
|
11
|
+
itemClassName?: string;
|
|
12
|
+
order: string[];
|
|
13
|
+
min?: number;
|
|
14
|
+
max?: number;
|
|
15
|
+
/** Pre-rendered (server-side, via the theme dev's render-prop) nodes for
|
|
16
|
+
* every id CURRENTLY known to the server. A function/render-prop itself
|
|
17
|
+
* can't cross the Server→Client boundary — only its already-rendered
|
|
18
|
+
* output can — so reordering/removing existing ids works with zero extra
|
|
19
|
+
* round trip, but a brand-new id has no node here until the page
|
|
20
|
+
* refreshes (see add(), below). */
|
|
21
|
+
items: RenderedItem[];
|
|
22
|
+
}
|
|
23
|
+
export declare function EditableRepeaterClient({ field, as, itemAs, className, itemClassName, order, min, max, items, }: EditableRepeaterClientProps): import("react").JSX.Element;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useRouter } from "next/navigation";
|
|
4
|
+
import { useEditableField } from "../useEditableField.js";
|
|
5
|
+
export function EditableRepeaterClient({ field, as, itemAs, className, itemClassName, order, min, max, items, }) {
|
|
6
|
+
const router = useRouter();
|
|
7
|
+
const As = as;
|
|
8
|
+
const ItemAs = itemAs;
|
|
9
|
+
// `immediate: true` — reorder/add/remove are discrete actions, saved right
|
|
10
|
+
// away rather than debounced like continuous typing.
|
|
11
|
+
const { value: currentOrder, setValue: setOrder } = useEditableField(field, order, {
|
|
12
|
+
type: "repeater",
|
|
13
|
+
immediate: true,
|
|
14
|
+
});
|
|
15
|
+
const byId = new Map(items.map((i) => [i.id, i.node]));
|
|
16
|
+
function move(index, dir) {
|
|
17
|
+
const target = index + dir;
|
|
18
|
+
if (target < 0 || target >= currentOrder.length)
|
|
19
|
+
return;
|
|
20
|
+
const next = [...currentOrder];
|
|
21
|
+
[next[index], next[target]] = [next[target], next[index]];
|
|
22
|
+
setOrder(next);
|
|
23
|
+
}
|
|
24
|
+
function remove(id) {
|
|
25
|
+
if (min !== undefined && currentOrder.length <= min)
|
|
26
|
+
return;
|
|
27
|
+
setOrder(currentOrder.filter((x) => x !== id));
|
|
28
|
+
}
|
|
29
|
+
function add() {
|
|
30
|
+
if (max !== undefined && currentOrder.length >= max)
|
|
31
|
+
return;
|
|
32
|
+
const id = `item-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
|
33
|
+
setOrder([...currentOrder, id]);
|
|
34
|
+
// The new id's editable sub-fields are Server Components declared in the
|
|
35
|
+
// theme's own render-prop — they only exist once the server re-renders
|
|
36
|
+
// with the new id in `order`. Until this resolves, the item shows a
|
|
37
|
+
// lightweight placeholder below.
|
|
38
|
+
router.refresh();
|
|
39
|
+
}
|
|
40
|
+
const atMax = max !== undefined && currentOrder.length >= max;
|
|
41
|
+
return (_jsxs(As, { className: className, children: [currentOrder.map((id, i) => (_jsxs(ItemAs, { className: itemClassName, "data-kurumera-repeater-item": id, children: [_jsxs("div", { className: "kurumera-editable-repeater__chrome", contentEditable: false, children: [_jsx("button", { type: "button", onClick: () => move(i, -1), disabled: i === 0, "aria-label": "Move up", children: "\u2191" }), _jsx("button", { type: "button", onClick: () => move(i, 1), disabled: i === currentOrder.length - 1, "aria-label": "Move down", children: "\u2193" }), _jsx("button", { type: "button", onClick: () => remove(id), disabled: min !== undefined && currentOrder.length <= min, children: "Remove" })] }), byId.get(id) ?? _jsx("div", { className: "kurumera-editable-repeater__pending", children: "Loading\u2026" })] }, id))), !atMax ? (_jsx("button", { type: "button", className: "kurumera-editable-repeater__add", onClick: add, children: "+ Add item" })) : null] }));
|
|
42
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ElementType } from "react";
|
|
2
|
+
export interface EditableRichTextProps {
|
|
3
|
+
field: string;
|
|
4
|
+
/** Sanitized HTML — round-trips through the backend's own sanitizer on
|
|
5
|
+
* save; this component never trusts browser-produced HTML as final. */
|
|
6
|
+
defaultValue: string;
|
|
7
|
+
as?: ElementType;
|
|
8
|
+
className?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function EditableRichText({ field, defaultValue, as: As, className }: EditableRichTextProps): Promise<import("react").JSX.Element>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { resolveEditableContent } from "../server/resolve.js";
|
|
3
|
+
import { EditableRichTextClient } from "./EditableRichTextClient.js";
|
|
4
|
+
export async function EditableRichText({ field, defaultValue, as: As = "div", className }) {
|
|
5
|
+
const { mode, fields } = await resolveEditableContent();
|
|
6
|
+
const value = fields[field]?.value ?? defaultValue;
|
|
7
|
+
if (mode !== "edit") {
|
|
8
|
+
return _jsx(As, { className: className, dangerouslySetInnerHTML: { __html: value } });
|
|
9
|
+
}
|
|
10
|
+
return _jsx(EditableRichTextClient, { field: field, defaultValue: defaultValue, as: As, className: className });
|
|
11
|
+
}
|