@tinacms/bridge 0.0.0-0509095-20260525122024
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 +176 -0
- package/dist/click-to-focus.d.ts +1 -0
- package/dist/config.d.ts +17 -0
- package/dist/data-store.d.ts +10 -0
- package/dist/debug.d.ts +1 -0
- package/dist/forms.d.ts +9 -0
- package/dist/forms.test.d.ts +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +529 -0
- package/dist/island-refresh.d.ts +22 -0
- package/dist/island-refresh.test.d.ts +1 -0
- package/dist/metadata.d.ts +22 -0
- package/dist/metadata.js +66 -0
- package/dist/preview.d.ts +16 -0
- package/dist/preview.js +45 -0
- package/dist/preview.test.d.ts +1 -0
- package/dist/quick-edit-css.d.ts +9 -0
- package/dist/quick-edit-css.js +38 -0
- package/dist/refresh-forms.test.d.ts +1 -0
- package/dist/tina-field.d.ts +14 -0
- package/dist/tina-field.js +15 -0
- package/dist/types.d.ts +61 -0
- package/dist/types.test-d.d.ts +1 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initClickToFocus(): void;
|
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;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DataStore } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Holds the latest resolved data per form id (keyed by `hashFromQuery`-style
|
|
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).
|
|
9
|
+
*/
|
|
10
|
+
export declare function initDataStore(): DataStore;
|
package/dist/debug.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function debug(...args: unknown[]): void;
|
package/dist/forms.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { DataStore } from './types';
|
|
2
|
+
export declare const FORM_SELECTOR = "[data-tina-form]";
|
|
3
|
+
export declare const FORM_ATTR = "data-tina-form";
|
|
4
|
+
export declare const PRIMARY_FORM_ATTR = "data-tina-primary";
|
|
5
|
+
export declare function initForms(store: DataStore): void;
|
|
6
|
+
/** Diff active forms against the DOM and post the deltas. Safe to call
|
|
7
|
+
* before init() — no-op then. */
|
|
8
|
+
export declare function refreshForms(): void;
|
|
9
|
+
export declare function reportQuickEdit(): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface BridgeOptions {
|
|
2
|
+
/** Debounce for refetches after the first edit. The first refetch
|
|
3
|
+
* fires immediately. Default 300ms. */
|
|
4
|
+
debounceMs?: number;
|
|
5
|
+
/** Allowed origin(s) of the admin parent for in-iframe postMessage.
|
|
6
|
+
* Defaults to `window.location.origin` (same-host `/admin`). */
|
|
7
|
+
adminOrigin?: string | string[];
|
|
8
|
+
}
|
|
9
|
+
export declare function init(options?: BridgeOptions): void;
|
|
10
|
+
/**
|
|
11
|
+
* Re-scan for `[data-tina-form]` payloads after a soft navigation
|
|
12
|
+
* (Astro view transitions, Turbo, htmx). On a prerendered page with no
|
|
13
|
+
* server-injected payloads, prime from the island endpoints first.
|
|
14
|
+
*/
|
|
15
|
+
export declare function refreshForms(): void;
|
|
16
|
+
export { tinaField } from './tina-field';
|
|
17
|
+
export type * from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,529 @@
|
|
|
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
|
+
}
|
|
15
|
+
const ENABLED = typeof window !== "undefined";
|
|
16
|
+
function debug(...args) {
|
|
17
|
+
if (!ENABLED)
|
|
18
|
+
return;
|
|
19
|
+
console.log("[@tinacms/bridge]", ...args);
|
|
20
|
+
}
|
|
21
|
+
const QUICK_EDIT_CSS = `
|
|
22
|
+
[data-tina-field] {
|
|
23
|
+
outline: 2px dashed rgba(34,150,254,0.5);
|
|
24
|
+
transition: box-shadow ease-out 150ms;
|
|
25
|
+
}
|
|
26
|
+
[data-tina-field]:hover {
|
|
27
|
+
box-shadow: inset 100vi 100vh rgba(34,150,254,0.3);
|
|
28
|
+
outline: 2px solid rgba(34,150,254,1);
|
|
29
|
+
cursor: pointer;
|
|
30
|
+
}
|
|
31
|
+
[data-tina-field-overlay] {
|
|
32
|
+
outline: 2px dashed rgba(34,150,254,0.5);
|
|
33
|
+
position: relative;
|
|
34
|
+
}
|
|
35
|
+
[data-tina-field-overlay]:hover {
|
|
36
|
+
cursor: pointer;
|
|
37
|
+
outline: 2px solid rgba(34,150,254,1);
|
|
38
|
+
}
|
|
39
|
+
[data-tina-field-overlay]::after {
|
|
40
|
+
content: '';
|
|
41
|
+
position: absolute;
|
|
42
|
+
inset: 0;
|
|
43
|
+
z-index: 20;
|
|
44
|
+
transition: opacity ease-out 150ms;
|
|
45
|
+
background-color: rgba(34,150,254,0.3);
|
|
46
|
+
opacity: 0;
|
|
47
|
+
}
|
|
48
|
+
[data-tina-field-overlay]:hover::after {
|
|
49
|
+
opacity: 1;
|
|
50
|
+
}
|
|
51
|
+
`;
|
|
52
|
+
const QUICK_EDIT_BODY_CLASS = "__tina-quick-editing-enabled";
|
|
53
|
+
const QUICK_EDIT_STYLE_ID = "__tina-bridge-quick-edit-style";
|
|
54
|
+
function initClickToFocus() {
|
|
55
|
+
let enabled = false;
|
|
56
|
+
document.addEventListener(
|
|
57
|
+
"click",
|
|
58
|
+
(event) => {
|
|
59
|
+
const target = event.target;
|
|
60
|
+
const tagName = target instanceof Element ? target.tagName : "?";
|
|
61
|
+
if (!enabled) {
|
|
62
|
+
debug("click ignored — quickEdit disabled", tagName);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const fieldName = resolveFieldName(target);
|
|
66
|
+
if (!fieldName) {
|
|
67
|
+
debug("click ignored — no data-tina-field ancestor for", tagName);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
debug("click captured →", fieldName, "(target:", tagName, ")");
|
|
71
|
+
event.preventDefault();
|
|
72
|
+
event.stopPropagation();
|
|
73
|
+
window.parent.postMessage(
|
|
74
|
+
{ type: "field:selected", fieldName },
|
|
75
|
+
getAdminOrigin()
|
|
76
|
+
);
|
|
77
|
+
},
|
|
78
|
+
true
|
|
79
|
+
);
|
|
80
|
+
window.addEventListener("message", (event) => {
|
|
81
|
+
if (!isFromAdmin(event))
|
|
82
|
+
return;
|
|
83
|
+
const message = event.data;
|
|
84
|
+
if (!message || message.type !== "quickEditEnabled")
|
|
85
|
+
return;
|
|
86
|
+
enabled = !!message.value;
|
|
87
|
+
if (enabled)
|
|
88
|
+
installStyle();
|
|
89
|
+
else
|
|
90
|
+
removeStyle();
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function resolveFieldName(target) {
|
|
94
|
+
const el = target instanceof Element ? target : null;
|
|
95
|
+
if (!el)
|
|
96
|
+
return null;
|
|
97
|
+
const direct = readTinaField(el);
|
|
98
|
+
if (direct)
|
|
99
|
+
return direct;
|
|
100
|
+
const ancestor = el.closest("[data-tina-field], [data-tina-field-overlay]");
|
|
101
|
+
if (!ancestor)
|
|
102
|
+
return null;
|
|
103
|
+
return readTinaField(ancestor);
|
|
104
|
+
}
|
|
105
|
+
function readTinaField(el) {
|
|
106
|
+
for (const name of el.getAttributeNames()) {
|
|
107
|
+
if (name.startsWith("data-tina-field")) {
|
|
108
|
+
const value = el.getAttribute(name);
|
|
109
|
+
if (value)
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
function installStyle() {
|
|
116
|
+
if (document.getElementById(QUICK_EDIT_STYLE_ID))
|
|
117
|
+
return;
|
|
118
|
+
const style = document.createElement("style");
|
|
119
|
+
style.id = QUICK_EDIT_STYLE_ID;
|
|
120
|
+
style.textContent = QUICK_EDIT_CSS;
|
|
121
|
+
document.head.appendChild(style);
|
|
122
|
+
document.body.classList.add(QUICK_EDIT_BODY_CLASS);
|
|
123
|
+
}
|
|
124
|
+
function removeStyle() {
|
|
125
|
+
var _a;
|
|
126
|
+
(_a = document.getElementById(QUICK_EDIT_STYLE_ID)) == null ? void 0 : _a.remove();
|
|
127
|
+
document.body.classList.remove(QUICK_EDIT_BODY_CLASS);
|
|
128
|
+
}
|
|
129
|
+
function initDataStore() {
|
|
130
|
+
const data = /* @__PURE__ */ new Map();
|
|
131
|
+
const updated = /* @__PURE__ */ new Set();
|
|
132
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
133
|
+
return {
|
|
134
|
+
get: (id) => data.get(id),
|
|
135
|
+
has: (id) => data.has(id),
|
|
136
|
+
seed(id, next) {
|
|
137
|
+
data.set(id, next);
|
|
138
|
+
},
|
|
139
|
+
set(id, next) {
|
|
140
|
+
const firstUpdate = !updated.has(id);
|
|
141
|
+
updated.add(id);
|
|
142
|
+
data.set(id, next);
|
|
143
|
+
for (const listener of listeners)
|
|
144
|
+
listener({ id, firstUpdate });
|
|
145
|
+
},
|
|
146
|
+
ids: () => Array.from(data.keys()),
|
|
147
|
+
subscribe(listener) {
|
|
148
|
+
listeners.add(listener);
|
|
149
|
+
return () => listeners.delete(listener);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
const FORM_SELECTOR = "[data-tina-form]";
|
|
154
|
+
const FORM_ATTR = "data-tina-form";
|
|
155
|
+
const PRIMARY_FORM_ATTR = "data-tina-primary";
|
|
156
|
+
const RETRY_INTERVAL_MS = 250;
|
|
157
|
+
const MAX_ATTEMPTS = 40;
|
|
158
|
+
let controller = null;
|
|
159
|
+
function initForms(store) {
|
|
160
|
+
if (controller) {
|
|
161
|
+
debug("initForms called twice; ignoring");
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
controller = {
|
|
165
|
+
store,
|
|
166
|
+
active: /* @__PURE__ */ new Map(),
|
|
167
|
+
acknowledged: /* @__PURE__ */ new Set(),
|
|
168
|
+
primaryId: null,
|
|
169
|
+
retryTimer: null,
|
|
170
|
+
attempts: 0
|
|
171
|
+
};
|
|
172
|
+
window.addEventListener("message", onAck);
|
|
173
|
+
window.addEventListener("beforeunload", onBeforeUnload);
|
|
174
|
+
}
|
|
175
|
+
function refreshForms$1() {
|
|
176
|
+
if (!controller) {
|
|
177
|
+
debug("refreshForms called before initForms; ignoring");
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const { payloads: next, primaryId } = readPayloads();
|
|
181
|
+
const nextIds = new Set(next.map((p) => p.id));
|
|
182
|
+
for (const [id] of controller.active) {
|
|
183
|
+
if (nextIds.has(id))
|
|
184
|
+
continue;
|
|
185
|
+
debug("posting close for", id);
|
|
186
|
+
window.parent.postMessage({ type: "close", id }, getAdminOrigin());
|
|
187
|
+
controller.acknowledged.delete(id);
|
|
188
|
+
}
|
|
189
|
+
for (const payload of next) {
|
|
190
|
+
controller.store.seed(payload.id, payload.data ?? {});
|
|
191
|
+
}
|
|
192
|
+
controller.active = new Map(next.map((p) => [p.id, p]));
|
|
193
|
+
controller.primaryId = primaryId && nextIds.has(primaryId) ? primaryId : null;
|
|
194
|
+
if (next.some((p) => !controller.acknowledged.has(p.id))) {
|
|
195
|
+
startAnnounceLoop();
|
|
196
|
+
}
|
|
197
|
+
reportQuickEdit();
|
|
198
|
+
}
|
|
199
|
+
function readPayloads() {
|
|
200
|
+
const elements = document.querySelectorAll(FORM_SELECTOR);
|
|
201
|
+
const payloads = [];
|
|
202
|
+
let primaryId = null;
|
|
203
|
+
for (const el of elements) {
|
|
204
|
+
const raw = el.getAttribute(FORM_ATTR);
|
|
205
|
+
if (!raw)
|
|
206
|
+
continue;
|
|
207
|
+
try {
|
|
208
|
+
const payload = JSON.parse(raw);
|
|
209
|
+
if (!payload.id || !payload.query)
|
|
210
|
+
continue;
|
|
211
|
+
payloads.push(payload);
|
|
212
|
+
if (primaryId === null && el.hasAttribute(PRIMARY_FORM_ATTR)) {
|
|
213
|
+
primaryId = payload.id;
|
|
214
|
+
}
|
|
215
|
+
} catch (error) {
|
|
216
|
+
debug("failed to parse form payload", error);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
debug("discovered", payloads.length, "form(s)");
|
|
220
|
+
return { payloads, primaryId };
|
|
221
|
+
}
|
|
222
|
+
function onAck(event) {
|
|
223
|
+
if (!isFromAdmin(event))
|
|
224
|
+
return;
|
|
225
|
+
const msg = event.data;
|
|
226
|
+
if (!msg || typeof msg !== "object")
|
|
227
|
+
return;
|
|
228
|
+
if (msg.type !== "updateData" || typeof msg.id !== "string")
|
|
229
|
+
return;
|
|
230
|
+
if (!controller)
|
|
231
|
+
return;
|
|
232
|
+
if (!controller.acknowledged.has(msg.id)) {
|
|
233
|
+
debug("admin acked form", msg.id);
|
|
234
|
+
controller.acknowledged.add(msg.id);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function onBeforeUnload() {
|
|
238
|
+
if (!controller)
|
|
239
|
+
return;
|
|
240
|
+
for (const [id] of controller.active) {
|
|
241
|
+
window.parent.postMessage({ type: "close", id }, getAdminOrigin());
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function startAnnounceLoop() {
|
|
245
|
+
if (!controller)
|
|
246
|
+
return;
|
|
247
|
+
if (controller.retryTimer) {
|
|
248
|
+
clearTimeout(controller.retryTimer);
|
|
249
|
+
controller.retryTimer = null;
|
|
250
|
+
}
|
|
251
|
+
controller.attempts = 0;
|
|
252
|
+
announce();
|
|
253
|
+
}
|
|
254
|
+
function announce() {
|
|
255
|
+
if (!controller)
|
|
256
|
+
return;
|
|
257
|
+
controller.attempts++;
|
|
258
|
+
const pending = [];
|
|
259
|
+
for (const [id, payload] of controller.active) {
|
|
260
|
+
if (!controller.acknowledged.has(id))
|
|
261
|
+
pending.push(payload);
|
|
262
|
+
}
|
|
263
|
+
if (pending.length === 0) {
|
|
264
|
+
debug("all forms acked after", controller.attempts, "attempt(s)");
|
|
265
|
+
controller.retryTimer = null;
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (controller.attempts > MAX_ATTEMPTS) {
|
|
269
|
+
debug(
|
|
270
|
+
"giving up after",
|
|
271
|
+
MAX_ATTEMPTS,
|
|
272
|
+
"attempts; pending ids:",
|
|
273
|
+
pending.map((p) => p.id)
|
|
274
|
+
);
|
|
275
|
+
controller.retryTimer = null;
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
for (const payload of pending) {
|
|
279
|
+
debug("posting open for", payload.id, "attempt", controller.attempts);
|
|
280
|
+
window.parent.postMessage(
|
|
281
|
+
{
|
|
282
|
+
type: "open",
|
|
283
|
+
id: payload.id,
|
|
284
|
+
query: payload.query,
|
|
285
|
+
variables: payload.variables,
|
|
286
|
+
data: payload.data
|
|
287
|
+
},
|
|
288
|
+
getAdminOrigin()
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
if (controller.primaryId && pending.some((p) => p.id === controller.primaryId)) {
|
|
292
|
+
window.parent.postMessage(
|
|
293
|
+
{ type: "user-select-form", formId: controller.primaryId },
|
|
294
|
+
getAdminOrigin()
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
controller.retryTimer = setTimeout(announce, RETRY_INTERVAL_MS);
|
|
298
|
+
}
|
|
299
|
+
function reportQuickEdit() {
|
|
300
|
+
const hasMarkers = !!document.querySelector("[data-tina-field]");
|
|
301
|
+
window.parent.postMessage(
|
|
302
|
+
{ type: "quick-edit", value: hasMarkers },
|
|
303
|
+
getAdminOrigin()
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const PREVIEW_CONTENT_TYPE = "application/x-tina-preview+json";
|
|
307
|
+
const PRIME_HEADER = "X-Tina-Prime";
|
|
308
|
+
const ISLAND_SELECTOR = "[data-tina-island]";
|
|
309
|
+
const ENDPOINT_ATTR = "data-tina-island";
|
|
310
|
+
const PRIMARY_ISLAND_ATTR = "data-tina-island-primary";
|
|
311
|
+
const PRIMED_FORM_ATTR = "data-tina-primed";
|
|
312
|
+
function initIslandRefresh(store, options) {
|
|
313
|
+
let pendingRefresh = null;
|
|
314
|
+
const refreshAll = () => {
|
|
315
|
+
const islands = document.querySelectorAll(ISLAND_SELECTOR);
|
|
316
|
+
for (const island of islands) {
|
|
317
|
+
void refreshIsland(island, store);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
store.subscribe(({ firstUpdate }) => {
|
|
321
|
+
if (pendingRefresh) {
|
|
322
|
+
clearTimeout(pendingRefresh);
|
|
323
|
+
pendingRefresh = null;
|
|
324
|
+
}
|
|
325
|
+
if (firstUpdate) {
|
|
326
|
+
refreshAll();
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
pendingRefresh = setTimeout(() => {
|
|
330
|
+
pendingRefresh = null;
|
|
331
|
+
refreshAll();
|
|
332
|
+
}, options.debounceMs);
|
|
333
|
+
});
|
|
334
|
+
window.addEventListener("message", (event) => {
|
|
335
|
+
if (!isFromAdmin(event))
|
|
336
|
+
return;
|
|
337
|
+
const message = event.data;
|
|
338
|
+
if (!message || typeof message !== "object")
|
|
339
|
+
return;
|
|
340
|
+
if (message.type === "updateData" && typeof message.id === "string") {
|
|
341
|
+
if (!store.has(message.id)) {
|
|
342
|
+
debug("updateData for unknown id", message.id, "— ignoring");
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
debug("updateData received for", message.id);
|
|
346
|
+
store.set(message.id, message.data ?? {});
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
async function refreshIsland(island, store) {
|
|
351
|
+
const endpoint = island.getAttribute(ENDPOINT_ATTR);
|
|
352
|
+
if (!endpoint)
|
|
353
|
+
return;
|
|
354
|
+
const overlay = {};
|
|
355
|
+
for (const id of store.ids()) {
|
|
356
|
+
const data = store.get(id);
|
|
357
|
+
if (data)
|
|
358
|
+
overlay[id] = data;
|
|
359
|
+
}
|
|
360
|
+
try {
|
|
361
|
+
debug("refreshing island", endpoint);
|
|
362
|
+
const response = await fetch(endpoint, {
|
|
363
|
+
method: "POST",
|
|
364
|
+
headers: { "Content-Type": PREVIEW_CONTENT_TYPE },
|
|
365
|
+
body: JSON.stringify(overlay),
|
|
366
|
+
cache: "no-store",
|
|
367
|
+
credentials: "same-origin"
|
|
368
|
+
});
|
|
369
|
+
if (!response.ok) {
|
|
370
|
+
debug("island refetch failed", endpoint, response.status);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if (!isHtmlResponse(response)) {
|
|
374
|
+
debug("island refetch wrong content-type", endpoint);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const html = await response.text();
|
|
378
|
+
swapIslandHtml(island, html);
|
|
379
|
+
reportQuickEdit();
|
|
380
|
+
} catch (error) {
|
|
381
|
+
debug("island refetch error", endpoint, error);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
async function primeIslands() {
|
|
385
|
+
const islands = document.querySelectorAll(ISLAND_SELECTOR);
|
|
386
|
+
const results = await Promise.all(Array.from(islands, primeIsland));
|
|
387
|
+
for (const formEl of results.flat()) {
|
|
388
|
+
formEl.setAttribute(PRIMED_FORM_ATTR, "");
|
|
389
|
+
document.body.appendChild(formEl);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
async function primeIsland(island) {
|
|
393
|
+
const endpoint = island.getAttribute(ENDPOINT_ATTR);
|
|
394
|
+
if (!endpoint)
|
|
395
|
+
return [];
|
|
396
|
+
const isPrimary = island.hasAttribute(PRIMARY_ISLAND_ATTR);
|
|
397
|
+
try {
|
|
398
|
+
debug("priming island", endpoint);
|
|
399
|
+
const response = await fetch(endpoint, {
|
|
400
|
+
method: "POST",
|
|
401
|
+
headers: {
|
|
402
|
+
"Content-Type": PREVIEW_CONTENT_TYPE,
|
|
403
|
+
[PRIME_HEADER]: "1"
|
|
404
|
+
},
|
|
405
|
+
body: "{}",
|
|
406
|
+
cache: "no-store",
|
|
407
|
+
credentials: "same-origin"
|
|
408
|
+
});
|
|
409
|
+
if (!response.ok) {
|
|
410
|
+
debug("island prime failed", endpoint, response.status);
|
|
411
|
+
return [];
|
|
412
|
+
}
|
|
413
|
+
if (!isHtmlResponse(response)) {
|
|
414
|
+
debug("island prime wrong content-type", endpoint);
|
|
415
|
+
return [];
|
|
416
|
+
}
|
|
417
|
+
const template = document.createElement("template");
|
|
418
|
+
template.innerHTML = (await response.text()).trim();
|
|
419
|
+
const formEls = Array.from(
|
|
420
|
+
template.content.querySelectorAll(FORM_SELECTOR)
|
|
421
|
+
);
|
|
422
|
+
if (isPrimary && formEls[0]) {
|
|
423
|
+
formEls[0].setAttribute(PRIMARY_FORM_ATTR, "");
|
|
424
|
+
}
|
|
425
|
+
return formEls;
|
|
426
|
+
} catch (error) {
|
|
427
|
+
debug("island prime error", endpoint, error);
|
|
428
|
+
return [];
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
function isHtmlResponse(response) {
|
|
432
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
433
|
+
return contentType.includes("text/html");
|
|
434
|
+
}
|
|
435
|
+
function isAllowedSwapAttribute(name) {
|
|
436
|
+
if (name === "class" || name === "id")
|
|
437
|
+
return true;
|
|
438
|
+
return name.startsWith("data-tina-");
|
|
439
|
+
}
|
|
440
|
+
function swapIslandHtml(island, html) {
|
|
441
|
+
const template = document.createElement("template");
|
|
442
|
+
template.innerHTML = html.trim();
|
|
443
|
+
const replacement = template.content.firstElementChild;
|
|
444
|
+
if (replacement) {
|
|
445
|
+
for (const attr of Array.from(island.attributes)) {
|
|
446
|
+
if (attr.name === ENDPOINT_ATTR)
|
|
447
|
+
continue;
|
|
448
|
+
island.removeAttribute(attr.name);
|
|
449
|
+
}
|
|
450
|
+
for (const attr of Array.from(replacement.attributes)) {
|
|
451
|
+
if (!isAllowedSwapAttribute(attr.name))
|
|
452
|
+
continue;
|
|
453
|
+
island.setAttribute(attr.name, attr.value);
|
|
454
|
+
}
|
|
455
|
+
island.innerHTML = replacement.innerHTML;
|
|
456
|
+
} else {
|
|
457
|
+
island.innerHTML = html;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
const tinaField = (object, property, index) => {
|
|
461
|
+
const contentSource = object == null ? void 0 : object._content_source;
|
|
462
|
+
if (!contentSource) {
|
|
463
|
+
return "";
|
|
464
|
+
}
|
|
465
|
+
const { queryId, path } = contentSource;
|
|
466
|
+
if (!property) {
|
|
467
|
+
return `${queryId}---${path.join(".")}`;
|
|
468
|
+
}
|
|
469
|
+
const fullPath = typeof index === "number" ? [...path, property, index] : [...path, property];
|
|
470
|
+
return `${queryId}---${fullPath.join(".")}`;
|
|
471
|
+
};
|
|
472
|
+
let running = false;
|
|
473
|
+
function init(options = {}) {
|
|
474
|
+
if (running)
|
|
475
|
+
return;
|
|
476
|
+
if (typeof window === "undefined" || window.parent === window) {
|
|
477
|
+
debug("not in an iframe; bridge is a no-op");
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
debug("initialising in iframe");
|
|
481
|
+
const { debounceMs = 300, adminOrigin = window.location.origin } = options;
|
|
482
|
+
setAdminOrigin(adminOrigin);
|
|
483
|
+
const store = initDataStore();
|
|
484
|
+
initIslandRefresh(store, { debounceMs });
|
|
485
|
+
initClickToFocus();
|
|
486
|
+
initForms(store);
|
|
487
|
+
running = true;
|
|
488
|
+
refreshForms();
|
|
489
|
+
}
|
|
490
|
+
let primingInFlight = null;
|
|
491
|
+
let reprimePending = false;
|
|
492
|
+
function refreshForms() {
|
|
493
|
+
if (!running) {
|
|
494
|
+
debug("refreshForms called before init() finished; ignoring");
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
removePrimedForms();
|
|
498
|
+
const hasServerForms = document.querySelector("[data-tina-form]");
|
|
499
|
+
if (!hasServerForms && document.querySelector("[data-tina-island]")) {
|
|
500
|
+
if (primingInFlight) {
|
|
501
|
+
reprimePending = true;
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
debug("no server-injected forms; priming from island endpoints");
|
|
505
|
+
primingInFlight = primeIslands().finally(() => {
|
|
506
|
+
primingInFlight = null;
|
|
507
|
+
});
|
|
508
|
+
void primingInFlight.then(() => {
|
|
509
|
+
if (reprimePending) {
|
|
510
|
+
reprimePending = false;
|
|
511
|
+
refreshForms();
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
refreshForms$1();
|
|
515
|
+
});
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
refreshForms$1();
|
|
519
|
+
}
|
|
520
|
+
function removePrimedForms() {
|
|
521
|
+
for (const el of document.querySelectorAll(`[${PRIMED_FORM_ATTR}]`)) {
|
|
522
|
+
el.remove();
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
export {
|
|
526
|
+
init,
|
|
527
|
+
refreshForms,
|
|
528
|
+
tinaField
|
|
529
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { DataStore } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Listen for admin `updateData` events and re-fetch every island on the
|
|
4
|
+
* page with the unsaved data POSTed as a JSON body. POST (vs header)
|
|
5
|
+
* avoids the ~8KB Latin-1 cap. Refetches collapse into one debounced
|
|
6
|
+
* pass (every refresh re-renders every island).
|
|
7
|
+
*/
|
|
8
|
+
export interface IslandRefreshOptions {
|
|
9
|
+
debounceMs: number;
|
|
10
|
+
}
|
|
11
|
+
/** Marks a `[data-tina-form]` div that a prime pass appended (vs. one the
|
|
12
|
+
* server injected), so a later re-scan can drop it before re-priming. */
|
|
13
|
+
export declare const PRIMED_FORM_ATTR = "data-tina-primed";
|
|
14
|
+
export declare function initIslandRefresh(store: DataStore, options: IslandRefreshOptions): void;
|
|
15
|
+
/**
|
|
16
|
+
* Hydrate `[data-tina-form]` payloads for prerendered pages by hitting
|
|
17
|
+
* each island endpoint with the prime header set; the endpoint prepends
|
|
18
|
+
* payloads to the region HTML. The caller follows up with refreshForms()
|
|
19
|
+
* to announce them. Region HTML isn't swapped — canonical data is
|
|
20
|
+
* already on the page; the first real `updateData` handles updates.
|
|
21
|
+
*/
|
|
22
|
+
export declare function primeIslands(): Promise<void>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side helpers for the X-Tina-Preview channel. The bridge POSTs
|
|
3
|
+
* island refetches with body `{ [queryId]: data, ... }`; handlers read
|
|
4
|
+
* their slice via `readOverlay(request, queryId)`.
|
|
5
|
+
*/
|
|
6
|
+
export declare const PREVIEW_HEADER = "X-Tina-Preview";
|
|
7
|
+
export declare const PREVIEW_CONTENT_TYPE = "application/x-tina-preview+json";
|
|
8
|
+
/** Set on the one-time prime refetch for prerendered pages; the endpoint
|
|
9
|
+
* prepends `<div data-tina-form>` payloads to the region HTML. */
|
|
10
|
+
export declare const PRIME_HEADER = "X-Tina-Prime";
|
|
11
|
+
export interface PreviewEnvelope {
|
|
12
|
+
[queryId: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
export declare function readOverlay<T>(request: Request, queryId: string): Promise<T | undefined>;
|
|
15
|
+
/** Read and parse the full overlay envelope (every form on the page). */
|
|
16
|
+
export declare function readEnvelope(request: Request): Promise<PreviewEnvelope | undefined>;
|
package/dist/preview.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const PREVIEW_HEADER = "X-Tina-Preview";
|
|
2
|
+
const PREVIEW_CONTENT_TYPE = "application/x-tina-preview+json";
|
|
3
|
+
const PRIME_HEADER = "X-Tina-Prime";
|
|
4
|
+
const MAX_ENVELOPE_BYTES = 1e6;
|
|
5
|
+
async function readOverlay(request, queryId) {
|
|
6
|
+
const envelope = await readEnvelope(request);
|
|
7
|
+
if (!envelope)
|
|
8
|
+
return void 0;
|
|
9
|
+
const value = envelope[queryId];
|
|
10
|
+
return value === void 0 ? void 0 : value;
|
|
11
|
+
}
|
|
12
|
+
const envelopeCache = /* @__PURE__ */ new WeakMap();
|
|
13
|
+
function readEnvelope(request) {
|
|
14
|
+
let cached = envelopeCache.get(request);
|
|
15
|
+
if (!cached) {
|
|
16
|
+
cached = parseEnvelope(request);
|
|
17
|
+
envelopeCache.set(request, cached);
|
|
18
|
+
}
|
|
19
|
+
return cached;
|
|
20
|
+
}
|
|
21
|
+
async function parseEnvelope(request) {
|
|
22
|
+
const contentType = request.headers.get("content-type") ?? "";
|
|
23
|
+
if (!contentType.includes(PREVIEW_CONTENT_TYPE))
|
|
24
|
+
return void 0;
|
|
25
|
+
const declaredLength = Number(request.headers.get("content-length") ?? "0");
|
|
26
|
+
if (declaredLength > MAX_ENVELOPE_BYTES)
|
|
27
|
+
return void 0;
|
|
28
|
+
try {
|
|
29
|
+
const text = await request.text();
|
|
30
|
+
if (!text)
|
|
31
|
+
return void 0;
|
|
32
|
+
if (text.length > MAX_ENVELOPE_BYTES)
|
|
33
|
+
return void 0;
|
|
34
|
+
return JSON.parse(text);
|
|
35
|
+
} catch {
|
|
36
|
+
return void 0;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
PREVIEW_CONTENT_TYPE,
|
|
41
|
+
PREVIEW_HEADER,
|
|
42
|
+
PRIME_HEADER,
|
|
43
|
+
readEnvelope,
|
|
44
|
+
readOverlay
|
|
45
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
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;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const tinaField = (object, property, index) => {
|
|
2
|
+
const contentSource = object == null ? void 0 : object._content_source;
|
|
3
|
+
if (!contentSource) {
|
|
4
|
+
return "";
|
|
5
|
+
}
|
|
6
|
+
const { queryId, path } = contentSource;
|
|
7
|
+
if (!property) {
|
|
8
|
+
return `${queryId}---${path.join(".")}`;
|
|
9
|
+
}
|
|
10
|
+
const fullPath = typeof index === "number" ? [...path, property, index] : [...path, property];
|
|
11
|
+
return `${queryId}---${fullPath.join(".")}`;
|
|
12
|
+
};
|
|
13
|
+
export {
|
|
14
|
+
tinaField
|
|
15
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* postMessage protocol shared between this bridge and the TinaCMS admin
|
|
3
|
+
* (packages/@tinacms/app). Mirrors the messages emitted by useTina in
|
|
4
|
+
* packages/tinacms/src/react.tsx so the admin sees identical traffic.
|
|
5
|
+
*/
|
|
6
|
+
export type BridgeOutgoing = {
|
|
7
|
+
type: 'isEditMode';
|
|
8
|
+
} | {
|
|
9
|
+
type: 'open';
|
|
10
|
+
id: string;
|
|
11
|
+
query: string;
|
|
12
|
+
variables: object;
|
|
13
|
+
data: object;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'close';
|
|
16
|
+
id: string;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'url-changed';
|
|
19
|
+
} | {
|
|
20
|
+
type: 'field:selected';
|
|
21
|
+
fieldName: string;
|
|
22
|
+
} | {
|
|
23
|
+
type: 'quick-edit';
|
|
24
|
+
value: boolean;
|
|
25
|
+
} | {
|
|
26
|
+
type: 'user-select-form';
|
|
27
|
+
formId: string;
|
|
28
|
+
};
|
|
29
|
+
export type BridgeIncoming = {
|
|
30
|
+
type: 'tina:editMode';
|
|
31
|
+
} | {
|
|
32
|
+
type: 'updateData';
|
|
33
|
+
id: string;
|
|
34
|
+
data: object;
|
|
35
|
+
} | {
|
|
36
|
+
type: 'quickEditEnabled';
|
|
37
|
+
value: boolean;
|
|
38
|
+
};
|
|
39
|
+
export interface FormPayload {
|
|
40
|
+
id: string;
|
|
41
|
+
query: string;
|
|
42
|
+
variables: object;
|
|
43
|
+
data: object;
|
|
44
|
+
}
|
|
45
|
+
export interface DataStore {
|
|
46
|
+
/** Latest resolved data per form id. */
|
|
47
|
+
get(id: string): object | undefined;
|
|
48
|
+
/** Whether a form id has been seeded or set. */
|
|
49
|
+
has(id: string): boolean;
|
|
50
|
+
/** Populate without notifying subscribers (used for the initial seed). */
|
|
51
|
+
seed(id: string, data: object): void;
|
|
52
|
+
/** Replace cached data for a form and notify subscribers. */
|
|
53
|
+
set(id: string, data: object): void;
|
|
54
|
+
/** All known form ids. */
|
|
55
|
+
ids(): string[];
|
|
56
|
+
/** Subscribe to data updates. Returns unsubscribe. */
|
|
57
|
+
subscribe(listener: (event: {
|
|
58
|
+
id: string;
|
|
59
|
+
firstUpdate: boolean;
|
|
60
|
+
}) => void): () => void;
|
|
61
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tinacms/bridge",
|
|
3
|
+
"version": "0.0.0-0509095-20260525122024",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"package.json",
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./tina-field": {
|
|
17
|
+
"types": "./dist/tina-field.d.ts",
|
|
18
|
+
"default": "./dist/tina-field.js"
|
|
19
|
+
},
|
|
20
|
+
"./preview": {
|
|
21
|
+
"types": "./dist/preview.d.ts",
|
|
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"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"license": "Apache-2.0",
|
|
34
|
+
"buildConfig": {
|
|
35
|
+
"entryPoints": [
|
|
36
|
+
"src/index.ts",
|
|
37
|
+
"src/tina-field.ts",
|
|
38
|
+
"src/preview.ts",
|
|
39
|
+
"src/metadata.ts",
|
|
40
|
+
"src/quick-edit-css.ts"
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^22.13.1",
|
|
45
|
+
"happy-dom": "15.10.2",
|
|
46
|
+
"typescript": "^5.7.3",
|
|
47
|
+
"vite": "^5.4.14",
|
|
48
|
+
"vitest": "^2.1.9",
|
|
49
|
+
"@tinacms/scripts": "1.6.1"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"registry": "https://registry.npmjs.org"
|
|
53
|
+
},
|
|
54
|
+
"repository": {
|
|
55
|
+
"url": "https://github.com/tinacms/tinacms.git",
|
|
56
|
+
"directory": "packages/@tinacms/bridge"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"types": "tsc",
|
|
60
|
+
"build": "tinacms-scripts build",
|
|
61
|
+
"test": "vitest run",
|
|
62
|
+
"test-watch": "vitest"
|
|
63
|
+
}
|
|
64
|
+
}
|