@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
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ElementType } from "react";
|
|
2
|
+
export interface EditableRichTextClientProps {
|
|
3
|
+
field: string;
|
|
4
|
+
defaultValue: string;
|
|
5
|
+
as: ElementType;
|
|
6
|
+
className?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function EditableRichTextClient({ field, defaultValue, as: As, className }: EditableRichTextClientProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useRef } from "react";
|
|
4
|
+
import { useEditableField } from "../useEditableField.js";
|
|
5
|
+
import { editableClassName } from "../chrome.js";
|
|
6
|
+
const TOOLBAR_COMMANDS = [
|
|
7
|
+
{ command: "bold", label: "B" },
|
|
8
|
+
{ command: "italic", label: "I" },
|
|
9
|
+
{ command: "createLink", label: "Link" },
|
|
10
|
+
];
|
|
11
|
+
export function EditableRichTextClient({ field, defaultValue, as: As, className }) {
|
|
12
|
+
const { value, setValue, status, retry } = useEditableField(field, defaultValue, { type: "richtext" });
|
|
13
|
+
const ref = useRef(null);
|
|
14
|
+
function handleBlur(e) {
|
|
15
|
+
const next = e.currentTarget.innerHTML ?? "";
|
|
16
|
+
if (next !== value)
|
|
17
|
+
setValue(next);
|
|
18
|
+
}
|
|
19
|
+
function runCommand(command) {
|
|
20
|
+
const el = ref.current;
|
|
21
|
+
if (!el)
|
|
22
|
+
return;
|
|
23
|
+
el.focus();
|
|
24
|
+
if (command === "createLink") {
|
|
25
|
+
const url = window.prompt("Link URL");
|
|
26
|
+
if (!url)
|
|
27
|
+
return;
|
|
28
|
+
document.execCommand(command, false, url);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
document.execCommand(command, false);
|
|
32
|
+
}
|
|
33
|
+
setValue(el.innerHTML ?? "");
|
|
34
|
+
}
|
|
35
|
+
return (_jsxs("div", { className: "kurumera-editable-richtext", style: { position: "relative" }, children: [_jsx("div", { className: "kurumera-editable-richtext__toolbar", contentEditable: false, children: TOOLBAR_COMMANDS.map((c) => (_jsx("button", { type: "button", onMouseDown: (e) => e.preventDefault() /* keep focus/selection in the editable area */, onClick: () => runCommand(c.command), children: c.label }, c.command))) }), _jsx(As, { ref: ref, className: editableClassName(className, status), contentEditable: true, suppressContentEditableWarning: true, onBlur: handleBlur, "data-kurumera-field": field, title: status === "error" ? "Couldn't save — click to retry" : undefined, onClick: status === "error" ? () => retry() : undefined, dangerouslySetInnerHTML: { __html: value } })] }));
|
|
36
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ElementType, ReactNode } from "react";
|
|
2
|
+
export interface EditableSectionProps {
|
|
3
|
+
/** Optional — omit for a section that's always visible (hover-outline in
|
|
4
|
+
* edit mode only, no hide/show toggle). Backs a `{visible: boolean}` field. */
|
|
5
|
+
field?: string;
|
|
6
|
+
/** Shown in the edit-mode label chip, e.g. "Hero", "Featured Collections". */
|
|
7
|
+
label: string;
|
|
8
|
+
as?: ElementType;
|
|
9
|
+
className?: string;
|
|
10
|
+
children: ReactNode;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Formalizes the `{hero.show && (<section>...)}` pattern themes already
|
|
14
|
+
* hand-write as a reusable primitive. `children` are the theme dev's own
|
|
15
|
+
* JSX (fixed at build time) — this component only ever toggles whether
|
|
16
|
+
* they render, never what they are; that's the whole safety boundary.
|
|
17
|
+
*/
|
|
18
|
+
export declare function EditableSection({ field, label, as: As, className, children }: EditableSectionProps): Promise<import("react").JSX.Element | null>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { resolveEditableContent } from "../server/resolve.js";
|
|
3
|
+
import { EditableSectionClient } from "./EditableSectionClient.js";
|
|
4
|
+
/**
|
|
5
|
+
* Formalizes the `{hero.show && (<section>...)}` pattern themes already
|
|
6
|
+
* hand-write as a reusable primitive. `children` are the theme dev's own
|
|
7
|
+
* JSX (fixed at build time) — this component only ever toggles whether
|
|
8
|
+
* they render, never what they are; that's the whole safety boundary.
|
|
9
|
+
*/
|
|
10
|
+
export async function EditableSection({ field, label, as: As = "section", className, children }) {
|
|
11
|
+
const { mode, fields } = await resolveEditableContent();
|
|
12
|
+
const raw = field ? fields[field]?.value : undefined;
|
|
13
|
+
const visible = typeof raw === "object" && raw !== null && "visible" in raw
|
|
14
|
+
? raw.visible !== false
|
|
15
|
+
: true;
|
|
16
|
+
if (mode !== "edit") {
|
|
17
|
+
if (!visible)
|
|
18
|
+
return null;
|
|
19
|
+
return _jsx(As, { className: className, children: children });
|
|
20
|
+
}
|
|
21
|
+
return (_jsx(EditableSectionClient, { field: field, label: label, as: As, className: className, visible: visible, children: children }));
|
|
22
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ElementType, ReactNode } from "react";
|
|
2
|
+
export interface EditableSectionClientProps {
|
|
3
|
+
field?: string;
|
|
4
|
+
label: string;
|
|
5
|
+
as: ElementType;
|
|
6
|
+
className?: string;
|
|
7
|
+
visible: boolean;
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
}
|
|
10
|
+
export declare function EditableSectionClient({ field, label, as: As, className, visible, children }: EditableSectionClientProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEditableField } from "../useEditableField.js";
|
|
4
|
+
export function EditableSectionClient({ field, label, as: As, className, visible, children }) {
|
|
5
|
+
// Always call the hook (rules-of-hooks) — a fixed placeholder key is fine
|
|
6
|
+
// when `field` is omitted, since setValue() is simply never invoked.
|
|
7
|
+
const { value, setValue } = useEditableField(field ?? `__section_no_field__:${label}`, {
|
|
8
|
+
visible,
|
|
9
|
+
});
|
|
10
|
+
const isVisible = field ? value.visible !== false : true;
|
|
11
|
+
return (_jsxs("div", { className: "kurumera-editable-section", "data-kurumera-section": field ?? undefined, children: [_jsxs("div", { className: "kurumera-editable-section__chip", contentEditable: false, children: [_jsx("span", { children: label }), field ? (_jsx("button", { type: "button", onClick: () => setValue({ visible: !isVisible }), children: isVisible ? "Hide" : "Show" })) : null] }), isVisible ? _jsx(As, { className: className, children: children }) : null] }));
|
|
12
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ElementType } from "react";
|
|
2
|
+
export interface EditableTextProps {
|
|
3
|
+
/** Dotted field key, e.g. "home.hero.heading". */
|
|
4
|
+
field: string;
|
|
5
|
+
/** Rendered verbatim until a merchant edits this field — existing stores
|
|
6
|
+
* are unaffected until something is actually edited. */
|
|
7
|
+
defaultValue: string;
|
|
8
|
+
as?: ElementType;
|
|
9
|
+
className?: string;
|
|
10
|
+
maxLength?: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Server Component — no "use client". Outside edit mode this renders a
|
|
14
|
+
* plain tag with zero extra markup and zero client JS: Next's RSC
|
|
15
|
+
* code-splitting means `EditableTextClient`'s bytes are never even
|
|
16
|
+
* requested by a shopper's browser, not merely hidden.
|
|
17
|
+
*/
|
|
18
|
+
export declare function EditableText({ field, defaultValue, as: As, className, maxLength }: EditableTextProps): Promise<import("react").JSX.Element>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { resolveEditableContent } from "../server/resolve.js";
|
|
3
|
+
import { EditableTextClient } from "./EditableTextClient.js";
|
|
4
|
+
/**
|
|
5
|
+
* Server Component — no "use client". Outside edit mode this renders a
|
|
6
|
+
* plain tag with zero extra markup and zero client JS: Next's RSC
|
|
7
|
+
* code-splitting means `EditableTextClient`'s bytes are never even
|
|
8
|
+
* requested by a shopper's browser, not merely hidden.
|
|
9
|
+
*/
|
|
10
|
+
export async function EditableText({ field, defaultValue, as: As = "span", className, maxLength }) {
|
|
11
|
+
const { mode, fields } = await resolveEditableContent();
|
|
12
|
+
const value = fields[field]?.value ?? defaultValue;
|
|
13
|
+
if (mode !== "edit") {
|
|
14
|
+
return _jsx(As, { className: className, children: value });
|
|
15
|
+
}
|
|
16
|
+
return (_jsx(EditableTextClient, { field: field, defaultValue: defaultValue, as: As, className: className, maxLength: maxLength }));
|
|
17
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ElementType } from "react";
|
|
2
|
+
export interface EditableTextClientProps {
|
|
3
|
+
field: string;
|
|
4
|
+
defaultValue: string;
|
|
5
|
+
as: ElementType;
|
|
6
|
+
className?: string;
|
|
7
|
+
maxLength?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function EditableTextClient({ field, defaultValue, as: As, className, maxLength }: EditableTextClientProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useEditableField } from "../useEditableField.js";
|
|
4
|
+
import { editableClassName } from "../chrome.js";
|
|
5
|
+
export function EditableTextClient({ field, defaultValue, as: As, className, maxLength }) {
|
|
6
|
+
const { value, setValue, status, retry } = useEditableField(field, defaultValue, { type: "text" });
|
|
7
|
+
function handleBlur(e) {
|
|
8
|
+
let next = e.currentTarget.textContent ?? "";
|
|
9
|
+
if (maxLength && next.length > maxLength)
|
|
10
|
+
next = next.slice(0, maxLength);
|
|
11
|
+
if (next !== value)
|
|
12
|
+
setValue(next);
|
|
13
|
+
}
|
|
14
|
+
return (_jsx(As, { className: editableClassName(className, status), contentEditable: true, suppressContentEditableWarning: true, onBlur: handleBlur, "data-kurumera-field": field, title: status === "error" ? "Couldn't save — click to retry" : undefined, onClick: status === "error" ? () => retry() : undefined, children: value }));
|
|
15
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type VideoProvider } from "./videoField.js";
|
|
2
|
+
export type { VideoProvider, EditableVideoValue } from "./videoField.js";
|
|
3
|
+
export interface EditableVideoProps {
|
|
4
|
+
field: string;
|
|
5
|
+
defaultSrc: string;
|
|
6
|
+
defaultPoster?: string;
|
|
7
|
+
defaultProvider?: VideoProvider;
|
|
8
|
+
className?: string;
|
|
9
|
+
autoPlay?: boolean;
|
|
10
|
+
loop?: boolean;
|
|
11
|
+
muted?: boolean;
|
|
12
|
+
controls?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Live rendering is a plain native element either way — `<video>` for an
|
|
16
|
+
* uploaded file, a plain `<iframe>` embed for YouTube/Vimeo — so, like
|
|
17
|
+
* EditableImage, a real shopper's page ships zero extra client JS for this
|
|
18
|
+
* field even though the value itself is merchant-editable.
|
|
19
|
+
*/
|
|
20
|
+
export declare function EditableVideo({ field, defaultSrc, defaultPoster, defaultProvider, className, autoPlay, loop, muted, controls, }: EditableVideoProps): 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 { EditableVideoClient } from "./EditableVideoClient.js";
|
|
4
|
+
import { toEmbedSrc } from "./videoField.js";
|
|
5
|
+
function resolveValue(raw, defaults) {
|
|
6
|
+
if (raw && typeof raw === "object" && "src" in raw) {
|
|
7
|
+
const v = raw;
|
|
8
|
+
return {
|
|
9
|
+
src: v.src || defaults.src,
|
|
10
|
+
poster: v.poster ?? defaults.poster,
|
|
11
|
+
provider: v.provider === "youtube" || v.provider === "vimeo" || v.provider === "file" ? v.provider : defaults.provider,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
return defaults;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Live rendering is a plain native element either way — `<video>` for an
|
|
18
|
+
* uploaded file, a plain `<iframe>` embed for YouTube/Vimeo — so, like
|
|
19
|
+
* EditableImage, a real shopper's page ships zero extra client JS for this
|
|
20
|
+
* field even though the value itself is merchant-editable.
|
|
21
|
+
*/
|
|
22
|
+
export async function EditableVideo({ field, defaultSrc, defaultPoster, defaultProvider = "file", className, autoPlay, loop, muted, controls = true, }) {
|
|
23
|
+
const { mode, fields } = await resolveEditableContent();
|
|
24
|
+
const value = resolveValue(fields[field]?.value, { src: defaultSrc, poster: defaultPoster, provider: defaultProvider });
|
|
25
|
+
if (mode !== "edit") {
|
|
26
|
+
if (value.provider === "file") {
|
|
27
|
+
return (_jsx("video", { src: value.src, poster: value.poster, className: className, autoPlay: autoPlay, loop: loop, muted: muted, controls: controls, playsInline: true }));
|
|
28
|
+
}
|
|
29
|
+
return (_jsx("iframe", { src: toEmbedSrc(value.provider, value.src), className: className, title: "Video", allow: "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture", allowFullScreen: true }));
|
|
30
|
+
}
|
|
31
|
+
return (_jsx(EditableVideoClient, { field: field, defaultValue: { src: defaultSrc, poster: defaultPoster, provider: defaultProvider }, className: className, autoPlay: autoPlay, loop: loop, muted: muted, controls: controls }));
|
|
32
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type EditableVideoValue } from "./videoField.js";
|
|
2
|
+
export interface EditableVideoClientProps {
|
|
3
|
+
field: string;
|
|
4
|
+
defaultValue: EditableVideoValue;
|
|
5
|
+
className?: string;
|
|
6
|
+
autoPlay?: boolean;
|
|
7
|
+
loop?: boolean;
|
|
8
|
+
muted?: boolean;
|
|
9
|
+
controls?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function EditableVideoClient({ field, defaultValue, className, autoPlay, loop, muted, controls }: EditableVideoClientProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
import { toEmbedSrc } from "./videoField.js";
|
|
8
|
+
function detectProvider(src) {
|
|
9
|
+
if (/youtu\.?be/.test(src))
|
|
10
|
+
return "youtube";
|
|
11
|
+
if (/vimeo\.com/.test(src))
|
|
12
|
+
return "vimeo";
|
|
13
|
+
return "file";
|
|
14
|
+
}
|
|
15
|
+
export function EditableVideoClient({ field, defaultValue, className, autoPlay, loop, muted, controls = true }) {
|
|
16
|
+
const { value, setValue, status, retry } = useEditableField(field, defaultValue, { type: "video" });
|
|
17
|
+
const { uploadMedia } = useEditableContext();
|
|
18
|
+
const fileInputRef = useRef(null);
|
|
19
|
+
const urlInputRef = useRef(null);
|
|
20
|
+
const [editing, setEditing] = useState(false);
|
|
21
|
+
const [uploading, setUploading] = useState(false);
|
|
22
|
+
async function handleFile(e) {
|
|
23
|
+
const file = e.target.files?.[0];
|
|
24
|
+
e.target.value = "";
|
|
25
|
+
if (!file)
|
|
26
|
+
return;
|
|
27
|
+
setUploading(true);
|
|
28
|
+
try {
|
|
29
|
+
const uploaded = await uploadMedia(file, file.name, { contentType: "VIDEO" });
|
|
30
|
+
if (uploaded.url)
|
|
31
|
+
setValue({ src: uploaded.url, poster: value.poster, provider: "file" });
|
|
32
|
+
setEditing(false);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
// merchant can retry from the still-open popover; nothing was set
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
setUploading(false);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function commitUrl() {
|
|
42
|
+
const next = urlInputRef.current?.value?.trim();
|
|
43
|
+
if (next)
|
|
44
|
+
setValue({ src: next, poster: value.poster, provider: detectProvider(next) });
|
|
45
|
+
setEditing(false);
|
|
46
|
+
}
|
|
47
|
+
return (_jsxs("span", { className: "kurumera-editable-video", style: { position: "relative", display: "inline-block" }, children: [value.provider === "file" ? (_jsx("video", { src: value.src, poster: value.poster, className: editableClassName(className, status), autoPlay: autoPlay, loop: loop, muted: muted, controls: controls, playsInline: true, "data-kurumera-field": field, title: status === "error" ? "Couldn't save — click to retry" : undefined, onClick: status === "error" ? () => retry() : undefined })) : (_jsx("iframe", { src: toEmbedSrc(value.provider, value.src), className: editableClassName(className, status), title: "Video", allow: "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture", allowFullScreen: true, "data-kurumera-field": field })), _jsx("button", { type: "button", className: "kurumera-editable-video__change", onClick: () => setEditing((v) => !v), children: "Change video" }), editing ? (_jsxs("span", { className: "kurumera-editable-video__popover", children: [_jsx("input", { ref: urlInputRef, type: "text", defaultValue: value.provider !== "file" ? value.src : "", placeholder: "YouTube / Vimeo URL\u2026" }), _jsx("button", { type: "button", onClick: commitUrl, children: "Use URL" }), _jsx("span", { className: "kurumera-editable-video__popover-or", children: "or" }), _jsx("button", { type: "button", onClick: () => fileInputRef.current?.click(), disabled: uploading, children: uploading ? "Uploading…" : "Upload file" }), _jsx("input", { ref: fileInputRef, type: "file", accept: "video/mp4,video/webm,video/quicktime", hidden: true, onChange: handleFile })] })) : null] }));
|
|
48
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface LinkFieldValue {
|
|
2
|
+
label: string;
|
|
3
|
+
href: string;
|
|
4
|
+
}
|
|
5
|
+
export interface LinkFieldClientProps {
|
|
6
|
+
field: string;
|
|
7
|
+
defaultValue: LinkFieldValue;
|
|
8
|
+
/** "button" | "link" — only affects the saved field's `type` tag; the
|
|
9
|
+
* two primitives share this exact implementation (~90% identical per
|
|
10
|
+
* design), differing only in what className the theme dev passes. */
|
|
11
|
+
fieldType: "button" | "link";
|
|
12
|
+
className?: string;
|
|
13
|
+
}
|
|
14
|
+
/** Shared engine for EditableButton and EditableLink — both store the same
|
|
15
|
+
* {label, href} shape and edit the same way (label is contentEditable,
|
|
16
|
+
* href via a small popover since contentEditable can't capture a URL). */
|
|
17
|
+
export declare function LinkFieldClient({ field, defaultValue, fieldType, className }: LinkFieldClientProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,29 @@
|
|
|
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 { editableClassName } from "../chrome.js";
|
|
6
|
+
/** Shared engine for EditableButton and EditableLink — both store the same
|
|
7
|
+
* {label, href} shape and edit the same way (label is contentEditable,
|
|
8
|
+
* href via a small popover since contentEditable can't capture a URL). */
|
|
9
|
+
export function LinkFieldClient({ field, defaultValue, fieldType, className }) {
|
|
10
|
+
const { value, setValue, status, retry } = useEditableField(field, defaultValue, { type: fieldType });
|
|
11
|
+
const [editingHref, setEditingHref] = useState(false);
|
|
12
|
+
const hrefInputRef = useRef(null);
|
|
13
|
+
function handleLabelBlur(e) {
|
|
14
|
+
const next = e.currentTarget.textContent ?? "";
|
|
15
|
+
if (next !== value.label)
|
|
16
|
+
setValue({ ...value, label: next });
|
|
17
|
+
}
|
|
18
|
+
function commitHref() {
|
|
19
|
+
const next = hrefInputRef.current?.value ?? value.href;
|
|
20
|
+
if (next !== value.href)
|
|
21
|
+
setValue({ ...value, href: next });
|
|
22
|
+
setEditingHref(false);
|
|
23
|
+
}
|
|
24
|
+
return (_jsxs("span", { className: "kurumera-editable-linkfield", style: { position: "relative", display: "inline-block" }, children: [_jsx("a", { href: value.href, className: editableClassName(className, status), contentEditable: true, suppressContentEditableWarning: true, onBlur: handleLabelBlur, onClick: (e) => {
|
|
25
|
+
e.preventDefault(); // never navigate away from the editor
|
|
26
|
+
if (status === "error")
|
|
27
|
+
retry();
|
|
28
|
+
}, "data-kurumera-field": field, title: status === "error" ? "Couldn't save — click to retry" : undefined, children: value.label }), _jsx("button", { type: "button", className: "kurumera-editable-linkfield__edit-link", contentEditable: false, onClick: () => setEditingHref((v) => !v), children: "Edit link" }), editingHref ? (_jsxs("span", { className: "kurumera-editable-linkfield__popover", contentEditable: false, children: [_jsx("input", { ref: hrefInputRef, type: "text", defaultValue: value.href, placeholder: "https://\u2026" }), _jsx("button", { type: "button", onClick: commitHref, children: "Save" }), _jsx("button", { type: "button", onClick: () => setEditingHref(false), children: "Cancel" })] })) : null] }));
|
|
29
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers shared by EditableVideo (Server Component, imports
|
|
3
|
+
* `resolveEditableContent` → `next/headers`) and EditableVideoClient (a
|
|
4
|
+
* "use client" leaf). Deliberately kept in their OWN module with zero
|
|
5
|
+
* server-only imports: a "use client" file importing even one VALUE
|
|
6
|
+
* (non-type-only) export from EditableVideo.tsx would pull that whole
|
|
7
|
+
* module — including its `next/headers` import — into the client bundle
|
|
8
|
+
* graph and fail the build. `import type` alone gets erased at compile
|
|
9
|
+
* time and would have been safe, but `toEmbedSrc` is a real function, so
|
|
10
|
+
* this split is required, not just tidy.
|
|
11
|
+
*/
|
|
12
|
+
export type VideoProvider = "file" | "youtube" | "vimeo";
|
|
13
|
+
export interface EditableVideoValue {
|
|
14
|
+
src: string;
|
|
15
|
+
poster?: string;
|
|
16
|
+
provider: VideoProvider;
|
|
17
|
+
}
|
|
18
|
+
/** Resolves a merchant-pasted URL (or bare id) to an embeddable iframe src. */
|
|
19
|
+
export declare function toEmbedSrc(provider: "youtube" | "vimeo", src: string): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function extractYouTubeId(src) {
|
|
2
|
+
const m = /(?:youtu\.be\/|youtube\.com\/(?:watch\?v=|embed\/|shorts\/))([a-zA-Z0-9_-]{6,})/.exec(src);
|
|
3
|
+
return m ? m[1] : null;
|
|
4
|
+
}
|
|
5
|
+
function extractVimeoId(src) {
|
|
6
|
+
const m = /vimeo\.com\/(?:video\/)?(\d+)/.exec(src);
|
|
7
|
+
return m ? m[1] : null;
|
|
8
|
+
}
|
|
9
|
+
/** Resolves a merchant-pasted URL (or bare id) to an embeddable iframe src. */
|
|
10
|
+
export function toEmbedSrc(provider, src) {
|
|
11
|
+
if (provider === "youtube") {
|
|
12
|
+
return `https://www.youtube-nocookie.com/embed/${extractYouTubeId(src) ?? src}`;
|
|
13
|
+
}
|
|
14
|
+
return `https://player.vimeo.com/video/${extractVimeoId(src) ?? src}`;
|
|
15
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { EditableStore } from "./store.js";
|
|
2
|
+
/** Default debounce for continuous edits (typing). Shorter than the visual
|
|
3
|
+
* builder's 1500ms (website-builder/src/builder/hooks/useAutosave.ts) since
|
|
4
|
+
* a single field's payload is tiny — editing two fields in quick succession
|
|
5
|
+
* shouldn't feel laggy. */
|
|
6
|
+
export declare const DEFAULT_DEBOUNCE_MS = 1000;
|
|
7
|
+
export interface SaveQueueDeps {
|
|
8
|
+
store: EditableStore;
|
|
9
|
+
save: (fieldKey: string, value: unknown, type?: string) => Promise<void>;
|
|
10
|
+
debounceMs?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface SaveQueue {
|
|
13
|
+
/** Debounced — call on every keystroke/change. */
|
|
14
|
+
setValue(key: string, value: unknown, type?: string): void;
|
|
15
|
+
/** Immediate — for discrete actions (repeater add/remove/reorder), which
|
|
16
|
+
* shouldn't wait out a debounce window like continuous typing does. */
|
|
17
|
+
saveNow(key: string, value: unknown, type?: string): void;
|
|
18
|
+
/** Manually re-attempt a failed save. */
|
|
19
|
+
retry(key: string, type?: string): void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Per-field-key debounced autosave. Never clears a field's dirty value on
|
|
23
|
+
* failure (same principle as the visual builder's autosave) — a failed save
|
|
24
|
+
* auto-retries with backoff and leaves the merchant's edit visible either
|
|
25
|
+
* way; `retry()` is also exposed for a manual retry affordance.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createSaveQueue({ store, save, debounceMs }: SaveQueueDeps): SaveQueue;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** Default debounce for continuous edits (typing). Shorter than the visual
|
|
2
|
+
* builder's 1500ms (website-builder/src/builder/hooks/useAutosave.ts) since
|
|
3
|
+
* a single field's payload is tiny — editing two fields in quick succession
|
|
4
|
+
* shouldn't feel laggy. */
|
|
5
|
+
export const DEFAULT_DEBOUNCE_MS = 1000;
|
|
6
|
+
const RETRY_BACKOFF_MS = [2000, 5000, 10000];
|
|
7
|
+
/**
|
|
8
|
+
* Per-field-key debounced autosave. Never clears a field's dirty value on
|
|
9
|
+
* failure (same principle as the visual builder's autosave) — a failed save
|
|
10
|
+
* auto-retries with backoff and leaves the merchant's edit visible either
|
|
11
|
+
* way; `retry()` is also exposed for a manual retry affordance.
|
|
12
|
+
*/
|
|
13
|
+
export function createSaveQueue({ store, save, debounceMs = DEFAULT_DEBOUNCE_MS }) {
|
|
14
|
+
const debounceTimers = new Map();
|
|
15
|
+
const retryTimers = new Map();
|
|
16
|
+
const inFlight = new Set();
|
|
17
|
+
function clearDebounce(key) {
|
|
18
|
+
const t = debounceTimers.get(key);
|
|
19
|
+
if (t) {
|
|
20
|
+
clearTimeout(t);
|
|
21
|
+
debounceTimers.delete(key);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function clearRetry(key) {
|
|
25
|
+
const t = retryTimers.get(key);
|
|
26
|
+
if (t) {
|
|
27
|
+
clearTimeout(t);
|
|
28
|
+
retryTimers.delete(key);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function flush(key, type, attempt) {
|
|
32
|
+
if (inFlight.has(key))
|
|
33
|
+
return; // a save is already in flight; its own resolve() re-checks the latest value
|
|
34
|
+
inFlight.add(key);
|
|
35
|
+
const valueAtStart = store.get(key, undefined).value;
|
|
36
|
+
store.setStatus(key, "saving");
|
|
37
|
+
try {
|
|
38
|
+
await save(key, valueAtStart, type);
|
|
39
|
+
inFlight.delete(key);
|
|
40
|
+
const current = store.get(key, undefined).value;
|
|
41
|
+
if (current !== valueAtStart) {
|
|
42
|
+
// more edits landed while this save was in flight — save the latest immediately
|
|
43
|
+
void flush(key, type, 0);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
store.setStatus(key, "saved");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
inFlight.delete(key);
|
|
51
|
+
const message = err instanceof Error ? err.message : "Save failed.";
|
|
52
|
+
store.setStatus(key, "error", message);
|
|
53
|
+
const backoff = RETRY_BACKOFF_MS[attempt];
|
|
54
|
+
if (backoff !== undefined) {
|
|
55
|
+
const t = setTimeout(() => void flush(key, type, attempt + 1), backoff);
|
|
56
|
+
retryTimers.set(key, t);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function schedule(key, type, delay) {
|
|
61
|
+
clearDebounce(key);
|
|
62
|
+
const t = setTimeout(() => void flush(key, type, 0), delay);
|
|
63
|
+
debounceTimers.set(key, t);
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
setValue(key, value, type) {
|
|
67
|
+
store.setValue(key, value);
|
|
68
|
+
clearRetry(key);
|
|
69
|
+
schedule(key, type, debounceMs);
|
|
70
|
+
},
|
|
71
|
+
saveNow(key, value, type) {
|
|
72
|
+
store.setValue(key, value);
|
|
73
|
+
clearRetry(key);
|
|
74
|
+
clearDebounce(key);
|
|
75
|
+
void flush(key, type, 0);
|
|
76
|
+
},
|
|
77
|
+
retry(key, type) {
|
|
78
|
+
clearRetry(key);
|
|
79
|
+
void flush(key, type, 0);
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { EditableResolution } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Per-request, cache()-deduped resolution of "what should this page render:
|
|
4
|
+
* live, edit, or preview" — every `Editable*` Server Component AND the root
|
|
5
|
+
* layout's `<EditableProvider>` call this independently; React dedupes to
|
|
6
|
+
* ONE `content.getAll()` network call per request (same pattern as
|
|
7
|
+
* `getStoreConfig()` in base-theme/lib/kurumera.ts).
|
|
8
|
+
*
|
|
9
|
+
* Reads the SAME `x-kurumera-tenant`/`x-kurumera-domain` headers
|
|
10
|
+
* middleware.ts already sets for tenant resolution, plus two new ones this
|
|
11
|
+
* package's middleware snippet adds: `x-kurumera-edit-token` and
|
|
12
|
+
* `x-kurumera-edit-mode`. Self-contained — does not depend on (or need) a
|
|
13
|
+
* theme-specific `lib/kurumera.ts` helper.
|
|
14
|
+
*
|
|
15
|
+
* Never throws: a missing tenant, an invalid/expired edit token, or a
|
|
16
|
+
* failed fetch all resolve to `{ mode: "off", fields: {} }` — the same
|
|
17
|
+
* plain rendering a real shopper gets. This is deliberate defense in depth;
|
|
18
|
+
* the backend already degrades a bad edit-session token to `editable:
|
|
19
|
+
* false` on its own (see api/v1/storefront/editable_content_views.py).
|
|
20
|
+
*/
|
|
21
|
+
export declare const resolveEditableContent: () => Promise<EditableResolution>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { cache } from "react";
|
|
2
|
+
import { headers } from "next/headers";
|
|
3
|
+
import { createKurumeraClient } from "@kurumera/storefront";
|
|
4
|
+
/**
|
|
5
|
+
* Per-request, cache()-deduped resolution of "what should this page render:
|
|
6
|
+
* live, edit, or preview" — every `Editable*` Server Component AND the root
|
|
7
|
+
* layout's `<EditableProvider>` call this independently; React dedupes to
|
|
8
|
+
* ONE `content.getAll()` network call per request (same pattern as
|
|
9
|
+
* `getStoreConfig()` in base-theme/lib/kurumera.ts).
|
|
10
|
+
*
|
|
11
|
+
* Reads the SAME `x-kurumera-tenant`/`x-kurumera-domain` headers
|
|
12
|
+
* middleware.ts already sets for tenant resolution, plus two new ones this
|
|
13
|
+
* package's middleware snippet adds: `x-kurumera-edit-token` and
|
|
14
|
+
* `x-kurumera-edit-mode`. Self-contained — does not depend on (or need) a
|
|
15
|
+
* theme-specific `lib/kurumera.ts` helper.
|
|
16
|
+
*
|
|
17
|
+
* Never throws: a missing tenant, an invalid/expired edit token, or a
|
|
18
|
+
* failed fetch all resolve to `{ mode: "off", fields: {} }` — the same
|
|
19
|
+
* plain rendering a real shopper gets. This is deliberate defense in depth;
|
|
20
|
+
* the backend already degrades a bad edit-session token to `editable:
|
|
21
|
+
* false` on its own (see api/v1/storefront/editable_content_views.py).
|
|
22
|
+
*/
|
|
23
|
+
export const resolveEditableContent = cache(async () => {
|
|
24
|
+
const h = await headers();
|
|
25
|
+
const tenant = h.get("x-kurumera-tenant") || process.env.KURUMERA_TENANT || "";
|
|
26
|
+
const domain = h.get("x-kurumera-domain") || "";
|
|
27
|
+
const editToken = h.get("x-kurumera-edit-token") || "";
|
|
28
|
+
const modeParam = (h.get("x-kurumera-edit-mode") || "").toLowerCase();
|
|
29
|
+
const apiUrl = process.env.KURUMERA_API_URL;
|
|
30
|
+
const empty = { mode: "off", tenant, editToken: null, apiUrl, fields: {} };
|
|
31
|
+
if (!tenant && !domain)
|
|
32
|
+
return empty;
|
|
33
|
+
try {
|
|
34
|
+
const client = createKurumeraClient({
|
|
35
|
+
tenant: tenant || undefined,
|
|
36
|
+
domain: domain || undefined,
|
|
37
|
+
apiUrl,
|
|
38
|
+
editSessionToken: editToken || undefined,
|
|
39
|
+
});
|
|
40
|
+
const res = await client.content.getAll();
|
|
41
|
+
const mode = !res.editable ? "off" : modeParam === "preview" ? "preview" : "edit";
|
|
42
|
+
return { mode, tenant, editToken: editToken || null, apiUrl, fields: res.fields };
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return empty;
|
|
46
|
+
}
|
|
47
|
+
});
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kurumera/editable/server — headers()-touching helpers, kept out of the
|
|
3
|
+
* main `.` entry so nothing in a "use client" module's dependency graph can
|
|
4
|
+
* accidentally pull in `next/headers`.
|
|
5
|
+
*
|
|
6
|
+
* Themes generally don't need to import this directly — the root layout is
|
|
7
|
+
* the one place that does, to seed `<EditableProvider>` (see the package
|
|
8
|
+
* README / base-theme's app/layout.tsx for the integration snippet); every
|
|
9
|
+
* `Editable*` primitive already calls it internally.
|
|
10
|
+
*/
|
|
11
|
+
export { resolveEditableContent } from "./server/resolve.js";
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @kurumera/editable/server — headers()-touching helpers, kept out of the
|
|
3
|
+
* main `.` entry so nothing in a "use client" module's dependency graph can
|
|
4
|
+
* accidentally pull in `next/headers`.
|
|
5
|
+
*
|
|
6
|
+
* Themes generally don't need to import this directly — the root layout is
|
|
7
|
+
* the one place that does, to seed `<EditableProvider>` (see the package
|
|
8
|
+
* README / base-theme's app/layout.tsx for the integration snippet); every
|
|
9
|
+
* `Editable*` primitive already calls it internally.
|
|
10
|
+
*/
|
|
11
|
+
export { resolveEditableContent } from "./server/resolve.js";
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { FieldRecord, FieldStatus } from "./types.js";
|
|
2
|
+
type Listener = () => void;
|
|
3
|
+
/**
|
|
4
|
+
* Per-field-key external store (feeds `useSyncExternalStore`), NOT one big
|
|
5
|
+
* object in `useState` — editing one field must never re-render every other
|
|
6
|
+
* `Editable*` instance on the page. Records are replaced (never mutated) on
|
|
7
|
+
* every write so `Object.is` snapshot comparisons in React correctly detect
|
|
8
|
+
* a change.
|
|
9
|
+
*/
|
|
10
|
+
export declare class EditableStore {
|
|
11
|
+
private records;
|
|
12
|
+
private listeners;
|
|
13
|
+
constructor(initial: Record<string, {
|
|
14
|
+
value: unknown;
|
|
15
|
+
type?: string;
|
|
16
|
+
}>);
|
|
17
|
+
/** Lazily seeds (and caches) a record for a field the initial batch fetch
|
|
18
|
+
* didn't know about yet (never edited). Returns a STABLE reference when
|
|
19
|
+
* nothing has changed, as `useSyncExternalStore` requires. */
|
|
20
|
+
get(key: string, fallback: unknown): FieldRecord;
|
|
21
|
+
subscribe(key: string, listener: Listener): () => void;
|
|
22
|
+
private notify;
|
|
23
|
+
setValue(key: string, value: unknown): void;
|
|
24
|
+
setStatus(key: string, status: FieldStatus, error?: string | null): void;
|
|
25
|
+
}
|
|
26
|
+
export {};
|