@tinacms/bridge 0.0.0-06ef2b1-20260511030441
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 +11 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +412 -0
- package/dist/island-refresh.d.ts +22 -0
- package/dist/metadata.d.ts +22 -0
- package/dist/metadata.js +66 -0
- package/dist/preview.d.ts +29 -0
- package/dist/preview.js +34 -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/tina-field.d.ts +14 -0
- package/dist/tina-field.js +15 -0
- package/dist/types.d.ts +59 -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,11 @@
|
|
|
1
|
+
import type { DataStore } from './types';
|
|
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;
|
|
11
|
+
export declare function reportQuickEdit(): void;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,412 @@
|
|
|
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
|
+
seed(id, next) {
|
|
136
|
+
data.set(id, next);
|
|
137
|
+
},
|
|
138
|
+
set(id, next) {
|
|
139
|
+
const firstUpdate = !updated.has(id);
|
|
140
|
+
updated.add(id);
|
|
141
|
+
data.set(id, next);
|
|
142
|
+
for (const listener of listeners)
|
|
143
|
+
listener({ id, firstUpdate });
|
|
144
|
+
},
|
|
145
|
+
ids: () => Array.from(data.keys()),
|
|
146
|
+
subscribe(listener) {
|
|
147
|
+
listeners.add(listener);
|
|
148
|
+
return () => listeners.delete(listener);
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
const FORM_SELECTOR = "[data-tina-form]";
|
|
153
|
+
const FORM_ATTR = "data-tina-form";
|
|
154
|
+
const RETRY_INTERVAL_MS = 250;
|
|
155
|
+
const MAX_ATTEMPTS = 40;
|
|
156
|
+
let controller = null;
|
|
157
|
+
function initForms(store) {
|
|
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);
|
|
198
|
+
const payloads = [];
|
|
199
|
+
for (const el of elements) {
|
|
200
|
+
const raw = el.getAttribute(FORM_ATTR);
|
|
201
|
+
if (!raw)
|
|
202
|
+
continue;
|
|
203
|
+
try {
|
|
204
|
+
const payload = JSON.parse(raw);
|
|
205
|
+
if (!payload.id || !payload.query)
|
|
206
|
+
continue;
|
|
207
|
+
payloads.push(payload);
|
|
208
|
+
} catch (error) {
|
|
209
|
+
debug("failed to parse form payload", error);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
debug("discovered", payloads.length, "form(s)");
|
|
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;
|
|
245
|
+
announce();
|
|
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);
|
|
285
|
+
}
|
|
286
|
+
function reportQuickEdit() {
|
|
287
|
+
const hasMarkers = !!document.querySelector("[data-tina-field]");
|
|
288
|
+
window.parent.postMessage(
|
|
289
|
+
{ type: "quick-edit", value: hasMarkers },
|
|
290
|
+
getAdminOrigin()
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
const PREVIEW_CONTENT_TYPE = "application/x-tina-preview+json";
|
|
294
|
+
const ISLAND_SELECTOR = "[data-tina-island]";
|
|
295
|
+
const ENDPOINT_ATTR = "data-tina-island";
|
|
296
|
+
function initIslandRefresh(store, options) {
|
|
297
|
+
let pendingRefresh = null;
|
|
298
|
+
const refreshAll = () => {
|
|
299
|
+
const islands = document.querySelectorAll(ISLAND_SELECTOR);
|
|
300
|
+
for (const island of islands) {
|
|
301
|
+
void refreshIsland(island, store);
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
store.subscribe(({ firstUpdate }) => {
|
|
305
|
+
if (pendingRefresh) {
|
|
306
|
+
clearTimeout(pendingRefresh);
|
|
307
|
+
pendingRefresh = null;
|
|
308
|
+
}
|
|
309
|
+
if (firstUpdate) {
|
|
310
|
+
refreshAll();
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
pendingRefresh = setTimeout(() => {
|
|
314
|
+
pendingRefresh = null;
|
|
315
|
+
refreshAll();
|
|
316
|
+
}, options.debounceMs);
|
|
317
|
+
});
|
|
318
|
+
window.addEventListener("message", (event) => {
|
|
319
|
+
if (!isFromAdmin(event))
|
|
320
|
+
return;
|
|
321
|
+
const message = event.data;
|
|
322
|
+
if (!message || typeof message !== "object")
|
|
323
|
+
return;
|
|
324
|
+
if (message.type === "updateData" && typeof message.id === "string") {
|
|
325
|
+
debug("updateData received for", message.id);
|
|
326
|
+
store.set(message.id, message.data ?? {});
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
async function refreshIsland(island, store) {
|
|
331
|
+
const endpoint = island.getAttribute(ENDPOINT_ATTR);
|
|
332
|
+
if (!endpoint)
|
|
333
|
+
return;
|
|
334
|
+
const overlay = {};
|
|
335
|
+
for (const id of store.ids()) {
|
|
336
|
+
const data = store.get(id);
|
|
337
|
+
if (data)
|
|
338
|
+
overlay[id] = data;
|
|
339
|
+
}
|
|
340
|
+
try {
|
|
341
|
+
debug("refreshing island", endpoint);
|
|
342
|
+
const response = await fetch(endpoint, {
|
|
343
|
+
method: "POST",
|
|
344
|
+
headers: { "Content-Type": PREVIEW_CONTENT_TYPE },
|
|
345
|
+
body: JSON.stringify(overlay),
|
|
346
|
+
cache: "no-store",
|
|
347
|
+
credentials: "same-origin"
|
|
348
|
+
});
|
|
349
|
+
if (!response.ok) {
|
|
350
|
+
debug("island refetch failed", endpoint, response.status);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const html = await response.text();
|
|
354
|
+
swapIslandHtml(island, html);
|
|
355
|
+
reportQuickEdit();
|
|
356
|
+
} catch (error) {
|
|
357
|
+
debug("island refetch error", endpoint, error);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function swapIslandHtml(island, html) {
|
|
361
|
+
const template = document.createElement("template");
|
|
362
|
+
template.innerHTML = html.trim();
|
|
363
|
+
const fragment = template.content;
|
|
364
|
+
const replacement = fragment.firstElementChild;
|
|
365
|
+
if (replacement) {
|
|
366
|
+
for (const attr of Array.from(island.attributes)) {
|
|
367
|
+
if (attr.name === ENDPOINT_ATTR)
|
|
368
|
+
continue;
|
|
369
|
+
island.removeAttribute(attr.name);
|
|
370
|
+
}
|
|
371
|
+
for (const attr of Array.from(replacement.attributes)) {
|
|
372
|
+
island.setAttribute(attr.name, attr.value);
|
|
373
|
+
}
|
|
374
|
+
island.innerHTML = replacement.innerHTML;
|
|
375
|
+
} else {
|
|
376
|
+
island.innerHTML = html;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
const tinaField = (object, property, index) => {
|
|
380
|
+
const contentSource = object == null ? void 0 : object._content_source;
|
|
381
|
+
if (!contentSource) {
|
|
382
|
+
return "";
|
|
383
|
+
}
|
|
384
|
+
const { queryId, path } = contentSource;
|
|
385
|
+
if (!property) {
|
|
386
|
+
return `${queryId}---${path.join(".")}`;
|
|
387
|
+
}
|
|
388
|
+
const fullPath = typeof index === "number" ? [...path, property, index] : [...path, property];
|
|
389
|
+
return `${queryId}---${fullPath.join(".")}`;
|
|
390
|
+
};
|
|
391
|
+
let initialized = false;
|
|
392
|
+
function init(options = {}) {
|
|
393
|
+
if (initialized)
|
|
394
|
+
return;
|
|
395
|
+
initialized = true;
|
|
396
|
+
if (typeof window === "undefined" || window.parent === window) {
|
|
397
|
+
debug("not in an iframe; bridge is a no-op");
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
debug("initialising in iframe");
|
|
401
|
+
const { debounceMs = 300, adminOrigin = window.location.origin } = options;
|
|
402
|
+
setAdminOrigin(adminOrigin);
|
|
403
|
+
const store = initDataStore();
|
|
404
|
+
initIslandRefresh(store, { debounceMs });
|
|
405
|
+
initClickToFocus();
|
|
406
|
+
initForms(store);
|
|
407
|
+
}
|
|
408
|
+
export {
|
|
409
|
+
init,
|
|
410
|
+
refreshForms,
|
|
411
|
+
tinaField
|
|
412
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { DataStore } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Listens for `{type:'updateData', id, data}` from the admin and re-fetches
|
|
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
|
+
*
|
|
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.
|
|
18
|
+
*/
|
|
19
|
+
export interface IslandRefreshOptions {
|
|
20
|
+
debounceMs: number;
|
|
21
|
+
}
|
|
22
|
+
export declare function initIslandRefresh(store: DataStore, options: IslandRefreshOptions): void;
|
|
@@ -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,29 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const PREVIEW_HEADER = "X-Tina-Preview";
|
|
2
|
+
const PREVIEW_CONTENT_TYPE = "application/x-tina-preview+json";
|
|
3
|
+
const MAX_ENVELOPE_BYTES = 1e6;
|
|
4
|
+
async function readOverlay(request, queryId) {
|
|
5
|
+
const envelope = await readEnvelope(request);
|
|
6
|
+
if (!envelope)
|
|
7
|
+
return void 0;
|
|
8
|
+
const value = envelope[queryId];
|
|
9
|
+
return value === void 0 ? void 0 : value;
|
|
10
|
+
}
|
|
11
|
+
async function readEnvelope(request) {
|
|
12
|
+
const contentType = request.headers.get("content-type") ?? "";
|
|
13
|
+
if (!contentType.includes(PREVIEW_CONTENT_TYPE))
|
|
14
|
+
return void 0;
|
|
15
|
+
const declaredLength = Number(request.headers.get("content-length") ?? "0");
|
|
16
|
+
if (declaredLength > MAX_ENVELOPE_BYTES)
|
|
17
|
+
return void 0;
|
|
18
|
+
try {
|
|
19
|
+
const text = await request.text();
|
|
20
|
+
if (!text)
|
|
21
|
+
return void 0;
|
|
22
|
+
if (text.length > MAX_ENVELOPE_BYTES)
|
|
23
|
+
return void 0;
|
|
24
|
+
return JSON.parse(text);
|
|
25
|
+
} catch {
|
|
26
|
+
return void 0;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
PREVIEW_CONTENT_TYPE,
|
|
31
|
+
PREVIEW_HEADER,
|
|
32
|
+
readEnvelope,
|
|
33
|
+
readOverlay
|
|
34
|
+
};
|
|
@@ -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,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,59 @@
|
|
|
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
|
+
/** 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. */
|
|
51
|
+
set(id: string, data: object): void;
|
|
52
|
+
/** All known form ids. */
|
|
53
|
+
ids(): string[];
|
|
54
|
+
/** Subscribe to data updates. Returns unsubscribe. */
|
|
55
|
+
subscribe(listener: (event: {
|
|
56
|
+
id: string;
|
|
57
|
+
firstUpdate: boolean;
|
|
58
|
+
}) => void): () => void;
|
|
59
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tinacms/bridge",
|
|
3
|
+
"version": "0.0.0-06ef2b1-20260511030441",
|
|
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
|
+
}
|