@uniweb/runtime 0.28.10 → 0.29.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/dist/ssr.js +18 -96
- package/dist/ssr.js.map +1 -1
- package/package.json +4 -4
- package/src/isolate-api.js +3 -2
- package/src/prepare-props.js +54 -222
- package/src/ssr.js +0 -1
package/dist/ssr.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { isRichSchema, deriveCacheKey, resolveDefaultLocale, hasDarkScheme, createUniweb, substitutePlaceholders, resolveServiceUrl, evaluateQuery, sortRecords, sortToWire } from "@uniweb/core";
|
|
2
1
|
import React from "react";
|
|
3
2
|
import { renderToString } from "react-dom/server";
|
|
3
|
+
import { deriveCacheKey, resolveDefaultLocale, hasDarkScheme, createUniweb, substitutePlaceholders, resolveServiceUrl, evaluateQuery, sortRecords, sortToWire } from "@uniweb/core";
|
|
4
4
|
import { sectionDomId } from "@uniweb/core/section-id";
|
|
5
5
|
import { applyBasePath } from "@uniweb/core/base-path";
|
|
6
6
|
import { isDynamicRoute, routePatternToRegex } from "@uniweb/core/route-match";
|
|
@@ -85,95 +85,6 @@ function guaranteeContentStructure(parsedContent) {
|
|
|
85
85
|
raw: content.raw
|
|
86
86
|
};
|
|
87
87
|
}
|
|
88
|
-
function applySchemaToObject(obj, schema) {
|
|
89
|
-
if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
|
|
90
|
-
return obj;
|
|
91
|
-
}
|
|
92
|
-
const result = { ...obj };
|
|
93
|
-
for (const [field, fieldDef] of Object.entries(schema)) {
|
|
94
|
-
const defaultValue = typeof fieldDef === "object" ? fieldDef.default : void 0;
|
|
95
|
-
if (result[field] === void 0 && defaultValue !== void 0) {
|
|
96
|
-
result[field] = defaultValue;
|
|
97
|
-
}
|
|
98
|
-
if (typeof fieldDef !== "object") continue;
|
|
99
|
-
if (Array.isArray(fieldDef.enum)) {
|
|
100
|
-
const allowed = fieldDef.enum.map((e) => e && typeof e === "object" && "value" in e ? e.value : e);
|
|
101
|
-
if (result[field] !== void 0 && !allowed.includes(result[field]) && defaultValue !== void 0) {
|
|
102
|
-
result[field] = defaultValue;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (fieldDef.type === "object" && fieldDef.fields && result[field]) {
|
|
106
|
-
result[field] = applySchemaToObject(result[field], fieldDef.fields);
|
|
107
|
-
}
|
|
108
|
-
if (fieldDef.type === "array" && fieldDef.items && Array.isArray(result[field])) {
|
|
109
|
-
const items = fieldDef.items;
|
|
110
|
-
if (items && typeof items === "object" && items.type === "object" && items.fields) {
|
|
111
|
-
result[field] = result[field].map((item) => applySchemaToObject(item, items.fields));
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return result;
|
|
116
|
-
}
|
|
117
|
-
function applySchemaToValue(value, schema) {
|
|
118
|
-
if (Array.isArray(value)) {
|
|
119
|
-
return value.map((item) => applySchemaToObject(item, schema));
|
|
120
|
-
}
|
|
121
|
-
return applySchemaToObject(value, schema);
|
|
122
|
-
}
|
|
123
|
-
function applyRichFieldDefaults(obj, fields) {
|
|
124
|
-
if (!obj || typeof obj !== "object" || Array.isArray(obj)) return obj;
|
|
125
|
-
if (!Array.isArray(fields)) return obj;
|
|
126
|
-
const result = { ...obj };
|
|
127
|
-
for (const field of fields) {
|
|
128
|
-
if (!field || typeof field !== "object" || !field.id) continue;
|
|
129
|
-
const id = field.id;
|
|
130
|
-
if (result[id] === void 0 && field.default !== void 0) {
|
|
131
|
-
result[id] = field.default;
|
|
132
|
-
}
|
|
133
|
-
if (field.type === "form" && field.childSchema && Array.isArray(result[id])) {
|
|
134
|
-
result[id] = result[id].map(
|
|
135
|
-
(item) => applyRichFieldDefaults(item, field.childSchema.fields)
|
|
136
|
-
);
|
|
137
|
-
} else if ((field.type === "nestedObject" || field.type === "object") && Array.isArray(field.fields) && result[id] && typeof result[id] === "object") {
|
|
138
|
-
result[id] = applyRichFieldDefaults(result[id], field.fields);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
return result;
|
|
142
|
-
}
|
|
143
|
-
function applyRichSchemaToValue(value, schema) {
|
|
144
|
-
if (value == null) return value;
|
|
145
|
-
if (schema.isComposite && schema.childSchema) {
|
|
146
|
-
const childFields = schema.childSchema.fields;
|
|
147
|
-
const queryKey = schema.childRecords;
|
|
148
|
-
if (queryKey && value && typeof value === "object" && !Array.isArray(value)) {
|
|
149
|
-
const arr = Array.isArray(value[queryKey]) ? value[queryKey] : [];
|
|
150
|
-
return {
|
|
151
|
-
...value,
|
|
152
|
-
[queryKey]: arr.map((row) => applyRichFieldDefaults(row, childFields))
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
if (Array.isArray(value)) {
|
|
156
|
-
return value.map((row) => applyRichFieldDefaults(row, childFields));
|
|
157
|
-
}
|
|
158
|
-
return value;
|
|
159
|
-
}
|
|
160
|
-
if (Array.isArray(schema.fields)) {
|
|
161
|
-
return applyRichFieldDefaults(value, schema.fields);
|
|
162
|
-
}
|
|
163
|
-
return value;
|
|
164
|
-
}
|
|
165
|
-
function applySchemas(data, schemas) {
|
|
166
|
-
if (!schemas || !data || typeof data !== "object") {
|
|
167
|
-
return data || {};
|
|
168
|
-
}
|
|
169
|
-
const result = { ...data };
|
|
170
|
-
for (const [tag, rawValue] of Object.entries(data)) {
|
|
171
|
-
const schema = schemas[tag];
|
|
172
|
-
if (!schema) continue;
|
|
173
|
-
result[tag] = isRichSchema(schema) ? applyRichSchemaToValue(rawValue, schema) : applySchemaToValue(rawValue, schema);
|
|
174
|
-
}
|
|
175
|
-
return result;
|
|
176
|
-
}
|
|
177
88
|
function applyDefaults(params, defaults) {
|
|
178
89
|
if (!defaults || Object.keys(defaults).length === 0) {
|
|
179
90
|
return params || {};
|
|
@@ -231,6 +142,10 @@ function runContentHandler(block) {
|
|
|
231
142
|
try {
|
|
232
143
|
const transformed = handler(block.parsedContent.data, block);
|
|
233
144
|
if (!transformed || transformed === block.rawContent) return;
|
|
145
|
+
if (!isDocument(transformed)) {
|
|
146
|
+
warnNotADocument(handler, transformed, block.parsedContent.data);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
234
149
|
const reparsed = block.parseContent(transformed);
|
|
235
150
|
reparsed.data = block.parsedContent.data;
|
|
236
151
|
block.parsedContent = reparsed;
|
|
@@ -239,6 +154,18 @@ function runContentHandler(block) {
|
|
|
239
154
|
console.error("Foundation content handler failed:", err);
|
|
240
155
|
}
|
|
241
156
|
}
|
|
157
|
+
function isDocument(value) {
|
|
158
|
+
return value?.type === "doc" || value?.doc?.type === "doc";
|
|
159
|
+
}
|
|
160
|
+
const warnedContentHandlers = /* @__PURE__ */ new WeakSet();
|
|
161
|
+
function warnNotADocument(handler, value, data) {
|
|
162
|
+
if (warnedContentHandlers.has(handler)) return;
|
|
163
|
+
warnedContentHandlers.add(handler);
|
|
164
|
+
const what = value === data ? "the data it was given" : Array.isArray(value) ? "an array" : `${typeof value === "object" ? "an object" : typeof value}`;
|
|
165
|
+
console.warn(
|
|
166
|
+
`[uniweb] handlers.content returned ${what}, not a ProseMirror document — ignored, and the section keeps its content. The handler is called (data, block) and returns the section's raw content transformed (block.rawContent, as Loom's does), or null for no change.`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
242
169
|
function runPropsHandler(content, params, block) {
|
|
243
170
|
const handler = globalThis.uniweb?.foundationConfig?.handlers?.props;
|
|
244
171
|
if (typeof handler !== "function") return null;
|
|
@@ -257,11 +184,7 @@ function prepareProps(block, meta, entityData = null) {
|
|
|
257
184
|
runContentHandler(block);
|
|
258
185
|
const defaults = meta?.defaults || {};
|
|
259
186
|
const params = applyDefaults(block.properties, defaults);
|
|
260
|
-
|
|
261
|
-
const schemas = meta?.schemas || null;
|
|
262
|
-
if (schemas && content.data) {
|
|
263
|
-
content.data = applySchemas(content.data, schemas);
|
|
264
|
-
}
|
|
187
|
+
const content = guaranteeContentStructure(block.parsedContent);
|
|
265
188
|
const adjusted = runPropsHandler(content, params, block);
|
|
266
189
|
if (adjusted) {
|
|
267
190
|
return {
|
|
@@ -1299,7 +1222,6 @@ function empty() {
|
|
|
1299
1222
|
export {
|
|
1300
1223
|
RESOLUTION_STATUS,
|
|
1301
1224
|
applyDefaults,
|
|
1302
|
-
applySchemas,
|
|
1303
1225
|
classifyRenderError,
|
|
1304
1226
|
collectSiteRecords,
|
|
1305
1227
|
createPageRenderer,
|