@thebes/cadmus 0.4.1 → 0.4.2

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.
@@ -1271,6 +1271,45 @@ function decodeEditRef(value) {
1271
1271
  function editAttr(ref) {
1272
1272
  return { [EDIT_ATTR]: encodeEditRef(ref) };
1273
1273
  }
1274
+ /** `postMessage` type carrying in-progress field values into the preview. */
1275
+ const PREVIEW_VALUES_MESSAGE = "cadmus:preview-values";
1276
+ /**
1277
+ * Patch tagged regions' text from in-progress field values. For each string
1278
+ * value, updates every `[data-cadmus-edit="collection:id:field"]` element's
1279
+ * `textContent`. Pure (takes the root to search), so it's unit-testable
1280
+ * without a live preview window.
1281
+ */
1282
+ function applyPreviewValues(root, target, values) {
1283
+ for (const [field, value] of Object.entries(values)) {
1284
+ if (typeof value !== "string") continue;
1285
+ const attr = encodeEditRef({
1286
+ collection: target.collection,
1287
+ id: target.id,
1288
+ field
1289
+ });
1290
+ for (const el of root.querySelectorAll(`[${EDIT_ATTR}="${attr}"]`)) el.textContent = value;
1291
+ }
1292
+ }
1293
+ /**
1294
+ * Mount the live-preview receiver on a preview page (browser-only). Listens
1295
+ * for {@link PreviewValuesMessage} from the studio window and patches tagged
1296
+ * text regions via {@link applyPreviewValues}. Returns a cleanup function.
1297
+ */
1298
+ function mountPreviewSync(options) {
1299
+ const root = options.root ?? document;
1300
+ const handler = (event) => {
1301
+ if (options.allowedOrigin && event.origin !== options.allowedOrigin) return;
1302
+ const data = event.data;
1303
+ if (data?.type !== "cadmus:preview-values") return;
1304
+ if (data.collection !== options.collection || data.id !== options.id) return;
1305
+ if (data.values) applyPreviewValues(root, {
1306
+ collection: options.collection,
1307
+ id: options.id
1308
+ }, data.values);
1309
+ };
1310
+ window.addEventListener("message", handler);
1311
+ return () => window.removeEventListener("message", handler);
1312
+ }
1274
1313
  /**
1275
1314
  * Mount the click-to-edit overlay. Browser-only — call from a preview page's
1276
1315
  * client script. Highlights `[data-cadmus-edit]` elements on hover and, on
@@ -1407,9 +1446,11 @@ async function deliverWebhookMessage(message) {
1407
1446
  //#endregion
1408
1447
  exports.DEFAULT_STUDIO_GROUP = DEFAULT_STUDIO_GROUP;
1409
1448
  exports.EDIT_ATTR = EDIT_ATTR;
1449
+ exports.PREVIEW_VALUES_MESSAGE = PREVIEW_VALUES_MESSAGE;
1410
1450
  exports.Rule = Rule;
1411
1451
  exports.VISUAL_EDIT_MESSAGE = VISUAL_EDIT_MESSAGE;
1412
1452
  exports.applyPatch = applyPatch;
1453
+ exports.applyPreviewValues = applyPreviewValues;
1413
1454
  exports.assertValid = assertValid;
1414
1455
  exports.buildStudioStructure = buildStudioStructure;
1415
1456
  exports.can = can;
@@ -1438,6 +1479,7 @@ exports.flattenFields = flattenFields;
1438
1479
  exports.generateSchemaSource = generateSchemaSource;
1439
1480
  exports.getCollectionsMeta = getCollectionsMeta;
1440
1481
  exports.getRegisteredApi = getRegisteredApi;
1482
+ exports.mountPreviewSync = mountPreviewSync;
1441
1483
  exports.mountVisualEditing = mountVisualEditing;
1442
1484
  exports.nestDoc = nestDoc;
1443
1485
  exports.relationshipJoinTables = relationshipJoinTables;