Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
1 line
10 KiB
Plaintext
1 line
10 KiB
Plaintext
{"version":3,"sources":["../../src/utilities/filterDataToSelectedLocales.ts"],"sourcesContent":["import type { Block, Field, FlattenedBlock } from '../fields/config/types.js'\nimport type { SanitizedConfig } from '../index.js'\nimport type { JsonObject } from '../types/index.js'\n\nimport { fieldAffectsData, fieldShouldBeLocalized, tabHasName } from '../fields/config/types.js'\n\ntype FilterDataToSelectedLocalesArgs = {\n configBlockReferences: SanitizedConfig['blocks']\n docWithLocales: JsonObject\n fields: Field[]\n parentIsLocalized?: boolean\n selectedLocales: string[]\n}\n\n/**\n * Filters localized field data to only include specified locales.\n * For non-localized fields, returns all data as-is.\n * For localized fields, if selectedLocales is provided, returns only those locales.\n * If selectedLocales is not provided and field is localized, returns all locales.\n */\nexport function filterDataToSelectedLocales({\n configBlockReferences,\n docWithLocales,\n fields,\n parentIsLocalized = false,\n selectedLocales,\n}: FilterDataToSelectedLocalesArgs): JsonObject {\n if (!docWithLocales || typeof docWithLocales !== 'object') {\n return docWithLocales\n }\n\n const result: JsonObject = {}\n\n for (const field of fields) {\n if (fieldAffectsData(field)) {\n const fieldIsLocalized = fieldShouldBeLocalized({ field, parentIsLocalized })\n\n switch (field.type) {\n case 'array': {\n if (Array.isArray(docWithLocales[field.name])) {\n result[field.name] = docWithLocales[field.name].map((item: JsonObject) =>\n filterDataToSelectedLocales({\n configBlockReferences,\n docWithLocales: item,\n fields: field.fields,\n parentIsLocalized: fieldIsLocalized,\n selectedLocales,\n }),\n )\n }\n break\n }\n\n case 'blocks': {\n if (field.name in docWithLocales && Array.isArray(docWithLocales[field.name])) {\n result[field.name] = docWithLocales[field.name].map((blockData: JsonObject) => {\n let block: Block | FlattenedBlock | undefined\n if (configBlockReferences && field.blockReferences) {\n for (const blockOrReference of field.blockReferences) {\n if (typeof blockOrReference === 'string') {\n block = configBlockReferences.find((b) => b.slug === blockData.blockType)\n } else {\n block = blockOrReference\n }\n }\n } else if (field.blocks) {\n block = field.blocks.find((b) => b.slug === blockData.blockType)\n }\n\n if (block) {\n return filterDataToSelectedLocales({\n configBlockReferences,\n docWithLocales: blockData,\n fields: block?.fields || [],\n parentIsLocalized: fieldIsLocalized,\n selectedLocales,\n })\n }\n\n return blockData\n })\n }\n break\n }\n\n case 'group': {\n // Named groups create a nested data structure\n if (\n fieldAffectsData(field) &&\n field.name in docWithLocales &&\n typeof docWithLocales[field.name] === 'object'\n ) {\n result[field.name] = filterDataToSelectedLocales({\n configBlockReferences,\n docWithLocales: docWithLocales[field.name] as JsonObject,\n fields: field.fields,\n parentIsLocalized: fieldIsLocalized,\n selectedLocales,\n })\n } else {\n // Unnamed groups pass through the same data level\n const nestedResult = filterDataToSelectedLocales({\n configBlockReferences,\n docWithLocales,\n fields: field.fields,\n parentIsLocalized,\n selectedLocales,\n })\n Object.assign(result, nestedResult)\n }\n break\n }\n\n default: {\n // For all other data-affecting fields (text, number, select, etc.)\n if (field.name in docWithLocales) {\n const value = docWithLocales[field.name]\n\n // If the field is localized and has locale data\n if (fieldIsLocalized && value && typeof value === 'object' && !Array.isArray(value)) {\n // If selectedLocales is provided, filter to only those locales\n if (selectedLocales && selectedLocales.length > 0) {\n const filtered: Record<string, unknown> = {}\n for (const locale of selectedLocales) {\n if (locale in value) {\n filtered[locale] = value[locale]\n }\n }\n if (Object.keys(filtered).length > 0) {\n result[field.name] = filtered\n }\n } else {\n // If no selectedLocales, include all locales\n result[field.name] = value\n }\n } else {\n // Non-localized field or non-object value\n result[field.name] = value\n }\n }\n break\n }\n }\n } else {\n // Layout-only fields that don't affect data structure\n switch (field.type) {\n case 'collapsible':\n case 'row': {\n // These pass through the same data level\n const nestedResult = filterDataToSelectedLocales({\n configBlockReferences,\n docWithLocales,\n fields: field.fields,\n parentIsLocalized,\n selectedLocales,\n })\n Object.assign(result, nestedResult)\n break\n }\n\n case 'tabs': {\n for (const tab of field.tabs) {\n if (tabHasName(tab)) {\n // Named tabs create a nested data structure\n if (tab.name in docWithLocales && typeof docWithLocales[tab.name] === 'object') {\n result[tab.name] = filterDataToSelectedLocales({\n configBlockReferences,\n docWithLocales: docWithLocales[tab.name],\n fields: tab.fields,\n parentIsLocalized,\n selectedLocales,\n })\n }\n } else {\n // Unnamed tabs pass through the same data level\n const nestedResult = filterDataToSelectedLocales({\n configBlockReferences,\n docWithLocales,\n fields: tab.fields,\n parentIsLocalized,\n selectedLocales,\n })\n Object.assign(result, nestedResult)\n }\n }\n break\n }\n }\n }\n }\n\n return result\n}\n"],"names":["fieldAffectsData","fieldShouldBeLocalized","tabHasName","filterDataToSelectedLocales","configBlockReferences","docWithLocales","fields","parentIsLocalized","selectedLocales","result","field","fieldIsLocalized","type","Array","isArray","name","map","item","blockData","block","blockReferences","blockOrReference","find","b","slug","blockType","blocks","nestedResult","Object","assign","value","length","filtered","locale","keys","tab","tabs"],"mappings":"AAIA,SAASA,gBAAgB,EAAEC,sBAAsB,EAAEC,UAAU,QAAQ,4BAA2B;AAUhG;;;;;CAKC,GACD,OAAO,SAASC,4BAA4B,EAC1CC,qBAAqB,EACrBC,cAAc,EACdC,MAAM,EACNC,oBAAoB,KAAK,EACzBC,eAAe,EACiB;IAChC,IAAI,CAACH,kBAAkB,OAAOA,mBAAmB,UAAU;QACzD,OAAOA;IACT;IAEA,MAAMI,SAAqB,CAAC;IAE5B,KAAK,MAAMC,SAASJ,OAAQ;QAC1B,IAAIN,iBAAiBU,QAAQ;YAC3B,MAAMC,mBAAmBV,uBAAuB;gBAAES;gBAAOH;YAAkB;YAE3E,OAAQG,MAAME,IAAI;gBAChB,KAAK;oBAAS;wBACZ,IAAIC,MAAMC,OAAO,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