@broberg/cms-inline-edit 0.1.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/LICENSE +21 -0
- package/README.md +38 -0
- package/dist/index.cjs +172 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +147 -0
- package/dist/index.js.map +1 -0
- package/dist/server/index.cjs +57 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +41 -0
- package/dist/server/index.d.ts +41 -0
- package/dist/server/index.js +31 -0
- package/dist/server/index.js.map +1 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 WebHouse
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# @broberg/cms-inline-edit
|
|
2
|
+
|
|
3
|
+
Click-to-edit inline editing for live [@webhouse/cms](https://docs.webhouse.app)-powered sites. Copy-owned: each site installs this as a normal npm dependency and owns its own integration/version — no centrally-hosted script.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Mark the plain-text elements a document's rendering code prints from CMS fields with `data-cms-collection` / `data-cms-slug` / `data-cms-field`:
|
|
8
|
+
|
|
9
|
+
```html
|
|
10
|
+
<h2 data-cms-collection="sections" data-cms-slug="hero-home" data-cms-field="eyebrow">
|
|
11
|
+
Some CMS-sourced text
|
|
12
|
+
</h2>
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Then, once per page load:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { initInlineEdit } from "@broberg/cms-inline-edit";
|
|
19
|
+
|
|
20
|
+
initInlineEdit({
|
|
21
|
+
cmsBaseUrl: "https://webhouse.app",
|
|
22
|
+
siteId: "broberg-ai",
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
`initInlineEdit` looks for a `?cms_edit=<token>` URL param (minted by cms-admin's "Redigér live" button), persists it in `sessionStorage`, and — only when a token is present — makes every `[data-cms-field]` element click-to-edit, saving on blur directly against the CMS API.
|
|
27
|
+
|
|
28
|
+
Only plain text fields are supported. Do not attach these attributes to elements rendered via `dangerouslySetInnerHTML` (richtext/HTML fields) — not supported in this version.
|
|
29
|
+
|
|
30
|
+
## `./server` (optional)
|
|
31
|
+
|
|
32
|
+
Thin Node/Bun helpers for a site that wants a same-origin relay instead of the browser calling the CMS directly:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { saveInlineEditField, verifyEditSession } from "@broberg/cms-inline-edit/server";
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
See `docs/features/F157-inline-editing.md` in the [@webhouse/cms](https://github.com/webhousecode/cms) repo for the full design.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
initInlineEdit: () => initInlineEdit
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
function resolveOptions(options) {
|
|
27
|
+
return {
|
|
28
|
+
tokenParam: "cms_edit",
|
|
29
|
+
storageKey: "wh-inline-edit-token",
|
|
30
|
+
...options
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function initInlineEdit(options) {
|
|
34
|
+
if (typeof window === "undefined") return;
|
|
35
|
+
const resolved = resolveOptions(options);
|
|
36
|
+
const token = bootstrapToken(resolved);
|
|
37
|
+
if (!token) return;
|
|
38
|
+
activateEditMode(token, resolved);
|
|
39
|
+
}
|
|
40
|
+
function bootstrapToken(options) {
|
|
41
|
+
const url = new URL(window.location.href);
|
|
42
|
+
const urlToken = url.searchParams.get(options.tokenParam);
|
|
43
|
+
if (urlToken) {
|
|
44
|
+
sessionStorage.setItem(options.storageKey, urlToken);
|
|
45
|
+
url.searchParams.delete(options.tokenParam);
|
|
46
|
+
window.history.replaceState({}, "", url.toString());
|
|
47
|
+
return urlToken;
|
|
48
|
+
}
|
|
49
|
+
return sessionStorage.getItem(options.storageKey);
|
|
50
|
+
}
|
|
51
|
+
function activateEditMode(token, options) {
|
|
52
|
+
injectStyles();
|
|
53
|
+
showActiveBadge(token);
|
|
54
|
+
const fields = document.querySelectorAll("[data-cms-field]");
|
|
55
|
+
fields.forEach((el) => wireField(el, token, options));
|
|
56
|
+
}
|
|
57
|
+
function wireField(el, token, options) {
|
|
58
|
+
el.addEventListener("click", () => {
|
|
59
|
+
if (el.getAttribute("contenteditable") === "true") return;
|
|
60
|
+
el.dataset.cmsOriginalValue = el.textContent ?? "";
|
|
61
|
+
el.setAttribute("contenteditable", "true");
|
|
62
|
+
el.focus();
|
|
63
|
+
});
|
|
64
|
+
el.addEventListener("blur", () => {
|
|
65
|
+
el.removeAttribute("contenteditable");
|
|
66
|
+
const original = el.dataset.cmsOriginalValue ?? "";
|
|
67
|
+
const current = el.textContent ?? "";
|
|
68
|
+
if (current.trim() === original.trim()) return;
|
|
69
|
+
void saveField(el, token, options);
|
|
70
|
+
});
|
|
71
|
+
el.addEventListener("paste", (e) => {
|
|
72
|
+
e.preventDefault();
|
|
73
|
+
const text = e.clipboardData?.getData("text/plain") ?? "";
|
|
74
|
+
document.execCommand("insertText", false, text);
|
|
75
|
+
});
|
|
76
|
+
el.addEventListener("keydown", (e) => {
|
|
77
|
+
if (e.key === "Enter") {
|
|
78
|
+
e.preventDefault();
|
|
79
|
+
el.blur();
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
async function saveField(el, token, options) {
|
|
84
|
+
const collection = el.dataset.cmsCollection;
|
|
85
|
+
const slug = el.dataset.cmsSlug;
|
|
86
|
+
const field = el.dataset.cmsField;
|
|
87
|
+
if (!collection || !slug || !field) return;
|
|
88
|
+
const value = el.textContent?.trim() ?? "";
|
|
89
|
+
showPill(el, "saving");
|
|
90
|
+
try {
|
|
91
|
+
const getRes = await fetch(
|
|
92
|
+
`${options.cmsBaseUrl}/api/cms/${collection}/${slug}?site=${options.siteId}`,
|
|
93
|
+
{ headers: { Authorization: `Bearer ${token}` } }
|
|
94
|
+
);
|
|
95
|
+
if (!getRes.ok) throw new Error(`GET failed: ${getRes.status}`);
|
|
96
|
+
const doc = await getRes.json();
|
|
97
|
+
const mergedData = { ...doc.data, [field]: value };
|
|
98
|
+
const patchRes = await fetch(
|
|
99
|
+
`${options.cmsBaseUrl}/api/cms/${collection}/${slug}?site=${options.siteId}`,
|
|
100
|
+
{
|
|
101
|
+
method: "PATCH",
|
|
102
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
|
|
103
|
+
body: JSON.stringify({ data: mergedData })
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
if (!patchRes.ok) throw new Error(`PATCH failed: ${patchRes.status}`);
|
|
107
|
+
showPill(el, "saved");
|
|
108
|
+
} catch {
|
|
109
|
+
showPill(el, "error");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
var pills = /* @__PURE__ */ new WeakMap();
|
|
113
|
+
function showPill(el, state) {
|
|
114
|
+
let pill = pills.get(el);
|
|
115
|
+
if (!pill) {
|
|
116
|
+
pill = document.createElement("span");
|
|
117
|
+
pill.setAttribute("data-cms-inline-edit-pill", "");
|
|
118
|
+
document.body.appendChild(pill);
|
|
119
|
+
pills.set(el, pill);
|
|
120
|
+
}
|
|
121
|
+
const rect = el.getBoundingClientRect();
|
|
122
|
+
pill.style.cssText = `position:fixed;top:${rect.top - 26}px;left:${rect.left}px;font:600 11px system-ui,sans-serif;padding:3px 9px;border-radius:5px;z-index:2147483647;pointer-events:none;box-shadow:0 2px 8px rgba(0,0,0,.25);`;
|
|
123
|
+
if (state === "saving") {
|
|
124
|
+
pill.textContent = "Gemmer\u2026";
|
|
125
|
+
pill.style.background = "#1c2027";
|
|
126
|
+
pill.style.color = "#fff";
|
|
127
|
+
} else if (state === "saved") {
|
|
128
|
+
pill.textContent = "Gemt \u2713";
|
|
129
|
+
pill.style.background = "#16a34a";
|
|
130
|
+
pill.style.color = "#fff";
|
|
131
|
+
setTimeout(() => {
|
|
132
|
+
pill?.remove();
|
|
133
|
+
pills.delete(el);
|
|
134
|
+
}, 1500);
|
|
135
|
+
} else {
|
|
136
|
+
pill.textContent = "Fejl \u2014 pr\xF8v igen";
|
|
137
|
+
pill.style.background = "#dc2626";
|
|
138
|
+
pill.style.color = "#fff";
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function showActiveBadge(token) {
|
|
142
|
+
const claims = decodeJwtPayload(token);
|
|
143
|
+
const name = claims?.name || claims?.email || "ukendt";
|
|
144
|
+
const badge = document.createElement("div");
|
|
145
|
+
badge.setAttribute("data-cms-inline-edit-badge", "");
|
|
146
|
+
badge.textContent = `\u270F\uFE0F Redigerer som ${name}`;
|
|
147
|
+
badge.style.cssText = "position:fixed;bottom:16px;left:16px;background:#1c2027;color:#fff;font:600 12px system-ui,sans-serif;padding:8px 14px;border-radius:999px;z-index:2147483647;box-shadow:0 4px 16px rgba(0,0,0,.3);";
|
|
148
|
+
document.body.appendChild(badge);
|
|
149
|
+
}
|
|
150
|
+
function decodeJwtPayload(token) {
|
|
151
|
+
try {
|
|
152
|
+
const payload = token.split(".")[1];
|
|
153
|
+
if (!payload) return null;
|
|
154
|
+
return JSON.parse(atob(payload.replace(/-/g, "+").replace(/_/g, "/")));
|
|
155
|
+
} catch {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function injectStyles() {
|
|
160
|
+
const style = document.createElement("style");
|
|
161
|
+
style.textContent = `
|
|
162
|
+
[data-cms-field] { outline: 1px dashed transparent; outline-offset: 2px; cursor: text; transition: outline-color .15s; }
|
|
163
|
+
[data-cms-field]:hover { outline-color: rgba(0,178,255,.5); }
|
|
164
|
+
[data-cms-field][contenteditable="true"] { outline: 2px solid #00b2ff; outline-offset: 2px; }
|
|
165
|
+
`;
|
|
166
|
+
document.head.appendChild(style);
|
|
167
|
+
}
|
|
168
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
169
|
+
0 && (module.exports = {
|
|
170
|
+
initInlineEdit
|
|
171
|
+
});
|
|
172
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Browser entry — zero dependencies. Activates click-to-edit on any element\n * carrying data-cms-collection/data-cms-slug/data-cms-field (F129's attribute\n * convention), when a valid edit-session token is present.\n */\n\nexport interface InlineEditOptions {\n /** Base URL of the CMS API, e.g. \"https://webhouse.app\". */\n cmsBaseUrl: string;\n /** Site id passed as ?site= on every CMS API call, e.g. \"broberg-ai\". */\n siteId: string;\n /** URL query param carrying the short-lived edit-session token. Default \"cms_edit\". */\n tokenParam?: string;\n /** sessionStorage key the token is persisted under across page loads. Default \"wh-inline-edit-token\". */\n storageKey?: string;\n}\n\ninterface ResolvedOptions extends Required<InlineEditOptions> {}\n\nfunction resolveOptions(options: InlineEditOptions): ResolvedOptions {\n return {\n tokenParam: \"cms_edit\",\n storageKey: \"wh-inline-edit-token\",\n ...options,\n };\n}\n\nexport function initInlineEdit(options: InlineEditOptions): void {\n if (typeof window === \"undefined\") return;\n const resolved = resolveOptions(options);\n const token = bootstrapToken(resolved);\n if (!token) return;\n activateEditMode(token, resolved);\n}\n\nfunction bootstrapToken(options: ResolvedOptions): string | null {\n const url = new URL(window.location.href);\n const urlToken = url.searchParams.get(options.tokenParam);\n if (urlToken) {\n sessionStorage.setItem(options.storageKey, urlToken);\n url.searchParams.delete(options.tokenParam);\n window.history.replaceState({}, \"\", url.toString());\n return urlToken;\n }\n return sessionStorage.getItem(options.storageKey);\n}\n\nfunction activateEditMode(token: string, options: ResolvedOptions): void {\n injectStyles();\n showActiveBadge(token);\n\n const fields = document.querySelectorAll<HTMLElement>(\"[data-cms-field]\");\n fields.forEach((el) => wireField(el, token, options));\n}\n\nfunction wireField(el: HTMLElement, token: string, options: ResolvedOptions): void {\n el.addEventListener(\"click\", () => {\n if (el.getAttribute(\"contenteditable\") === \"true\") return;\n el.dataset.cmsOriginalValue = el.textContent ?? \"\";\n el.setAttribute(\"contenteditable\", \"true\");\n el.focus();\n });\n\n el.addEventListener(\"blur\", () => {\n el.removeAttribute(\"contenteditable\");\n const original = el.dataset.cmsOriginalValue ?? \"\";\n const current = el.textContent ?? \"\";\n if (current.trim() === original.trim()) return;\n void saveField(el, token, options);\n });\n\n el.addEventListener(\"paste\", (e) => {\n e.preventDefault();\n const text = e.clipboardData?.getData(\"text/plain\") ?? \"\";\n document.execCommand(\"insertText\", false, text);\n });\n\n el.addEventListener(\"keydown\", (e) => {\n if (e.key === \"Enter\") {\n e.preventDefault();\n el.blur();\n }\n });\n}\n\nasync function saveField(el: HTMLElement, token: string, options: ResolvedOptions): Promise<void> {\n const collection = el.dataset.cmsCollection;\n const slug = el.dataset.cmsSlug;\n const field = el.dataset.cmsField;\n if (!collection || !slug || !field) return;\n const value = el.textContent?.trim() ?? \"\";\n\n showPill(el, \"saving\");\n try {\n const getRes = await fetch(\n `${options.cmsBaseUrl}/api/cms/${collection}/${slug}?site=${options.siteId}`,\n { headers: { Authorization: `Bearer ${token}` } },\n );\n if (!getRes.ok) throw new Error(`GET failed: ${getRes.status}`);\n const doc = (await getRes.json()) as { data?: Record<string, unknown> };\n const mergedData = { ...doc.data, [field]: value };\n\n const patchRes = await fetch(\n `${options.cmsBaseUrl}/api/cms/${collection}/${slug}?site=${options.siteId}`,\n {\n method: \"PATCH\",\n headers: { Authorization: `Bearer ${token}`, \"Content-Type\": \"application/json\" },\n body: JSON.stringify({ data: mergedData }),\n },\n );\n if (!patchRes.ok) throw new Error(`PATCH failed: ${patchRes.status}`);\n showPill(el, \"saved\");\n } catch {\n showPill(el, \"error\");\n }\n}\n\nconst pills = new WeakMap<HTMLElement, HTMLElement>();\n\nfunction showPill(el: HTMLElement, state: \"saving\" | \"saved\" | \"error\"): void {\n let pill = pills.get(el);\n if (!pill) {\n pill = document.createElement(\"span\");\n pill.setAttribute(\"data-cms-inline-edit-pill\", \"\");\n document.body.appendChild(pill);\n pills.set(el, pill);\n }\n const rect = el.getBoundingClientRect();\n pill.style.cssText =\n `position:fixed;top:${rect.top - 26}px;left:${rect.left}px;font:600 11px system-ui,sans-serif;` +\n `padding:3px 9px;border-radius:5px;z-index:2147483647;pointer-events:none;` +\n `box-shadow:0 2px 8px rgba(0,0,0,.25);`;\n\n if (state === \"saving\") {\n pill.textContent = \"Gemmer…\";\n pill.style.background = \"#1c2027\";\n pill.style.color = \"#fff\";\n } else if (state === \"saved\") {\n pill.textContent = \"Gemt ✓\";\n pill.style.background = \"#16a34a\";\n pill.style.color = \"#fff\";\n setTimeout(() => {\n pill?.remove();\n pills.delete(el);\n }, 1500);\n } else {\n pill.textContent = \"Fejl — prøv igen\";\n pill.style.background = \"#dc2626\";\n pill.style.color = \"#fff\";\n }\n}\n\nfunction showActiveBadge(token: string): void {\n const claims = decodeJwtPayload(token);\n const name = (claims?.name as string) || (claims?.email as string) || \"ukendt\";\n const badge = document.createElement(\"div\");\n badge.setAttribute(\"data-cms-inline-edit-badge\", \"\");\n badge.textContent = `✏️ Redigerer som ${name}`;\n badge.style.cssText =\n \"position:fixed;bottom:16px;left:16px;background:#1c2027;color:#fff;\" +\n \"font:600 12px system-ui,sans-serif;padding:8px 14px;border-radius:999px;\" +\n \"z-index:2147483647;box-shadow:0 4px 16px rgba(0,0,0,.3);\";\n document.body.appendChild(badge);\n}\n\n/** Reads JWT claims for DISPLAY ONLY — never used for authorization decisions. */\nfunction decodeJwtPayload(token: string): Record<string, unknown> | null {\n try {\n const payload = token.split(\".\")[1];\n if (!payload) return null;\n return JSON.parse(atob(payload.replace(/-/g, \"+\").replace(/_/g, \"/\")));\n } catch {\n return null;\n }\n}\n\nfunction injectStyles(): void {\n const style = document.createElement(\"style\");\n style.textContent = `\n [data-cms-field] { outline: 1px dashed transparent; outline-offset: 2px; cursor: text; transition: outline-color .15s; }\n [data-cms-field]:hover { outline-color: rgba(0,178,255,.5); }\n [data-cms-field][contenteditable=\"true\"] { outline: 2px solid #00b2ff; outline-offset: 2px; }\n `;\n document.head.appendChild(style);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBA,SAAS,eAAe,SAA6C;AACnE,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,YAAY;AAAA,IACZ,GAAG;AAAA,EACL;AACF;AAEO,SAAS,eAAe,SAAkC;AAC/D,MAAI,OAAO,WAAW,YAAa;AACnC,QAAM,WAAW,eAAe,OAAO;AACvC,QAAM,QAAQ,eAAe,QAAQ;AACrC,MAAI,CAAC,MAAO;AACZ,mBAAiB,OAAO,QAAQ;AAClC;AAEA,SAAS,eAAe,SAAyC;AAC/D,QAAM,MAAM,IAAI,IAAI,OAAO,SAAS,IAAI;AACxC,QAAM,WAAW,IAAI,aAAa,IAAI,QAAQ,UAAU;AACxD,MAAI,UAAU;AACZ,mBAAe,QAAQ,QAAQ,YAAY,QAAQ;AACnD,QAAI,aAAa,OAAO,QAAQ,UAAU;AAC1C,WAAO,QAAQ,aAAa,CAAC,GAAG,IAAI,IAAI,SAAS,CAAC;AAClD,WAAO;AAAA,EACT;AACA,SAAO,eAAe,QAAQ,QAAQ,UAAU;AAClD;AAEA,SAAS,iBAAiB,OAAe,SAAgC;AACvE,eAAa;AACb,kBAAgB,KAAK;AAErB,QAAM,SAAS,SAAS,iBAA8B,kBAAkB;AACxE,SAAO,QAAQ,CAAC,OAAO,UAAU,IAAI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T,OAAO;AACL,SAAK,cAAc;AACnB,SAAK,MAAM,aAAa;AACxB,SAAK,MAAM,QAAQ;AAAA,EACrB;AACF;AAEA,SAAS,gBAAgB,OAAqB;AAC5C,QAAM,SAAS,iBAAiB,KAAK;AACrC,QAAM,OAAQ,QAAQ,QAAoB,QAAQ,SAAoB;AACtE,QAAM,QAAQ,SAAS,cAAc,KAAK;AAC1C,QAAM,aAAa,8BAA8B,EAAE;AACnD,QAAM,cAAc,8BAAoB,IAAI;AAC5C,QAAM,MAAM,UACV;AAGF,WAAS,KAAK,YAAY,KAAK;AACjC;AAGA,SAAS,iBAAiB,OAA+C;AACvE,MAAI;AACF,UAAM,UAAU,MAAM,MAAM,GAAG,EAAE,CAAC;AAClC,QAAI,CAAC,QAAS,QAAO;AACrB,WAAO,KAAK,MAAM,KAAK,QAAQ,QAAQ,MAAM,GAAG,EAAE,QAAQ,MAAM,GAAG,CAAC,CAAC;AAAA,EACvE,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,SAAS,eAAqB;AAC5B,QAAM,QAAQ,SAAS,cAAc,OAAO;AAC5C,QAAM,cAAc;AAAA;AAAA;AAAA;AAAA;AAKpB,WAAS,KAAK,YAAY,KAAK;AACjC;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser entry — zero dependencies. Activates click-to-edit on any element
|
|
3
|
+
* carrying data-cms-collection/data-cms-slug/data-cms-field (F129's attribute
|
|
4
|
+
* convention), when a valid edit-session token is present.
|
|
5
|
+
*/
|
|
6
|
+
interface InlineEditOptions {
|
|
7
|
+
/** Base URL of the CMS API, e.g. "https://webhouse.app". */
|
|
8
|
+
cmsBaseUrl: string;
|
|
9
|
+
/** Site id passed as ?site= on every CMS API call, e.g. "broberg-ai". */
|
|
10
|
+
siteId: string;
|
|
11
|
+
/** URL query param carrying the short-lived edit-session token. Default "cms_edit". */
|
|
12
|
+
tokenParam?: string;
|
|
13
|
+
/** sessionStorage key the token is persisted under across page loads. Default "wh-inline-edit-token". */
|
|
14
|
+
storageKey?: string;
|
|
15
|
+
}
|
|
16
|
+
declare function initInlineEdit(options: InlineEditOptions): void;
|
|
17
|
+
|
|
18
|
+
export { type InlineEditOptions, initInlineEdit };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser entry — zero dependencies. Activates click-to-edit on any element
|
|
3
|
+
* carrying data-cms-collection/data-cms-slug/data-cms-field (F129's attribute
|
|
4
|
+
* convention), when a valid edit-session token is present.
|
|
5
|
+
*/
|
|
6
|
+
interface InlineEditOptions {
|
|
7
|
+
/** Base URL of the CMS API, e.g. "https://webhouse.app". */
|
|
8
|
+
cmsBaseUrl: string;
|
|
9
|
+
/** Site id passed as ?site= on every CMS API call, e.g. "broberg-ai". */
|
|
10
|
+
siteId: string;
|
|
11
|
+
/** URL query param carrying the short-lived edit-session token. Default "cms_edit". */
|
|
12
|
+
tokenParam?: string;
|
|
13
|
+
/** sessionStorage key the token is persisted under across page loads. Default "wh-inline-edit-token". */
|
|
14
|
+
storageKey?: string;
|
|
15
|
+
}
|
|
16
|
+
declare function initInlineEdit(options: InlineEditOptions): void;
|
|
17
|
+
|
|
18
|
+
export { type InlineEditOptions, initInlineEdit };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function resolveOptions(options) {
|
|
3
|
+
return {
|
|
4
|
+
tokenParam: "cms_edit",
|
|
5
|
+
storageKey: "wh-inline-edit-token",
|
|
6
|
+
...options
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function initInlineEdit(options) {
|
|
10
|
+
if (typeof window === "undefined") return;
|
|
11
|
+
const resolved = resolveOptions(options);
|
|
12
|
+
const token = bootstrapToken(resolved);
|
|
13
|
+
if (!token) return;
|
|
14
|
+
activateEditMode(token, resolved);
|
|
15
|
+
}
|
|
16
|
+
function bootstrapToken(options) {
|
|
17
|
+
const url = new URL(window.location.href);
|
|
18
|
+
const urlToken = url.searchParams.get(options.tokenParam);
|
|
19
|
+
if (urlToken) {
|
|
20
|
+
sessionStorage.setItem(options.storageKey, urlToken);
|
|
21
|
+
url.searchParams.delete(options.tokenParam);
|
|
22
|
+
window.history.replaceState({}, "", url.toString());
|
|
23
|
+
return urlToken;
|
|
24
|
+
}
|
|
25
|
+
return sessionStorage.getItem(options.storageKey);
|
|
26
|
+
}
|
|
27
|
+
function activateEditMode(token, options) {
|
|
28
|
+
injectStyles();
|
|
29
|
+
showActiveBadge(token);
|
|
30
|
+
const fields = document.querySelectorAll("[data-cms-field]");
|
|
31
|
+
fields.forEach((el) => wireField(el, token, options));
|
|
32
|
+
}
|
|
33
|
+
function wireField(el, token, options) {
|
|
34
|
+
el.addEventListener("click", () => {
|
|
35
|
+
if (el.getAttribute("contenteditable") === "true") return;
|
|
36
|
+
el.dataset.cmsOriginalValue = el.textContent ?? "";
|
|
37
|
+
el.setAttribute("contenteditable", "true");
|
|
38
|
+
el.focus();
|
|
39
|
+
});
|
|
40
|
+
el.addEventListener("blur", () => {
|
|
41
|
+
el.removeAttribute("contenteditable");
|
|
42
|
+
const original = el.dataset.cmsOriginalValue ?? "";
|
|
43
|
+
const current = el.textContent ?? "";
|
|
44
|
+
if (current.trim() === original.trim()) return;
|
|
45
|
+
void saveField(el, token, options);
|
|
46
|
+
});
|
|
47
|
+
el.addEventListener("paste", (e) => {
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
const text = e.clipboardData?.getData("text/plain") ?? "";
|
|
50
|
+
document.execCommand("insertText", false, text);
|
|
51
|
+
});
|
|
52
|
+
el.addEventListener("keydown", (e) => {
|
|
53
|
+
if (e.key === "Enter") {
|
|
54
|
+
e.preventDefault();
|
|
55
|
+
el.blur();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
async function saveField(el, token, options) {
|
|
60
|
+
const collection = el.dataset.cmsCollection;
|
|
61
|
+
const slug = el.dataset.cmsSlug;
|
|
62
|
+
const field = el.dataset.cmsField;
|
|
63
|
+
if (!collection || !slug || !field) return;
|
|
64
|
+
const value = el.textContent?.trim() ?? "";
|
|
65
|
+
showPill(el, "saving");
|
|
66
|
+
try {
|
|
67
|
+
const getRes = await fetch(
|
|
68
|
+
`${options.cmsBaseUrl}/api/cms/${collection}/${slug}?site=${options.siteId}`,
|
|
69
|
+
{ headers: { Authorization: `Bearer ${token}` } }
|
|
70
|
+
);
|
|
71
|
+
if (!getRes.ok) throw new Error(`GET failed: ${getRes.status}`);
|
|
72
|
+
const doc = await getRes.json();
|
|
73
|
+
const mergedData = { ...doc.data, [field]: value };
|
|
74
|
+
const patchRes = await fetch(
|
|
75
|
+
`${options.cmsBaseUrl}/api/cms/${collection}/${slug}?site=${options.siteId}`,
|
|
76
|
+
{
|
|
77
|
+
method: "PATCH",
|
|
78
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
|
|
79
|
+
body: JSON.stringify({ data: mergedData })
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
if (!patchRes.ok) throw new Error(`PATCH failed: ${patchRes.status}`);
|
|
83
|
+
showPill(el, "saved");
|
|
84
|
+
} catch {
|
|
85
|
+
showPill(el, "error");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
var pills = /* @__PURE__ */ new WeakMap();
|
|
89
|
+
function showPill(el, state) {
|
|
90
|
+
let pill = pills.get(el);
|
|
91
|
+
if (!pill) {
|
|
92
|
+
pill = document.createElement("span");
|
|
93
|
+
pill.setAttribute("data-cms-inline-edit-pill", "");
|
|
94
|
+
document.body.appendChild(pill);
|
|
95
|
+
pills.set(el, pill);
|
|
96
|
+
}
|
|
97
|
+
const rect = el.getBoundingClientRect();
|
|
98
|
+
pill.style.cssText = `position:fixed;top:${rect.top - 26}px;left:${rect.left}px;font:600 11px system-ui,sans-serif;padding:3px 9px;border-radius:5px;z-index:2147483647;pointer-events:none;box-shadow:0 2px 8px rgba(0,0,0,.25);`;
|
|
99
|
+
if (state === "saving") {
|
|
100
|
+
pill.textContent = "Gemmer\u2026";
|
|
101
|
+
pill.style.background = "#1c2027";
|
|
102
|
+
pill.style.color = "#fff";
|
|
103
|
+
} else if (state === "saved") {
|
|
104
|
+
pill.textContent = "Gemt \u2713";
|
|
105
|
+
pill.style.background = "#16a34a";
|
|
106
|
+
pill.style.color = "#fff";
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
pill?.remove();
|
|
109
|
+
pills.delete(el);
|
|
110
|
+
}, 1500);
|
|
111
|
+
} else {
|
|
112
|
+
pill.textContent = "Fejl \u2014 pr\xF8v igen";
|
|
113
|
+
pill.style.background = "#dc2626";
|
|
114
|
+
pill.style.color = "#fff";
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function showActiveBadge(token) {
|
|
118
|
+
const claims = decodeJwtPayload(token);
|
|
119
|
+
const name = claims?.name || claims?.email || "ukendt";
|
|
120
|
+
const badge = document.createElement("div");
|
|
121
|
+
badge.setAttribute("data-cms-inline-edit-badge", "");
|
|
122
|
+
badge.textContent = `\u270F\uFE0F Redigerer som ${name}`;
|
|
123
|
+
badge.style.cssText = "position:fixed;bottom:16px;left:16px;background:#1c2027;color:#fff;font:600 12px system-ui,sans-serif;padding:8px 14px;border-radius:999px;z-index:2147483647;box-shadow:0 4px 16px rgba(0,0,0,.3);";
|
|
124
|
+
document.body.appendChild(badge);
|
|
125
|
+
}
|
|
126
|
+
function decodeJwtPayload(token) {
|
|
127
|
+
try {
|
|
128
|
+
const payload = token.split(".")[1];
|
|
129
|
+
if (!payload) return null;
|
|
130
|
+
return JSON.parse(atob(payload.replace(/-/g, "+").replace(/_/g, "/")));
|
|
131
|
+
} catch {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function injectStyles() {
|
|
136
|
+
const style = document.createElement("style");
|
|
137
|
+
style.textContent = `
|
|
138
|
+
[data-cms-field] { outline: 1px dashed transparent; outline-offset: 2px; cursor: text; transition: outline-color .15s; }
|
|
139
|
+
[data-cms-field]:hover { outline-color: rgba(0,178,255,.5); }
|
|
140
|
+
[data-cms-field][contenteditable="true"] { outline: 2px solid #00b2ff; outline-offset: 2px; }
|
|
141
|
+
`;
|
|
142
|
+
document.head.appendChild(style);
|
|
143
|
+
}
|
|
144
|
+
export {
|
|
145
|
+
initInlineEdit
|
|
146
|
+
};
|
|
147
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Browser entry — zero dependencies. Activates click-to-edit on any element\n * carrying data-cms-collection/data-cms-slug/data-cms-field (F129's attribute\n * convention), when a valid edit-session token is present.\n */\n\nexport interface InlineEditOptions {\n /** Base URL of the CMS API, e.g. \"https://webhouse.app\". */\n cmsBaseUrl: string;\n /** Site id passed as ?site= on every CMS API call, e.g. \"broberg-ai\". */\n siteId: string;\n /** URL query param carrying the short-lived edit-session token. Default \"cms_edit\". */\n tokenParam?: string;\n /** sessionStorage key the token is persisted under across page loads. Default \"wh-inline-edit-token\". */\n storageKey?: string;\n}\n\ninterface ResolvedOptions extends Required<InlineEditOptions> {}\n\nfunction resolveOptions(options: InlineEditOptions): ResolvedOptions {\n return {\n tokenParam: \"cms_edit\",\n storageKey: \"wh-inline-edit-token\",\n ...options,\n };\n}\n\nexport function initInlineEdit(options: InlineEditOptions): void {\n if (typeof window === \"undefined\") return;\n const resolved = resolveOptions(options);\n const token = bootstrapToken(resolved);\n if (!token) return;\n activateEditMode(token, resolved);\n}\n\nfunction bootstrapToken(options: ResolvedOptions): string | null {\n const url = new URL(window.location.href);\n const urlToken = url.searchParams.get(options.tokenParam);\n if (urlToken) {\n sessionStorage.setItem(options.storageKey, urlToken);\n url.searchParams.delete(options.tokenParam);\n window.history.replaceState({}, \"\", url.toString());\n return urlToken;\n }\n return sessionStorage.getItem(options.storageKey);\n}\n\nfunction activateEditMode(token: string, options: ResolvedOptions): void {\n injectStyles();\n showActiveBadge(token);\n\n const fields = document.querySelectorAll<HTMLElement>(\"[data-cms-field]\");\n fields.forEach((el) => wireField(el, token, options));\n}\n\nfunction wireField(el: HTMLElement, token: string, options: ResolvedOptions): void {\n el.addEventListener(\"click\", () => {\n if (el.getAttribute(\"contenteditable\") === \"true\") return;\n el.dataset.cmsOriginalValue = el.textContent ?? \"\";\n el.setAttribute(\"contenteditable\", \"true\");\n el.focus();\n });\n\n el.addEventListener(\"blur\", () => {\n el.removeAttribute(\"contenteditable\");\n const original = el.dataset.cmsOriginalValue ?? \"\";\n const current = el.textContent ?? \"\";\n if (current.trim() === original.trim()) return;\n void saveField(el, token, options);\n });\n\n el.addEventListener(\"paste\", (e) => {\n e.preventDefault();\n const text = e.clipboardData?.getData(\"text/plain\") ?? \"\";\n document.execCommand(\"insertText\", false, text);\n });\n\n el.addEventListener(\"keydown\", (e) => {\n if (e.key === \"Enter\") {\n e.preventDefault();\n el.blur();\n }\n });\n}\n\nasync function saveField(el: HTMLElement, token: string, options: ResolvedOptions): Promise<void> {\n const collection = el.dataset.cmsCollection;\n const slug = el.dataset.cmsSlug;\n const field = el.dataset.cmsField;\n if (!collection || !slug || !field) return;\n const value = el.textContent?.trim() ?? \"\";\n\n showPill(el, \"saving\");\n try {\n const getRes = await fetch(\n `${options.cmsBaseUrl}/api/cms/${collection}/${slug}?site=${options.siteId}`,\n { headers: { Authorization: `Bearer ${token}` } },\n );\n if (!getRes.ok) throw new Error(`GET failed: ${getRes.status}`);\n const doc = (await getRes.json()) as { data?: Record<string, unknown> };\n const mergedData = { ...doc.data, [field]: value };\n\n const patchRes = await fetch(\n `${options.cmsBaseUrl}/api/cms/${collection}/${slug}?site=${options.siteId}`,\n {\n method: \"PATCH\",\n headers: { Authorization: `Bearer ${token}`, \"Content-Type\": \"application/json\" },\n body: JSON.stringify({ data: mergedData }),\n },\n );\n if (!patchRes.ok) throw new Error(`PATCH failed: ${patchRes.status}`);\n showPill(el, \"saved\");\n } catch {\n showPill(el, \"error\");\n }\n}\n\nconst pills = new WeakMap<HTMLElement, HTMLElement>();\n\nfunction showPill(el: HTMLElement, state: \"saving\" | \"saved\" | \"error\"): void {\n let pill = pills.get(el);\n if (!pill) {\n pill = document.createElement(\"span\");\n pill.setAttribute(\"data-cms-inline-edit-pill\", \"\");\n document.body.appendChild(pill);\n pills.set(el, pill);\n }\n const rect = el.getBoundingClientRect();\n pill.style.cssText =\n `position:fixed;top:${rect.top - 26}px;left:${rect.left}px;font:600 11px system-ui,sans-serif;` +\n `padding:3px 9px;border-radius:5px;z-index:2147483647;pointer-events:none;` +\n `box-shadow:0 2px 8px rgba(0,0,0,.25);`;\n\n if (state === \"saving\") {\n pill.textContent = \"Gemmer…\";\n pill.style.background = \"#1c2027\";\n pill.style.color = \"#fff\";\n } else if (state === \"saved\") {\n pill.textContent = \"Gemt ✓\";\n pill.style.background = \"#16a34a\";\n pill.style.color = \"#fff\";\n setTimeout(() => {\n pill?.remove();\n pills.delete(el);\n }, 1500);\n } else {\n pill.textContent = \"Fejl — prøv igen\";\n pill.style.background = \"#dc2626\";\n pill.style.color = \"#fff\";\n }\n}\n\nfunction showActiveBadge(token: string): void {\n const claims = decodeJwtPayload(token);\n const name = (claims?.name as string) || (claims?.email as string) || \"ukendt\";\n const badge = document.createElement(\"div\");\n badge.setAttribute(\"data-cms-inline-edit-badge\", \"\");\n badge.textContent = `✏️ Redigerer som ${name}`;\n badge.style.cssText =\n \"position:fixed;bottom:16px;left:16px;background:#1c2027;color:#fff;\" +\n \"font:600 12px system-ui,sans-serif;padding:8px 14px;border-radius:999px;\" +\n \"z-index:2147483647;box-shadow:0 4px 16px rgba(0,0,0,.3);\";\n document.body.appendChild(badge);\n}\n\n/** Reads JWT claims for DISPLAY ONLY — never used for authorization decisions. */\nfunction decodeJwtPayload(token: string): Record<string, unknown> | null {\n try {\n const payload = token.split(\".\")[1];\n if (!payload) return null;\n return JSON.parse(atob(payload.replace(/-/g, \"+\").replace(/_/g, \"/\")));\n } catch {\n return null;\n }\n}\n\nfunction injectStyles(): void {\n const style = document.createElement(\"style\");\n style.textContent = `\n [data-cms-field] { outline: 1px dashed transparent; outline-offset: 2px; cursor: text; transition: outline-color .15s; }\n [data-cms-field]:hover { outline-color: rgba(0,178,255,.5); }\n [data-cms-field][contenteditable=\"true\"] { outline: 2px solid #00b2ff; outline-offset: 2px; }\n `;\n document.head.appendChild(style);\n}\n"],"mappings":";AAmBA,SAAS,eAAe,SAA6C;AACnE,SAAO;AAAA,IACL,YAAY;AAAA,IACZ,YAAY;AAAA,IACZ,GAAG;AAAA,EACL;AACF;AAEO,SAAS,eAAe,SAAkC;AAC/D,MAAI,OAAO,WAAW,YAAa;AACnC,QAAM,WAAW,eAAe,OAAO;AACvC,QAAM,QAAQ,eAAe,QAAQ;AACrC,MAAI,CAAC,MAAO;AACZ,mBAAiB,OAAO,QAAQ;AAClC;AAEA,SAAS,eAAe,SAAyC;AAC/D,QAAM,MAAM,IAAI,IAAI,OAAO,SAAS,IAAI;AACxC,QAAM,WAAW,IAAI,aAAa,IAAI,QAAQ,UAAU;AACxD,MAAI,UAAU;AACZ,mBAAe,QAAQ,QAAQ,YAAY,QAAQ;AACnD,QAAI,aAAa,OAAO,QAAQ,UAAU;AAC1C,WAAO,QAAQ,aAAa,CAAC,GAAG,IAAI,IAAI,SAAS,CAAC;AAClD,WAAO;AAAA,EACT;AACA,SAAO,eAAe,QAAQ,QAAQ,UAAU;AAClD;AAEA,SAAS,iBAAiB,OAAe,SAAgC;AACvE,eAAa;AACb,kBAAgB,KAAK;AAErB,QAAM,SAAS,SAAS,iBAA8B,kBAAkB;AACxE,SAAO,QAAQ,CAAC,OAAO,UAAU,IAAI,OAAO,OAAO,CAAC;AACtD;AAEA,SAAS,UAAU,IAAiB,OAAe,SAAgC;AACjF,KAAG,iBAAiB,SAAS,MAAM;AACjC,QAAI,GAAG,aAAa,iBAAiB,MAAM,OAAQ;AACnD,OAAG,QAAQ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AAM,OAAQ,QAAQ,QAAoB,QAAQ,SAAoB;AACtE,QAAM,QAAQ,SAAS,cAAc,KAAK;AAC1C,QAAM,aAAa,8BAA8B,EAAE;AACnD,QAAM,cAAc,8BAAoB,IAAI;AAC5C,QAAM,MAAM,UACV;AAGF,WAAS,KAAK,YAAY,KAAK;AACjC;AAGA,SAAS,iBAAiB,OAA+C;AACvE,MAAI;AACF,UAAM,UAAU,MAAM,MAAM,GAAG,EAAE,CAAC;AAClC,QAAI,CAAC,QAAS,QAAO;AACrB,WAAO,KAAK,MAAM,KAAK,QAAQ,QAAQ,MAAM,GAAG,EAAE,QAAQ,MAAM,GAAG,CAAC,CAAC;AAAA,EACvE,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,SAAS,eAAqB;AAC5B,QAAM,QAAQ,SAAS,cAAc,OAAO;AAC5C,QAAM,cAAc;AAAA;AAAA;AAAA;AAAA;AAKpB,WAAS,KAAK,YAAY,KAAK;AACjC;","names":[]}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/server/index.ts
|
|
21
|
+
var server_exports = {};
|
|
22
|
+
__export(server_exports, {
|
|
23
|
+
saveInlineEditField: () => saveInlineEditField,
|
|
24
|
+
verifyEditSession: () => verifyEditSession
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(server_exports);
|
|
27
|
+
async function saveInlineEditField(options) {
|
|
28
|
+
const { cmsBaseUrl, siteId, collection, slug, field, value, sessionToken } = options;
|
|
29
|
+
const headers = { Authorization: `Bearer ${sessionToken}` };
|
|
30
|
+
const getRes = await fetch(`${cmsBaseUrl}/api/cms/${collection}/${slug}?site=${siteId}`, {
|
|
31
|
+
headers
|
|
32
|
+
});
|
|
33
|
+
if (!getRes.ok) return { ok: false, error: `GET failed: ${getRes.status}` };
|
|
34
|
+
const doc = await getRes.json();
|
|
35
|
+
const mergedData = { ...doc.data, [field]: value };
|
|
36
|
+
const patchRes = await fetch(`${cmsBaseUrl}/api/cms/${collection}/${slug}?site=${siteId}`, {
|
|
37
|
+
method: "PATCH",
|
|
38
|
+
headers: { ...headers, "Content-Type": "application/json" },
|
|
39
|
+
body: JSON.stringify({ data: mergedData })
|
|
40
|
+
});
|
|
41
|
+
if (!patchRes.ok) return { ok: false, error: `PATCH failed: ${patchRes.status}` };
|
|
42
|
+
return { ok: true };
|
|
43
|
+
}
|
|
44
|
+
async function verifyEditSession(options) {
|
|
45
|
+
const res = await fetch(`${options.cmsBaseUrl}/api/auth/me`, {
|
|
46
|
+
headers: { Authorization: `Bearer ${options.token}` }
|
|
47
|
+
});
|
|
48
|
+
if (!res.ok) return null;
|
|
49
|
+
const body = await res.json();
|
|
50
|
+
return body.user ?? null;
|
|
51
|
+
}
|
|
52
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
53
|
+
0 && (module.exports = {
|
|
54
|
+
saveInlineEditField,
|
|
55
|
+
verifyEditSession
|
|
56
|
+
});
|
|
57
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/server/index.ts"],"sourcesContent":["/**\n * Optional server-side helpers (Node/Bun) for sites that want a same-origin\n * relay instead of calling the CMS directly from the browser. Not required\n * for the direct-from-browser flow used by initInlineEdit().\n */\n\nexport interface SaveInlineEditFieldOptions {\n cmsBaseUrl: string;\n siteId: string;\n collection: string;\n slug: string;\n field: string;\n value: string;\n sessionToken: string;\n}\n\nexport type SaveInlineEditFieldResult = { ok: true } | { ok: false; error: string };\n\n/** GET the doc, merge the changed field into .data, PATCH the full merged object back. */\nexport async function saveInlineEditField(\n options: SaveInlineEditFieldOptions,\n): Promise<SaveInlineEditFieldResult> {\n const { cmsBaseUrl, siteId, collection, slug, field, value, sessionToken } = options;\n const headers = { Authorization: `Bearer ${sessionToken}` };\n\n const getRes = await fetch(`${cmsBaseUrl}/api/cms/${collection}/${slug}?site=${siteId}`, {\n headers,\n });\n if (!getRes.ok) return { ok: false, error: `GET failed: ${getRes.status}` };\n const doc = (await getRes.json()) as { data?: Record<string, unknown> };\n const mergedData = { ...doc.data, [field]: value };\n\n const patchRes = await fetch(`${cmsBaseUrl}/api/cms/${collection}/${slug}?site=${siteId}`, {\n method: \"PATCH\",\n headers: { ...headers, \"Content-Type\": \"application/json\" },\n body: JSON.stringify({ data: mergedData }),\n });\n if (!patchRes.ok) return { ok: false, error: `PATCH failed: ${patchRes.status}` };\n return { ok: true };\n}\n\nexport interface VerifyEditSessionOptions {\n cmsBaseUrl: string;\n token: string;\n}\n\nexport interface EditSessionUser {\n sub: string;\n email: string;\n name: string;\n role: string;\n}\n\n/**\n * Calls the CMS's existing GET /api/auth/me with the token as a Bearer header.\n * That endpoint always returns 200 — {\"user\": null} for anonymous/invalid\n * tokens, never a 401 — so absence of a user is read from the body, not the\n * status code.\n */\nexport async function verifyEditSession(\n options: VerifyEditSessionOptions,\n): Promise<EditSessionUser | null> {\n const res = await fetch(`${options.cmsBaseUrl}/api/auth/me`, {\n headers: { Authorization: `Bearer ${options.token}` },\n });\n if (!res.ok) return null;\n const body = (await res.json()) as { user?: EditSessionUser | null };\n return body.user ?? null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBA,eAAsB,oBACpB,SACoC;AACpC,QAAM,EAAE,YAAY,QAAQ,YAAY,MAAM,OAAO,OAAO,aAAa,IAAI;AAC7E,QAAM,UAAU,EAAE,eAAe,UAAU,YAAY,GAAG;AAE1D,QAAM,SAAS,MAAM,MAAM,GAAG,UAAU,YAAY,UAAU,IAAI,IAAI,SAAS,MAAM,IAAI;AAAA,IACvF;AAAA,EACF,CAAC;AACD,MAAI,CAAC,OAAO,GAAI,QAAO,EAAE,IAAI,OAAO,OAAO,eAAe,OAAO,MAAM,GAAG;AAC1E,QAAM,MAAO,MAAM,OAAO,KAAK;AAC/B,QAAM,aAAa,EAAE,GAAG,IAAI,MAAM,CAAC,KAAK,GAAG,MAAM;AAEjD,QAAM,WAAW,MAAM,MAAM,GAAG,UAAU,YAAY,UAAU,IAAI,IAAI,SAAS,MAAM,IAAI;AAAA,IACzF,QAAQ;AAAA,IACR,SAAS,EAAE,GAAG,SAAS,gBAAgB,mBAAmB;AAAA,IAC1D,MAAM,KAAK,UAAU,EAAE,MAAM,WAAW,CAAC;AAAA,EAC3C,CAAC;AACD,MAAI,CAAC,SAAS,GAAI,QAAO,EAAE,IAAI,OAAO,OAAO,iBAAiB,SAAS,MAAM,GAAG;AAChF,SAAO,EAAE,IAAI,KAAK;AACpB;AAoBA,eAAsB,kBACpB,SACiC;AACjC,QAAM,MAAM,MAAM,MAAM,GAAG,QAAQ,UAAU,gBAAgB;AAAA,IAC3D,SAAS,EAAE,eAAe,UAAU,QAAQ,KAAK,GAAG;AAAA,EACtD,CAAC;AACD,MAAI,CAAC,IAAI,GAAI,QAAO;AACpB,QAAM,OAAQ,MAAM,IAAI,KAAK;AAC7B,SAAO,KAAK,QAAQ;AACtB;","names":[]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional server-side helpers (Node/Bun) for sites that want a same-origin
|
|
3
|
+
* relay instead of calling the CMS directly from the browser. Not required
|
|
4
|
+
* for the direct-from-browser flow used by initInlineEdit().
|
|
5
|
+
*/
|
|
6
|
+
interface SaveInlineEditFieldOptions {
|
|
7
|
+
cmsBaseUrl: string;
|
|
8
|
+
siteId: string;
|
|
9
|
+
collection: string;
|
|
10
|
+
slug: string;
|
|
11
|
+
field: string;
|
|
12
|
+
value: string;
|
|
13
|
+
sessionToken: string;
|
|
14
|
+
}
|
|
15
|
+
type SaveInlineEditFieldResult = {
|
|
16
|
+
ok: true;
|
|
17
|
+
} | {
|
|
18
|
+
ok: false;
|
|
19
|
+
error: string;
|
|
20
|
+
};
|
|
21
|
+
/** GET the doc, merge the changed field into .data, PATCH the full merged object back. */
|
|
22
|
+
declare function saveInlineEditField(options: SaveInlineEditFieldOptions): Promise<SaveInlineEditFieldResult>;
|
|
23
|
+
interface VerifyEditSessionOptions {
|
|
24
|
+
cmsBaseUrl: string;
|
|
25
|
+
token: string;
|
|
26
|
+
}
|
|
27
|
+
interface EditSessionUser {
|
|
28
|
+
sub: string;
|
|
29
|
+
email: string;
|
|
30
|
+
name: string;
|
|
31
|
+
role: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Calls the CMS's existing GET /api/auth/me with the token as a Bearer header.
|
|
35
|
+
* That endpoint always returns 200 — {"user": null} for anonymous/invalid
|
|
36
|
+
* tokens, never a 401 — so absence of a user is read from the body, not the
|
|
37
|
+
* status code.
|
|
38
|
+
*/
|
|
39
|
+
declare function verifyEditSession(options: VerifyEditSessionOptions): Promise<EditSessionUser | null>;
|
|
40
|
+
|
|
41
|
+
export { type EditSessionUser, type SaveInlineEditFieldOptions, type SaveInlineEditFieldResult, type VerifyEditSessionOptions, saveInlineEditField, verifyEditSession };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional server-side helpers (Node/Bun) for sites that want a same-origin
|
|
3
|
+
* relay instead of calling the CMS directly from the browser. Not required
|
|
4
|
+
* for the direct-from-browser flow used by initInlineEdit().
|
|
5
|
+
*/
|
|
6
|
+
interface SaveInlineEditFieldOptions {
|
|
7
|
+
cmsBaseUrl: string;
|
|
8
|
+
siteId: string;
|
|
9
|
+
collection: string;
|
|
10
|
+
slug: string;
|
|
11
|
+
field: string;
|
|
12
|
+
value: string;
|
|
13
|
+
sessionToken: string;
|
|
14
|
+
}
|
|
15
|
+
type SaveInlineEditFieldResult = {
|
|
16
|
+
ok: true;
|
|
17
|
+
} | {
|
|
18
|
+
ok: false;
|
|
19
|
+
error: string;
|
|
20
|
+
};
|
|
21
|
+
/** GET the doc, merge the changed field into .data, PATCH the full merged object back. */
|
|
22
|
+
declare function saveInlineEditField(options: SaveInlineEditFieldOptions): Promise<SaveInlineEditFieldResult>;
|
|
23
|
+
interface VerifyEditSessionOptions {
|
|
24
|
+
cmsBaseUrl: string;
|
|
25
|
+
token: string;
|
|
26
|
+
}
|
|
27
|
+
interface EditSessionUser {
|
|
28
|
+
sub: string;
|
|
29
|
+
email: string;
|
|
30
|
+
name: string;
|
|
31
|
+
role: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Calls the CMS's existing GET /api/auth/me with the token as a Bearer header.
|
|
35
|
+
* That endpoint always returns 200 — {"user": null} for anonymous/invalid
|
|
36
|
+
* tokens, never a 401 — so absence of a user is read from the body, not the
|
|
37
|
+
* status code.
|
|
38
|
+
*/
|
|
39
|
+
declare function verifyEditSession(options: VerifyEditSessionOptions): Promise<EditSessionUser | null>;
|
|
40
|
+
|
|
41
|
+
export { type EditSessionUser, type SaveInlineEditFieldOptions, type SaveInlineEditFieldResult, type VerifyEditSessionOptions, saveInlineEditField, verifyEditSession };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/server/index.ts
|
|
2
|
+
async function saveInlineEditField(options) {
|
|
3
|
+
const { cmsBaseUrl, siteId, collection, slug, field, value, sessionToken } = options;
|
|
4
|
+
const headers = { Authorization: `Bearer ${sessionToken}` };
|
|
5
|
+
const getRes = await fetch(`${cmsBaseUrl}/api/cms/${collection}/${slug}?site=${siteId}`, {
|
|
6
|
+
headers
|
|
7
|
+
});
|
|
8
|
+
if (!getRes.ok) return { ok: false, error: `GET failed: ${getRes.status}` };
|
|
9
|
+
const doc = await getRes.json();
|
|
10
|
+
const mergedData = { ...doc.data, [field]: value };
|
|
11
|
+
const patchRes = await fetch(`${cmsBaseUrl}/api/cms/${collection}/${slug}?site=${siteId}`, {
|
|
12
|
+
method: "PATCH",
|
|
13
|
+
headers: { ...headers, "Content-Type": "application/json" },
|
|
14
|
+
body: JSON.stringify({ data: mergedData })
|
|
15
|
+
});
|
|
16
|
+
if (!patchRes.ok) return { ok: false, error: `PATCH failed: ${patchRes.status}` };
|
|
17
|
+
return { ok: true };
|
|
18
|
+
}
|
|
19
|
+
async function verifyEditSession(options) {
|
|
20
|
+
const res = await fetch(`${options.cmsBaseUrl}/api/auth/me`, {
|
|
21
|
+
headers: { Authorization: `Bearer ${options.token}` }
|
|
22
|
+
});
|
|
23
|
+
if (!res.ok) return null;
|
|
24
|
+
const body = await res.json();
|
|
25
|
+
return body.user ?? null;
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
saveInlineEditField,
|
|
29
|
+
verifyEditSession
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/server/index.ts"],"sourcesContent":["/**\n * Optional server-side helpers (Node/Bun) for sites that want a same-origin\n * relay instead of calling the CMS directly from the browser. Not required\n * for the direct-from-browser flow used by initInlineEdit().\n */\n\nexport interface SaveInlineEditFieldOptions {\n cmsBaseUrl: string;\n siteId: string;\n collection: string;\n slug: string;\n field: string;\n value: string;\n sessionToken: string;\n}\n\nexport type SaveInlineEditFieldResult = { ok: true } | { ok: false; error: string };\n\n/** GET the doc, merge the changed field into .data, PATCH the full merged object back. */\nexport async function saveInlineEditField(\n options: SaveInlineEditFieldOptions,\n): Promise<SaveInlineEditFieldResult> {\n const { cmsBaseUrl, siteId, collection, slug, field, value, sessionToken } = options;\n const headers = { Authorization: `Bearer ${sessionToken}` };\n\n const getRes = await fetch(`${cmsBaseUrl}/api/cms/${collection}/${slug}?site=${siteId}`, {\n headers,\n });\n if (!getRes.ok) return { ok: false, error: `GET failed: ${getRes.status}` };\n const doc = (await getRes.json()) as { data?: Record<string, unknown> };\n const mergedData = { ...doc.data, [field]: value };\n\n const patchRes = await fetch(`${cmsBaseUrl}/api/cms/${collection}/${slug}?site=${siteId}`, {\n method: \"PATCH\",\n headers: { ...headers, \"Content-Type\": \"application/json\" },\n body: JSON.stringify({ data: mergedData }),\n });\n if (!patchRes.ok) return { ok: false, error: `PATCH failed: ${patchRes.status}` };\n return { ok: true };\n}\n\nexport interface VerifyEditSessionOptions {\n cmsBaseUrl: string;\n token: string;\n}\n\nexport interface EditSessionUser {\n sub: string;\n email: string;\n name: string;\n role: string;\n}\n\n/**\n * Calls the CMS's existing GET /api/auth/me with the token as a Bearer header.\n * That endpoint always returns 200 — {\"user\": null} for anonymous/invalid\n * tokens, never a 401 — so absence of a user is read from the body, not the\n * status code.\n */\nexport async function verifyEditSession(\n options: VerifyEditSessionOptions,\n): Promise<EditSessionUser | null> {\n const res = await fetch(`${options.cmsBaseUrl}/api/auth/me`, {\n headers: { Authorization: `Bearer ${options.token}` },\n });\n if (!res.ok) return null;\n const body = (await res.json()) as { user?: EditSessionUser | null };\n return body.user ?? null;\n}\n"],"mappings":";AAmBA,eAAsB,oBACpB,SACoC;AACpC,QAAM,EAAE,YAAY,QAAQ,YAAY,MAAM,OAAO,OAAO,aAAa,IAAI;AAC7E,QAAM,UAAU,EAAE,eAAe,UAAU,YAAY,GAAG;AAE1D,QAAM,SAAS,MAAM,MAAM,GAAG,UAAU,YAAY,UAAU,IAAI,IAAI,SAAS,MAAM,IAAI;AAAA,IACvF;AAAA,EACF,CAAC;AACD,MAAI,CAAC,OAAO,GAAI,QAAO,EAAE,IAAI,OAAO,OAAO,eAAe,OAAO,MAAM,GAAG;AAC1E,QAAM,MAAO,MAAM,OAAO,KAAK;AAC/B,QAAM,aAAa,EAAE,GAAG,IAAI,MAAM,CAAC,KAAK,GAAG,MAAM;AAEjD,QAAM,WAAW,MAAM,MAAM,GAAG,UAAU,YAAY,UAAU,IAAI,IAAI,SAAS,MAAM,IAAI;AAAA,IACzF,QAAQ;AAAA,IACR,SAAS,EAAE,GAAG,SAAS,gBAAgB,mBAAmB;AAAA,IAC1D,MAAM,KAAK,UAAU,EAAE,MAAM,WAAW,CAAC;AAAA,EAC3C,CAAC;AACD,MAAI,CAAC,SAAS,GAAI,QAAO,EAAE,IAAI,OAAO,OAAO,iBAAiB,SAAS,MAAM,GAAG;AAChF,SAAO,EAAE,IAAI,KAAK;AACpB;AAoBA,eAAsB,kBACpB,SACiC;AACjC,QAAM,MAAM,MAAM,MAAM,GAAG,QAAQ,UAAU,gBAAgB;AAAA,IAC3D,SAAS,EAAE,eAAe,UAAU,QAAQ,KAAK,GAAG;AAAA,EACtD,CAAC;AACD,MAAI,CAAC,IAAI,GAAI,QAAO;AACpB,QAAM,OAAQ,MAAM,IAAI,KAAK;AAC7B,SAAO,KAAK,QAAQ;AACtB;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@broberg/cms-inline-edit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Click-to-edit inline editing for live @webhouse/cms-powered sites — copy-owned, framework-agnostic",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
},
|
|
16
|
+
"./server": {
|
|
17
|
+
"types": "./dist/server/index.d.ts",
|
|
18
|
+
"import": "./dist/server/index.js",
|
|
19
|
+
"require": "./dist/server/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup",
|
|
29
|
+
"dev": "tsup --watch",
|
|
30
|
+
"typecheck": "tsc --noEmit"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/webhousecode/cms.git",
|
|
35
|
+
"directory": "packages/cms-inline-edit"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://docs.webhouse.app",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/webhousecode/cms/issues"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"cms",
|
|
43
|
+
"inline-editing",
|
|
44
|
+
"visual-editing",
|
|
45
|
+
"webhouse"
|
|
46
|
+
],
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22"
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/node": "^22.10.2",
|
|
55
|
+
"tsup": "^8.3.5",
|
|
56
|
+
"typescript": "^5.7.2"
|
|
57
|
+
}
|
|
58
|
+
}
|