@contentstack/live-preview-utils
Version:
Contentstack provides the Live Preview SDK to establish a communication channel between the various Contentstack SDKs and your website, transmitting live changes to the preview pane.
1 lines • 4.02 kB
Source Map (JSON)
{"version":3,"sources":["../../../../src/visualBuilder/utils/handleInlineEditableField.ts"],"sourcesContent":["import { ALLOWED_INLINE_EDITABLE_FIELD } from \"./constants\";\nimport { isFieldMultiple } from \"./isFieldMultiple\";\nimport { FieldDataType, VisualBuilderEditContext } from \"./types/index.types\";\nimport { enableInlineEditing } from \"./enableInlineEditing\";\n\n/**\n * Handles inline editing for supported fields.\n */\nexport function handleInlineEditableField({\n fieldType,\n fieldSchema,\n fieldMetadata,\n expectedFieldData,\n editableElement,\n elements,\n}: {\n fieldType: FieldDataType;\n fieldSchema: any;\n fieldMetadata: any;\n expectedFieldData: any;\n editableElement: HTMLElement;\n elements: VisualBuilderEditContext;\n}) {\n if (!ALLOWED_INLINE_EDITABLE_FIELD.includes(fieldType)) return;\n\n // Instances of ALLOWED_INLINE_EDITABLE_FIELD will always have index at last\n const index = Number(\n fieldMetadata.instance.fieldPathWithIndex.split(\".\").at(-1)\n );\n const isInstance = Number.isFinite(index);\n\n // CASE 1: Handle inline editing for multiple field\n if (isFieldMultiple(fieldSchema)) {\n let expectedFieldInstanceData = null;\n if (Array.isArray(expectedFieldData)) {\n // CASE: Selected element is the multiple field itself.\n // Inline Editing not allowed on field, only allowed on instance.\n // (We receive unreliable `multipleFieldMetadata` in this case)\n if (!isInstance) {\n return;\n }\n\n // CASE: Value does not exist for the provided instance's index\n if (index >= expectedFieldData.length) {\n // TODO: What should be the behavior here?\n } else {\n expectedFieldInstanceData = expectedFieldData.at(index);\n }\n }\n // CASE: ContentType's Field changed from single to multiple, while Entry's Field still single.\n else {\n expectedFieldInstanceData = expectedFieldData;\n }\n\n enableInlineEditing({\n fieldType,\n expectedFieldData: expectedFieldInstanceData,\n editableElement,\n elements,\n });\n }\n // CASE 2: Handle inline editing for a single field\n else {\n let expectedFieldInstanceData = null;\n // CASE: ContentType's Field changed from multiple to single, while Entry's Field still multiple.\n if (isInstance) {\n if (index !== 0) {\n // TODO: Handle this with UX\n // Let user know, CSLP is invalid due to change in Content Type\n return;\n }\n expectedFieldInstanceData = Array.isArray(expectedFieldData)\n ? expectedFieldData.at(0)\n : expectedFieldData;\n }\n enableInlineEditing({\n fieldType,\n expectedFieldData: expectedFieldInstanceData ?? expectedFieldData,\n editableElement,\n elements,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAA8C;AAC9C,6BAAgC;AAEhC,iCAAoC;AAK7B,SAAS,0BAA0B;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACJ,GAOG;AACC,MAAI,CAAC,+CAA8B,SAAS,SAAS,EAAG;AAGxD,QAAM,QAAQ;AAAA,IACV,cAAc,SAAS,mBAAmB,MAAM,GAAG,EAAE,GAAG,EAAE;AAAA,EAC9D;AACA,QAAM,aAAa,OAAO,SAAS,KAAK;AAGxC,UAAI,wCAAgB,WAAW,GAAG;AAC9B,QAAI,4BAA4B;AAChC,QAAI,MAAM,QAAQ,iBAAiB,GAAG;AAIlC,UAAI,CAAC,YAAY;AACb;AAAA,MACJ;AAGA,UAAI,SAAS,kBAAkB,QAAQ;AAAA,MAEvC,OAAO;AACH,oCAA4B,kBAAkB,GAAG,KAAK;AAAA,MAC1D;AAAA,IACJ,OAEK;AACD,kCAA4B;AAAA,IAChC;AAEA,wDAAoB;AAAA,MAChB;AAAA,MACA,mBAAmB;AAAA,MACnB;AAAA,MACA;AAAA,IACJ,CAAC;AAAA,EACL,OAEK;AACD,QAAI,4BAA4B;AAEhC,QAAI,YAAY;AACZ,UAAI,UAAU,GAAG;AAGb;AAAA,MACJ;AACA,kCAA4B,MAAM,QAAQ,iBAAiB,IACrD,kBAAkB,GAAG,CAAC,IACtB;AAAA,IACV;AACA,wDAAoB;AAAA,MAChB;AAAA,MACA,mBAAmB,6BAA6B;AAAA,MAChD;AAAA,MACA;AAAA,IACJ,CAAC;AAAA,EACL;AACJ;","names":[]}