@tinacms/bridge 0.0.1 → 0.2.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/config.d.ts +17 -0
- package/dist/data-store.d.ts +5 -3
- package/dist/forms.d.ts +8 -0
- package/dist/index.d.ts +44 -1
- package/dist/index.js +156 -84
- package/dist/island-refresh.d.ts +13 -6
- package/dist/metadata.d.ts +22 -0
- package/dist/metadata.js +66 -0
- package/dist/preview.d.ts +29 -1
- package/dist/preview.js +6 -0
- package/dist/quick-edit-css.d.ts +9 -0
- package/dist/quick-edit-css.js +38 -0
- package/dist/tina-field.d.ts +14 -1
- package/dist/types.d.ts +3 -1
- package/package.json +13 -3
- package/dist/edit-mode.d.ts +0 -1
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare function setAdminOrigin(origin: string | string[]): void;
|
|
2
|
+
/**
|
|
3
|
+
* Returns the canonical admin origin used as the `targetOrigin` for outbound
|
|
4
|
+
* postMessage. When multiple origins are configured the first entry wins —
|
|
5
|
+
* that's the deployment's primary admin host; the others are accepted for
|
|
6
|
+
* inbound traffic but the bridge still posts to the canonical one.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getAdminOrigin(): string;
|
|
9
|
+
/**
|
|
10
|
+
* Returns true only for postMessage events that originated from the admin
|
|
11
|
+
* iframe parent — `event.origin` matches one of the configured admin
|
|
12
|
+
* origins AND `event.source` is the parent window. Both checks are
|
|
13
|
+
* required: origin alone leaves us open to sibling frames sharing the
|
|
14
|
+
* same origin, source alone leaves us open to cross-origin parents that
|
|
15
|
+
* happen to have a handle to our window.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isFromAdmin(event: MessageEvent): boolean;
|
package/dist/data-store.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { DataStore } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Holds the latest resolved data per form id (keyed by `hashFromQuery`-style
|
|
4
|
-
* id).
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* id). `seed()` populates the initial server-rendered payload silently so
|
|
5
|
+
* the bridge doesn't trigger a refresh on page load. `set()` records edits
|
|
6
|
+
* from the admin and fires subscribers; `firstUpdate` is true only for the
|
|
7
|
+
* first admin update per id (used by island-refresh to skip the debounce
|
|
8
|
+
* on cold-start so newly-created docs reach a populated state ASAP).
|
|
7
9
|
*/
|
|
8
10
|
export declare function initDataStore(): DataStore;
|
package/dist/forms.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
import type { DataStore } from './types';
|
|
2
2
|
export declare function initForms(store: DataStore): void;
|
|
3
|
+
/**
|
|
4
|
+
* Re-scan the DOM for form payloads after a soft navigation. Diff
|
|
5
|
+
* against the previous mount and post `close` / `open` for the delta.
|
|
6
|
+
*
|
|
7
|
+
* Safe to call even if the bridge isn't initialised — used for setups
|
|
8
|
+
* that wire `astro:page-load` unconditionally. No-op outside an iframe.
|
|
9
|
+
*/
|
|
10
|
+
export declare function refreshForms(): void;
|
|
3
11
|
export declare function reportQuickEdit(): void;
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,44 @@
|
|
|
1
|
-
|
|
1
|
+
import { refreshForms } from './forms';
|
|
2
|
+
export interface BridgeOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Per-island debounce for refetches triggered by subsequent edits.
|
|
5
|
+
* The first refetch after page load fires immediately so newly-created
|
|
6
|
+
* docs reach a populated state ASAP. Default 300ms.
|
|
7
|
+
*/
|
|
8
|
+
debounceMs?: number;
|
|
9
|
+
/**
|
|
10
|
+
* Origin(s) of the TinaCMS admin parent. Inbound postMessage events are
|
|
11
|
+
* accepted only when `event.origin` matches one of these AND
|
|
12
|
+
* `event.source` is `window.parent`. Outbound posts use the first entry
|
|
13
|
+
* (or the single string) as `targetOrigin`.
|
|
14
|
+
*
|
|
15
|
+
* Defaults to `window.location.origin` — correct when the admin is
|
|
16
|
+
* mounted at `/admin` on the same host (the common case). Override when
|
|
17
|
+
* the admin runs on a different origin (cross-domain self-hosted
|
|
18
|
+
* deployments, Codespaces, Docker setups). Mirrors the role of
|
|
19
|
+
* `server.allowedOrigins` in `tina.config` for the dev server's CORS,
|
|
20
|
+
* but applied to the in-iframe postMessage channel instead of HTTP.
|
|
21
|
+
*/
|
|
22
|
+
adminOrigin?: string | string[];
|
|
23
|
+
}
|
|
24
|
+
export declare function init(options?: BridgeOptions): void;
|
|
25
|
+
/**
|
|
26
|
+
* Re-scan the page for `[data-tina-form]` payloads after a soft
|
|
27
|
+
* navigation (Astro view transitions, Turbo, htmx, etc.). Posts `close`
|
|
28
|
+
* for forms that left and `open` for forms that appeared. Safe to call
|
|
29
|
+
* before `init()` — no-op when the bridge isn't running.
|
|
30
|
+
*
|
|
31
|
+
* Astro projects using `@tinacms/astro/integration` get this wired
|
|
32
|
+
* automatically (the middleware splices the bootstrap script that
|
|
33
|
+
* listens for `astro:page-load`). Sites consuming `@tinacms/bridge`
|
|
34
|
+
* directly need:
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { init, refreshForms } from '@tinacms/bridge';
|
|
38
|
+
* init();
|
|
39
|
+
* document.addEventListener('astro:page-load', refreshForms);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export { refreshForms };
|
|
43
|
+
export { tinaField } from './tina-field';
|
|
44
|
+
export type * from './types';
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
|
+
let configuredAdminOrigins = [];
|
|
2
|
+
function setAdminOrigin(origin) {
|
|
3
|
+
configuredAdminOrigins = Array.isArray(origin) ? [...origin] : [origin];
|
|
4
|
+
}
|
|
5
|
+
function getAdminOrigin() {
|
|
6
|
+
return configuredAdminOrigins[0] ?? "";
|
|
7
|
+
}
|
|
8
|
+
function isFromAdmin(event) {
|
|
9
|
+
if (typeof window === "undefined")
|
|
10
|
+
return false;
|
|
11
|
+
if (!configuredAdminOrigins.includes(event.origin))
|
|
12
|
+
return false;
|
|
13
|
+
return event.source === window.parent;
|
|
14
|
+
}
|
|
1
15
|
const ENABLED = typeof window !== "undefined";
|
|
2
16
|
function debug(...args) {
|
|
3
17
|
if (!ENABLED)
|
|
4
18
|
return;
|
|
5
19
|
console.log("[@tinacms/bridge]", ...args);
|
|
6
20
|
}
|
|
7
|
-
const STYLE_ID = "__tina-bridge-quick-edit-style";
|
|
8
|
-
const BODY_CLASS = "__tina-quick-editing-enabled";
|
|
9
21
|
const QUICK_EDIT_CSS = `
|
|
10
22
|
[data-tina-field] {
|
|
11
23
|
outline: 2px dashed rgba(34,150,254,0.5);
|
|
@@ -37,8 +49,10 @@ const QUICK_EDIT_CSS = `
|
|
|
37
49
|
opacity: 1;
|
|
38
50
|
}
|
|
39
51
|
`;
|
|
52
|
+
const QUICK_EDIT_BODY_CLASS = "__tina-quick-editing-enabled";
|
|
53
|
+
const QUICK_EDIT_STYLE_ID = "__tina-bridge-quick-edit-style";
|
|
40
54
|
function initClickToFocus() {
|
|
41
|
-
let enabled =
|
|
55
|
+
let enabled = false;
|
|
42
56
|
document.addEventListener(
|
|
43
57
|
"click",
|
|
44
58
|
(event) => {
|
|
@@ -58,12 +72,14 @@ function initClickToFocus() {
|
|
|
58
72
|
event.stopPropagation();
|
|
59
73
|
window.parent.postMessage(
|
|
60
74
|
{ type: "field:selected", fieldName },
|
|
61
|
-
|
|
75
|
+
getAdminOrigin()
|
|
62
76
|
);
|
|
63
77
|
},
|
|
64
78
|
true
|
|
65
79
|
);
|
|
66
80
|
window.addEventListener("message", (event) => {
|
|
81
|
+
if (!isFromAdmin(event))
|
|
82
|
+
return;
|
|
67
83
|
const message = event.data;
|
|
68
84
|
if (!message || message.type !== "quickEditEnabled")
|
|
69
85
|
return;
|
|
@@ -97,18 +113,18 @@ function readTinaField(el) {
|
|
|
97
113
|
return null;
|
|
98
114
|
}
|
|
99
115
|
function installStyle() {
|
|
100
|
-
if (document.getElementById(
|
|
116
|
+
if (document.getElementById(QUICK_EDIT_STYLE_ID))
|
|
101
117
|
return;
|
|
102
118
|
const style = document.createElement("style");
|
|
103
|
-
style.id =
|
|
119
|
+
style.id = QUICK_EDIT_STYLE_ID;
|
|
104
120
|
style.textContent = QUICK_EDIT_CSS;
|
|
105
121
|
document.head.appendChild(style);
|
|
106
|
-
document.body.classList.add(
|
|
122
|
+
document.body.classList.add(QUICK_EDIT_BODY_CLASS);
|
|
107
123
|
}
|
|
108
124
|
function removeStyle() {
|
|
109
125
|
var _a;
|
|
110
|
-
(_a = document.getElementById(
|
|
111
|
-
document.body.classList.remove(
|
|
126
|
+
(_a = document.getElementById(QUICK_EDIT_STYLE_ID)) == null ? void 0 : _a.remove();
|
|
127
|
+
document.body.classList.remove(QUICK_EDIT_BODY_CLASS);
|
|
112
128
|
}
|
|
113
129
|
function initDataStore() {
|
|
114
130
|
const data = /* @__PURE__ */ new Map();
|
|
@@ -133,16 +149,55 @@ function initDataStore() {
|
|
|
133
149
|
}
|
|
134
150
|
};
|
|
135
151
|
}
|
|
136
|
-
const
|
|
152
|
+
const FORM_SELECTOR = "[data-tina-form]";
|
|
153
|
+
const FORM_ATTR = "data-tina-form";
|
|
137
154
|
const RETRY_INTERVAL_MS = 250;
|
|
138
155
|
const MAX_ATTEMPTS = 40;
|
|
156
|
+
let controller = null;
|
|
139
157
|
function initForms(store) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
158
|
+
if (controller) {
|
|
159
|
+
debug("initForms called twice; ignoring");
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
controller = {
|
|
163
|
+
store,
|
|
164
|
+
active: /* @__PURE__ */ new Map(),
|
|
165
|
+
acknowledged: /* @__PURE__ */ new Set(),
|
|
166
|
+
retryTimer: null,
|
|
167
|
+
attempts: 0
|
|
168
|
+
};
|
|
169
|
+
window.addEventListener("message", onAck);
|
|
170
|
+
window.addEventListener("beforeunload", onBeforeUnload);
|
|
171
|
+
refreshForms();
|
|
172
|
+
}
|
|
173
|
+
function refreshForms() {
|
|
174
|
+
if (!controller) {
|
|
175
|
+
debug("refreshForms called before initForms; ignoring");
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const next = readPayloads();
|
|
179
|
+
const nextIds = new Set(next.map((p) => p.id));
|
|
180
|
+
for (const [id] of controller.active) {
|
|
181
|
+
if (nextIds.has(id))
|
|
182
|
+
continue;
|
|
183
|
+
debug("posting close for", id);
|
|
184
|
+
window.parent.postMessage({ type: "close", id }, getAdminOrigin());
|
|
185
|
+
controller.acknowledged.delete(id);
|
|
186
|
+
}
|
|
187
|
+
for (const payload of next) {
|
|
188
|
+
controller.store.seed(payload.id, payload.data ?? {});
|
|
189
|
+
}
|
|
190
|
+
controller.active = new Map(next.map((p) => [p.id, p]));
|
|
191
|
+
if (next.some((p) => !controller.acknowledged.has(p.id))) {
|
|
192
|
+
startAnnounceLoop();
|
|
193
|
+
}
|
|
194
|
+
reportQuickEdit();
|
|
195
|
+
}
|
|
196
|
+
function readPayloads() {
|
|
197
|
+
const elements = document.querySelectorAll(FORM_SELECTOR);
|
|
143
198
|
const payloads = [];
|
|
144
|
-
for (const
|
|
145
|
-
const raw =
|
|
199
|
+
for (const el of elements) {
|
|
200
|
+
const raw = el.getAttribute(FORM_ATTR);
|
|
146
201
|
if (!raw)
|
|
147
202
|
continue;
|
|
148
203
|
try {
|
|
@@ -150,80 +205,96 @@ function initForms(store) {
|
|
|
150
205
|
if (!payload.id || !payload.query)
|
|
151
206
|
continue;
|
|
152
207
|
payloads.push(payload);
|
|
153
|
-
store.seed(payload.id, payload.data ?? {});
|
|
154
208
|
} catch (error) {
|
|
155
209
|
debug("failed to parse form payload", error);
|
|
156
210
|
}
|
|
157
211
|
}
|
|
158
212
|
debug("discovered", payloads.length, "form(s)");
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
window.parent.postMessage(
|
|
192
|
-
{
|
|
193
|
-
type: "open",
|
|
194
|
-
id: payload.id,
|
|
195
|
-
query: payload.query,
|
|
196
|
-
variables: payload.variables,
|
|
197
|
-
data: payload.data
|
|
198
|
-
},
|
|
199
|
-
window.location.origin
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
setTimeout(announce, RETRY_INTERVAL_MS);
|
|
203
|
-
};
|
|
213
|
+
return payloads;
|
|
214
|
+
}
|
|
215
|
+
function onAck(event) {
|
|
216
|
+
if (!isFromAdmin(event))
|
|
217
|
+
return;
|
|
218
|
+
const msg = event.data;
|
|
219
|
+
if (!msg || typeof msg !== "object")
|
|
220
|
+
return;
|
|
221
|
+
if (msg.type !== "updateData" || typeof msg.id !== "string")
|
|
222
|
+
return;
|
|
223
|
+
if (!controller)
|
|
224
|
+
return;
|
|
225
|
+
if (!controller.acknowledged.has(msg.id)) {
|
|
226
|
+
debug("admin acked form", msg.id);
|
|
227
|
+
controller.acknowledged.add(msg.id);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function onBeforeUnload() {
|
|
231
|
+
if (!controller)
|
|
232
|
+
return;
|
|
233
|
+
for (const [id] of controller.active) {
|
|
234
|
+
window.parent.postMessage({ type: "close", id }, getAdminOrigin());
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function startAnnounceLoop() {
|
|
238
|
+
if (!controller)
|
|
239
|
+
return;
|
|
240
|
+
if (controller.retryTimer) {
|
|
241
|
+
clearTimeout(controller.retryTimer);
|
|
242
|
+
controller.retryTimer = null;
|
|
243
|
+
}
|
|
244
|
+
controller.attempts = 0;
|
|
204
245
|
announce();
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
246
|
+
}
|
|
247
|
+
function announce() {
|
|
248
|
+
if (!controller)
|
|
249
|
+
return;
|
|
250
|
+
controller.attempts++;
|
|
251
|
+
const pending = [];
|
|
252
|
+
for (const [id, payload] of controller.active) {
|
|
253
|
+
if (!controller.acknowledged.has(id))
|
|
254
|
+
pending.push(payload);
|
|
255
|
+
}
|
|
256
|
+
if (pending.length === 0) {
|
|
257
|
+
debug("all forms acked after", controller.attempts, "attempt(s)");
|
|
258
|
+
controller.retryTimer = null;
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (controller.attempts > MAX_ATTEMPTS) {
|
|
262
|
+
debug(
|
|
263
|
+
"giving up after",
|
|
264
|
+
MAX_ATTEMPTS,
|
|
265
|
+
"attempts; pending ids:",
|
|
266
|
+
pending.map((p) => p.id)
|
|
267
|
+
);
|
|
268
|
+
controller.retryTimer = null;
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
for (const payload of pending) {
|
|
272
|
+
debug("posting open for", payload.id, "attempt", controller.attempts);
|
|
273
|
+
window.parent.postMessage(
|
|
274
|
+
{
|
|
275
|
+
type: "open",
|
|
276
|
+
id: payload.id,
|
|
277
|
+
query: payload.query,
|
|
278
|
+
variables: payload.variables,
|
|
279
|
+
data: payload.data
|
|
280
|
+
},
|
|
281
|
+
getAdminOrigin()
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
controller.retryTimer = setTimeout(announce, RETRY_INTERVAL_MS);
|
|
214
285
|
}
|
|
215
286
|
function reportQuickEdit() {
|
|
216
287
|
const hasMarkers = !!document.querySelector("[data-tina-field]");
|
|
217
288
|
window.parent.postMessage(
|
|
218
289
|
{ type: "quick-edit", value: hasMarkers },
|
|
219
|
-
|
|
290
|
+
getAdminOrigin()
|
|
220
291
|
);
|
|
221
292
|
}
|
|
222
293
|
const PREVIEW_CONTENT_TYPE = "application/x-tina-preview+json";
|
|
223
294
|
const ISLAND_SELECTOR = "[data-tina-island]";
|
|
224
295
|
const ENDPOINT_ATTR = "data-tina-island";
|
|
225
296
|
function initIslandRefresh(store, options) {
|
|
226
|
-
|
|
297
|
+
let pendingRefresh = null;
|
|
227
298
|
const refreshAll = () => {
|
|
228
299
|
const islands = document.querySelectorAll(ISLAND_SELECTOR);
|
|
229
300
|
for (const island of islands) {
|
|
@@ -231,23 +302,22 @@ function initIslandRefresh(store, options) {
|
|
|
231
302
|
}
|
|
232
303
|
};
|
|
233
304
|
store.subscribe(({ firstUpdate }) => {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
305
|
+
if (pendingRefresh) {
|
|
306
|
+
clearTimeout(pendingRefresh);
|
|
307
|
+
pendingRefresh = null;
|
|
308
|
+
}
|
|
238
309
|
if (firstUpdate) {
|
|
239
310
|
refreshAll();
|
|
240
311
|
return;
|
|
241
312
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
refreshAll();
|
|
247
|
-
}, options.debounceMs)
|
|
248
|
-
);
|
|
313
|
+
pendingRefresh = setTimeout(() => {
|
|
314
|
+
pendingRefresh = null;
|
|
315
|
+
refreshAll();
|
|
316
|
+
}, options.debounceMs);
|
|
249
317
|
});
|
|
250
318
|
window.addEventListener("message", (event) => {
|
|
319
|
+
if (!isFromAdmin(event))
|
|
320
|
+
return;
|
|
251
321
|
const message = event.data;
|
|
252
322
|
if (!message || typeof message !== "object")
|
|
253
323
|
return;
|
|
@@ -328,7 +398,8 @@ function init(options = {}) {
|
|
|
328
398
|
return;
|
|
329
399
|
}
|
|
330
400
|
debug("initialising in iframe");
|
|
331
|
-
const { debounceMs = 300 } = options;
|
|
401
|
+
const { debounceMs = 300, adminOrigin = window.location.origin } = options;
|
|
402
|
+
setAdminOrigin(adminOrigin);
|
|
332
403
|
const store = initDataStore();
|
|
333
404
|
initIslandRefresh(store, { debounceMs });
|
|
334
405
|
initClickToFocus();
|
|
@@ -336,5 +407,6 @@ function init(options = {}) {
|
|
|
336
407
|
}
|
|
337
408
|
export {
|
|
338
409
|
init,
|
|
410
|
+
refreshForms,
|
|
339
411
|
tinaField
|
|
340
412
|
};
|
package/dist/island-refresh.d.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import type { DataStore } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Listens for `{type:'updateData', id, data}` from the admin and re-fetches
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* every `[data-tina-island]` on the page with the unsaved data attached as
|
|
5
|
+
* a JSON POST body. The island endpoint reads the body via `readOverlay()`
|
|
6
|
+
* from `@tinacms/bridge/preview` and renders with overlay data instead of
|
|
7
|
+
* hitting the canonical content store.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* Why POST: HTTP headers are capped at ~8 KB (server-dependent) — large
|
|
10
|
+
* posts overflow easily — and are restricted to Latin-1 so UTF-8 content
|
|
11
|
+
* needs base64 padding. A POST body has neither limit and round-trips
|
|
12
|
+
* UTF-8 directly.
|
|
13
|
+
*
|
|
14
|
+
* The very first updateData fires immediately so newly-created docs leave
|
|
15
|
+
* the empty-template state ASAP. Subsequent updates collapse into a single
|
|
16
|
+
* debounced refetch — each refresh re-renders every island anyway, so a
|
|
17
|
+
* per-id timer would just fire N redundant times for the same DOM scan.
|
|
11
18
|
*/
|
|
12
19
|
export interface IslandRefreshOptions {
|
|
13
20
|
debounceMs: number;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical content-source metadata helpers.
|
|
3
|
+
*
|
|
4
|
+
* `addMetadata` walks a query result and stamps every non-system object with
|
|
5
|
+
* `_content_source: { queryId, path }`. The pair is what `tinaField()` reads
|
|
6
|
+
* to build the `data-tina-field` markers the admin uses for click-to-focus.
|
|
7
|
+
*
|
|
8
|
+
* `hashFromQuery` derives the queryId from `JSON.stringify({ query, variables })`.
|
|
9
|
+
* Both ends of the wire (page render, admin sidebar) hash the same string so
|
|
10
|
+
* overlay updates address the same form.
|
|
11
|
+
*
|
|
12
|
+
* One source of truth for React (useTina), Astro (server-side overlay), and
|
|
13
|
+
* any future framework integration. Kept dependency-free so it runs in any
|
|
14
|
+
* runtime — browser, Node, edge.
|
|
15
|
+
*/
|
|
16
|
+
export declare const addMetadata: <T>(id: string, obj: T, path?: (string | number)[]) => T;
|
|
17
|
+
/**
|
|
18
|
+
* Rudimentary string hash. Both ends of the wire derive the queryId from
|
|
19
|
+
* the same JSON.stringify({ query, variables }) — collisions are theoretically
|
|
20
|
+
* possible but vanishingly rare in practice.
|
|
21
|
+
*/
|
|
22
|
+
export declare const hashFromQuery: (input: string) => string;
|
package/dist/metadata.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const SYSTEM_KEYS = /* @__PURE__ */ new Set([
|
|
2
|
+
"__typename",
|
|
3
|
+
"_sys",
|
|
4
|
+
"_internalSys",
|
|
5
|
+
"_values",
|
|
6
|
+
"_internalValues",
|
|
7
|
+
"_content_source",
|
|
8
|
+
"_tina_metadata"
|
|
9
|
+
]);
|
|
10
|
+
const addMetadata = (id, obj, path = []) => {
|
|
11
|
+
if (obj === null)
|
|
12
|
+
return obj;
|
|
13
|
+
if (isScalarOrUndefined(obj))
|
|
14
|
+
return obj;
|
|
15
|
+
if (obj instanceof String)
|
|
16
|
+
return obj.valueOf();
|
|
17
|
+
if (Array.isArray(obj)) {
|
|
18
|
+
return obj.map(
|
|
19
|
+
(item, index) => addMetadata(id, item, [...path, index])
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
const next = {};
|
|
23
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
24
|
+
if (SYSTEM_KEYS.has(key)) {
|
|
25
|
+
next[key] = value;
|
|
26
|
+
} else {
|
|
27
|
+
next[key] = addMetadata(id, value, [...path, key]);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (next && typeof next === "object" && "type" in next && next.type === "root") {
|
|
31
|
+
return next;
|
|
32
|
+
}
|
|
33
|
+
return { ...next, _content_source: { queryId: id, path } };
|
|
34
|
+
};
|
|
35
|
+
function isScalarOrUndefined(value) {
|
|
36
|
+
const type = typeof value;
|
|
37
|
+
if (type === "string")
|
|
38
|
+
return true;
|
|
39
|
+
if (type === "number")
|
|
40
|
+
return true;
|
|
41
|
+
if (type === "boolean")
|
|
42
|
+
return true;
|
|
43
|
+
if (type === "undefined")
|
|
44
|
+
return true;
|
|
45
|
+
if (value == null)
|
|
46
|
+
return true;
|
|
47
|
+
if (value instanceof String)
|
|
48
|
+
return true;
|
|
49
|
+
if (value instanceof Number)
|
|
50
|
+
return true;
|
|
51
|
+
if (value instanceof Boolean)
|
|
52
|
+
return true;
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
const hashFromQuery = (input) => {
|
|
56
|
+
let hash = 0;
|
|
57
|
+
for (let i = 0; i < input.length; i++) {
|
|
58
|
+
const char = input.charCodeAt(i);
|
|
59
|
+
hash = (hash << 5) - hash + char & 4294967295;
|
|
60
|
+
}
|
|
61
|
+
return Math.abs(hash).toString(36);
|
|
62
|
+
};
|
|
63
|
+
export {
|
|
64
|
+
addMetadata,
|
|
65
|
+
hashFromQuery
|
|
66
|
+
};
|
package/dist/preview.d.ts
CHANGED
|
@@ -1 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Shared preview-protocol helpers for both ends of the X-Tina-Preview
|
|
3
|
+
* channel. The bridge POSTs island refetches with a JSON body of shape
|
|
4
|
+
*
|
|
5
|
+
* { [queryId]: data, ... }
|
|
6
|
+
*
|
|
7
|
+
* Server-side island handlers call `readOverlay(request, queryId)` to
|
|
8
|
+
* fetch the overlay data for their own query without having to know the
|
|
9
|
+
* transport details (POST body vs header, JSON vs base64-encoded JSON).
|
|
10
|
+
*
|
|
11
|
+
* Runs in Node (Astro server endpoints, Hugo plugins, etc.) — no DOM
|
|
12
|
+
* dependencies. The browser-side encoder lives in island-refresh.ts.
|
|
13
|
+
*/
|
|
14
|
+
export declare const PREVIEW_HEADER = "X-Tina-Preview";
|
|
15
|
+
export declare const PREVIEW_CONTENT_TYPE = "application/x-tina-preview+json";
|
|
16
|
+
export interface PreviewEnvelope {
|
|
17
|
+
[queryId: string]: unknown;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Read the overlay envelope for a given query id from an incoming
|
|
21
|
+
* request. Returns `undefined` if no overlay is present (production
|
|
22
|
+
* traffic, or an island refetch with no matching id).
|
|
23
|
+
*/
|
|
24
|
+
export declare function readOverlay<T>(request: Request, queryId: string): Promise<T | undefined>;
|
|
25
|
+
/**
|
|
26
|
+
* Read and parse the full overlay envelope (every form on the page),
|
|
27
|
+
* for callers that want to inspect the raw payload.
|
|
28
|
+
*/
|
|
29
|
+
export declare function readEnvelope(request: Request): Promise<PreviewEnvelope | undefined>;
|
package/dist/preview.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const PREVIEW_HEADER = "X-Tina-Preview";
|
|
2
2
|
const PREVIEW_CONTENT_TYPE = "application/x-tina-preview+json";
|
|
3
|
+
const MAX_ENVELOPE_BYTES = 1e6;
|
|
3
4
|
async function readOverlay(request, queryId) {
|
|
4
5
|
const envelope = await readEnvelope(request);
|
|
5
6
|
if (!envelope)
|
|
@@ -11,10 +12,15 @@ async function readEnvelope(request) {
|
|
|
11
12
|
const contentType = request.headers.get("content-type") ?? "";
|
|
12
13
|
if (!contentType.includes(PREVIEW_CONTENT_TYPE))
|
|
13
14
|
return void 0;
|
|
15
|
+
const declaredLength = Number(request.headers.get("content-length") ?? "0");
|
|
16
|
+
if (declaredLength > MAX_ENVELOPE_BYTES)
|
|
17
|
+
return void 0;
|
|
14
18
|
try {
|
|
15
19
|
const text = await request.text();
|
|
16
20
|
if (!text)
|
|
17
21
|
return void 0;
|
|
22
|
+
if (text.length > MAX_ENVELOPE_BYTES)
|
|
23
|
+
return void 0;
|
|
18
24
|
return JSON.parse(text);
|
|
19
25
|
} catch {
|
|
20
26
|
return void 0;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared CSS for the quick-edit outline. Used by both the vanilla bridge
|
|
3
|
+
* (click-to-focus.ts) and the React `useTina` hook (packages/tinacms/src/react.tsx),
|
|
4
|
+
* so the visible affordance is identical regardless of which integration
|
|
5
|
+
* the consumer is running.
|
|
6
|
+
*/
|
|
7
|
+
export declare const QUICK_EDIT_CSS = "\n [data-tina-field] {\n outline: 2px dashed rgba(34,150,254,0.5);\n transition: box-shadow ease-out 150ms;\n }\n [data-tina-field]:hover {\n box-shadow: inset 100vi 100vh rgba(34,150,254,0.3);\n outline: 2px solid rgba(34,150,254,1);\n cursor: pointer;\n }\n [data-tina-field-overlay] {\n outline: 2px dashed rgba(34,150,254,0.5);\n position: relative;\n }\n [data-tina-field-overlay]:hover {\n cursor: pointer;\n outline: 2px solid rgba(34,150,254,1);\n }\n [data-tina-field-overlay]::after {\n content: '';\n position: absolute;\n inset: 0;\n z-index: 20;\n transition: opacity ease-out 150ms;\n background-color: rgba(34,150,254,0.3);\n opacity: 0;\n }\n [data-tina-field-overlay]:hover::after {\n opacity: 1;\n }\n";
|
|
8
|
+
export declare const QUICK_EDIT_BODY_CLASS = "__tina-quick-editing-enabled";
|
|
9
|
+
export declare const QUICK_EDIT_STYLE_ID = "__tina-bridge-quick-edit-style";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const QUICK_EDIT_CSS = `
|
|
2
|
+
[data-tina-field] {
|
|
3
|
+
outline: 2px dashed rgba(34,150,254,0.5);
|
|
4
|
+
transition: box-shadow ease-out 150ms;
|
|
5
|
+
}
|
|
6
|
+
[data-tina-field]:hover {
|
|
7
|
+
box-shadow: inset 100vi 100vh rgba(34,150,254,0.3);
|
|
8
|
+
outline: 2px solid rgba(34,150,254,1);
|
|
9
|
+
cursor: pointer;
|
|
10
|
+
}
|
|
11
|
+
[data-tina-field-overlay] {
|
|
12
|
+
outline: 2px dashed rgba(34,150,254,0.5);
|
|
13
|
+
position: relative;
|
|
14
|
+
}
|
|
15
|
+
[data-tina-field-overlay]:hover {
|
|
16
|
+
cursor: pointer;
|
|
17
|
+
outline: 2px solid rgba(34,150,254,1);
|
|
18
|
+
}
|
|
19
|
+
[data-tina-field-overlay]::after {
|
|
20
|
+
content: '';
|
|
21
|
+
position: absolute;
|
|
22
|
+
inset: 0;
|
|
23
|
+
z-index: 20;
|
|
24
|
+
transition: opacity ease-out 150ms;
|
|
25
|
+
background-color: rgba(34,150,254,0.3);
|
|
26
|
+
opacity: 0;
|
|
27
|
+
}
|
|
28
|
+
[data-tina-field-overlay]:hover::after {
|
|
29
|
+
opacity: 1;
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
const QUICK_EDIT_BODY_CLASS = "__tina-quick-editing-enabled";
|
|
33
|
+
const QUICK_EDIT_STYLE_ID = "__tina-bridge-quick-edit-style";
|
|
34
|
+
export {
|
|
35
|
+
QUICK_EDIT_BODY_CLASS,
|
|
36
|
+
QUICK_EDIT_CSS,
|
|
37
|
+
QUICK_EDIT_STYLE_ID
|
|
38
|
+
};
|
package/dist/tina-field.d.ts
CHANGED
|
@@ -1 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generate a field identifier for Tina to associate DOM elements with form fields.
|
|
3
|
+
* Format: "queryId---path.to.field" or "queryId---path.to.array.index"
|
|
4
|
+
*
|
|
5
|
+
* Canonical implementation. The React-side `tinacms/tina-field` and Astro-side
|
|
6
|
+
* `@tinacms/astro/tina-field` both re-export from here so non-React frontends
|
|
7
|
+
* can consume it without pulling tinacms (and its React deps) into their bundle.
|
|
8
|
+
*/
|
|
9
|
+
export declare const tinaField: <T extends {
|
|
10
|
+
_content_source?: {
|
|
11
|
+
queryId: string;
|
|
12
|
+
path: (number | string)[];
|
|
13
|
+
};
|
|
14
|
+
} | Record<string, unknown> | null | undefined>(object: T, property?: keyof Omit<NonNullable<T>, "__typename" | "_sys">, index?: number) => string;
|
package/dist/types.d.ts
CHANGED
|
@@ -45,7 +45,9 @@ export interface FormPayload {
|
|
|
45
45
|
export interface DataStore {
|
|
46
46
|
/** Latest resolved data per form id. */
|
|
47
47
|
get(id: string): object | undefined;
|
|
48
|
-
/**
|
|
48
|
+
/** Populate without notifying subscribers (used for the initial seed). */
|
|
49
|
+
seed(id: string, data: object): void;
|
|
50
|
+
/** Replace cached data for a form and notify subscribers. */
|
|
49
51
|
set(id: string, data: object): void;
|
|
50
52
|
/** All known form ids. */
|
|
51
53
|
ids(): string[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinacms/bridge",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -20,6 +20,14 @@
|
|
|
20
20
|
"./preview": {
|
|
21
21
|
"types": "./dist/preview.d.ts",
|
|
22
22
|
"default": "./dist/preview.js"
|
|
23
|
+
},
|
|
24
|
+
"./metadata": {
|
|
25
|
+
"types": "./dist/metadata.d.ts",
|
|
26
|
+
"default": "./dist/metadata.js"
|
|
27
|
+
},
|
|
28
|
+
"./quick-edit-css": {
|
|
29
|
+
"types": "./dist/quick-edit-css.d.ts",
|
|
30
|
+
"default": "./dist/quick-edit-css.js"
|
|
23
31
|
}
|
|
24
32
|
},
|
|
25
33
|
"license": "Apache-2.0",
|
|
@@ -27,7 +35,9 @@
|
|
|
27
35
|
"entryPoints": [
|
|
28
36
|
"src/index.ts",
|
|
29
37
|
"src/tina-field.ts",
|
|
30
|
-
"src/preview.ts"
|
|
38
|
+
"src/preview.ts",
|
|
39
|
+
"src/metadata.ts",
|
|
40
|
+
"src/quick-edit-css.ts"
|
|
31
41
|
]
|
|
32
42
|
},
|
|
33
43
|
"devDependencies": {
|
|
@@ -36,7 +46,7 @@
|
|
|
36
46
|
"typescript": "^5.7.3",
|
|
37
47
|
"vite": "^5.4.14",
|
|
38
48
|
"vitest": "^2.1.9",
|
|
39
|
-
"@tinacms/scripts": "1.6.
|
|
49
|
+
"@tinacms/scripts": "1.6.1"
|
|
40
50
|
},
|
|
41
51
|
"publishConfig": {
|
|
42
52
|
"registry": "https://registry.npmjs.org"
|
package/dist/edit-mode.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function initEditMode(timeoutMs?: number): Promise<boolean>;
|