@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/store.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-field-key external store (feeds `useSyncExternalStore`), NOT one big
|
|
3
|
+
* object in `useState` — editing one field must never re-render every other
|
|
4
|
+
* `Editable*` instance on the page. Records are replaced (never mutated) on
|
|
5
|
+
* every write so `Object.is` snapshot comparisons in React correctly detect
|
|
6
|
+
* a change.
|
|
7
|
+
*/
|
|
8
|
+
export class EditableStore {
|
|
9
|
+
constructor(initial) {
|
|
10
|
+
this.records = new Map();
|
|
11
|
+
this.listeners = new Map();
|
|
12
|
+
for (const [key, f] of Object.entries(initial)) {
|
|
13
|
+
this.records.set(key, { value: f.value, status: "idle", error: null });
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Lazily seeds (and caches) a record for a field the initial batch fetch
|
|
17
|
+
* didn't know about yet (never edited). Returns a STABLE reference when
|
|
18
|
+
* nothing has changed, as `useSyncExternalStore` requires. */
|
|
19
|
+
get(key, fallback) {
|
|
20
|
+
let record = this.records.get(key);
|
|
21
|
+
if (!record) {
|
|
22
|
+
record = { value: fallback, status: "idle", error: null };
|
|
23
|
+
this.records.set(key, record);
|
|
24
|
+
}
|
|
25
|
+
return record;
|
|
26
|
+
}
|
|
27
|
+
subscribe(key, listener) {
|
|
28
|
+
let set = this.listeners.get(key);
|
|
29
|
+
if (!set) {
|
|
30
|
+
set = new Set();
|
|
31
|
+
this.listeners.set(key, set);
|
|
32
|
+
}
|
|
33
|
+
set.add(listener);
|
|
34
|
+
return () => set.delete(listener);
|
|
35
|
+
}
|
|
36
|
+
notify(key) {
|
|
37
|
+
this.listeners.get(key)?.forEach((cb) => cb());
|
|
38
|
+
}
|
|
39
|
+
setValue(key, value) {
|
|
40
|
+
this.records.set(key, { value, status: "dirty", error: null });
|
|
41
|
+
this.notify(key);
|
|
42
|
+
}
|
|
43
|
+
setStatus(key, status, error = null) {
|
|
44
|
+
const prev = this.records.get(key);
|
|
45
|
+
if (!prev)
|
|
46
|
+
return;
|
|
47
|
+
this.records.set(key, { ...prev, status, error });
|
|
48
|
+
this.notify(key);
|
|
49
|
+
}
|
|
50
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Kurumera Editable Components — shared types.
|
|
3
|
+
*
|
|
4
|
+
* `off` = every real shopper (no client leaf JS is ever shipped for this
|
|
5
|
+
* mode — see the per-primitive Server Component split). `edit` = the
|
|
6
|
+
* merchant's in-dashboard content editor iframe. `preview` = the same
|
|
7
|
+
* draft VALUES as `edit`, but rendered through the exact same plain
|
|
8
|
+
* (non-interactive) path `off` uses — see resolveEditableContent.
|
|
9
|
+
*/
|
|
10
|
+
export type EditMode = "off" | "edit" | "preview";
|
|
11
|
+
export type FieldStatus = "idle" | "dirty" | "saving" | "saved" | "error";
|
|
12
|
+
export interface FieldRecord {
|
|
13
|
+
value: unknown;
|
|
14
|
+
status: FieldStatus;
|
|
15
|
+
error: string | null;
|
|
16
|
+
}
|
|
17
|
+
/** What the batched content fetch resolves to for one request. */
|
|
18
|
+
export interface EditableResolution {
|
|
19
|
+
mode: EditMode;
|
|
20
|
+
tenant: string;
|
|
21
|
+
editToken: string | null;
|
|
22
|
+
apiUrl?: string;
|
|
23
|
+
fields: Record<string, {
|
|
24
|
+
value: unknown;
|
|
25
|
+
type?: string;
|
|
26
|
+
}>;
|
|
27
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { FieldStatus } from "./types.js";
|
|
2
|
+
export interface UseEditableFieldOptions {
|
|
3
|
+
type?: string;
|
|
4
|
+
/** Immediate save (repeater add/remove/reorder) instead of the debounced
|
|
5
|
+
* default — use for discrete actions, not continuous typing. */
|
|
6
|
+
immediate?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface UseEditableFieldResult<T> {
|
|
9
|
+
value: T;
|
|
10
|
+
setValue: (next: T) => void;
|
|
11
|
+
editMode: boolean;
|
|
12
|
+
status: FieldStatus;
|
|
13
|
+
error: string | null;
|
|
14
|
+
retry: () => void;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The shared engine every Editable* primitive's client leaf is built on —
|
|
18
|
+
* also the public escape hatch for a theme dev's OWN component that needs
|
|
19
|
+
* an editable value but isn't covered by one of the built-in primitives.
|
|
20
|
+
*
|
|
21
|
+
* Outside edit mode (or outside an `<EditableProvider>` entirely) this is a
|
|
22
|
+
* cheap context+snapshot read with no timers/listeners ever armed — it
|
|
23
|
+
* doesn't make an already-client component more expensive than it chose to
|
|
24
|
+
* be, but it also can't give a Server Component the same "ship zero JS"
|
|
25
|
+
* guarantee the primitives get from never including their client leaf in
|
|
26
|
+
* the bundle at all — that guarantee only applies to the primitives.
|
|
27
|
+
*/
|
|
28
|
+
export declare function useEditableField<T = unknown>(field: string, defaultValue: T, options?: UseEditableFieldOptions): UseEditableFieldResult<T>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useSyncExternalStore } from "react";
|
|
3
|
+
import { useEditableContext } from "./context.js";
|
|
4
|
+
/**
|
|
5
|
+
* The shared engine every Editable* primitive's client leaf is built on —
|
|
6
|
+
* also the public escape hatch for a theme dev's OWN component that needs
|
|
7
|
+
* an editable value but isn't covered by one of the built-in primitives.
|
|
8
|
+
*
|
|
9
|
+
* Outside edit mode (or outside an `<EditableProvider>` entirely) this is a
|
|
10
|
+
* cheap context+snapshot read with no timers/listeners ever armed — it
|
|
11
|
+
* doesn't make an already-client component more expensive than it chose to
|
|
12
|
+
* be, but it also can't give a Server Component the same "ship zero JS"
|
|
13
|
+
* guarantee the primitives get from never including their client leaf in
|
|
14
|
+
* the bundle at all — that guarantee only applies to the primitives.
|
|
15
|
+
*/
|
|
16
|
+
export function useEditableField(field, defaultValue, options) {
|
|
17
|
+
const { mode, store, saveQueue } = useEditableContext();
|
|
18
|
+
const subscribe = useCallback((cb) => store.subscribe(field, cb), [store, field]);
|
|
19
|
+
const getSnapshot = useCallback(() => store.get(field, defaultValue), [store, field, defaultValue]);
|
|
20
|
+
const record = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
21
|
+
const setValue = useCallback((next) => {
|
|
22
|
+
if (mode !== "edit" || !saveQueue)
|
|
23
|
+
return;
|
|
24
|
+
if (options?.immediate)
|
|
25
|
+
saveQueue.saveNow(field, next, options?.type);
|
|
26
|
+
else
|
|
27
|
+
saveQueue.setValue(field, next, options?.type);
|
|
28
|
+
}, [mode, saveQueue, field, options?.type, options?.immediate]);
|
|
29
|
+
const retry = useCallback(() => saveQueue?.retry(field, options?.type), [saveQueue, field, options?.type]);
|
|
30
|
+
return {
|
|
31
|
+
value: record.value ?? defaultValue,
|
|
32
|
+
setValue,
|
|
33
|
+
editMode: mode === "edit",
|
|
34
|
+
status: record.status,
|
|
35
|
+
error: record.error,
|
|
36
|
+
retry,
|
|
37
|
+
};
|
|
38
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kurumera/editable",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Kurumera Editable Components — inline, draft/publish, no-rebuild content editing for code themes.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
|
|
10
|
+
"./client": { "types": "./dist/client.d.ts", "import": "./dist/client.js" },
|
|
11
|
+
"./server": { "types": "./dist/server.d.ts", "import": "./dist/server.js" }
|
|
12
|
+
},
|
|
13
|
+
"files": ["dist"],
|
|
14
|
+
"publishConfig": { "access": "public" },
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc -p tsconfig.json",
|
|
17
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
18
|
+
"prepack": "npm run build"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"next": "^15.0.0",
|
|
22
|
+
"react": "^19.0.0",
|
|
23
|
+
"react-dom": "^19.0.0"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@kurumera/storefront": "^0.3.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/react": "^19.0.0",
|
|
30
|
+
"@types/react-dom": "^19.0.0"
|
|
31
|
+
},
|
|
32
|
+
"license": "UNLICENSED"
|
|
33
|
+
}
|