@braze/sanity-plugin-ai-translation 0.2.1 → 0.2.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.
package/dist/index.js CHANGED
@@ -2178,10 +2178,27 @@ const fetchTranslationPrompts = async ({
2178
2178
  return;
2179
2179
  }
2180
2180
  const children = block.children;
2181
- Array.isArray(children) && blockNode.children.forEach((spanNode, idx) => {
2181
+ Array.isArray(children) && blockNode.children.some((c) => c.type === "span") ? blockNode.children.forEach((spanNode, idx) => {
2182
2182
  const childItem = children[idx];
2183
2183
  spanNode.type === "span" && childItem && applySpanChanges(childItem, spanNode);
2184
+ }) : blockNode.children.some((c) => c.type === "field") && blockNode.children.forEach((fieldNode) => {
2185
+ fieldNode.type === "field" && fieldNode.name && applyBlockFieldChanges(block, fieldNode.name, fieldNode);
2184
2186
  });
2187
+ }, applyBlockFieldChanges = (block, fieldName, fieldNode) => {
2188
+ const currentValue = block[fieldName];
2189
+ if (fieldNode.attributes.type === "array") {
2190
+ Array.isArray(currentValue) && fieldNode.children.forEach((childNode, idx) => {
2191
+ childNode.type === "block" && currentValue[idx] ? applyBlockChanges(currentValue[idx], childNode) : childNode.type === "field" && applyBlockFieldChanges(currentValue[idx], childNode.name || String(idx), childNode);
2192
+ });
2193
+ return;
2194
+ }
2195
+ if (fieldNode.attributes.type === "object") {
2196
+ currentValue && typeof currentValue == "object" && fieldNode.children.forEach((childNode) => {
2197
+ childNode.name && applyBlockFieldChanges(currentValue, childNode.name, childNode);
2198
+ });
2199
+ return;
2200
+ }
2201
+ fieldNode.text !== void 0 && !fieldNode.attributes.readonly && (block[fieldName] = fieldNode.text);
2185
2202
  }, applySpanChanges = (span, spanNode) => {
2186
2203
  if (spanNode.attributes._key && span._key !== spanNode.attributes._key) {
2187
2204
  devLog.warn("Span key mismatch, skipping", {
@@ -2229,9 +2246,36 @@ const fetchTranslationPrompts = async ({
2229
2246
  const indentStr = " ".repeat(indent), metadata = {};
2230
2247
  block._type && (metadata._type = block._type), block._key && (metadata._key = block._key), block.style && (metadata.style = block.style), block.listItem && (metadata.listItem = block.listItem), block.level && (metadata.level = block.level), block.markDefs && (metadata.markDefs = block.markDefs);
2231
2248
  const metaStr = Object.entries(metadata).map(([k, v]) => `${k}="${escapeXml(typeof v == "string" ? v : JSON.stringify(v))}"`).join(" ");
2232
- lines.push(`${indentStr}<block ${metaStr}>`), Array.isArray(block.children) && block.children.forEach((child, idx) => {
2233
- serializeSpan(child, [...path, "children", idx], indent + 1, lines);
2234
- }), lines.push(`${indentStr}</block>`);
2249
+ if (lines.push(`${indentStr}<block ${metaStr}>`), Array.isArray(block.children) && block._type === "block")
2250
+ block.children.forEach((child, idx) => {
2251
+ serializeSpan(child, [...path, "children", idx], indent + 1, lines);
2252
+ });
2253
+ else
2254
+ for (const [key, value] of Object.entries(block))
2255
+ key.startsWith("_") || key === "style" || key === "listItem" || key === "level" || key === "markDefs" || serializeBlockField(value, key, [...path, key], indent + 1, lines);
2256
+ lines.push(`${indentStr}</block>`);
2257
+ }, serializeBlockField = (value, fieldName, path, indent, lines) => {
2258
+ const indentStr = " ".repeat(indent);
2259
+ if (Array.isArray(value)) {
2260
+ value.some((item) => item?._type) ? (lines.push(`${indentStr}<field name="${fieldName}" type="array">`), value.forEach((item, idx) => {
2261
+ item?._type === "block" && item?.children ? serializeBlock(item, [...path, idx], indent + 1, lines) : item?._type ? serializeBlock(item, [...path, idx], indent + 1, lines) : serializeBlockField(item, String(idx), [...path, idx], indent + 1, lines);
2262
+ }), lines.push(`${indentStr}</field>`)) : (lines.push(`${indentStr}<field name="${fieldName}" type="array">`), value.forEach((item, idx) => {
2263
+ serializeBlockField(item, String(idx), [...path, idx], indent + 1, lines);
2264
+ }), lines.push(`${indentStr}</field>`));
2265
+ return;
2266
+ }
2267
+ if (isPlainObject(value)) {
2268
+ lines.push(`${indentStr}<field name="${fieldName}" type="object">`);
2269
+ for (const [key, val] of Object.entries(value))
2270
+ key.startsWith("_") || serializeBlockField(val, key, [...path, key], indent + 1, lines);
2271
+ lines.push(`${indentStr}</field>`);
2272
+ return;
2273
+ }
2274
+ if (typeof value == "string" && value.length > 0) {
2275
+ lines.push(`${indentStr}<field name="${fieldName}">${escapeXml(value)}</field>`);
2276
+ return;
2277
+ }
2278
+ (typeof value == "number" || typeof value == "boolean") && lines.push(`${indentStr}<field name="${fieldName}" type="${typeof value}" readonly="true">${value}</field>`);
2235
2279
  }, serializeSpan = (span, _path, indent, lines) => {
2236
2280
  const indentStr = " ".repeat(indent);
2237
2281
  if (!span || typeof span != "object") return;