@eldrajs/theme-core 0.1.0 → 0.3.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/CHANGELOG.md +38 -0
- package/dist/{bridge-DfNINXLL.js → bridge-CLi2P6Wh.js} +1 -1
- package/dist/bridge.js +1 -1
- package/dist/index.d.ts +438 -1
- package/dist/index.js +634 -6
- package/dist/{overlay-BvkEOiMB.js → overlay-C8-F1jzU.js} +1 -1
- package/dist/overlay.js +1 -1
- package/dist/template-layout.js +2 -327
- package/dist/templateLayout-CpZb6EEg.js +382 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { decodeStega, stripStega } from "./stega.js";
|
|
2
|
-
import { l as RICH_TEXT_COMMAND_NAMES } from "./bridge-
|
|
2
|
+
import { l as RICH_TEXT_COMMAND_NAMES } from "./bridge-CLi2P6Wh.js";
|
|
3
3
|
import { framingDrag, framingZoom, imageFramingStyle, parseImageFramingValueAttr, unscaledFrameRect } from "./image-framing.js";
|
|
4
4
|
import { activeLayoutBreakpoint } from "./layout.js";
|
|
5
5
|
//#region src/layoutIdentity.ts
|
package/dist/overlay.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "./stega.js";
|
|
2
2
|
import "./image-framing.js";
|
|
3
|
-
import { n as createOverlayRuntime, t as classifyBeforeInput } from "./overlay-
|
|
3
|
+
import { n as createOverlayRuntime, t as classifyBeforeInput } from "./overlay-C8-F1jzU.js";
|
|
4
4
|
import "./layout.js";
|
|
5
5
|
import { PREVIEW_ROUTER_MESSAGE_TYPES, createPreviewMessageRouter } from "./preview-router.js";
|
|
6
6
|
export { PREVIEW_ROUTER_MESSAGE_TYPES, classifyBeforeInput, createOverlayRuntime, createPreviewMessageRouter };
|
package/dist/template-layout.js
CHANGED
|
@@ -1,328 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
//#region src/templateLayout.ts
|
|
4
|
-
/**
|
|
5
|
-
* Flattens a block's `migrations` array (each step's `renames: [{ from, to
|
|
6
|
-
* }]`) into one `from -> to` map, applied in ascending `version` order with
|
|
7
|
-
* chains resolved: a step renaming `a -> b` followed by one renaming `b -> c`
|
|
8
|
-
* yields `{ a: 'c', b: 'c' }`, so every historical name for a field resolves
|
|
9
|
-
* straight to its current one. `migrations` is untrusted input (a manifest
|
|
10
|
-
* read at runtime) — any entry that is not a well-formed `{ version: number,
|
|
11
|
-
* renames: Array<{ from: string, to: string }> }` step is ignored rather than
|
|
12
|
-
* thrown on, and malformed rename pairs within an otherwise valid step are
|
|
13
|
-
* skipped the same way.
|
|
14
|
-
*/
|
|
15
|
-
function buildTemplateBlockRenames(migrations) {
|
|
16
|
-
const renames = {};
|
|
17
|
-
if (!Array.isArray(migrations)) return renames;
|
|
18
|
-
const steps = migrations.filter((step) => isRecord(step) && typeof step.version === "number" && Array.isArray(step.renames)).sort((a, b) => a.version - b.version);
|
|
19
|
-
for (const step of steps) for (const entry of step.renames) {
|
|
20
|
-
if (!isRecord(entry)) continue;
|
|
21
|
-
const { from, to } = entry;
|
|
22
|
-
if (typeof from !== "string" || typeof to !== "string" || from === "" || to === "") continue;
|
|
23
|
-
for (const key of Object.keys(renames)) if (renames[key] === from) renames[key] = to;
|
|
24
|
-
renames[from] = to;
|
|
25
|
-
}
|
|
26
|
-
return renames;
|
|
27
|
-
}
|
|
28
|
-
const API_ID = /^[a-z][a-z0-9-]{1,48}$/;
|
|
29
|
-
const FIELD_ID = /^[a-z][a-zA-Z0-9]{0,48}$/;
|
|
30
|
-
const LIST_INDEX = /^(?:0|[1-9][0-9]*)$/;
|
|
31
|
-
const TARGET_FIELD_PATH = /^[a-z][a-zA-Z0-9]{0,48}(?:\.(?:[a-z][a-zA-Z0-9]{0,48}|0|[1-9][0-9]*))*$/;
|
|
32
|
-
const MAX_BINDINGS = 100;
|
|
33
|
-
const MAX_TEMPLATE_BYTES = 2048;
|
|
34
|
-
const MAX_TEMPLATE_TOKENS = 32;
|
|
35
|
-
const TEMPLATE_TOKEN = /\{\{\s*([a-z][a-zA-Z0-9]*(?:\.[a-z][a-zA-Z0-9]*)*)\s*\}\}/g;
|
|
36
|
-
function createTemplateLayoutRenderModel(value, context) {
|
|
37
|
-
if (!isRecord(value) || value.version !== 1) fail("/version", "INVALID_VALUE");
|
|
38
|
-
const placements = /* @__PURE__ */ new Map();
|
|
39
|
-
let sequence = 0;
|
|
40
|
-
const transformed = transformNode(value.root, "/root", context, placements, () => {
|
|
41
|
-
sequence += 1;
|
|
42
|
-
return `00000000-0000-4000-8000-${String(sequence).padStart(12, "0")}`;
|
|
43
|
-
});
|
|
44
|
-
if (transformed.type === "block") fail("/root/type", "INVALID_VALUE");
|
|
45
|
-
const expansion = expandReusablePlacements({
|
|
46
|
-
root: transformed,
|
|
47
|
-
path: "/root",
|
|
48
|
-
projection: context.reusableComponentProjection ?? {
|
|
49
|
-
bindings: [],
|
|
50
|
-
revisions: []
|
|
51
|
-
},
|
|
52
|
-
allowReusable: true,
|
|
53
|
-
...context.allowedContainerIds === void 0 ? {} : { allowedContainerIds: context.allowedContainerIds }
|
|
54
|
-
});
|
|
55
|
-
const expanded = expansion.root;
|
|
56
|
-
const normalized = normalizeLayoutDocument({
|
|
57
|
-
version: 1,
|
|
58
|
-
root: expanded
|
|
59
|
-
}, new Set(collectEntryIds(expanded)), context.allowedContainerIds);
|
|
60
|
-
const document = restoreTemplateDocument(normalized.document.root, placements);
|
|
61
|
-
return {
|
|
62
|
-
document: {
|
|
63
|
-
version: 1,
|
|
64
|
-
root: document
|
|
65
|
-
},
|
|
66
|
-
root: decorateReusableIdentity(renderTemplateNode(document, placements), expansion.identities),
|
|
67
|
-
css: generateLayoutCss(normalized.document, void 0, void 0, context.breakpoints)
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
function transformNode(value, path, context, placements, nextEntryId) {
|
|
71
|
-
if (!isRecord(value)) fail(path, "INVALID_TYPE");
|
|
72
|
-
if (value.type === "template-block") {
|
|
73
|
-
exactKeys(value, [
|
|
74
|
-
"id",
|
|
75
|
-
"type",
|
|
76
|
-
"apiId",
|
|
77
|
-
"entryId",
|
|
78
|
-
"bindings",
|
|
79
|
-
"templates"
|
|
80
|
-
], path);
|
|
81
|
-
if (typeof value.id !== "string") fail(`${path}/id`, "INVALID_TYPE");
|
|
82
|
-
if (typeof value.apiId !== "string" || !API_ID.test(value.apiId)) fail(`${path}/apiId`, "INVALID_VALUE");
|
|
83
|
-
const definition = context.blockCatalog[value.apiId];
|
|
84
|
-
if (definition === void 0) fail(`${path}/apiId`, "BLOCK_NOT_FOUND");
|
|
85
|
-
const declared = new Set(definition.fields.map((field) => field.fieldId));
|
|
86
|
-
const renames = definition.renames ?? {};
|
|
87
|
-
const bindings = normalizeBindings(resolveRenamedKeys(value.bindings, path, "bindings", declared, renames), path, definition, context.entry);
|
|
88
|
-
const templates = normalizeTemplates(resolveRenamedKeys(value.templates, path, "templates", declared, renames), path, definition, context.entry, bindings);
|
|
89
|
-
const entryId = value.entryId;
|
|
90
|
-
if (entryId !== void 0 && typeof entryId !== "string") fail(`${path}/entryId`, "INVALID_TYPE");
|
|
91
|
-
const baseEntry = entryId === void 0 ? void 0 : context.blockEntries?.find((candidate) => candidate.id === entryId);
|
|
92
|
-
if (entryId !== void 0 && baseEntry === void 0) fail(`${path}/entryId`, "BLOCK_NOT_FOUND");
|
|
93
|
-
const schemaApiId = baseEntry?.schemaApiId;
|
|
94
|
-
if (typeof schemaApiId === "string" && schemaApiId !== value.apiId) fail(`${path}/entryId`, "BLOCK_NOT_FOUND");
|
|
95
|
-
const data = baseEntry === void 0 ? {} : cloneContentRecord(baseEntry.data);
|
|
96
|
-
for (const field of definition.fields) if (baseEntry === void 0 && Object.hasOwn(field, "default")) data[field.fieldId] = cloneContent(field.default);
|
|
97
|
-
for (const [targetPath, binding] of Object.entries(bindings)) {
|
|
98
|
-
const resolved = readBindingPath(context.entry.data, binding);
|
|
99
|
-
const issuePath = `${path}/bindings/${pointerToken(targetPath)}`;
|
|
100
|
-
if (!resolved.found || !writeTargetPath(data, targetPath, resolved.value)) fail(issuePath, "INVALID_VALUE");
|
|
101
|
-
}
|
|
102
|
-
for (const [targetPath, template] of Object.entries(templates)) {
|
|
103
|
-
const issuePath = `${path}/templates/${pointerToken(targetPath)}`;
|
|
104
|
-
if (!writeTargetPath(data, targetPath, renderTextTemplate(template, context.entry, issuePath))) fail(issuePath, "INVALID_VALUE");
|
|
105
|
-
}
|
|
106
|
-
placements.set(value.id, {
|
|
107
|
-
apiId: value.apiId,
|
|
108
|
-
...entryId === void 0 ? {} : { entryId },
|
|
109
|
-
bindings,
|
|
110
|
-
templates,
|
|
111
|
-
entry: baseEntry === void 0 ? {
|
|
112
|
-
...context.entry,
|
|
113
|
-
schemaApiId: value.apiId,
|
|
114
|
-
data
|
|
115
|
-
} : {
|
|
116
|
-
...baseEntry,
|
|
117
|
-
schemaApiId: value.apiId,
|
|
118
|
-
data
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
return {
|
|
122
|
-
id: value.id,
|
|
123
|
-
type: "block",
|
|
124
|
-
entryId: nextEntryId()
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
if (value.type === "reusable") {
|
|
128
|
-
if (path === "/root") fail(`${path}/type`, "INVALID_VALUE");
|
|
129
|
-
exactKeys(value, [
|
|
130
|
-
"id",
|
|
131
|
-
"type",
|
|
132
|
-
"componentId"
|
|
133
|
-
], path);
|
|
134
|
-
return value;
|
|
135
|
-
}
|
|
136
|
-
if (value.type !== "flex" && value.type !== "grid") return value;
|
|
137
|
-
if (!Array.isArray(value.children)) return value;
|
|
138
|
-
return {
|
|
139
|
-
...value,
|
|
140
|
-
children: value.children.map((child, index) => transformNode(child, `${path}/children/${index}`, context, placements, nextEntryId))
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
function resolveRenamedKeys(value, path, container, declared, renames) {
|
|
144
|
-
if (!isRecord(value)) return value;
|
|
145
|
-
const rewritten = {};
|
|
146
|
-
const sourcesByTarget = /* @__PURE__ */ new Map();
|
|
147
|
-
for (const key of Object.keys(value)) {
|
|
148
|
-
const dot = key.indexOf(".");
|
|
149
|
-
const head = dot === -1 ? key : key.slice(0, dot);
|
|
150
|
-
const rest = dot === -1 ? "" : key.slice(dot);
|
|
151
|
-
const renamed = !declared.has(head) && Object.hasOwn(renames, head) ? renames[head] : void 0;
|
|
152
|
-
const newKey = renamed === void 0 ? key : renamed + rest;
|
|
153
|
-
rewritten[newKey] = value[key];
|
|
154
|
-
const sources = sourcesByTarget.get(newKey);
|
|
155
|
-
if (sources === void 0) sourcesByTarget.set(newKey, [key]);
|
|
156
|
-
else sources.push(key);
|
|
157
|
-
}
|
|
158
|
-
for (const [newKey, sources] of sourcesByTarget) if (sources.length > 1) fail(`${path}/${container}/${pointerToken(newKey)}`, "INVALID_VALUE");
|
|
159
|
-
return rewritten;
|
|
160
|
-
}
|
|
161
|
-
function normalizeTemplates(value, path, definition, entry, bindings) {
|
|
162
|
-
if (value === void 0) return {};
|
|
163
|
-
if (!isRecord(value)) fail(`${path}/templates`, "INVALID_TYPE");
|
|
164
|
-
const keys = Object.keys(value);
|
|
165
|
-
if (keys.length > MAX_BINDINGS || keys.length + Object.keys(bindings).length > MAX_BINDINGS) fail(`${path}/templates`, "LIMIT_EXCEEDED");
|
|
166
|
-
const declared = new Set(definition.fields.map((field) => field.fieldId));
|
|
167
|
-
const templates = {};
|
|
168
|
-
for (const key of keys.sort()) {
|
|
169
|
-
const template = value[key];
|
|
170
|
-
const issuePath = `${path}/templates/${pointerToken(key)}`;
|
|
171
|
-
if (!validTargetPath(key, declared) || Object.hasOwn(bindings, key)) fail(issuePath, "INVALID_VALUE");
|
|
172
|
-
if (typeof template !== "string") fail(issuePath, "INVALID_TYPE");
|
|
173
|
-
const paths = templatePaths(template);
|
|
174
|
-
if (paths === null || paths.some((binding) => !readBindingPath(entry.data, binding).found)) fail(issuePath, "INVALID_VALUE");
|
|
175
|
-
templates[key] = template;
|
|
176
|
-
}
|
|
177
|
-
return templates;
|
|
178
|
-
}
|
|
179
|
-
function templatePaths(value) {
|
|
180
|
-
if (value === "" || new TextEncoder().encode(value).byteLength > MAX_TEMPLATE_BYTES) return null;
|
|
181
|
-
const paths = [...value.matchAll(TEMPLATE_TOKEN)].map((match) => match[1]);
|
|
182
|
-
if (paths.length === 0 || paths.length > MAX_TEMPLATE_TOKENS) return null;
|
|
183
|
-
const remainder = value.replace(TEMPLATE_TOKEN, "");
|
|
184
|
-
return remainder.includes("{{") || remainder.includes("}}") ? null : paths;
|
|
185
|
-
}
|
|
186
|
-
function renderTextTemplate(value, entry, path) {
|
|
187
|
-
return value.replace(TEMPLATE_TOKEN, (_token, binding) => {
|
|
188
|
-
const resolved = readBindingPath(entry.data, binding);
|
|
189
|
-
if (!resolved.found || typeof resolved.value !== "string") fail(path, "INVALID_VALUE");
|
|
190
|
-
return resolved.value;
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
function normalizeBindings(value, path, definition, entry) {
|
|
194
|
-
if (value === void 0) return {};
|
|
195
|
-
if (!isRecord(value)) fail(`${path}/bindings`, "INVALID_TYPE");
|
|
196
|
-
const keys = Object.keys(value);
|
|
197
|
-
if (keys.length > MAX_BINDINGS) fail(`${path}/bindings`, "LIMIT_EXCEEDED");
|
|
198
|
-
const declared = new Set(definition.fields.map((field) => field.fieldId));
|
|
199
|
-
const bindings = {};
|
|
200
|
-
for (const key of keys.sort()) {
|
|
201
|
-
const binding = value[key];
|
|
202
|
-
if (!validTargetPath(key, declared)) fail(`${path}/bindings/${pointerToken(key)}`, "INVALID_VALUE");
|
|
203
|
-
if (typeof binding !== "string" || !validBindingPath(binding)) fail(`${path}/bindings/${pointerToken(key)}`, typeof binding === "string" ? "INVALID_VALUE" : "INVALID_TYPE");
|
|
204
|
-
if (!readBindingPath(entry.data, binding).found) fail(`${path}/bindings/${pointerToken(key)}`, "INVALID_VALUE");
|
|
205
|
-
bindings[key] = binding;
|
|
206
|
-
}
|
|
207
|
-
return bindings;
|
|
208
|
-
}
|
|
209
|
-
function restoreTemplateDocument(node, placements) {
|
|
210
|
-
const restore = (current) => {
|
|
211
|
-
if (current.type === "block") {
|
|
212
|
-
const placement = placements.get(current.id);
|
|
213
|
-
if (placement === void 0) return current;
|
|
214
|
-
return {
|
|
215
|
-
id: current.id,
|
|
216
|
-
type: "template-block",
|
|
217
|
-
apiId: placement.apiId,
|
|
218
|
-
...placement.entryId === void 0 ? {} : { entryId: placement.entryId },
|
|
219
|
-
...Object.keys(placement.bindings).length === 0 ? {} : { bindings: placement.bindings },
|
|
220
|
-
...Object.keys(placement.templates).length === 0 ? {} : { templates: placement.templates }
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
return {
|
|
224
|
-
...current,
|
|
225
|
-
children: current.children.map(restore)
|
|
226
|
-
};
|
|
227
|
-
};
|
|
228
|
-
return restore(node);
|
|
229
|
-
}
|
|
230
|
-
function renderTemplateNode(node, placements) {
|
|
231
|
-
if (node.type === "template-block") {
|
|
232
|
-
const placement = placements.get(node.id);
|
|
233
|
-
if (placement === void 0) fail("", "INVALID_VALUE");
|
|
234
|
-
return {
|
|
235
|
-
...node,
|
|
236
|
-
entry: placement.entry,
|
|
237
|
-
className: layoutNodeClass(node.id)
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
if (node.type === "flex" || node.type === "grid") return {
|
|
241
|
-
...node,
|
|
242
|
-
className: layoutNodeClass(node.id),
|
|
243
|
-
children: node.children.map((child) => renderTemplateNode(child, placements))
|
|
244
|
-
};
|
|
245
|
-
return {
|
|
246
|
-
...node,
|
|
247
|
-
className: layoutNodeClass(node.id)
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
function collectEntryIds(node) {
|
|
251
|
-
if (node.type === "block") return [node.entryId];
|
|
252
|
-
return node.children.flatMap(collectEntryIds);
|
|
253
|
-
}
|
|
254
|
-
function readBindingPath(root, path) {
|
|
255
|
-
let current = root;
|
|
256
|
-
for (const segment of path.split(".")) {
|
|
257
|
-
if (LIST_INDEX.test(segment)) {
|
|
258
|
-
if (!Array.isArray(current)) return { found: false };
|
|
259
|
-
const index = Number(segment);
|
|
260
|
-
if (index >= current.length) return { found: false };
|
|
261
|
-
current = current[index];
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
if (!isRecord(current)) return { found: false };
|
|
265
|
-
if (!Object.hasOwn(current, segment) && isRecord(current.data)) current = current.data;
|
|
266
|
-
if (!isRecord(current) || !Object.hasOwn(current, segment)) return { found: false };
|
|
267
|
-
current = current[segment];
|
|
268
|
-
}
|
|
269
|
-
return {
|
|
270
|
-
found: true,
|
|
271
|
-
value: current
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
function validBindingPath(path) {
|
|
275
|
-
return path !== "" && new TextEncoder().encode(path).byteLength <= 255 && path.split(".").every((segment) => FIELD_ID.test(segment) || LIST_INDEX.test(segment));
|
|
276
|
-
}
|
|
277
|
-
function validTargetPath(path, declaredRoots) {
|
|
278
|
-
return TARGET_FIELD_PATH.test(path) && declaredRoots.has(path.split(".")[0]);
|
|
279
|
-
}
|
|
280
|
-
function writeTargetPath(root, path, value) {
|
|
281
|
-
const segments = path.split(".");
|
|
282
|
-
let current = root;
|
|
283
|
-
for (let index = 0; index < segments.length - 1; index += 1) {
|
|
284
|
-
const segment = segments[index];
|
|
285
|
-
if (Array.isArray(current)) {
|
|
286
|
-
const itemIndex = Number(segment);
|
|
287
|
-
if (!Number.isSafeInteger(itemIndex) || itemIndex < 0 || itemIndex >= current.length) return false;
|
|
288
|
-
current = current[itemIndex];
|
|
289
|
-
continue;
|
|
290
|
-
}
|
|
291
|
-
if (!isRecord(current) || !Object.hasOwn(current, segment)) return false;
|
|
292
|
-
current = current[segment];
|
|
293
|
-
}
|
|
294
|
-
const finalSegment = segments.at(-1);
|
|
295
|
-
if (Array.isArray(current)) {
|
|
296
|
-
const itemIndex = Number(finalSegment);
|
|
297
|
-
if (!Number.isSafeInteger(itemIndex) || itemIndex < 0 || itemIndex >= current.length) return false;
|
|
298
|
-
current[itemIndex] = value;
|
|
299
|
-
return true;
|
|
300
|
-
}
|
|
301
|
-
if (!isRecord(current) || !FIELD_ID.test(finalSegment)) return false;
|
|
302
|
-
current[finalSegment] = value;
|
|
303
|
-
return true;
|
|
304
|
-
}
|
|
305
|
-
function exactKeys(value, allowed, path) {
|
|
306
|
-
for (const key of Object.keys(value)) if (!allowed.includes(key)) fail(`${path}/${pointerToken(key)}`, "UNKNOWN_KEY");
|
|
307
|
-
}
|
|
308
|
-
function cloneContent(value) {
|
|
309
|
-
if (Array.isArray(value)) return value.map(cloneContent);
|
|
310
|
-
return isRecord(value) ? cloneContentRecord(value) : value;
|
|
311
|
-
}
|
|
312
|
-
function cloneContentRecord(value) {
|
|
313
|
-
return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, cloneContent(child)]));
|
|
314
|
-
}
|
|
315
|
-
function isRecord(value) {
|
|
316
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
317
|
-
}
|
|
318
|
-
function pointerToken(value) {
|
|
319
|
-
return value.replaceAll("~", "~0").replaceAll("/", "~1");
|
|
320
|
-
}
|
|
321
|
-
function fail(path, code) {
|
|
322
|
-
throw new LayoutValidationError({
|
|
323
|
-
path,
|
|
324
|
-
code
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
//#endregion
|
|
1
|
+
import { n as createTemplateLayoutRenderModel, t as buildTemplateBlockRenames } from "./templateLayout-CpZb6EEg.js";
|
|
2
|
+
import "./layout.js";
|
|
328
3
|
export { buildTemplateBlockRenames, createTemplateLayoutRenderModel };
|