@tinacms/bridge 0.0.1
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/data-store.d.ts +8 -0
- package/dist/debug.d.ts +1 -0
- package/dist/edit-mode.d.ts +1 -0
- package/dist/forms.d.ts +3 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +340 -0
- package/dist/island-refresh.d.ts +15 -0
- package/dist/preview.d.ts +1 -0
- package/dist/preview.js +28 -0
- package/dist/preview.test.d.ts +1 -0
- package/dist/tina-field.d.ts +1 -0
- package/dist/tina-field.js +15 -0
- package/dist/types.d.ts +57 -0
- package/package.json +54 -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;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { DataStore } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Holds the latest resolved data per form id (keyed by `hashFromQuery`-style
|
|
4
|
+
* id). Subscribers learn whether each update is the first one for that form,
|
|
5
|
+
* so island-refresh can fire immediately on the first push (newly-created
|
|
6
|
+
* docs reach a populated state ASAP) and debounce subsequent edits.
|
|
7
|
+
*/
|
|
8
|
+
export declare function initDataStore(): DataStore;
|
package/dist/debug.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function debug(...args: unknown[]): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initEditMode(timeoutMs?: number): Promise<boolean>;
|
package/dist/forms.d.ts
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../src/index"
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
const ENABLED = typeof window !== "undefined";
|
|
2
|
+
function debug(...args) {
|
|
3
|
+
if (!ENABLED)
|
|
4
|
+
return;
|
|
5
|
+
console.log("[@tinacms/bridge]", ...args);
|
|
6
|
+
}
|
|
7
|
+
const STYLE_ID = "__tina-bridge-quick-edit-style";
|
|
8
|
+
const BODY_CLASS = "__tina-quick-editing-enabled";
|
|
9
|
+
const QUICK_EDIT_CSS = `
|
|
10
|
+
[data-tina-field] {
|
|
11
|
+
outline: 2px dashed rgba(34,150,254,0.5);
|
|
12
|
+
transition: box-shadow ease-out 150ms;
|
|
13
|
+
}
|
|
14
|
+
[data-tina-field]:hover {
|
|
15
|
+
box-shadow: inset 100vi 100vh rgba(34,150,254,0.3);
|
|
16
|
+
outline: 2px solid rgba(34,150,254,1);
|
|
17
|
+
cursor: pointer;
|
|
18
|
+
}
|
|
19
|
+
[data-tina-field-overlay] {
|
|
20
|
+
outline: 2px dashed rgba(34,150,254,0.5);
|
|
21
|
+
position: relative;
|
|
22
|
+
}
|
|
23
|
+
[data-tina-field-overlay]:hover {
|
|
24
|
+
cursor: pointer;
|
|
25
|
+
outline: 2px solid rgba(34,150,254,1);
|
|
26
|
+
}
|
|
27
|
+
[data-tina-field-overlay]::after {
|
|
28
|
+
content: '';
|
|
29
|
+
position: absolute;
|
|
30
|
+
inset: 0;
|
|
31
|
+
z-index: 20;
|
|
32
|
+
transition: opacity ease-out 150ms;
|
|
33
|
+
background-color: rgba(34,150,254,0.3);
|
|
34
|
+
opacity: 0;
|
|
35
|
+
}
|
|
36
|
+
[data-tina-field-overlay]:hover::after {
|
|
37
|
+
opacity: 1;
|
|
38
|
+
}
|
|
39
|
+
`;
|
|
40
|
+
function initClickToFocus() {
|
|
41
|
+
let enabled = true;
|
|
42
|
+
document.addEventListener(
|
|
43
|
+
"click",
|
|
44
|
+
(event) => {
|
|
45
|
+
const target = event.target;
|
|
46
|
+
const tagName = target instanceof Element ? target.tagName : "?";
|
|
47
|
+
if (!enabled) {
|
|
48
|
+
debug("click ignored — quickEdit disabled", tagName);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const fieldName = resolveFieldName(target);
|
|
52
|
+
if (!fieldName) {
|
|
53
|
+
debug("click ignored — no data-tina-field ancestor for", tagName);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
debug("click captured →", fieldName, "(target:", tagName, ")");
|
|
57
|
+
event.preventDefault();
|
|
58
|
+
event.stopPropagation();
|
|
59
|
+
window.parent.postMessage(
|
|
60
|
+
{ type: "field:selected", fieldName },
|
|
61
|
+
window.location.origin
|
|
62
|
+
);
|
|
63
|
+
},
|
|
64
|
+
true
|
|
65
|
+
);
|
|
66
|
+
window.addEventListener("message", (event) => {
|
|
67
|
+
const message = event.data;
|
|
68
|
+
if (!message || message.type !== "quickEditEnabled")
|
|
69
|
+
return;
|
|
70
|
+
enabled = !!message.value;
|
|
71
|
+
if (enabled)
|
|
72
|
+
installStyle();
|
|
73
|
+
else
|
|
74
|
+
removeStyle();
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
function resolveFieldName(target) {
|
|
78
|
+
const el = target instanceof Element ? target : null;
|
|
79
|
+
if (!el)
|
|
80
|
+
return null;
|
|
81
|
+
const direct = readTinaField(el);
|
|
82
|
+
if (direct)
|
|
83
|
+
return direct;
|
|
84
|
+
const ancestor = el.closest("[data-tina-field], [data-tina-field-overlay]");
|
|
85
|
+
if (!ancestor)
|
|
86
|
+
return null;
|
|
87
|
+
return readTinaField(ancestor);
|
|
88
|
+
}
|
|
89
|
+
function readTinaField(el) {
|
|
90
|
+
for (const name of el.getAttributeNames()) {
|
|
91
|
+
if (name.startsWith("data-tina-field")) {
|
|
92
|
+
const value = el.getAttribute(name);
|
|
93
|
+
if (value)
|
|
94
|
+
return value;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
function installStyle() {
|
|
100
|
+
if (document.getElementById(STYLE_ID))
|
|
101
|
+
return;
|
|
102
|
+
const style = document.createElement("style");
|
|
103
|
+
style.id = STYLE_ID;
|
|
104
|
+
style.textContent = QUICK_EDIT_CSS;
|
|
105
|
+
document.head.appendChild(style);
|
|
106
|
+
document.body.classList.add(BODY_CLASS);
|
|
107
|
+
}
|
|
108
|
+
function removeStyle() {
|
|
109
|
+
var _a;
|
|
110
|
+
(_a = document.getElementById(STYLE_ID)) == null ? void 0 : _a.remove();
|
|
111
|
+
document.body.classList.remove(BODY_CLASS);
|
|
112
|
+
}
|
|
113
|
+
function initDataStore() {
|
|
114
|
+
const data = /* @__PURE__ */ new Map();
|
|
115
|
+
const updated = /* @__PURE__ */ new Set();
|
|
116
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
117
|
+
return {
|
|
118
|
+
get: (id) => data.get(id),
|
|
119
|
+
seed(id, next) {
|
|
120
|
+
data.set(id, next);
|
|
121
|
+
},
|
|
122
|
+
set(id, next) {
|
|
123
|
+
const firstUpdate = !updated.has(id);
|
|
124
|
+
updated.add(id);
|
|
125
|
+
data.set(id, next);
|
|
126
|
+
for (const listener of listeners)
|
|
127
|
+
listener({ id, firstUpdate });
|
|
128
|
+
},
|
|
129
|
+
ids: () => Array.from(data.keys()),
|
|
130
|
+
subscribe(listener) {
|
|
131
|
+
listeners.add(listener);
|
|
132
|
+
return () => listeners.delete(listener);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const SCRIPT_TYPE = "application/tina+json";
|
|
137
|
+
const RETRY_INTERVAL_MS = 250;
|
|
138
|
+
const MAX_ATTEMPTS = 40;
|
|
139
|
+
function initForms(store) {
|
|
140
|
+
const scripts = document.querySelectorAll(
|
|
141
|
+
`script[type="${SCRIPT_TYPE}"]`
|
|
142
|
+
);
|
|
143
|
+
const payloads = [];
|
|
144
|
+
for (const script of scripts) {
|
|
145
|
+
const raw = script.textContent;
|
|
146
|
+
if (!raw)
|
|
147
|
+
continue;
|
|
148
|
+
try {
|
|
149
|
+
const payload = JSON.parse(raw);
|
|
150
|
+
if (!payload.id || !payload.query)
|
|
151
|
+
continue;
|
|
152
|
+
payloads.push(payload);
|
|
153
|
+
store.seed(payload.id, payload.data ?? {});
|
|
154
|
+
} catch (error) {
|
|
155
|
+
debug("failed to parse form payload", error);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
debug("discovered", payloads.length, "form(s)");
|
|
159
|
+
const acknowledged = /* @__PURE__ */ new Set();
|
|
160
|
+
const onAck = (event) => {
|
|
161
|
+
const msg = event.data;
|
|
162
|
+
if (!msg || typeof msg !== "object")
|
|
163
|
+
return;
|
|
164
|
+
if (msg.type !== "updateData" || typeof msg.id !== "string")
|
|
165
|
+
return;
|
|
166
|
+
if (!acknowledged.has(msg.id)) {
|
|
167
|
+
debug("admin acked form", msg.id);
|
|
168
|
+
acknowledged.add(msg.id);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
window.addEventListener("message", onAck);
|
|
172
|
+
let attempts = 0;
|
|
173
|
+
const announce = () => {
|
|
174
|
+
attempts++;
|
|
175
|
+
const pending = payloads.filter((p) => !acknowledged.has(p.id));
|
|
176
|
+
if (pending.length === 0) {
|
|
177
|
+
debug("all forms acked after", attempts, "attempt(s)");
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (attempts > MAX_ATTEMPTS) {
|
|
181
|
+
debug(
|
|
182
|
+
"giving up after",
|
|
183
|
+
MAX_ATTEMPTS,
|
|
184
|
+
"attempts; pending ids:",
|
|
185
|
+
pending.map((p) => p.id)
|
|
186
|
+
);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
for (const payload of pending) {
|
|
190
|
+
debug("posting open for", payload.id, "attempt", attempts);
|
|
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
|
+
};
|
|
204
|
+
announce();
|
|
205
|
+
reportQuickEdit();
|
|
206
|
+
window.addEventListener("beforeunload", () => {
|
|
207
|
+
for (const payload of payloads) {
|
|
208
|
+
window.parent.postMessage(
|
|
209
|
+
{ type: "close", id: payload.id },
|
|
210
|
+
window.location.origin
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
function reportQuickEdit() {
|
|
216
|
+
const hasMarkers = !!document.querySelector("[data-tina-field]");
|
|
217
|
+
window.parent.postMessage(
|
|
218
|
+
{ type: "quick-edit", value: hasMarkers },
|
|
219
|
+
window.location.origin
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
const PREVIEW_CONTENT_TYPE = "application/x-tina-preview+json";
|
|
223
|
+
const ISLAND_SELECTOR = "[data-tina-island]";
|
|
224
|
+
const ENDPOINT_ATTR = "data-tina-island";
|
|
225
|
+
function initIslandRefresh(store, options) {
|
|
226
|
+
const debounceTimers = /* @__PURE__ */ new Map();
|
|
227
|
+
const refreshAll = () => {
|
|
228
|
+
const islands = document.querySelectorAll(ISLAND_SELECTOR);
|
|
229
|
+
for (const island of islands) {
|
|
230
|
+
void refreshIsland(island, store);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
store.subscribe(({ firstUpdate }) => {
|
|
234
|
+
const key = "__all__";
|
|
235
|
+
const existing = debounceTimers.get(key);
|
|
236
|
+
if (existing)
|
|
237
|
+
clearTimeout(existing);
|
|
238
|
+
if (firstUpdate) {
|
|
239
|
+
refreshAll();
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
debounceTimers.set(
|
|
243
|
+
key,
|
|
244
|
+
setTimeout(() => {
|
|
245
|
+
debounceTimers.delete(key);
|
|
246
|
+
refreshAll();
|
|
247
|
+
}, options.debounceMs)
|
|
248
|
+
);
|
|
249
|
+
});
|
|
250
|
+
window.addEventListener("message", (event) => {
|
|
251
|
+
const message = event.data;
|
|
252
|
+
if (!message || typeof message !== "object")
|
|
253
|
+
return;
|
|
254
|
+
if (message.type === "updateData" && typeof message.id === "string") {
|
|
255
|
+
debug("updateData received for", message.id);
|
|
256
|
+
store.set(message.id, message.data ?? {});
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
async function refreshIsland(island, store) {
|
|
261
|
+
const endpoint = island.getAttribute(ENDPOINT_ATTR);
|
|
262
|
+
if (!endpoint)
|
|
263
|
+
return;
|
|
264
|
+
const overlay = {};
|
|
265
|
+
for (const id of store.ids()) {
|
|
266
|
+
const data = store.get(id);
|
|
267
|
+
if (data)
|
|
268
|
+
overlay[id] = data;
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
debug("refreshing island", endpoint);
|
|
272
|
+
const response = await fetch(endpoint, {
|
|
273
|
+
method: "POST",
|
|
274
|
+
headers: { "Content-Type": PREVIEW_CONTENT_TYPE },
|
|
275
|
+
body: JSON.stringify(overlay),
|
|
276
|
+
cache: "no-store",
|
|
277
|
+
credentials: "same-origin"
|
|
278
|
+
});
|
|
279
|
+
if (!response.ok) {
|
|
280
|
+
debug("island refetch failed", endpoint, response.status);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const html = await response.text();
|
|
284
|
+
swapIslandHtml(island, html);
|
|
285
|
+
reportQuickEdit();
|
|
286
|
+
} catch (error) {
|
|
287
|
+
debug("island refetch error", endpoint, error);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
function swapIslandHtml(island, html) {
|
|
291
|
+
const template = document.createElement("template");
|
|
292
|
+
template.innerHTML = html.trim();
|
|
293
|
+
const fragment = template.content;
|
|
294
|
+
const replacement = fragment.firstElementChild;
|
|
295
|
+
if (replacement) {
|
|
296
|
+
for (const attr of Array.from(island.attributes)) {
|
|
297
|
+
if (attr.name === ENDPOINT_ATTR)
|
|
298
|
+
continue;
|
|
299
|
+
island.removeAttribute(attr.name);
|
|
300
|
+
}
|
|
301
|
+
for (const attr of Array.from(replacement.attributes)) {
|
|
302
|
+
island.setAttribute(attr.name, attr.value);
|
|
303
|
+
}
|
|
304
|
+
island.innerHTML = replacement.innerHTML;
|
|
305
|
+
} else {
|
|
306
|
+
island.innerHTML = html;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
const tinaField = (object, property, index) => {
|
|
310
|
+
const contentSource = object == null ? void 0 : object._content_source;
|
|
311
|
+
if (!contentSource) {
|
|
312
|
+
return "";
|
|
313
|
+
}
|
|
314
|
+
const { queryId, path } = contentSource;
|
|
315
|
+
if (!property) {
|
|
316
|
+
return `${queryId}---${path.join(".")}`;
|
|
317
|
+
}
|
|
318
|
+
const fullPath = typeof index === "number" ? [...path, property, index] : [...path, property];
|
|
319
|
+
return `${queryId}---${fullPath.join(".")}`;
|
|
320
|
+
};
|
|
321
|
+
let initialized = false;
|
|
322
|
+
function init(options = {}) {
|
|
323
|
+
if (initialized)
|
|
324
|
+
return;
|
|
325
|
+
initialized = true;
|
|
326
|
+
if (typeof window === "undefined" || window.parent === window) {
|
|
327
|
+
debug("not in an iframe; bridge is a no-op");
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
debug("initialising in iframe");
|
|
331
|
+
const { debounceMs = 300 } = options;
|
|
332
|
+
const store = initDataStore();
|
|
333
|
+
initIslandRefresh(store, { debounceMs });
|
|
334
|
+
initClickToFocus();
|
|
335
|
+
initForms(store);
|
|
336
|
+
}
|
|
337
|
+
export {
|
|
338
|
+
init,
|
|
339
|
+
tinaField
|
|
340
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { DataStore } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Listens for `{type:'updateData', id, data}` from the admin and re-fetches
|
|
4
|
+
* each `[data-tina-island]` with the unsaved data attached as the
|
|
5
|
+
* `X-Tina-Preview` header. The island endpoint reads the header (via
|
|
6
|
+
* `tina-preview` helper in the example) and renders with overlay data
|
|
7
|
+
* instead of hitting the canonical content store.
|
|
8
|
+
*
|
|
9
|
+
* The very first updateData per form fires immediately so newly-created
|
|
10
|
+
* docs leave the empty-template state ASAP. Subsequent updates are debounced.
|
|
11
|
+
*/
|
|
12
|
+
export interface IslandRefreshOptions {
|
|
13
|
+
debounceMs: number;
|
|
14
|
+
}
|
|
15
|
+
export declare function initIslandRefresh(store: DataStore, options: IslandRefreshOptions): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../src/preview"
|
package/dist/preview.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const PREVIEW_HEADER = "X-Tina-Preview";
|
|
2
|
+
const PREVIEW_CONTENT_TYPE = "application/x-tina-preview+json";
|
|
3
|
+
async function readOverlay(request, queryId) {
|
|
4
|
+
const envelope = await readEnvelope(request);
|
|
5
|
+
if (!envelope)
|
|
6
|
+
return void 0;
|
|
7
|
+
const value = envelope[queryId];
|
|
8
|
+
return value === void 0 ? void 0 : value;
|
|
9
|
+
}
|
|
10
|
+
async function readEnvelope(request) {
|
|
11
|
+
const contentType = request.headers.get("content-type") ?? "";
|
|
12
|
+
if (!contentType.includes(PREVIEW_CONTENT_TYPE))
|
|
13
|
+
return void 0;
|
|
14
|
+
try {
|
|
15
|
+
const text = await request.text();
|
|
16
|
+
if (!text)
|
|
17
|
+
return void 0;
|
|
18
|
+
return JSON.parse(text);
|
|
19
|
+
} catch {
|
|
20
|
+
return void 0;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
PREVIEW_CONTENT_TYPE,
|
|
25
|
+
PREVIEW_HEADER,
|
|
26
|
+
readEnvelope,
|
|
27
|
+
readOverlay
|
|
28
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../src/tina-field"
|
|
@@ -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,57 @@
|
|
|
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
|
+
/** Replace cached data for a form. */
|
|
49
|
+
set(id: string, data: object): void;
|
|
50
|
+
/** All known form ids. */
|
|
51
|
+
ids(): string[];
|
|
52
|
+
/** Subscribe to data updates. Returns unsubscribe. */
|
|
53
|
+
subscribe(listener: (event: {
|
|
54
|
+
id: string;
|
|
55
|
+
firstUpdate: boolean;
|
|
56
|
+
}) => void): () => void;
|
|
57
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tinacms/bridge",
|
|
3
|
+
"version": "0.0.1",
|
|
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
|
+
},
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"buildConfig": {
|
|
27
|
+
"entryPoints": [
|
|
28
|
+
"src/index.ts",
|
|
29
|
+
"src/tina-field.ts",
|
|
30
|
+
"src/preview.ts"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "^22.13.1",
|
|
35
|
+
"happy-dom": "15.10.2",
|
|
36
|
+
"typescript": "^5.7.3",
|
|
37
|
+
"vite": "^5.4.14",
|
|
38
|
+
"vitest": "^2.1.9",
|
|
39
|
+
"@tinacms/scripts": "1.6.0"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"registry": "https://registry.npmjs.org"
|
|
43
|
+
},
|
|
44
|
+
"repository": {
|
|
45
|
+
"url": "https://github.com/tinacms/tinacms.git",
|
|
46
|
+
"directory": "packages/@tinacms/bridge"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"types": "tsc",
|
|
50
|
+
"build": "tinacms-scripts build",
|
|
51
|
+
"test": "vitest run",
|
|
52
|
+
"test-watch": "vitest"
|
|
53
|
+
}
|
|
54
|
+
}
|