@407dev/cms-astro 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/{chunk-7E3XJLKX.js → chunk-SGHAANOW.js} +30 -3
- package/dist/chunk-SGHAANOW.js.map +1 -0
- package/dist/{chunk-QYUVAKMC.js → chunk-YCQMH2T7.js} +51 -10
- package/dist/chunk-YCQMH2T7.js.map +1 -0
- package/dist/fields/collections.js +1 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +32 -31
- package/dist/index.js.map +1 -1
- package/dist/middleware/index.js +2 -2
- package/package.json +5 -5
- package/src/client.ts +0 -1
- package/src/fields/attrs.ts +4 -0
- package/src/fields/collection.ts +8 -0
- package/src/fields/entryScope.test.ts +22 -1
- package/src/fields/index.ts +10 -1
- package/src/index.test.ts +117 -1
- package/src/integration.ts +29 -7
- package/src/middleware/preview.test.ts +39 -7
- package/src/middleware/preview.ts +18 -4
- package/src/preview/bridge-script.test.ts +81 -2
- package/src/preview/bridge-script.ts +30 -1
- package/dist/chunk-53OQ5RXJ.js +0 -912
- package/dist/chunk-53OQ5RXJ.js.map +0 -1
- package/dist/chunk-56OMNQLH.js +0 -1013
- package/dist/chunk-56OMNQLH.js.map +0 -1
- package/dist/chunk-5AOOMQ7K.js +0 -1031
- package/dist/chunk-5AOOMQ7K.js.map +0 -1
- package/dist/chunk-5XRUCFDC.js +0 -743
- package/dist/chunk-5XRUCFDC.js.map +0 -1
- package/dist/chunk-62ZHCOLZ.js +0 -25
- package/dist/chunk-62ZHCOLZ.js.map +0 -1
- package/dist/chunk-6AZLRSYA.js +0 -126
- package/dist/chunk-6AZLRSYA.js.map +0 -1
- package/dist/chunk-6L3ZTCFB.js +0 -14
- package/dist/chunk-6L3ZTCFB.js.map +0 -1
- package/dist/chunk-6O7ZNNDT.js +0 -697
- package/dist/chunk-6O7ZNNDT.js.map +0 -1
- package/dist/chunk-6PQIXTMK.js +0 -697
- package/dist/chunk-6PQIXTMK.js.map +0 -1
- package/dist/chunk-7AW6M7QW.js +0 -1013
- package/dist/chunk-7AW6M7QW.js.map +0 -1
- package/dist/chunk-7E3XJLKX.js.map +0 -1
- package/dist/chunk-7LM54OTA.js +0 -1031
- package/dist/chunk-7LM54OTA.js.map +0 -1
- package/dist/chunk-A3OGL4OX.js +0 -706
- package/dist/chunk-A3OGL4OX.js.map +0 -1
- package/dist/chunk-AO4UAWOV.js +0 -1030
- package/dist/chunk-AO4UAWOV.js.map +0 -1
- package/dist/chunk-BDFVGXM2.js +0 -697
- package/dist/chunk-BDFVGXM2.js.map +0 -1
- package/dist/chunk-CXM7NZVX.js +0 -1031
- package/dist/chunk-CXM7NZVX.js.map +0 -1
- package/dist/chunk-DXJSYGS6.js +0 -743
- package/dist/chunk-DXJSYGS6.js.map +0 -1
- package/dist/chunk-EFUSAQZM.js +0 -72
- package/dist/chunk-EFUSAQZM.js.map +0 -1
- package/dist/chunk-F7KDGPBN.js +0 -1013
- package/dist/chunk-F7KDGPBN.js.map +0 -1
- package/dist/chunk-FBSQT45X.js +0 -126
- package/dist/chunk-FBSQT45X.js.map +0 -1
- package/dist/chunk-FEPDOMVH.js +0 -697
- package/dist/chunk-FEPDOMVH.js.map +0 -1
- package/dist/chunk-FQDOL2QW.js +0 -25
- package/dist/chunk-FQDOL2QW.js.map +0 -1
- package/dist/chunk-GB3LGQJM.js +0 -109
- package/dist/chunk-GB3LGQJM.js.map +0 -1
- package/dist/chunk-HCNP6R3P.js +0 -736
- package/dist/chunk-HCNP6R3P.js.map +0 -1
- package/dist/chunk-J6WEJDRI.js +0 -743
- package/dist/chunk-J6WEJDRI.js.map +0 -1
- package/dist/chunk-JHEAPNJ2.js +0 -117
- package/dist/chunk-JHEAPNJ2.js.map +0 -1
- package/dist/chunk-JOJDJ6GW.js +0 -25
- package/dist/chunk-JOJDJ6GW.js.map +0 -1
- package/dist/chunk-KTDV6WYB.js +0 -202
- package/dist/chunk-KTDV6WYB.js.map +0 -1
- package/dist/chunk-KZWJBKPC.js +0 -734
- package/dist/chunk-KZWJBKPC.js.map +0 -1
- package/dist/chunk-LEAQBGL6.js +0 -109
- package/dist/chunk-LEAQBGL6.js.map +0 -1
- package/dist/chunk-LGZJL2Q7.js +0 -697
- package/dist/chunk-LGZJL2Q7.js.map +0 -1
- package/dist/chunk-LQPBU46R.js +0 -109
- package/dist/chunk-LQPBU46R.js.map +0 -1
- package/dist/chunk-M55NNUTY.js +0 -1013
- package/dist/chunk-M55NNUTY.js.map +0 -1
- package/dist/chunk-M6NCYCC3.js +0 -1013
- package/dist/chunk-M6NCYCC3.js.map +0 -1
- package/dist/chunk-MGN7OZAU.js +0 -109
- package/dist/chunk-MGN7OZAU.js.map +0 -1
- package/dist/chunk-MGYZOX4K.js +0 -1013
- package/dist/chunk-MGYZOX4K.js.map +0 -1
- package/dist/chunk-MY4IEKJS.js +0 -1031
- package/dist/chunk-MY4IEKJS.js.map +0 -1
- package/dist/chunk-NCQRGAB4.js +0 -705
- package/dist/chunk-NCQRGAB4.js.map +0 -1
- package/dist/chunk-NI4NHXI4.js +0 -697
- package/dist/chunk-NI4NHXI4.js.map +0 -1
- package/dist/chunk-NPI5N7AU.js +0 -14
- package/dist/chunk-NPI5N7AU.js.map +0 -1
- package/dist/chunk-QN4CPMJF.js +0 -126
- package/dist/chunk-QN4CPMJF.js.map +0 -1
- package/dist/chunk-QYUVAKMC.js.map +0 -1
- package/dist/chunk-RAAT6PDT.js +0 -25
- package/dist/chunk-RAAT6PDT.js.map +0 -1
- package/dist/chunk-RDPO5VPS.js +0 -1013
- package/dist/chunk-RDPO5VPS.js.map +0 -1
- package/dist/chunk-RQHIW2PF.js +0 -25
- package/dist/chunk-RQHIW2PF.js.map +0 -1
- package/dist/chunk-RREVHA52.js +0 -722
- package/dist/chunk-RREVHA52.js.map +0 -1
- package/dist/chunk-S7PT4PZN.js +0 -25
- package/dist/chunk-S7PT4PZN.js.map +0 -1
- package/dist/chunk-SUXV5M32.js +0 -109
- package/dist/chunk-SUXV5M32.js.map +0 -1
- package/dist/chunk-SZEORUY6.js +0 -696
- package/dist/chunk-SZEORUY6.js.map +0 -1
- package/dist/chunk-T7NGGWGY.js +0 -25
- package/dist/chunk-T7NGGWGY.js.map +0 -1
- package/dist/chunk-UEVWP5VE.js +0 -696
- package/dist/chunk-UEVWP5VE.js.map +0 -1
- package/dist/chunk-UJNKNP7U.js +0 -748
- package/dist/chunk-UJNKNP7U.js.map +0 -1
- package/dist/chunk-V4XVFWFI.js +0 -1013
- package/dist/chunk-V4XVFWFI.js.map +0 -1
- package/dist/chunk-VJU2CIKZ.js +0 -729
- package/dist/chunk-VJU2CIKZ.js.map +0 -1
- package/dist/chunk-VRQUP5L3.js +0 -202
- package/dist/chunk-VRQUP5L3.js.map +0 -1
- package/dist/chunk-WBJ5E7CL.js +0 -742
- package/dist/chunk-WBJ5E7CL.js.map +0 -1
- package/dist/chunk-Y3Q65C7R.js +0 -14
- package/dist/chunk-Y3Q65C7R.js.map +0 -1
- package/dist/chunk-YHEGFPTR.js +0 -1030
- package/dist/chunk-YHEGFPTR.js.map +0 -1
- package/dist/chunk-ZDLBN46C.js +0 -697
- package/dist/chunk-ZDLBN46C.js.map +0 -1
- package/dist/preview-BEkLMD6N.d.ts +0 -23
- package/dist/preview-DJEA4fj7.d.ts +0 -23
- package/dist/preview-DeD1Jn7U.d.ts +0 -23
- package/dist/types-15jfCQKb.d.ts +0 -141
- package/dist/types-BREdL9Gx.d.ts +0 -140
- package/dist/types-C6sUZNa2.d.ts +0 -104
- package/dist/types-DpeHpobR.d.ts +0 -104
package/dist/chunk-56OMNQLH.js
DELETED
|
@@ -1,1013 +0,0 @@
|
|
|
1
|
-
import { DEFAULT_DEV_CONTENT_URL } from './chunk-B4YYLS7Y.js';
|
|
2
|
-
import { isDraftMode, setActiveDraftBundle, runWithDraftBundle } from './chunk-DMLOBKYE.js';
|
|
3
|
-
import { chromeTokens } from './chunk-2TKIQIOE.js';
|
|
4
|
-
import { resolveEditorOrigins } from './chunk-XJPVYMZM.js';
|
|
5
|
-
|
|
6
|
-
// package.json
|
|
7
|
-
var package_default = {
|
|
8
|
-
version: "0.1.0"};
|
|
9
|
-
|
|
10
|
-
// src/version.ts
|
|
11
|
-
var CMS_ASTRO_VERSION = package_default.version;
|
|
12
|
-
|
|
13
|
-
// src/client.ts
|
|
14
|
-
function createCmsClient(config) {
|
|
15
|
-
const siteId = config.siteId;
|
|
16
|
-
const contentUrl = config.contentUrl.replace(/\/+$/, "");
|
|
17
|
-
const fetchFn = config.fetch || fetch;
|
|
18
|
-
let cachedPublishedBundle = null;
|
|
19
|
-
const client = {
|
|
20
|
-
siteId,
|
|
21
|
-
contentUrl,
|
|
22
|
-
previewSecret: config.previewSecret,
|
|
23
|
-
async getBundle(options) {
|
|
24
|
-
const isDraft = Boolean(options?.previewToken);
|
|
25
|
-
if (!isDraft && cachedPublishedBundle) {
|
|
26
|
-
return cachedPublishedBundle;
|
|
27
|
-
}
|
|
28
|
-
const url = isDraft ? `${contentUrl}/draft/${siteId}/bundle` : `${contentUrl}/content/${siteId}/bundle`;
|
|
29
|
-
const headers = {
|
|
30
|
-
"x-cms-sdk": `cms-astro@${CMS_ASTRO_VERSION}`
|
|
31
|
-
};
|
|
32
|
-
if (options?.previewToken) {
|
|
33
|
-
headers["x-preview-token"] = options.previewToken;
|
|
34
|
-
}
|
|
35
|
-
const res = await fetchFn(url, {
|
|
36
|
-
headers,
|
|
37
|
-
cache: "no-store"
|
|
38
|
-
});
|
|
39
|
-
if (!res.ok) {
|
|
40
|
-
const error = new Error(
|
|
41
|
-
`CMS Client: Failed to fetch bundle from ${url} (status ${res.status}): ${res.statusText}`
|
|
42
|
-
);
|
|
43
|
-
error.status = res.status;
|
|
44
|
-
throw error;
|
|
45
|
-
}
|
|
46
|
-
const bundle = await res.json();
|
|
47
|
-
if (!isDraft) {
|
|
48
|
-
cachedPublishedBundle = bundle;
|
|
49
|
-
}
|
|
50
|
-
return bundle;
|
|
51
|
-
},
|
|
52
|
-
async getFields(options) {
|
|
53
|
-
const bundle = await this.getBundle(options);
|
|
54
|
-
return bundle.fields || {};
|
|
55
|
-
},
|
|
56
|
-
async getField(key, options) {
|
|
57
|
-
const fields = await this.getFields(options);
|
|
58
|
-
return fields[key] ?? null;
|
|
59
|
-
},
|
|
60
|
-
async getCollections(options) {
|
|
61
|
-
const bundle = await this.getBundle(options);
|
|
62
|
-
return bundle.collections || {};
|
|
63
|
-
},
|
|
64
|
-
async getEntries(collectionKey, options) {
|
|
65
|
-
const bundle = await this.getBundle(options);
|
|
66
|
-
return bundle.entries?.[collectionKey] || [];
|
|
67
|
-
},
|
|
68
|
-
async getEntry(collectionKey, slug, options) {
|
|
69
|
-
const entries = await this.getEntries(collectionKey, options);
|
|
70
|
-
return entries.find((e) => e.slug === slug) || null;
|
|
71
|
-
},
|
|
72
|
-
async getRoutes(options) {
|
|
73
|
-
const bundle = await this.getBundle(options);
|
|
74
|
-
return bundle.routes || [];
|
|
75
|
-
},
|
|
76
|
-
async getSiteConfig(options) {
|
|
77
|
-
const bundle = await this.getBundle(options);
|
|
78
|
-
return {
|
|
79
|
-
site_id: bundle.site_id,
|
|
80
|
-
redirects: bundle.redirects || [],
|
|
81
|
-
robots: bundle.settings?.robots || {},
|
|
82
|
-
default_seo: bundle.settings?.default_seo || {},
|
|
83
|
-
sitemap: bundle.settings?.sitemap || {},
|
|
84
|
-
not_found_path: bundle.settings?.not_found_path || null
|
|
85
|
-
};
|
|
86
|
-
},
|
|
87
|
-
clearCache() {
|
|
88
|
-
cachedPublishedBundle = null;
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
return client;
|
|
92
|
-
}
|
|
93
|
-
var globalWithCms = globalThis;
|
|
94
|
-
var globalClient = typeof globalThis !== "undefined" && globalWithCms.__CMS_CLIENT__ || null;
|
|
95
|
-
function initCmsClient(options) {
|
|
96
|
-
if (options.client) {
|
|
97
|
-
globalClient = options.client;
|
|
98
|
-
if (typeof globalThis !== "undefined") {
|
|
99
|
-
globalWithCms.__CMS_CLIENT__ = options.client;
|
|
100
|
-
}
|
|
101
|
-
return globalClient;
|
|
102
|
-
}
|
|
103
|
-
globalClient = createCmsClient({
|
|
104
|
-
siteId: options.siteId || process.env.CMS_SITE_ID || "",
|
|
105
|
-
contentUrl: options.contentUrl || process.env.CMS_CONTENT_URL || DEFAULT_DEV_CONTENT_URL,
|
|
106
|
-
previewSecret: options.previewSecret || process.env.CMS_PREVIEW_SECRET
|
|
107
|
-
});
|
|
108
|
-
if (typeof globalThis !== "undefined") {
|
|
109
|
-
globalWithCms.__CMS_CLIENT__ = globalClient;
|
|
110
|
-
}
|
|
111
|
-
return globalClient;
|
|
112
|
-
}
|
|
113
|
-
function getClient() {
|
|
114
|
-
return globalClient || typeof globalThis !== "undefined" && globalWithCms.__CMS_CLIENT__ || null;
|
|
115
|
-
}
|
|
116
|
-
function setClient(client) {
|
|
117
|
-
globalClient = client;
|
|
118
|
-
if (typeof globalThis !== "undefined") {
|
|
119
|
-
globalWithCms.__CMS_CLIENT__ = client;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// src/preview/bridge-script.ts
|
|
124
|
-
var BRIDGE_PROTOCOL_VERSION = 1;
|
|
125
|
-
function buildBridgeScript(options = {}) {
|
|
126
|
-
const allowedOrigins = resolveEditorOrigins(options.editorOrigins);
|
|
127
|
-
const allowedOriginsJson = JSON.stringify(allowedOrigins);
|
|
128
|
-
return `(function() {
|
|
129
|
-
if (typeof window === 'undefined') return;
|
|
130
|
-
if (window.__CMS_BRIDGE_INITIALIZED__) return;
|
|
131
|
-
window.__CMS_BRIDGE_INITIALIZED__ = true;
|
|
132
|
-
|
|
133
|
-
var PROTOCOL_VERSION = ${BRIDGE_PROTOCOL_VERSION};
|
|
134
|
-
var allowedOrigins = ${allowedOriginsJson};
|
|
135
|
-
var connectedOrigin = null;
|
|
136
|
-
var connectedSource = null;
|
|
137
|
-
var activeKey = null;
|
|
138
|
-
var watchedAnchors = [];
|
|
139
|
-
var rafScheduled = false;
|
|
140
|
-
var isPatchingDom = false;
|
|
141
|
-
var debounceRescanTimer = null;
|
|
142
|
-
|
|
143
|
-
function normalizeOrigin(raw) {
|
|
144
|
-
if (!raw || typeof raw !== 'string') return null;
|
|
145
|
-
var trimmed = raw.trim();
|
|
146
|
-
if (!trimmed) return null;
|
|
147
|
-
try {
|
|
148
|
-
var withScheme = /^[a-zA-Z][a-zA-Z0-9+.-]*:\\/\\//.test(trimmed) ? trimmed : 'https://' + trimmed;
|
|
149
|
-
var url = new URL(withScheme);
|
|
150
|
-
if (!url.protocol || !url.host) return null;
|
|
151
|
-
return (url.protocol + '//' + url.host).toLowerCase();
|
|
152
|
-
} catch (e) {
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
var normalizedAllowed = allowedOrigins.map(normalizeOrigin).filter(Boolean);
|
|
158
|
-
|
|
159
|
-
function isOriginAllowed(origin) {
|
|
160
|
-
var norm = normalizeOrigin(origin);
|
|
161
|
-
if (!norm) return false;
|
|
162
|
-
return normalizedAllowed.indexOf(norm) !== -1;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function postToEditor(msg) {
|
|
166
|
-
if (!connectedSource || !connectedOrigin) return;
|
|
167
|
-
try {
|
|
168
|
-
connectedSource.postMessage(msg, connectedOrigin);
|
|
169
|
-
} catch (err) {
|
|
170
|
-
// Failed to post
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Inject Bridge Styles
|
|
175
|
-
var isEmbedded = false;
|
|
176
|
-
try {
|
|
177
|
-
isEmbedded = window.self !== window.top || window.parent !== window;
|
|
178
|
-
} catch (_e) {
|
|
179
|
-
isEmbedded = true;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
var toolbarTag = 'astro-' + 'dev-toolbar';
|
|
183
|
-
|
|
184
|
-
function ensureBridgeStyles() {
|
|
185
|
-
if (typeof document === 'undefined' || !document || typeof document.getElementById !== 'function') return;
|
|
186
|
-
if (document.getElementById('cms-bridge-styles')) return;
|
|
187
|
-
var style = document.createElement('style');
|
|
188
|
-
style.id = 'cms-bridge-styles';
|
|
189
|
-
style.textContent = [
|
|
190
|
-
isEmbedded ? toolbarTag + ' { display: none !important; }' : '',
|
|
191
|
-
'html[data-cms-comment-mode],',
|
|
192
|
-
'html[data-cms-comment-mode] * {',
|
|
193
|
-
' cursor: crosshair !important;',
|
|
194
|
-
'}',
|
|
195
|
-
'html[data-cms-edit-mode] [data-cms-field],',
|
|
196
|
-
'html[data-cms-edit-mode] [data-cms-entry-field] {',
|
|
197
|
-
' cursor: pointer !important;',
|
|
198
|
-
' outline: 1px dashed rgba(99, 102, 241, 0.5) !important;',
|
|
199
|
-
' outline-offset: 2px !important;',
|
|
200
|
-
' transition: outline 0.15s ease-in-out !important;',
|
|
201
|
-
'}',
|
|
202
|
-
'html[data-cms-edit-mode] [data-cms-field]:hover,',
|
|
203
|
-
'html[data-cms-edit-mode] [data-cms-entry-field]:hover {',
|
|
204
|
-
' outline: 2px solid ${chromeTokens.primary} !important;',
|
|
205
|
-
' outline-offset: 2px !important;',
|
|
206
|
-
'}',
|
|
207
|
-
'html[data-cms-edit-mode] [data-cms-group] {',
|
|
208
|
-
' outline: 1px dashed rgba(16, 185, 129, 0.4) !important;',
|
|
209
|
-
' outline-offset: 3px !important;',
|
|
210
|
-
'}',
|
|
211
|
-
'html[data-cms-edit-mode] [data-cms-group]:hover {',
|
|
212
|
-
' outline: 2px solid ${chromeTokens.success} !important;',
|
|
213
|
-
' outline-offset: 3px !important;',
|
|
214
|
-
'}'
|
|
215
|
-
].filter(Boolean).join('\\n');
|
|
216
|
-
|
|
217
|
-
var target = document.head || document.documentElement;
|
|
218
|
-
if (target) {
|
|
219
|
-
target.appendChild(style);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
function hideDevToolbar() {
|
|
224
|
-
if (!isEmbedded) return;
|
|
225
|
-
ensureBridgeStyles();
|
|
226
|
-
if (typeof document === 'undefined' || !document || typeof document.querySelector !== 'function') return;
|
|
227
|
-
try {
|
|
228
|
-
var tb = document.querySelector(toolbarTag);
|
|
229
|
-
if (tb && tb instanceof HTMLElement) {
|
|
230
|
-
tb.style.setProperty('display', 'none', 'important');
|
|
231
|
-
}
|
|
232
|
-
} catch (_e) {}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
ensureBridgeStyles();
|
|
236
|
-
hideDevToolbar();
|
|
237
|
-
|
|
238
|
-
function inferFieldKind(el) {
|
|
239
|
-
if (!el) return 'unknown';
|
|
240
|
-
var tag = el.tagName ? el.tagName.toLowerCase() : '';
|
|
241
|
-
if (tag === 'img') return 'image';
|
|
242
|
-
if (tag === 'video') return 'video';
|
|
243
|
-
var explicitKind = el.getAttribute('data-cms-kind');
|
|
244
|
-
if (explicitKind) return explicitKind;
|
|
245
|
-
if (el.classList.contains('rich-body') || el.classList.contains('cms-richtext') || el.getAttribute('data-cms-richtext') === 'true') {
|
|
246
|
-
return 'richtext';
|
|
247
|
-
}
|
|
248
|
-
return 'text';
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function getElementRect(el) {
|
|
252
|
-
var r = el.getBoundingClientRect();
|
|
253
|
-
return {
|
|
254
|
-
top: r.top,
|
|
255
|
-
left: r.left,
|
|
256
|
-
width: r.width,
|
|
257
|
-
height: r.height
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
function parseGroupAttr(raw) {
|
|
262
|
-
if (!raw) return undefined;
|
|
263
|
-
var idx = raw.indexOf(':');
|
|
264
|
-
if (idx === -1) return { group: raw, scope: '' };
|
|
265
|
-
return {
|
|
266
|
-
group: raw.slice(0, idx),
|
|
267
|
-
scope: raw.slice(idx + 1)
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
function scanRenderedFields() {
|
|
272
|
-
var fieldEls = document.querySelectorAll('[data-cms-field], [data-cms-entry-field]');
|
|
273
|
-
var fieldMap = {};
|
|
274
|
-
|
|
275
|
-
for (var i = 0; i < fieldEls.length; i++) {
|
|
276
|
-
var el = fieldEls[i];
|
|
277
|
-
var key = el.getAttribute('data-cms-field') || el.getAttribute('data-cms-entry-field') || '';
|
|
278
|
-
if (!key) continue;
|
|
279
|
-
|
|
280
|
-
var isEntry = el.hasAttribute('data-cms-entry-field');
|
|
281
|
-
var groupEl = el.closest('[data-cms-group]');
|
|
282
|
-
var collectionEl = el.closest('[data-cms-collection]');
|
|
283
|
-
var groupName = undefined;
|
|
284
|
-
if (groupEl) {
|
|
285
|
-
var g = parseGroupAttr(groupEl.getAttribute('data-cms-group'));
|
|
286
|
-
if (g) groupName = g.group;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
var entryRef = undefined;
|
|
290
|
-
if (isEntry || collectionEl) {
|
|
291
|
-
entryRef = {
|
|
292
|
-
collection: collectionEl ? collectionEl.getAttribute('data-cms-collection') || '' : '',
|
|
293
|
-
slug: collectionEl ? collectionEl.getAttribute('data-cms-entry') || '' : '',
|
|
294
|
-
path: isEntry ? key : key
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
var uniqueId = (isEntry && entryRef ? entryRef.collection + ':' + entryRef.slug + ':' : '') + key;
|
|
299
|
-
|
|
300
|
-
if (!fieldMap[uniqueId]) {
|
|
301
|
-
fieldMap[uniqueId] = {
|
|
302
|
-
key: key,
|
|
303
|
-
kind: inferFieldKind(el),
|
|
304
|
-
group: groupName,
|
|
305
|
-
entryRef: entryRef,
|
|
306
|
-
instances: 1
|
|
307
|
-
};
|
|
308
|
-
} else {
|
|
309
|
-
fieldMap[uniqueId].instances++;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
var fields = [];
|
|
314
|
-
for (var k in fieldMap) {
|
|
315
|
-
if (Object.prototype.hasOwnProperty.call(fieldMap, k)) {
|
|
316
|
-
fields.push(fieldMap[k]);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
window.__CMS_FIELDS__ = fields;
|
|
321
|
-
window.__CMS_RESCAN__ = scanRenderedFields;
|
|
322
|
-
|
|
323
|
-
try {
|
|
324
|
-
document.dispatchEvent(new CustomEvent('cms:fields-updated', { detail: fields }));
|
|
325
|
-
} catch (_e) {}
|
|
326
|
-
|
|
327
|
-
postToEditor({
|
|
328
|
-
source: 'cms-preview',
|
|
329
|
-
v: PROTOCOL_VERSION,
|
|
330
|
-
type: 'rendered-fields',
|
|
331
|
-
fields: fields
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
function debouncedRescan() {
|
|
336
|
-
if (isPatchingDom) return;
|
|
337
|
-
if (debounceRescanTimer) clearTimeout(debounceRescanTimer);
|
|
338
|
-
debounceRescanTimer = setTimeout(function() {
|
|
339
|
-
scanRenderedFields();
|
|
340
|
-
scheduleRectsUpdate();
|
|
341
|
-
}, 150);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
var currentObserver = null;
|
|
345
|
-
function setupMutationObserver() {
|
|
346
|
-
if (typeof MutationObserver === 'undefined' || !document.body) return;
|
|
347
|
-
if (currentObserver) {
|
|
348
|
-
try {
|
|
349
|
-
currentObserver.disconnect();
|
|
350
|
-
} catch (_e) {}
|
|
351
|
-
}
|
|
352
|
-
currentObserver = new MutationObserver(function(mutations) {
|
|
353
|
-
if (isPatchingDom) return;
|
|
354
|
-
hideDevToolbar();
|
|
355
|
-
debouncedRescan();
|
|
356
|
-
});
|
|
357
|
-
currentObserver.observe(document.body, {
|
|
358
|
-
childList: true,
|
|
359
|
-
subtree: true,
|
|
360
|
-
attributeFilter: ['data-cms-field', 'data-cms-entry-field', 'data-cms-group', 'data-cms-collection', 'data-cms-entry']
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
function buildStableSelector(target) {
|
|
365
|
-
if (!target || !target.tagName) return '';
|
|
366
|
-
if (target === document.documentElement) return 'html';
|
|
367
|
-
if (target === document.body) return 'body';
|
|
368
|
-
|
|
369
|
-
var parts = [];
|
|
370
|
-
var curr = target;
|
|
371
|
-
var maxLevels = 8;
|
|
372
|
-
|
|
373
|
-
while (curr && curr !== document.documentElement && parts.length < maxLevels) {
|
|
374
|
-
if (curr === document.body) {
|
|
375
|
-
parts.unshift('body');
|
|
376
|
-
break;
|
|
377
|
-
}
|
|
378
|
-
var id = curr.getAttribute('id');
|
|
379
|
-
if (id && id.trim()) {
|
|
380
|
-
var escapedId = (typeof CSS !== 'undefined' && CSS.escape) ? CSS.escape(id.trim()) : id.trim();
|
|
381
|
-
parts.unshift('#' + escapedId);
|
|
382
|
-
break;
|
|
383
|
-
}
|
|
384
|
-
var tag = curr.tagName ? curr.tagName.toLowerCase() : '';
|
|
385
|
-
if (!tag) break;
|
|
386
|
-
var count = 1;
|
|
387
|
-
var sib = curr.previousElementSibling;
|
|
388
|
-
while (sib) {
|
|
389
|
-
if (sib.tagName && sib.tagName.toLowerCase() === tag) {
|
|
390
|
-
count++;
|
|
391
|
-
}
|
|
392
|
-
sib = sib.previousElementSibling;
|
|
393
|
-
}
|
|
394
|
-
parts.unshift(tag + ':nth-of-type(' + count + ')');
|
|
395
|
-
curr = curr.parentElement;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
return parts.join(' > ');
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
function buildElementLabel(el) {
|
|
402
|
-
if (!el || !el.tagName) return 'element';
|
|
403
|
-
|
|
404
|
-
var ariaLabel = el.getAttribute('aria-label');
|
|
405
|
-
if (ariaLabel && ariaLabel.trim()) {
|
|
406
|
-
return ariaLabel.trim();
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
var id = el.getAttribute('id');
|
|
410
|
-
if (id && id.trim()) {
|
|
411
|
-
return '#' + id.trim();
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
var tag = el.tagName.toLowerCase();
|
|
415
|
-
|
|
416
|
-
if (tag === 'nav') return 'Navigation';
|
|
417
|
-
if (tag === 'header') return 'Header';
|
|
418
|
-
if (tag === 'footer') return 'Footer';
|
|
419
|
-
if (tag === 'aside') return 'Sidebar';
|
|
420
|
-
if (tag === 'form') return 'Form';
|
|
421
|
-
|
|
422
|
-
if (tag === 'img') {
|
|
423
|
-
var alt = el.getAttribute('alt');
|
|
424
|
-
if (alt && alt.trim()) return alt.trim();
|
|
425
|
-
return 'img';
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
if (tag === 'a' || tag === 'button') {
|
|
429
|
-
var text = (el.textContent || '').trim().replace(/\\s+/g, ' ');
|
|
430
|
-
if (text) {
|
|
431
|
-
return text.length > 40 ? text.slice(0, 37) + '...' : text;
|
|
432
|
-
}
|
|
433
|
-
return tag;
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
return tag || 'element';
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
function buildCandidateEntry(targetEl, clientX, clientY) {
|
|
440
|
-
if (!targetEl || !targetEl.tagName) return null;
|
|
441
|
-
var selector = buildStableSelector(targetEl);
|
|
442
|
-
if (!selector) return null;
|
|
443
|
-
var all = Array.prototype.slice.call(document.querySelectorAll(selector));
|
|
444
|
-
var instance = Math.max(0, all.indexOf(targetEl));
|
|
445
|
-
var hitRect = targetEl.getBoundingClientRect();
|
|
446
|
-
var offsetX = typeof clientX === 'number' ? Math.round(clientX - hitRect.left) : 0;
|
|
447
|
-
var offsetY = typeof clientY === 'number' ? Math.round(clientY - hitRect.top) : 0;
|
|
448
|
-
return {
|
|
449
|
-
selector: selector,
|
|
450
|
-
instance: instance,
|
|
451
|
-
kind: 'element',
|
|
452
|
-
label: buildElementLabel(targetEl),
|
|
453
|
-
offsetX: offsetX,
|
|
454
|
-
offsetY: offsetY,
|
|
455
|
-
rect: getElementRect(targetEl)
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
function buildAnchorChain(el, clientX, clientY) {
|
|
460
|
-
var chain = [];
|
|
461
|
-
var curr = el;
|
|
462
|
-
while (curr && curr !== document.documentElement && curr !== document.body) {
|
|
463
|
-
var hit = curr.closest('[data-cms-field], [data-cms-entry-field], [data-cms-group], [data-cms-collection][data-cms-entry]');
|
|
464
|
-
if (!hit) break;
|
|
465
|
-
|
|
466
|
-
var selector = '';
|
|
467
|
-
var kind = 'unknown';
|
|
468
|
-
var label = '';
|
|
469
|
-
|
|
470
|
-
if (hit.hasAttribute('data-cms-field')) {
|
|
471
|
-
var fieldKey = hit.getAttribute('data-cms-field') || '';
|
|
472
|
-
selector = '[data-cms-field="' + fieldKey + '"]';
|
|
473
|
-
kind = inferFieldKind(hit);
|
|
474
|
-
label = fieldKey;
|
|
475
|
-
} else if (hit.hasAttribute('data-cms-entry-field')) {
|
|
476
|
-
var entryFieldKey = hit.getAttribute('data-cms-entry-field') || '';
|
|
477
|
-
selector = '[data-cms-entry-field="' + entryFieldKey + '"]';
|
|
478
|
-
kind = inferFieldKind(hit);
|
|
479
|
-
label = entryFieldKey;
|
|
480
|
-
} else if (hit.hasAttribute('data-cms-group')) {
|
|
481
|
-
var groupAttr = hit.getAttribute('data-cms-group') || '';
|
|
482
|
-
selector = '[data-cms-group="' + groupAttr + '"]';
|
|
483
|
-
kind = 'group';
|
|
484
|
-
var parsed = parseGroupAttr(groupAttr);
|
|
485
|
-
label = parsed ? parsed.group : groupAttr;
|
|
486
|
-
} else if (hit.hasAttribute('data-cms-collection') && hit.hasAttribute('data-cms-entry')) {
|
|
487
|
-
var col = hit.getAttribute('data-cms-collection') || '';
|
|
488
|
-
var ent = hit.getAttribute('data-cms-entry') || '';
|
|
489
|
-
selector = '[data-cms-collection="' + col + '"][data-cms-entry="' + ent + '"]';
|
|
490
|
-
kind = 'entry';
|
|
491
|
-
label = col + '/' + ent;
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
if (selector) {
|
|
495
|
-
var all = Array.prototype.slice.call(document.querySelectorAll(selector));
|
|
496
|
-
var instance = Math.max(0, all.indexOf(hit));
|
|
497
|
-
var hitRect = hit.getBoundingClientRect();
|
|
498
|
-
var offsetX = typeof clientX === 'number' ? Math.round(clientX - hitRect.left) : 0;
|
|
499
|
-
var offsetY = typeof clientY === 'number' ? Math.round(clientY - hitRect.top) : 0;
|
|
500
|
-
chain.push({
|
|
501
|
-
selector: selector,
|
|
502
|
-
instance: instance,
|
|
503
|
-
kind: kind,
|
|
504
|
-
label: label,
|
|
505
|
-
offsetX: offsetX,
|
|
506
|
-
offsetY: offsetY,
|
|
507
|
-
rect: getElementRect(hit)
|
|
508
|
-
});
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
curr = hit.parentElement;
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
if (chain.length > 0) {
|
|
515
|
-
return chain;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
if (!el || !el.tagName) return chain;
|
|
519
|
-
|
|
520
|
-
var interactiveEl = el.closest ? el.closest('a, button') : null;
|
|
521
|
-
var selfEl = interactiveEl || (el.closest ? el.closest('a, button, nav, header, footer, aside, main, section, article, form, img, svg, [role]') : null) || el;
|
|
522
|
-
var landmarkEl = selfEl.closest ? selfEl.closest('nav, header, footer, aside, main, section, article, form, [role="navigation"], [role="banner"], [role="contentinfo"]') : null;
|
|
523
|
-
|
|
524
|
-
var selfEntry = buildCandidateEntry(selfEl, clientX, clientY);
|
|
525
|
-
if (selfEntry) {
|
|
526
|
-
chain.push(selfEntry);
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
if (landmarkEl && landmarkEl !== selfEl) {
|
|
530
|
-
var landmarkEntry = buildCandidateEntry(landmarkEl, clientX, clientY);
|
|
531
|
-
if (landmarkEntry) {
|
|
532
|
-
chain.push(landmarkEntry);
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
return chain;
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
function resolveAnchorEl(anchor) {
|
|
540
|
-
if (!anchor || !anchor.selector) return null;
|
|
541
|
-
try {
|
|
542
|
-
var els = document.querySelectorAll(anchor.selector);
|
|
543
|
-
if (!els || els.length === 0) return null;
|
|
544
|
-
var inst = typeof anchor.instance === 'number' ? anchor.instance : 0;
|
|
545
|
-
if (inst >= 0 && inst < els.length) return els[inst];
|
|
546
|
-
return els[0];
|
|
547
|
-
} catch (_e) {
|
|
548
|
-
return null;
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
function scheduleRectsUpdate() {
|
|
553
|
-
if (rafScheduled) return;
|
|
554
|
-
rafScheduled = true;
|
|
555
|
-
requestAnimationFrame(function() {
|
|
556
|
-
rafScheduled = false;
|
|
557
|
-
var rects = {};
|
|
558
|
-
if (activeKey) {
|
|
559
|
-
var el = document.querySelector('[data-cms-field="' + activeKey + '"], [data-cms-entry-field="' + activeKey + '"]');
|
|
560
|
-
if (el) {
|
|
561
|
-
rects[activeKey] = getElementRect(el);
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
postToEditor({
|
|
565
|
-
source: 'cms-preview',
|
|
566
|
-
v: PROTOCOL_VERSION,
|
|
567
|
-
type: 'rects-update',
|
|
568
|
-
rects: rects,
|
|
569
|
-
scrollY: window.scrollY || window.pageYOffset || 0
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
if (watchedAnchors && watchedAnchors.length > 0) {
|
|
573
|
-
var anchorRects = {};
|
|
574
|
-
for (var a = 0; a < watchedAnchors.length; a++) {
|
|
575
|
-
var wAnchor = watchedAnchors[a];
|
|
576
|
-
var resolvedEl = resolveAnchorEl(wAnchor);
|
|
577
|
-
if (resolvedEl) {
|
|
578
|
-
anchorRects[wAnchor.id] = getElementRect(resolvedEl);
|
|
579
|
-
} else {
|
|
580
|
-
anchorRects[wAnchor.id] = null;
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
postToEditor({
|
|
584
|
-
source: 'cms-preview',
|
|
585
|
-
v: PROTOCOL_VERSION,
|
|
586
|
-
type: 'anchor-rects',
|
|
587
|
-
rects: anchorRects
|
|
588
|
-
});
|
|
589
|
-
}
|
|
590
|
-
});
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
// Click handler
|
|
594
|
-
document.addEventListener('click', function(e) {
|
|
595
|
-
if (document.documentElement.hasAttribute('data-cms-comment-mode')) {
|
|
596
|
-
e.preventDefault();
|
|
597
|
-
e.stopPropagation();
|
|
598
|
-
var target = e.target;
|
|
599
|
-
if (!target || !(target instanceof Element)) return;
|
|
600
|
-
|
|
601
|
-
var docWidth = Math.max(
|
|
602
|
-
document.documentElement.scrollWidth || 0,
|
|
603
|
-
document.body ? document.body.scrollWidth : 0,
|
|
604
|
-
window.innerWidth || 0
|
|
605
|
-
);
|
|
606
|
-
var docHeight = Math.max(
|
|
607
|
-
document.documentElement.scrollHeight || 0,
|
|
608
|
-
document.body ? document.body.scrollHeight : 0,
|
|
609
|
-
window.innerHeight || 0
|
|
610
|
-
);
|
|
611
|
-
var pageX = e.pageX !== undefined ? e.pageX : (e.clientX + (window.scrollX || window.pageXOffset || 0));
|
|
612
|
-
var pageY = e.pageY !== undefined ? e.pageY : (e.clientY + (window.scrollY || window.pageYOffset || 0));
|
|
613
|
-
var pageXPct = docWidth > 0 ? Math.round((pageX / docWidth) * 10000) / 100 : 0;
|
|
614
|
-
var pageYPct = docHeight > 0 ? Math.round((pageY / docHeight) * 10000) / 100 : 0;
|
|
615
|
-
|
|
616
|
-
var chain = buildAnchorChain(target, e.clientX, e.clientY);
|
|
617
|
-
var clickRect = chain.length > 0 ? chain[0].rect : {
|
|
618
|
-
top: e.clientY,
|
|
619
|
-
left: e.clientX,
|
|
620
|
-
width: 0,
|
|
621
|
-
height: 0
|
|
622
|
-
};
|
|
623
|
-
|
|
624
|
-
postToEditor({
|
|
625
|
-
source: 'cms-preview',
|
|
626
|
-
v: PROTOCOL_VERSION,
|
|
627
|
-
type: 'comment-click',
|
|
628
|
-
chain: chain,
|
|
629
|
-
point: { pageXPct: pageXPct, pageYPct: pageYPct },
|
|
630
|
-
rect: clickRect
|
|
631
|
-
});
|
|
632
|
-
return;
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
if (!document.documentElement.hasAttribute('data-cms-edit-mode')) return;
|
|
636
|
-
var target = e.target;
|
|
637
|
-
if (!target || !(target instanceof Element)) return;
|
|
638
|
-
|
|
639
|
-
var fieldEl = target.closest('[data-cms-field], [data-cms-entry-field]');
|
|
640
|
-
var groupEl = target.closest('[data-cms-group]');
|
|
641
|
-
|
|
642
|
-
if (fieldEl) {
|
|
643
|
-
e.preventDefault();
|
|
644
|
-
e.stopPropagation();
|
|
645
|
-
|
|
646
|
-
var isEntry = fieldEl.hasAttribute('data-cms-entry-field');
|
|
647
|
-
var key = fieldEl.getAttribute('data-cms-field') || fieldEl.getAttribute('data-cms-entry-field') || '';
|
|
648
|
-
var kind = inferFieldKind(fieldEl);
|
|
649
|
-
var rect = getElementRect(fieldEl);
|
|
650
|
-
activeKey = key;
|
|
651
|
-
|
|
652
|
-
var selector = isEntry ? '[data-cms-entry-field="' + key + '"]' : '[data-cms-field="' + key + '"]';
|
|
653
|
-
var all = Array.prototype.slice.call(document.querySelectorAll(selector));
|
|
654
|
-
var instance = Math.max(0, all.indexOf(fieldEl));
|
|
655
|
-
|
|
656
|
-
var collectionEl = fieldEl.closest('[data-cms-collection]');
|
|
657
|
-
var entryRef = (isEntry || collectionEl) ? {
|
|
658
|
-
collection: collectionEl ? collectionEl.getAttribute('data-cms-collection') || '' : '',
|
|
659
|
-
slug: collectionEl ? collectionEl.getAttribute('data-cms-entry') || '' : '',
|
|
660
|
-
path: isEntry ? key : key
|
|
661
|
-
} : undefined;
|
|
662
|
-
|
|
663
|
-
var groupPath = groupEl ? parseGroupAttr(groupEl.getAttribute('data-cms-group')) : undefined;
|
|
664
|
-
|
|
665
|
-
try {
|
|
666
|
-
document.dispatchEvent(new CustomEvent('cms:field-selected', {
|
|
667
|
-
detail: {
|
|
668
|
-
key: key,
|
|
669
|
-
kind: kind,
|
|
670
|
-
entryRef: entryRef,
|
|
671
|
-
groupPath: groupPath
|
|
672
|
-
}
|
|
673
|
-
}));
|
|
674
|
-
} catch (_e) {}
|
|
675
|
-
|
|
676
|
-
postToEditor({
|
|
677
|
-
source: 'cms-preview',
|
|
678
|
-
v: PROTOCOL_VERSION,
|
|
679
|
-
type: 'field-click',
|
|
680
|
-
key: key,
|
|
681
|
-
kind: kind,
|
|
682
|
-
rect: rect,
|
|
683
|
-
instance: instance,
|
|
684
|
-
entryRef: entryRef,
|
|
685
|
-
groupPath: groupPath
|
|
686
|
-
});
|
|
687
|
-
return;
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
if (groupEl) {
|
|
691
|
-
e.preventDefault();
|
|
692
|
-
e.stopPropagation();
|
|
693
|
-
|
|
694
|
-
var g = parseGroupAttr(groupEl.getAttribute('data-cms-group'));
|
|
695
|
-
if (g) {
|
|
696
|
-
var gRect = getElementRect(groupEl);
|
|
697
|
-
postToEditor({
|
|
698
|
-
source: 'cms-preview',
|
|
699
|
-
v: PROTOCOL_VERSION,
|
|
700
|
-
type: 'group-click',
|
|
701
|
-
group: g.group,
|
|
702
|
-
scope: g.scope,
|
|
703
|
-
rect: gRect
|
|
704
|
-
});
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
}, true);
|
|
708
|
-
|
|
709
|
-
// Message handler
|
|
710
|
-
window.addEventListener('message', function(e) {
|
|
711
|
-
var data = e.data;
|
|
712
|
-
if (!data || typeof data !== 'object') return;
|
|
713
|
-
if (data.source !== 'cms-editor') return;
|
|
714
|
-
|
|
715
|
-
if (data.type === 'hello') {
|
|
716
|
-
if (!isOriginAllowed(e.origin)) return;
|
|
717
|
-
connectedOrigin = e.origin;
|
|
718
|
-
connectedSource = e.source;
|
|
719
|
-
|
|
720
|
-
postToEditor({
|
|
721
|
-
source: 'cms-preview',
|
|
722
|
-
v: PROTOCOL_VERSION,
|
|
723
|
-
type: 'ready',
|
|
724
|
-
protocolVersion: PROTOCOL_VERSION,
|
|
725
|
-
capabilities: ['comments']
|
|
726
|
-
});
|
|
727
|
-
|
|
728
|
-
scanRenderedFields();
|
|
729
|
-
return;
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
if (!connectedOrigin || e.origin !== connectedOrigin) return;
|
|
733
|
-
|
|
734
|
-
if (data.type === 'highlight-mode') {
|
|
735
|
-
if (data.on) {
|
|
736
|
-
document.documentElement.setAttribute('data-cms-edit-mode', 'true');
|
|
737
|
-
} else {
|
|
738
|
-
document.documentElement.removeAttribute('data-cms-edit-mode');
|
|
739
|
-
}
|
|
740
|
-
return;
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
if (data.type === 'comment-mode') {
|
|
744
|
-
if (data.on) {
|
|
745
|
-
document.documentElement.setAttribute('data-cms-comment-mode', 'true');
|
|
746
|
-
} else {
|
|
747
|
-
document.documentElement.removeAttribute('data-cms-comment-mode');
|
|
748
|
-
}
|
|
749
|
-
return;
|
|
750
|
-
}
|
|
751
|
-
|
|
752
|
-
if (data.type === 'watch-anchors') {
|
|
753
|
-
watchedAnchors = Array.isArray(data.anchors) ? data.anchors : [];
|
|
754
|
-
scheduleRectsUpdate();
|
|
755
|
-
return;
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
if (data.type === 'scroll-to-anchor') {
|
|
759
|
-
var sId = data.id;
|
|
760
|
-
var targetItem = null;
|
|
761
|
-
for (var si = 0; si < watchedAnchors.length; si++) {
|
|
762
|
-
if (watchedAnchors[si].id === sId) {
|
|
763
|
-
targetItem = watchedAnchors[si];
|
|
764
|
-
break;
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
if (targetItem) {
|
|
768
|
-
var elToScroll = resolveAnchorEl(targetItem);
|
|
769
|
-
if (elToScroll) {
|
|
770
|
-
elToScroll.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
771
|
-
scheduleRectsUpdate();
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
return;
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
if (data.type === 'patch') {
|
|
778
|
-
isPatchingDom = true;
|
|
779
|
-
var patchKey = data.key;
|
|
780
|
-
var patchKind = data.kind;
|
|
781
|
-
var patchVal = data.value;
|
|
782
|
-
var patchEntry = data.entryRef;
|
|
783
|
-
|
|
784
|
-
var patchSelector;
|
|
785
|
-
var fPath = patchEntry && patchEntry.path ? patchEntry.path : patchKey;
|
|
786
|
-
if (patchEntry && (patchEntry.collection || patchEntry.slug)) {
|
|
787
|
-
var colAttr = patchEntry.collection ? '[data-cms-collection="' + patchEntry.collection + '"]' : '';
|
|
788
|
-
var entryAttr = patchEntry.slug ? '[data-cms-entry="' + patchEntry.slug + '"]' : '';
|
|
789
|
-
var scopeSel = colAttr + entryAttr;
|
|
790
|
-
patchSelector = scopeSel + ' [data-cms-entry-field="' + fPath + '"], ' +
|
|
791
|
-
scopeSel + '[data-cms-entry-field="' + fPath + '"]';
|
|
792
|
-
} else if (patchEntry) {
|
|
793
|
-
patchSelector = '[data-cms-entry-field="' + fPath + '"]';
|
|
794
|
-
} else {
|
|
795
|
-
patchSelector = '[data-cms-field="' + patchKey + '"]';
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
var matchingEls = document.querySelectorAll(patchSelector);
|
|
799
|
-
for (var j = 0; j < matchingEls.length; j++) {
|
|
800
|
-
var mEl = matchingEls[j];
|
|
801
|
-
var isStructured = patchKind === 'geo.area' || patchKind === 'geoArea' ||
|
|
802
|
-
patchKind === 'address' || patchKind === 'address.list' ||
|
|
803
|
-
patchKind === 'media.videoList' || patchKind === 'videoList' ||
|
|
804
|
-
mEl.getAttribute('data-cms-kind') === 'geo.area' ||
|
|
805
|
-
mEl.getAttribute('data-cms-kind') === 'address.list' ||
|
|
806
|
-
mEl.getAttribute('data-cms-kind') === 'media.videoList';
|
|
807
|
-
|
|
808
|
-
if (patchKind === 'image' || mEl.tagName.toLowerCase() === 'img') {
|
|
809
|
-
if (patchVal && typeof patchVal === 'object') {
|
|
810
|
-
if (patchVal.url) mEl.setAttribute('src', patchVal.url);
|
|
811
|
-
if (patchVal.alt !== undefined) mEl.setAttribute('alt', patchVal.alt);
|
|
812
|
-
} else if (typeof patchVal === 'string') {
|
|
813
|
-
mEl.setAttribute('src', patchVal);
|
|
814
|
-
}
|
|
815
|
-
} else if (patchKind === 'video' || mEl.tagName.toLowerCase() === 'video') {
|
|
816
|
-
if (patchVal && typeof patchVal === 'object') {
|
|
817
|
-
if (patchVal.url) mEl.setAttribute('src', patchVal.url);
|
|
818
|
-
if (patchVal.poster !== undefined) mEl.setAttribute('poster', patchVal.poster);
|
|
819
|
-
} else if (typeof patchVal === 'string') {
|
|
820
|
-
mEl.setAttribute('src', patchVal);
|
|
821
|
-
}
|
|
822
|
-
if (typeof mEl.load === 'function') mEl.load();
|
|
823
|
-
} else if (isStructured || (patchVal && typeof patchVal === 'object')) {
|
|
824
|
-
// Structured non-image field values (e.g. videoList, geoArea, address) cannot be rendered
|
|
825
|
-
// via simple textContent replacement without producing [object Object].
|
|
826
|
-
// Skip the text write and leave server-rendered markup intact; structured fields refresh via reload.
|
|
827
|
-
} else {
|
|
828
|
-
mEl.textContent = patchVal !== null && patchVal !== undefined ? String(patchVal) : '';
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
isPatchingDom = false;
|
|
833
|
-
scanRenderedFields();
|
|
834
|
-
scheduleRectsUpdate();
|
|
835
|
-
return;
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
if (data.type === 'scroll-to') {
|
|
839
|
-
var scrollKey = data.key;
|
|
840
|
-
var scrollInstance = typeof data.instance === 'number' ? data.instance : 0;
|
|
841
|
-
var scrollEls = document.querySelectorAll('[data-cms-field="' + scrollKey + '"], [data-cms-entry-field="' + scrollKey + '"]');
|
|
842
|
-
if (scrollEls.length > scrollInstance) {
|
|
843
|
-
scrollEls[scrollInstance].scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
844
|
-
activeKey = scrollKey;
|
|
845
|
-
scheduleRectsUpdate();
|
|
846
|
-
}
|
|
847
|
-
return;
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
if (data.type === 'reload') {
|
|
851
|
-
window.location.reload();
|
|
852
|
-
return;
|
|
853
|
-
}
|
|
854
|
-
});
|
|
855
|
-
|
|
856
|
-
function handlePageTransition() {
|
|
857
|
-
hideDevToolbar();
|
|
858
|
-
scanRenderedFields();
|
|
859
|
-
setupMutationObserver();
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
window.addEventListener('scroll', scheduleRectsUpdate, { passive: true });
|
|
863
|
-
window.addEventListener('resize', scheduleRectsUpdate, { passive: true });
|
|
864
|
-
|
|
865
|
-
document.addEventListener('astro:page-load', handlePageTransition);
|
|
866
|
-
document.addEventListener('astro:after-swap', handlePageTransition);
|
|
867
|
-
window.addEventListener('popstate', handlePageTransition);
|
|
868
|
-
|
|
869
|
-
if (document.readyState === 'loading') {
|
|
870
|
-
document.addEventListener('DOMContentLoaded', function() {
|
|
871
|
-
handlePageTransition();
|
|
872
|
-
});
|
|
873
|
-
} else {
|
|
874
|
-
handlePageTransition();
|
|
875
|
-
}
|
|
876
|
-
})();`;
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
// src/middleware/preview.ts
|
|
880
|
-
function createPreviewMiddleware(options = {}) {
|
|
881
|
-
let client = options.client;
|
|
882
|
-
return async (context, next) => {
|
|
883
|
-
const globalWithCms2 = globalThis;
|
|
884
|
-
if (typeof globalThis !== "undefined" && globalWithCms2.__CMS_IS_REWRITING__ || isDraftMode() || context.locals?.__cms_is_draft) {
|
|
885
|
-
return next();
|
|
886
|
-
}
|
|
887
|
-
const rawUrl = context.url;
|
|
888
|
-
const url = rawUrl && typeof rawUrl.href === "string" ? new URL(rawUrl.href) : rawUrl && typeof rawUrl.pathname === "string" ? rawUrl : new URL(context.request?.url || "/", "http://localhost");
|
|
889
|
-
const pathname = url.pathname || "/";
|
|
890
|
-
const isPreviewRoute = pathname === "/_preview" || pathname.startsWith("/_preview/");
|
|
891
|
-
const previewToken = (rawUrl?.searchParams?.get ? rawUrl.searchParams.get("preview_token") : null) || (url?.searchParams?.get ? url.searchParams.get("preview_token") : null) || (context.request?.headers?.get ? context.request.headers.get("x-preview-token") : null);
|
|
892
|
-
if (!isPreviewRoute && !previewToken) {
|
|
893
|
-
return next();
|
|
894
|
-
}
|
|
895
|
-
if (!client) {
|
|
896
|
-
client = getClient() || void 0;
|
|
897
|
-
}
|
|
898
|
-
if (!client) {
|
|
899
|
-
const metaEnv = import.meta.env;
|
|
900
|
-
const siteId = options.siteId || typeof process !== "undefined" && process.env?.CMS_SITE_ID || typeof import.meta !== "undefined" && metaEnv?.CMS_SITE_ID || "";
|
|
901
|
-
const contentUrl = options.contentUrl || typeof process !== "undefined" && process.env?.CMS_CONTENT_URL || typeof import.meta !== "undefined" && metaEnv?.CMS_CONTENT_URL || "";
|
|
902
|
-
if (siteId && contentUrl) {
|
|
903
|
-
client = createCmsClient({
|
|
904
|
-
siteId,
|
|
905
|
-
contentUrl
|
|
906
|
-
});
|
|
907
|
-
}
|
|
908
|
-
}
|
|
909
|
-
if (!previewToken) {
|
|
910
|
-
return new Response("Preview token is missing", {
|
|
911
|
-
status: 400,
|
|
912
|
-
headers: {
|
|
913
|
-
"Cache-Control": "private, no-store",
|
|
914
|
-
"X-Robots-Tag": "noindex, nofollow"
|
|
915
|
-
}
|
|
916
|
-
});
|
|
917
|
-
}
|
|
918
|
-
if (!client) {
|
|
919
|
-
return new Response("CMS client is missing or not configured", {
|
|
920
|
-
status: 500,
|
|
921
|
-
headers: {
|
|
922
|
-
"Cache-Control": "private, no-store",
|
|
923
|
-
"X-Robots-Tag": "noindex, nofollow"
|
|
924
|
-
}
|
|
925
|
-
});
|
|
926
|
-
}
|
|
927
|
-
let draftBundle;
|
|
928
|
-
try {
|
|
929
|
-
draftBundle = await client.getBundle({ previewToken });
|
|
930
|
-
} catch (_err) {
|
|
931
|
-
return new Response("Preview token is not valid for this site", {
|
|
932
|
-
status: 404,
|
|
933
|
-
headers: {
|
|
934
|
-
"Cache-Control": "private, no-store",
|
|
935
|
-
"X-Robots-Tag": "noindex, nofollow"
|
|
936
|
-
}
|
|
937
|
-
});
|
|
938
|
-
}
|
|
939
|
-
if (context.locals) {
|
|
940
|
-
context.locals.__cms_is_draft = true;
|
|
941
|
-
context.locals.__cms_draft_bundle = draftBundle;
|
|
942
|
-
}
|
|
943
|
-
let finalResponse;
|
|
944
|
-
setActiveDraftBundle(draftBundle);
|
|
945
|
-
try {
|
|
946
|
-
finalResponse = await runWithDraftBundle(draftBundle, async () => {
|
|
947
|
-
let response;
|
|
948
|
-
if (isPreviewRoute) {
|
|
949
|
-
const basePath = pathname.replace(/^\/_preview/, "") || "/";
|
|
950
|
-
if (typeof context.rewrite === "function") {
|
|
951
|
-
if (typeof globalThis !== "undefined") {
|
|
952
|
-
globalWithCms2.__CMS_IS_REWRITING__ = true;
|
|
953
|
-
}
|
|
954
|
-
try {
|
|
955
|
-
const rewritten = await context.rewrite(basePath);
|
|
956
|
-
if (rewritten) {
|
|
957
|
-
response = rewritten;
|
|
958
|
-
} else {
|
|
959
|
-
response = await next();
|
|
960
|
-
}
|
|
961
|
-
} finally {
|
|
962
|
-
if (typeof globalThis !== "undefined") {
|
|
963
|
-
globalWithCms2.__CMS_IS_REWRITING__ = false;
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
} else {
|
|
967
|
-
response = await next();
|
|
968
|
-
}
|
|
969
|
-
} else {
|
|
970
|
-
response = await next();
|
|
971
|
-
}
|
|
972
|
-
const contentType = response?.headers?.get ? response.headers.get("content-type") : "";
|
|
973
|
-
const isStaticAsset = Boolean(
|
|
974
|
-
pathname.match(/\.(js|css|png|jpg|jpeg|gif|svg|ico|webp|woff|woff2|ttf|json|map)$/i)
|
|
975
|
-
);
|
|
976
|
-
const isHtml = typeof contentType === "string" && contentType.includes("text/html") || !contentType && !isStaticAsset;
|
|
977
|
-
if (isHtml) {
|
|
978
|
-
const html = await response.text();
|
|
979
|
-
const bridgeScript = buildBridgeScript({ editorOrigins: options.editorOrigins });
|
|
980
|
-
const scriptTag = `<script>${bridgeScript}</script>`;
|
|
981
|
-
const modifiedHtml = html.includes("</body>") ? html.replace("</body>", `${scriptTag}</body>`) : `${html}${scriptTag}`;
|
|
982
|
-
const newHeaders = new Headers(response.headers);
|
|
983
|
-
if (!newHeaders.has("content-type")) {
|
|
984
|
-
newHeaders.set("content-type", "text/html; charset=utf-8");
|
|
985
|
-
}
|
|
986
|
-
newHeaders.delete("content-length");
|
|
987
|
-
newHeaders.set("Cache-Control", "private, no-store, no-cache, must-revalidate");
|
|
988
|
-
newHeaders.set("X-Robots-Tag", "noindex, nofollow");
|
|
989
|
-
return new Response(modifiedHtml, {
|
|
990
|
-
status: response.status,
|
|
991
|
-
statusText: response.statusText,
|
|
992
|
-
headers: newHeaders
|
|
993
|
-
});
|
|
994
|
-
}
|
|
995
|
-
const nonHtmlHeaders = new Headers(response.headers);
|
|
996
|
-
nonHtmlHeaders.set("Cache-Control", "private, no-store, no-cache, must-revalidate");
|
|
997
|
-
nonHtmlHeaders.set("X-Robots-Tag", "noindex, nofollow");
|
|
998
|
-
return new Response(response.body, {
|
|
999
|
-
status: response.status,
|
|
1000
|
-
statusText: response.statusText,
|
|
1001
|
-
headers: nonHtmlHeaders
|
|
1002
|
-
});
|
|
1003
|
-
});
|
|
1004
|
-
} finally {
|
|
1005
|
-
setActiveDraftBundle(null);
|
|
1006
|
-
}
|
|
1007
|
-
return finalResponse;
|
|
1008
|
-
};
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
export { buildBridgeScript, createCmsClient, createPreviewMiddleware, getClient, initCmsClient, setClient };
|
|
1012
|
-
//# sourceMappingURL=chunk-56OMNQLH.js.map
|
|
1013
|
-
//# sourceMappingURL=chunk-56OMNQLH.js.map
|