@lifesg/web-frontend-engine 2.1.2 → 2.2.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/chunks/{file-upload-manager.322f9ebd.js → file-upload-manager.faa055fa.js} +2 -2
- package/chunks/{file-upload-manager.322f9ebd.js.map → file-upload-manager.faa055fa.js.map} +1 -1
- package/chunks/{index.525d4dc8.js → index.2d28e824.js} +93 -92
- package/chunks/index.2d28e824.js.map +1 -0
- package/chunks/{index.001bdfec.js → index.ef942fcf.js} +2 -2
- package/chunks/{index.001bdfec.js.map → index.ef942fcf.js.map} +1 -1
- package/chunks/{index.d39dccc1.js → index.fe54c734.js} +2 -2
- package/chunks/{index.d39dccc1.js.map → index.fe54c734.js.map} +1 -1
- package/chunks/{location-modal.db994c40.js → location-modal.5e4e98fd.js} +2 -2
- package/chunks/{location-modal.db994c40.js.map → location-modal.5e4e98fd.js.map} +1 -1
- package/cjs/chunks/{file-upload-manager.aebae412.js → file-upload-manager.bc7599d2.js} +2 -2
- package/cjs/chunks/{file-upload-manager.aebae412.js.map → file-upload-manager.bc7599d2.js.map} +1 -1
- package/cjs/chunks/{index.594875a2.js → index.17761159.js} +21 -20
- package/cjs/chunks/index.17761159.js.map +1 -0
- package/cjs/chunks/{index.21340464.js → index.51cff881.js} +2 -2
- package/cjs/chunks/{index.21340464.js.map → index.51cff881.js.map} +1 -1
- package/cjs/chunks/{index.516d4b9b.js → index.8d9d1609.js} +2 -2
- package/cjs/chunks/{index.516d4b9b.js.map → index.8d9d1609.js.map} +1 -1
- package/cjs/chunks/{location-modal.c859475a.js → location-modal.ca4904e4.js} +2 -2
- package/cjs/chunks/{location-modal.c859475a.js.map → location-modal.ca4904e4.js.map} +1 -1
- package/cjs/index.js +1 -1
- package/components/fields/checkbox-group/checkbox-group.styles.d.ts +2 -2
- package/components/fields/image-upload/image-review/image-review.styles.d.ts +3 -1
- package/components/fields/index.d.ts +1 -0
- package/components/fields/location-field/location-modal/location-modal.styles.d.ts +3 -1
- package/components/fields/otp-verification-field/index.d.ts +2 -0
- package/components/fields/otp-verification-field/otp-verification-field.d.ts +3 -0
- package/components/fields/otp-verification-field/types.d.ts +46 -0
- package/components/fields/radio-button/radio-button.styles.d.ts +2 -2
- package/components/fields/types.d.ts +3 -1
- package/components/shared/error-messages.d.ts +10 -0
- package/components/shared/prompt/prompt.styles.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +2 -2
- package/chunks/index.525d4dc8.js.map +0 -1
- package/cjs/chunks/index.594875a2.js.map +0 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useContext as e,useRef as t,useEffect as s}from"react";import{e as a,u as i,a as r,b as o,g as n,f as c,d as l,I as m,A as d}from"./index.
|
|
2
|
-
//# sourceMappingURL=index.
|
|
1
|
+
import{useContext as e,useRef as t,useEffect as s}from"react";import{e as a,u as i,a as r,b as o,g as n,f as c,d as l,I as m,A as d}from"./index.2d28e824.js";import"react/jsx-runtime";import"@lifesg/react-design-system/theme";import"styled-components";import"@lifesg/react-design-system/typography";import"@lifesg/react-design-system/button";import"@lifesg/react-design-system/modal";import"@lifesg/react-design-system/markup";import"react-dom/server";import"@lifesg/react-design-system/alert";import"@lifesg/react-design-system/layout";import"@lifesg/react-design-system/box-container";import"@lifesg/react-design-system/divider";import"@lifesg/react-design-system/v2_media";import"@lifesg/react-design-system/text-list";import"@lifesg/react-design-system/popover-v2";import"@lifesg/react-icons";import"@lifesg/react-design-system/tab";import"@lifesg/react-icons/plus-circle-fill";import"@lifesg/react-design-system/button-with-icon";import"@lifesg/react-design-system/error-display";import"@lifesg/react-design-system/filter";import"@lifesg/react-design-system/uneditable-section";import"@lifesg/react-design-system/timeline";import"@lifesg/react-design-system/form";import"@lifesg/react-design-system/checkbox";import"@lifesg/react-design-system/toggle";import"@lifesg/react-design-system/file-upload";import"@lifesg/react-icons/cross";import"@lifesg/react-design-system/icon-button";import"@lifesg/react-icons/bin";import"@lifesg/react-icons/eraser";import"@lifesg/react-icons/pencil";import"@lifesg/react-icons/pencil-stroke";import"@lifesg/react-icons/plus";import"@lifesg/react-icons/exclamation-triangle";import"@lifesg/react-icons/pin-fill";import"@lifesg/react-design-system/image-button";import"@lifesg/react-design-system/radio-button";import"@lifesg/react-design-system/v2_layout";const g=g=>{const{accepts:p,compress:u,crop:f,dimensions:E,editImage:R,id:h,maxSizeInKb:y,outputType:w,upload:D,filenameMatches:O,filenameMatchesErrorMessage:I,value:U}=g,{images:L,setImages:T,setErrorCount:M,setCurrentFileIds:b}=e(a),_=i(L),N=i(U),{setValue:v}=r(),S=t(),x=t(0),{dispatchFieldEvent:C,addFieldEventListener:F,removeFieldEventListener:P}=o();s((()=>{const e=e=>{T((t=>{const s=t.findIndex((t=>t.id===e.detail.id)),a={...t[s]};a.status=e.detail.updatedStatus,a.customErrorMessage=e.detail.errorMessage;const i=[...t];return i.splice(s,1,a),i}))};return F("update-image-status",h,e),()=>P("update-image-status",h,e)}),[F,h,P,T]),s((()=>{S.current=n()}),[]),s((()=>{L.forEach(((e,t)=>{const s=_?.[t];if(e.status!==s?.status||e.dataURL!==s.dataURL)switch(e.status){case c.INJECTED:l.dataUrlToBlob(e.dataURL).then((s=>{T((a=>{const i=[...a];return i[t]={...e,file:new File([s],e.name),status:c.NONE},i}))})).catch((()=>{T((e=>e.filter(((e,s)=>s!==t))))}));break;case c.NONE:if(O){const s=k(O);if(s&&!s.test(e.name)){T((s=>{const a=[...s];return a[t]={...e,status:c.ERROR_FILENAME,customErrorMessage:I},a}));break}}l.getType(e.file).then((s=>{const a=s.mime;a&&p.map(l.fileExtensionToMimeType).includes(a)?(T((s=>{const i=[...s];return i[t]={...e,name:l.deduplicateFileName(L.map((({name:e})=>e)),t,l.sanitizeFileName(e.name)),type:a,status:"schema"!==e.addedFrom?e.status:c.UPLOADED},i})),"schema"!==e.addedFrom&&(u?G(t,e):z(t,e))):T((s=>{const a=[...s];return a[t]={...e,status:c.ERROR_FORMAT},a}))}));break;case c.TO_RECOMPRESS:j(t,e);break;case c.COMPRESSED:case c.CONVERTED:case c.RECOMPRESSED:if(!R){const s=!C("upload-ready",h,{imageData:e});T((e=>{const a=[...e];return a[t]={...a[t],status:s?c.PENDING:c.UPLOAD_READY},a}))}break;case c.UPLOAD_READY:B(t,e);break;case c.TO_DELETE:T((e=>e.filter((({status:e})=>e!==c.TO_DELETE))));break;case c.UPLOADED:C("uploaded",h,{imageData:e})}}))}),[L.map((({status:e})=>e)).join(","),L.map((({dataURL:e})=>e)).join(",")]),s((()=>{let e=0;L.forEach((t=>{(t.type&&!p.map(l.fileExtensionToMimeType).includes(t.type)||[c.ERROR_GENERIC,c.ERROR_SIZE,c.ERROR_FILENAME].includes(t.status))&&e++})),M((t=>Math.max(0,t+e-x.current))),x.current=e;const t=L.filter((({status:e})=>e===c.UPLOADED||e===c.ERROR_CUSTOM_MUTED)),s=t.filter((({addedFrom:e})=>"schema"!==e)),a=L.filter((({status:e})=>e===c.TO_DELETE)).length>0,i=s.length>0,r=i||a;b(t.map((({id:e})=>e))),v(h,t.map((({id:e,dataURL:t,drawingDataURL:s,name:a,metadata:i,uploadResponse:r})=>({fileId:e,fileName:a,dataURL:s||t,metadata:i,uploadResponse:r}))),{shouldDirty:r,shouldTouch:i})}),[p,h,L,M,v]),s((()=>{void 0!==N&&void 0===U&&L.length&&T([])}),[void 0===N,void 0===U,L.length]);const A=(e,t)=>{let s=E.width/e;return t*s>E.height&&(s=E.height/t),s},k=e=>{try{const t=e.match(/^\/(.+)\/([gimsuy]*)$/);return t?new RegExp(t[1],t[2]||""):new RegExp(e)}catch{return}},z=async(e,t)=>{try{const s=await m.convertBlob(t.file,l.fileExtensionToMimeType(w)),a=l.getFilesizeFromBase64(s);if(y&&a>1024*y)T((t=>{const s=[...t];return s[e]={...t[e],status:c.ERROR_SIZE},s}));else{const a=await m.getMetadata(t.file);T((t=>{const i=[...t];return i[e]={...t[e],dataURL:s,metadata:a,status:c.CONVERTED},i}))}}catch(t){T((t=>{const s=[...t];return s[e]={...t[e],status:c.ERROR_GENERIC},s}))}},G=async(e,t)=>{try{const s=await m.convertBlob(t.file,l.fileExtensionToMimeType(w)),a=await m.dataUrlToImage(s),i={w:a.naturalWidth,h:a.naturalHeight};let r;if(f)r=await m.resampleImage(a,{width:E.width,height:E.height,crop:!0});else{const e=A(i.w,i.h);r=await m.resampleImage(a,{scale:e})}if(y&&(r=await m.compressImage(r,{fileSize:y})),y&&r.size>1024*y)T((t=>{const s=[...t];return s[e]={...t[e],status:c.ERROR_SIZE},s}));else{const s=await m.getMetadata(t.file),a=await l.fileToDataUrl(r);T((t=>{const i=[...t];return i[e]={...t[e],dataURL:a,metadata:s,status:c.COMPRESSED},i}))}}catch(t){T((t=>{const s=[...t];return s[e]={...t[e],status:c.ERROR_GENERIC},s}))}},j=async(e,t)=>{if(t.drawingDataURL)try{const s=await m.dataUrlToImage(t.drawingDataURL),a={w:s.naturalWidth,h:s.naturalHeight};let i;if(f)i=await m.resampleImage(s,{width:E.width,height:E.height,crop:!0});else{const e=A(a.w,a.h);i=await m.resampleImage(s,{scale:e})}if(i=await m.compressImage(i,{fileSize:y}),i.size>1024*y){const t=[...L];t[e]={...L[e],status:c.ERROR_SIZE},T(t)}else{const t=await l.fileToDataUrl(i),s=[...L];s[e]={...L[e],drawingDataURL:t,status:c.RECOMPRESSED},T(s)}}catch(t){T((t=>{const s=[...t];return s[e]={...t[e],status:c.ERROR_GENERIC},s}))}},B=async(e,t)=>{try{let s;if(T((t=>{const s=[...t];return s[e]={...t[e],status:c.UPLOADING},s})),D?.method&&D?.url){const a=new FormData;a.append("dataURL",t.drawingDataURL||t.dataURL||""),a.append("sessionId",D?.sessionId||S.current||""),a.append("slot",`${t.slot}`),s=await new d("",void 0,void 0,!0)[D.method](D.url,a,{onUploadProgress:t=>{const{loaded:s,total:a}=t,i=Math.floor(100*s/a);T((t=>{const s=[...t];return s[e]={...t[e],uploadProgress:i},s}))}})}T((t=>{const a=[...t];return a[e]={...t[e],uploadResponse:s,status:c.UPLOADED},a}))}catch(t){T((t=>{const s=[...t];return s[e]={...t[e],status:c.ERROR_GENERIC},s}))}};return null};export{g as default};
|
|
2
|
+
//# sourceMappingURL=index.fe54c734.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d39dccc1.js","sources":["../../src/components/fields/image-upload/image-manager/image-manager.ts"],"sourcesContent":["import { useContext, useEffect, useRef } from \"react\";\nimport { useFormContext } from \"react-hook-form\";\nimport { AxiosApiClient, FileHelper, ImageHelper, generateRandomId } from \"../../../../utils\";\nimport { useFieldEvent, usePrevious } from \"../../../../utils/hooks\";\nimport { ImageContext } from \"../image-context\";\nimport {\n\tEImageStatus,\n\tIImage,\n\tISharedImageProps,\n\tIUpdateImageStatus,\n\tTImageUploadOutputFileType,\n\tTUploadMethod,\n} from \"../types\";\n\ninterface IProps extends Omit<ISharedImageProps, \"maxFiles\"> {\n\teditImage: boolean;\n\tcompress: boolean;\n\tcrop: boolean;\n\tdimensions: { width: number; height: number };\n\toutputType: TImageUploadOutputFileType;\n\tupload?: {\n\t\tmethod: TUploadMethod;\n\t\turl: string;\n\t\tsessionId?: string | undefined;\n\t};\n\tfilenameMatches?: string | undefined;\n\tfilenameMatchesErrorMessage?: string | undefined;\n\tvalue: any;\n}\n\n/**\n * manages selected images by listening to images from context provider\n * rename / compress / upload\n */\nexport const ImageManager = (props: IProps) => {\n\t// =============================================================================\n\t// CONST, STATE, REFS\n\t// =============================================================================\n\tconst {\n\t\taccepts,\n\t\tcompress,\n\t\tcrop,\n\t\tdimensions,\n\t\teditImage,\n\t\tid,\n\t\tmaxSizeInKb,\n\t\toutputType,\n\t\tupload,\n\t\tfilenameMatches,\n\t\tfilenameMatchesErrorMessage,\n\t\tvalue,\n\t} = props;\n\tconst { images, setImages, setErrorCount, setCurrentFileIds } = useContext(ImageContext);\n\tconst previousImages = usePrevious(images);\n\tconst previousValue = usePrevious(value);\n\tconst { setValue } = useFormContext();\n\tconst sessionId = useRef<string>();\n\tconst managerErrorCount = useRef(0);\n\n\tconst { dispatchFieldEvent, addFieldEventListener, removeFieldEventListener } = useFieldEvent();\n\n\t// =============================================================================\n\t// EFFECTS\n\t// =============================================================================\n\tuseEffect(() => {\n\t\tconst handleUpdateImageStatus = (e: CustomEvent<IUpdateImageStatus>) => {\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst imageIndex = prev.findIndex((image) => image.id === e.detail.id);\n\t\t\t\tconst updatedImage = { ...prev[imageIndex] };\n\t\t\t\tupdatedImage.status = e.detail.updatedStatus;\n\t\t\t\tupdatedImage.customErrorMessage = e.detail.errorMessage;\n\n\t\t\t\tconst newImages = [...prev];\n\t\t\t\tnewImages.splice(imageIndex, 1, updatedImage);\n\t\t\t\treturn newImages;\n\t\t\t});\n\t\t};\n\n\t\taddFieldEventListener(\"update-image-status\", id, handleUpdateImageStatus);\n\t\treturn () => removeFieldEventListener(\"update-image-status\", id, handleUpdateImageStatus);\n\t}, [addFieldEventListener, id, removeFieldEventListener, setImages]);\n\n\t// generate pseudo-random session id\n\tuseEffect(() => {\n\t\tsessionId.current = generateRandomId();\n\t}, []);\n\n\tuseEffect(() => {\n\t\timages.forEach((image, index) => {\n\t\t\tconst previousFile = previousImages?.[index];\n\t\t\tif (image.status !== previousFile?.status || image.dataURL !== previousFile.dataURL) {\n\t\t\t\tswitch (image.status) {\n\t\t\t\t\tcase EImageStatus.INJECTED:\n\t\t\t\t\t\tFileHelper.dataUrlToBlob(image.dataURL)\n\t\t\t\t\t\t\t.then((blob) => {\n\t\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t\t...image,\n\t\t\t\t\t\t\t\t\t\tfile: new File([blob], image.name),\n\t\t\t\t\t\t\t\t\t\tstatus: EImageStatus.NONE,\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t.catch(() => {\n\t\t\t\t\t\t\t\tsetImages((prev) => prev.filter((prev, i) => i !== index));\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.NONE:\n\t\t\t\t\t\tif (filenameMatches) {\n\t\t\t\t\t\t\tconst pattern = resolveMatchesPattern(filenameMatches);\n\t\t\t\t\t\t\tif (pattern && !pattern.test(image.name)) {\n\t\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t\t...image,\n\t\t\t\t\t\t\t\t\t\tstatus: EImageStatus.ERROR_FILENAME,\n\t\t\t\t\t\t\t\t\t\tcustomErrorMessage: filenameMatchesErrorMessage,\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tFileHelper.getType(image.file).then((fileType) => {\n\t\t\t\t\t\t\tconst mimeType = fileType.mime;\n\t\t\t\t\t\t\tif (mimeType && accepts.map(FileHelper.fileExtensionToMimeType).includes(mimeType)) {\n\t\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t\t...image,\n\t\t\t\t\t\t\t\t\t\tname: FileHelper.deduplicateFileName(\n\t\t\t\t\t\t\t\t\t\t\timages.map(({ name }) => name),\n\t\t\t\t\t\t\t\t\t\t\tindex,\n\t\t\t\t\t\t\t\t\t\t\tFileHelper.sanitizeFileName(image.name)\n\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\ttype: mimeType,\n\t\t\t\t\t\t\t\t\t\tstatus: image.addedFrom !== \"schema\" ? image.status : EImageStatus.UPLOADED,\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (image.addedFrom !== \"schema\") {\n\t\t\t\t\t\t\t\t\tcompress ? compressImage(index, image) : convertImage(index, image);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t\t...image,\n\t\t\t\t\t\t\t\t\t\tstatus: EImageStatus.ERROR_FORMAT,\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.TO_RECOMPRESS:\n\t\t\t\t\t\trecompressImage(index, image);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.COMPRESSED:\n\t\t\t\t\tcase EImageStatus.CONVERTED:\n\t\t\t\t\tcase EImageStatus.RECOMPRESSED:\n\t\t\t\t\t\tif (!editImage) {\n\t\t\t\t\t\t\tconst shouldPreventDefault = !dispatchFieldEvent(\"upload-ready\", id, { imageData: image });\n\n\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t...updatedImages[index],\n\t\t\t\t\t\t\t\t\tstatus: shouldPreventDefault ? EImageStatus.PENDING : EImageStatus.UPLOAD_READY,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.UPLOAD_READY:\n\t\t\t\t\t\tuploadImage(index, image);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.TO_DELETE:\n\t\t\t\t\t\tsetImages((prev) => prev.filter(({ status }) => status !== EImageStatus.TO_DELETE));\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.UPLOADED:\n\t\t\t\t\t\tdispatchFieldEvent(\"uploaded\", id, { imageData: image });\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, [images.map(({ status }) => status).join(\",\"), images.map(({ dataURL }) => dataURL).join(\",\")]);\n\n\t// track / update errors and values\n\tuseEffect(() => {\n\t\tlet updatedManagerErrorCount = 0;\n\t\timages.forEach((image) => {\n\t\t\tif (\n\t\t\t\t(image.type && !accepts.map(FileHelper.fileExtensionToMimeType).includes(image.type)) ||\n\t\t\t\t[EImageStatus.ERROR_GENERIC, EImageStatus.ERROR_SIZE, EImageStatus.ERROR_FILENAME].includes(\n\t\t\t\t\timage.status\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tupdatedManagerErrorCount++;\n\t\t\t}\n\t\t});\n\t\tsetErrorCount((prev) => Math.max(0, prev + updatedManagerErrorCount - managerErrorCount.current));\n\t\tmanagerErrorCount.current = updatedManagerErrorCount;\n\n\t\tconst uploadedImages = images.filter(\n\t\t\t({ status }) => status === EImageStatus.UPLOADED || status === EImageStatus.ERROR_CUSTOM_MUTED\n\t\t);\n\t\tconst notPrefilledImages = uploadedImages.filter(({ addedFrom }) => addedFrom !== \"schema\");\n\t\tconst gotDeleteImages = images.filter(({ status }) => status === EImageStatus.TO_DELETE).length > 0;\n\n\t\t/**\n\t\t * should dirty if\n\t\t * - it is dirty in the first place\n\t\t * - there are non-prefilled images\n\t\t * - user deleted image (differentiated from reset)\n\t\t */\n\t\tconst hasNotPrefilledImages = notPrefilledImages.length > 0;\n\t\tconst shouldDirty = hasNotPrefilledImages || gotDeleteImages;\n\n\t\tsetCurrentFileIds(uploadedImages.map(({ id }) => id));\n\n\t\tsetValue(\n\t\t\tid,\n\t\t\tuploadedImages.map(({ id, dataURL, drawingDataURL, name, metadata, uploadResponse }) => ({\n\t\t\t\tfileId: id,\n\t\t\t\tfileName: name,\n\t\t\t\tdataURL: drawingDataURL || dataURL,\n\t\t\t\tmetadata,\n\t\t\t\tuploadResponse,\n\t\t\t})),\n\t\t\t{ shouldDirty, shouldTouch: hasNotPrefilledImages }\n\t\t);\n\t}, [accepts, id, images, setErrorCount, setValue]);\n\n\tuseEffect(() => {\n\t\tif (previousValue !== undefined && value === undefined && images.length) {\n\t\t\tsetImages([]);\n\t\t}\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, [previousValue === undefined, value === undefined, images.length]);\n\n\t// =============================================================================\n\t// HELPER FUNCTIONS\n\t// =============================================================================\n\tconst getScale = (origWidth: number, origHeight: number): number => {\n\t\tlet scale = dimensions.width / origWidth;\n\t\tif (origHeight * scale > dimensions.height) {\n\t\t\tscale = dimensions.height / origHeight;\n\t\t}\n\t\treturn scale;\n\t};\n\n\t/**\n\t * Converts a matches string (e.g. \"/^abc$/i\" or \"^abc$\") to a RegExp.\n\t * Returns undefined if the string is invalid.\n\t */\n\tconst resolveMatchesPattern = (matches: string): RegExp | undefined => {\n\t\ttry {\n\t\t\tconst parsed = matches.match(/^\\/(.+)\\/([gimsuy]*)$/);\n\t\t\treturn parsed ? new RegExp(parsed[1], parsed[2] || \"\") : new RegExp(matches);\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t};\n\n\tconst convertImage = async (index: number, image: IImage) => {\n\t\ttry {\n\t\t\tconst dataURL = await ImageHelper.convertBlob(image.file, FileHelper.fileExtensionToMimeType(outputType));\n\t\t\tconst filesize = FileHelper.getFilesizeFromBase64(dataURL);\n\n\t\t\tif (maxSizeInKb && filesize > maxSizeInKb * 1024) {\n\t\t\t\tsetImages((prev) => {\n\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\tstatus: EImageStatus.ERROR_SIZE,\n\t\t\t\t\t};\n\t\t\t\t\treturn updatedImages;\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tconst metadata = await ImageHelper.getMetadata(image.file);\n\t\t\t\tsetImages((prev) => {\n\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\tdataURL,\n\t\t\t\t\t\tmetadata,\n\t\t\t\t\t\tstatus: EImageStatus.CONVERTED,\n\t\t\t\t\t};\n\t\t\t\t\treturn updatedImages;\n\t\t\t\t});\n\t\t\t}\n\t\t} catch (e) {\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t...prev[index],\n\t\t\t\t\tstatus: EImageStatus.ERROR_GENERIC,\n\t\t\t\t};\n\t\t\t\treturn updatedImages;\n\t\t\t});\n\t\t}\n\t};\n\n\tconst compressImage = async (index: number, imageToCompress: IImage) => {\n\t\ttry {\n\t\t\tconst dataURL = await ImageHelper.convertBlob(\n\t\t\t\timageToCompress.file,\n\t\t\t\tFileHelper.fileExtensionToMimeType(outputType)\n\t\t\t);\n\t\t\tconst image = await ImageHelper.dataUrlToImage(dataURL);\n\t\t\tconst origDim = { w: image.naturalWidth, h: image.naturalHeight };\n\t\t\tlet compressed: Blob;\n\t\t\tif (crop) {\n\t\t\t\tcompressed = await ImageHelper.resampleImage(image, {\n\t\t\t\t\twidth: dimensions.width,\n\t\t\t\t\theight: dimensions.height,\n\t\t\t\t\tcrop: true,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tconst scale = getScale(origDim.w, origDim.h);\n\t\t\t\tcompressed = await ImageHelper.resampleImage(image, { scale });\n\t\t\t}\n\t\t\tif (maxSizeInKb) {\n\t\t\t\tcompressed = (await ImageHelper.compressImage(compressed, {\n\t\t\t\t\tfileSize: maxSizeInKb,\n\t\t\t\t})) as File;\n\t\t\t}\n\n\t\t\tif (maxSizeInKb && compressed.size > maxSizeInKb * 1024) {\n\t\t\t\tsetImages((prev) => {\n\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\tstatus: EImageStatus.ERROR_SIZE,\n\t\t\t\t\t};\n\t\t\t\t\treturn updatedImages;\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tconst metadata = await ImageHelper.getMetadata(imageToCompress.file);\n\t\t\t\tconst dataURL = await FileHelper.fileToDataUrl(compressed);\n\t\t\t\tsetImages((prev) => {\n\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\tdataURL,\n\t\t\t\t\t\tmetadata,\n\t\t\t\t\t\tstatus: EImageStatus.COMPRESSED,\n\t\t\t\t\t};\n\t\t\t\t\treturn updatedImages;\n\t\t\t\t});\n\t\t\t}\n\t\t} catch (e) {\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t...prev[index],\n\t\t\t\t\tstatus: EImageStatus.ERROR_GENERIC,\n\t\t\t\t};\n\t\t\t\treturn updatedImages;\n\t\t\t});\n\t\t}\n\t};\n\n\tconst recompressImage = async (index: number, imageToCompress: IImage) => {\n\t\tif (imageToCompress.drawingDataURL) {\n\t\t\ttry {\n\t\t\t\tconst image = await ImageHelper.dataUrlToImage(imageToCompress.drawingDataURL);\n\t\t\t\tconst origDim = { w: image.naturalWidth, h: image.naturalHeight };\n\t\t\t\tlet scaledFile: Blob;\n\t\t\t\tif (crop) {\n\t\t\t\t\tscaledFile = await ImageHelper.resampleImage(image, {\n\t\t\t\t\t\twidth: dimensions.width,\n\t\t\t\t\t\theight: dimensions.height,\n\t\t\t\t\t\tcrop: true,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tconst scale = getScale(origDim.w, origDim.h);\n\t\t\t\t\tscaledFile = await ImageHelper.resampleImage(image, { scale });\n\t\t\t\t}\n\t\t\t\tscaledFile = (await ImageHelper.compressImage(scaledFile, { fileSize: maxSizeInKb })) as File;\n\n\t\t\t\tif (scaledFile.size > maxSizeInKb * 1024) {\n\t\t\t\t\tconst updatedImages = [...images];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...images[index],\n\t\t\t\t\t\tstatus: EImageStatus.ERROR_SIZE,\n\t\t\t\t\t};\n\t\t\t\t\tsetImages(updatedImages);\n\t\t\t\t} else {\n\t\t\t\t\tconst dataURL = await FileHelper.fileToDataUrl(scaledFile);\n\t\t\t\t\tconst updatedImages = [...images];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...images[index],\n\t\t\t\t\t\tdrawingDataURL: dataURL,\n\t\t\t\t\t\tstatus: EImageStatus.RECOMPRESSED,\n\t\t\t\t\t};\n\t\t\t\t\tsetImages(updatedImages);\n\t\t\t\t}\n\t\t\t} catch (e) {\n\t\t\t\tsetImages((prev) => {\n\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\tstatus: EImageStatus.ERROR_GENERIC,\n\t\t\t\t\t};\n\t\t\t\t\treturn updatedImages;\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t};\n\n\tconst uploadImage = async (index: number, iFile: IImage) => {\n\t\ttry {\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t...prev[index],\n\t\t\t\t\tstatus: EImageStatus.UPLOADING,\n\t\t\t\t};\n\t\t\t\treturn updatedImages;\n\t\t\t});\n\n\t\t\tlet response: unknown;\n\t\t\tif (upload?.method && upload?.url) {\n\t\t\t\tconst formData = new FormData();\n\t\t\t\tformData.append(\"dataURL\", iFile.drawingDataURL || iFile.dataURL || \"\");\n\t\t\t\tformData.append(\"sessionId\", upload?.sessionId || sessionId.current || \"\");\n\t\t\t\tformData.append(\"slot\", `${iFile.slot}`);\n\t\t\t\tresponse = await new AxiosApiClient(\"\", undefined, undefined, true)[upload.method](\n\t\t\t\t\tupload.url,\n\t\t\t\t\tformData,\n\t\t\t\t\t{\n\t\t\t\t\t\tonUploadProgress: (progressEvent) => {\n\t\t\t\t\t\t\tconst { loaded, total } = progressEvent;\n\t\t\t\t\t\t\tconst percent = Math.floor((loaded * 100) / total);\n\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\t\t\t\tuploadProgress: percent,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t},\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t...prev[index],\n\t\t\t\t\tuploadResponse: response,\n\t\t\t\t\tstatus: EImageStatus.UPLOADED,\n\t\t\t\t};\n\t\t\t\treturn updatedImages;\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t...prev[index],\n\t\t\t\t\tstatus: EImageStatus.ERROR_GENERIC,\n\t\t\t\t};\n\t\t\t\treturn updatedImages;\n\t\t\t});\n\t\t}\n\t};\n\n\treturn null;\n};\n"],"names":["ImageManager","props","accepts","compress","crop","dimensions","editImage","id","maxSizeInKb","outputType","upload","filenameMatches","filenameMatchesErrorMessage","value","images","setImages","setErrorCount","setCurrentFileIds","useContext","ImageContext","previousImages","usePrevious","previousValue","setValue","useFormContext","sessionId","useRef","managerErrorCount","dispatchFieldEvent","addFieldEventListener","removeFieldEventListener","useFieldEvent","useEffect","handleUpdateImageStatus","e","prev","imageIndex","findIndex","image","detail","updatedImage","status","updatedStatus","customErrorMessage","errorMessage","newImages","splice","current","generateRandomId","forEach","index","previousFile","dataURL","EImageStatus","INJECTED","FileHelper","dataUrlToBlob","then","blob","updatedImages","file","File","name","NONE","catch","filter","i","pattern","resolveMatchesPattern","test","ERROR_FILENAME","getType","fileType","mimeType","mime","map","fileExtensionToMimeType","includes","deduplicateFileName","sanitizeFileName","type","addedFrom","UPLOADED","compressImage","convertImage","ERROR_FORMAT","TO_RECOMPRESS","recompressImage","COMPRESSED","CONVERTED","RECOMPRESSED","shouldPreventDefault","imageData","PENDING","UPLOAD_READY","uploadImage","TO_DELETE","join","updatedManagerErrorCount","ERROR_GENERIC","ERROR_SIZE","Math","max","uploadedImages","ERROR_CUSTOM_MUTED","notPrefilledImages","gotDeleteImages","length","hasNotPrefilledImages","shouldDirty","drawingDataURL","metadata","uploadResponse","fileId","fileName","shouldTouch","undefined","getScale","origWidth","origHeight","scale","width","height","matches","parsed","match","RegExp","async","ImageHelper","convertBlob","filesize","getFilesizeFromBase64","getMetadata","imageToCompress","dataUrlToImage","origDim","w","naturalWidth","h","naturalHeight","compressed","resampleImage","fileSize","size","fileToDataUrl","scaledFile","iFile","response","UPLOADING","method","url","formData","FormData","append","slot","AxiosApiClient","onUploadProgress","progressEvent","loaded","total","percent","floor","uploadProgress","err"],"mappings":"kwDAkCa,MAAAA,EAAgBC,IAI5B,MAAMC,QACLA,EAAOC,SACPA,EAAQC,KACRA,EAAIC,WACJA,EAAUC,UACVA,EAASC,GACTA,EAAEC,YACFA,EAAWC,WACXA,EAAUC,OACVA,EAAMC,gBACNA,EAAeC,4BACf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EACA,CACCK,iBAAmBC,IAClB,MAAMC,OAAEA,EAAMC,MAAEA,GAAUF,EACpBG,EAAU3D,KAAK4D,MAAgB,IAATH,EAAgBC,GAC5ChJ,GAAWoB,IACV,MAAMwB,EAAgB,IAAIxB,GAK1B,OAJAwB,EAAcT,GAAS,IACnBf,EAAKe,GACRgH,eAAgBF,GAEVrG,CAAa,GACnB,GAIL,CAED5C,GAAWoB,IACV,MAAMwB,EAAgB,IAAIxB,GAM1B,OALAwB,EAAcT,GAAS,IACnBf,EAAKe,GACR8D,eAAgBmC,EAChB1G,OAAQY,EAAa6B,UAEfvB,CAAa,GAErB,CAAC,MAAOwG,GACRpJ,GAAWoB,IACV,MAAMwB,EAAgB,IAAIxB,GAK1B,OAJAwB,EAAcT,GAAS,IACnBf,EAAKe,GACRT,OAAQY,EAAa8C,eAEfxC,CAAa,GAErB,GAGF,OAAO,IAAI"}
|
|
1
|
+
{"version":3,"file":"index.fe54c734.js","sources":["../../src/components/fields/image-upload/image-manager/image-manager.ts"],"sourcesContent":["import { useContext, useEffect, useRef } from \"react\";\nimport { useFormContext } from \"react-hook-form\";\nimport { AxiosApiClient, FileHelper, ImageHelper, generateRandomId } from \"../../../../utils\";\nimport { useFieldEvent, usePrevious } from \"../../../../utils/hooks\";\nimport { ImageContext } from \"../image-context\";\nimport {\n\tEImageStatus,\n\tIImage,\n\tISharedImageProps,\n\tIUpdateImageStatus,\n\tTImageUploadOutputFileType,\n\tTUploadMethod,\n} from \"../types\";\n\ninterface IProps extends Omit<ISharedImageProps, \"maxFiles\"> {\n\teditImage: boolean;\n\tcompress: boolean;\n\tcrop: boolean;\n\tdimensions: { width: number; height: number };\n\toutputType: TImageUploadOutputFileType;\n\tupload?: {\n\t\tmethod: TUploadMethod;\n\t\turl: string;\n\t\tsessionId?: string | undefined;\n\t};\n\tfilenameMatches?: string | undefined;\n\tfilenameMatchesErrorMessage?: string | undefined;\n\tvalue: any;\n}\n\n/**\n * manages selected images by listening to images from context provider\n * rename / compress / upload\n */\nexport const ImageManager = (props: IProps) => {\n\t// =============================================================================\n\t// CONST, STATE, REFS\n\t// =============================================================================\n\tconst {\n\t\taccepts,\n\t\tcompress,\n\t\tcrop,\n\t\tdimensions,\n\t\teditImage,\n\t\tid,\n\t\tmaxSizeInKb,\n\t\toutputType,\n\t\tupload,\n\t\tfilenameMatches,\n\t\tfilenameMatchesErrorMessage,\n\t\tvalue,\n\t} = props;\n\tconst { images, setImages, setErrorCount, setCurrentFileIds } = useContext(ImageContext);\n\tconst previousImages = usePrevious(images);\n\tconst previousValue = usePrevious(value);\n\tconst { setValue } = useFormContext();\n\tconst sessionId = useRef<string>();\n\tconst managerErrorCount = useRef(0);\n\n\tconst { dispatchFieldEvent, addFieldEventListener, removeFieldEventListener } = useFieldEvent();\n\n\t// =============================================================================\n\t// EFFECTS\n\t// =============================================================================\n\tuseEffect(() => {\n\t\tconst handleUpdateImageStatus = (e: CustomEvent<IUpdateImageStatus>) => {\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst imageIndex = prev.findIndex((image) => image.id === e.detail.id);\n\t\t\t\tconst updatedImage = { ...prev[imageIndex] };\n\t\t\t\tupdatedImage.status = e.detail.updatedStatus;\n\t\t\t\tupdatedImage.customErrorMessage = e.detail.errorMessage;\n\n\t\t\t\tconst newImages = [...prev];\n\t\t\t\tnewImages.splice(imageIndex, 1, updatedImage);\n\t\t\t\treturn newImages;\n\t\t\t});\n\t\t};\n\n\t\taddFieldEventListener(\"update-image-status\", id, handleUpdateImageStatus);\n\t\treturn () => removeFieldEventListener(\"update-image-status\", id, handleUpdateImageStatus);\n\t}, [addFieldEventListener, id, removeFieldEventListener, setImages]);\n\n\t// generate pseudo-random session id\n\tuseEffect(() => {\n\t\tsessionId.current = generateRandomId();\n\t}, []);\n\n\tuseEffect(() => {\n\t\timages.forEach((image, index) => {\n\t\t\tconst previousFile = previousImages?.[index];\n\t\t\tif (image.status !== previousFile?.status || image.dataURL !== previousFile.dataURL) {\n\t\t\t\tswitch (image.status) {\n\t\t\t\t\tcase EImageStatus.INJECTED:\n\t\t\t\t\t\tFileHelper.dataUrlToBlob(image.dataURL)\n\t\t\t\t\t\t\t.then((blob) => {\n\t\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t\t...image,\n\t\t\t\t\t\t\t\t\t\tfile: new File([blob], image.name),\n\t\t\t\t\t\t\t\t\t\tstatus: EImageStatus.NONE,\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t.catch(() => {\n\t\t\t\t\t\t\t\tsetImages((prev) => prev.filter((prev, i) => i !== index));\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.NONE:\n\t\t\t\t\t\tif (filenameMatches) {\n\t\t\t\t\t\t\tconst pattern = resolveMatchesPattern(filenameMatches);\n\t\t\t\t\t\t\tif (pattern && !pattern.test(image.name)) {\n\t\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t\t...image,\n\t\t\t\t\t\t\t\t\t\tstatus: EImageStatus.ERROR_FILENAME,\n\t\t\t\t\t\t\t\t\t\tcustomErrorMessage: filenameMatchesErrorMessage,\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tFileHelper.getType(image.file).then((fileType) => {\n\t\t\t\t\t\t\tconst mimeType = fileType.mime;\n\t\t\t\t\t\t\tif (mimeType && accepts.map(FileHelper.fileExtensionToMimeType).includes(mimeType)) {\n\t\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t\t...image,\n\t\t\t\t\t\t\t\t\t\tname: FileHelper.deduplicateFileName(\n\t\t\t\t\t\t\t\t\t\t\timages.map(({ name }) => name),\n\t\t\t\t\t\t\t\t\t\t\tindex,\n\t\t\t\t\t\t\t\t\t\t\tFileHelper.sanitizeFileName(image.name)\n\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\ttype: mimeType,\n\t\t\t\t\t\t\t\t\t\tstatus: image.addedFrom !== \"schema\" ? image.status : EImageStatus.UPLOADED,\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (image.addedFrom !== \"schema\") {\n\t\t\t\t\t\t\t\t\tcompress ? compressImage(index, image) : convertImage(index, image);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t\t...image,\n\t\t\t\t\t\t\t\t\t\tstatus: EImageStatus.ERROR_FORMAT,\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.TO_RECOMPRESS:\n\t\t\t\t\t\trecompressImage(index, image);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.COMPRESSED:\n\t\t\t\t\tcase EImageStatus.CONVERTED:\n\t\t\t\t\tcase EImageStatus.RECOMPRESSED:\n\t\t\t\t\t\tif (!editImage) {\n\t\t\t\t\t\t\tconst shouldPreventDefault = !dispatchFieldEvent(\"upload-ready\", id, { imageData: image });\n\n\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t...updatedImages[index],\n\t\t\t\t\t\t\t\t\tstatus: shouldPreventDefault ? EImageStatus.PENDING : EImageStatus.UPLOAD_READY,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.UPLOAD_READY:\n\t\t\t\t\t\tuploadImage(index, image);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.TO_DELETE:\n\t\t\t\t\t\tsetImages((prev) => prev.filter(({ status }) => status !== EImageStatus.TO_DELETE));\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase EImageStatus.UPLOADED:\n\t\t\t\t\t\tdispatchFieldEvent(\"uploaded\", id, { imageData: image });\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, [images.map(({ status }) => status).join(\",\"), images.map(({ dataURL }) => dataURL).join(\",\")]);\n\n\t// track / update errors and values\n\tuseEffect(() => {\n\t\tlet updatedManagerErrorCount = 0;\n\t\timages.forEach((image) => {\n\t\t\tif (\n\t\t\t\t(image.type && !accepts.map(FileHelper.fileExtensionToMimeType).includes(image.type)) ||\n\t\t\t\t[EImageStatus.ERROR_GENERIC, EImageStatus.ERROR_SIZE, EImageStatus.ERROR_FILENAME].includes(\n\t\t\t\t\timage.status\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tupdatedManagerErrorCount++;\n\t\t\t}\n\t\t});\n\t\tsetErrorCount((prev) => Math.max(0, prev + updatedManagerErrorCount - managerErrorCount.current));\n\t\tmanagerErrorCount.current = updatedManagerErrorCount;\n\n\t\tconst uploadedImages = images.filter(\n\t\t\t({ status }) => status === EImageStatus.UPLOADED || status === EImageStatus.ERROR_CUSTOM_MUTED\n\t\t);\n\t\tconst notPrefilledImages = uploadedImages.filter(({ addedFrom }) => addedFrom !== \"schema\");\n\t\tconst gotDeleteImages = images.filter(({ status }) => status === EImageStatus.TO_DELETE).length > 0;\n\n\t\t/**\n\t\t * should dirty if\n\t\t * - it is dirty in the first place\n\t\t * - there are non-prefilled images\n\t\t * - user deleted image (differentiated from reset)\n\t\t */\n\t\tconst hasNotPrefilledImages = notPrefilledImages.length > 0;\n\t\tconst shouldDirty = hasNotPrefilledImages || gotDeleteImages;\n\n\t\tsetCurrentFileIds(uploadedImages.map(({ id }) => id));\n\n\t\tsetValue(\n\t\t\tid,\n\t\t\tuploadedImages.map(({ id, dataURL, drawingDataURL, name, metadata, uploadResponse }) => ({\n\t\t\t\tfileId: id,\n\t\t\t\tfileName: name,\n\t\t\t\tdataURL: drawingDataURL || dataURL,\n\t\t\t\tmetadata,\n\t\t\t\tuploadResponse,\n\t\t\t})),\n\t\t\t{ shouldDirty, shouldTouch: hasNotPrefilledImages }\n\t\t);\n\t}, [accepts, id, images, setErrorCount, setValue]);\n\n\tuseEffect(() => {\n\t\tif (previousValue !== undefined && value === undefined && images.length) {\n\t\t\tsetImages([]);\n\t\t}\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, [previousValue === undefined, value === undefined, images.length]);\n\n\t// =============================================================================\n\t// HELPER FUNCTIONS\n\t// =============================================================================\n\tconst getScale = (origWidth: number, origHeight: number): number => {\n\t\tlet scale = dimensions.width / origWidth;\n\t\tif (origHeight * scale > dimensions.height) {\n\t\t\tscale = dimensions.height / origHeight;\n\t\t}\n\t\treturn scale;\n\t};\n\n\t/**\n\t * Converts a matches string (e.g. \"/^abc$/i\" or \"^abc$\") to a RegExp.\n\t * Returns undefined if the string is invalid.\n\t */\n\tconst resolveMatchesPattern = (matches: string): RegExp | undefined => {\n\t\ttry {\n\t\t\tconst parsed = matches.match(/^\\/(.+)\\/([gimsuy]*)$/);\n\t\t\treturn parsed ? new RegExp(parsed[1], parsed[2] || \"\") : new RegExp(matches);\n\t\t} catch {\n\t\t\treturn undefined;\n\t\t}\n\t};\n\n\tconst convertImage = async (index: number, image: IImage) => {\n\t\ttry {\n\t\t\tconst dataURL = await ImageHelper.convertBlob(image.file, FileHelper.fileExtensionToMimeType(outputType));\n\t\t\tconst filesize = FileHelper.getFilesizeFromBase64(dataURL);\n\n\t\t\tif (maxSizeInKb && filesize > maxSizeInKb * 1024) {\n\t\t\t\tsetImages((prev) => {\n\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\tstatus: EImageStatus.ERROR_SIZE,\n\t\t\t\t\t};\n\t\t\t\t\treturn updatedImages;\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tconst metadata = await ImageHelper.getMetadata(image.file);\n\t\t\t\tsetImages((prev) => {\n\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\tdataURL,\n\t\t\t\t\t\tmetadata,\n\t\t\t\t\t\tstatus: EImageStatus.CONVERTED,\n\t\t\t\t\t};\n\t\t\t\t\treturn updatedImages;\n\t\t\t\t});\n\t\t\t}\n\t\t} catch (e) {\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t...prev[index],\n\t\t\t\t\tstatus: EImageStatus.ERROR_GENERIC,\n\t\t\t\t};\n\t\t\t\treturn updatedImages;\n\t\t\t});\n\t\t}\n\t};\n\n\tconst compressImage = async (index: number, imageToCompress: IImage) => {\n\t\ttry {\n\t\t\tconst dataURL = await ImageHelper.convertBlob(\n\t\t\t\timageToCompress.file,\n\t\t\t\tFileHelper.fileExtensionToMimeType(outputType)\n\t\t\t);\n\t\t\tconst image = await ImageHelper.dataUrlToImage(dataURL);\n\t\t\tconst origDim = { w: image.naturalWidth, h: image.naturalHeight };\n\t\t\tlet compressed: Blob;\n\t\t\tif (crop) {\n\t\t\t\tcompressed = await ImageHelper.resampleImage(image, {\n\t\t\t\t\twidth: dimensions.width,\n\t\t\t\t\theight: dimensions.height,\n\t\t\t\t\tcrop: true,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tconst scale = getScale(origDim.w, origDim.h);\n\t\t\t\tcompressed = await ImageHelper.resampleImage(image, { scale });\n\t\t\t}\n\t\t\tif (maxSizeInKb) {\n\t\t\t\tcompressed = (await ImageHelper.compressImage(compressed, {\n\t\t\t\t\tfileSize: maxSizeInKb,\n\t\t\t\t})) as File;\n\t\t\t}\n\n\t\t\tif (maxSizeInKb && compressed.size > maxSizeInKb * 1024) {\n\t\t\t\tsetImages((prev) => {\n\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\tstatus: EImageStatus.ERROR_SIZE,\n\t\t\t\t\t};\n\t\t\t\t\treturn updatedImages;\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tconst metadata = await ImageHelper.getMetadata(imageToCompress.file);\n\t\t\t\tconst dataURL = await FileHelper.fileToDataUrl(compressed);\n\t\t\t\tsetImages((prev) => {\n\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\tdataURL,\n\t\t\t\t\t\tmetadata,\n\t\t\t\t\t\tstatus: EImageStatus.COMPRESSED,\n\t\t\t\t\t};\n\t\t\t\t\treturn updatedImages;\n\t\t\t\t});\n\t\t\t}\n\t\t} catch (e) {\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t...prev[index],\n\t\t\t\t\tstatus: EImageStatus.ERROR_GENERIC,\n\t\t\t\t};\n\t\t\t\treturn updatedImages;\n\t\t\t});\n\t\t}\n\t};\n\n\tconst recompressImage = async (index: number, imageToCompress: IImage) => {\n\t\tif (imageToCompress.drawingDataURL) {\n\t\t\ttry {\n\t\t\t\tconst image = await ImageHelper.dataUrlToImage(imageToCompress.drawingDataURL);\n\t\t\t\tconst origDim = { w: image.naturalWidth, h: image.naturalHeight };\n\t\t\t\tlet scaledFile: Blob;\n\t\t\t\tif (crop) {\n\t\t\t\t\tscaledFile = await ImageHelper.resampleImage(image, {\n\t\t\t\t\t\twidth: dimensions.width,\n\t\t\t\t\t\theight: dimensions.height,\n\t\t\t\t\t\tcrop: true,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tconst scale = getScale(origDim.w, origDim.h);\n\t\t\t\t\tscaledFile = await ImageHelper.resampleImage(image, { scale });\n\t\t\t\t}\n\t\t\t\tscaledFile = (await ImageHelper.compressImage(scaledFile, { fileSize: maxSizeInKb })) as File;\n\n\t\t\t\tif (scaledFile.size > maxSizeInKb * 1024) {\n\t\t\t\t\tconst updatedImages = [...images];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...images[index],\n\t\t\t\t\t\tstatus: EImageStatus.ERROR_SIZE,\n\t\t\t\t\t};\n\t\t\t\t\tsetImages(updatedImages);\n\t\t\t\t} else {\n\t\t\t\t\tconst dataURL = await FileHelper.fileToDataUrl(scaledFile);\n\t\t\t\t\tconst updatedImages = [...images];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...images[index],\n\t\t\t\t\t\tdrawingDataURL: dataURL,\n\t\t\t\t\t\tstatus: EImageStatus.RECOMPRESSED,\n\t\t\t\t\t};\n\t\t\t\t\tsetImages(updatedImages);\n\t\t\t\t}\n\t\t\t} catch (e) {\n\t\t\t\tsetImages((prev) => {\n\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\tstatus: EImageStatus.ERROR_GENERIC,\n\t\t\t\t\t};\n\t\t\t\t\treturn updatedImages;\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t};\n\n\tconst uploadImage = async (index: number, iFile: IImage) => {\n\t\ttry {\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t...prev[index],\n\t\t\t\t\tstatus: EImageStatus.UPLOADING,\n\t\t\t\t};\n\t\t\t\treturn updatedImages;\n\t\t\t});\n\n\t\t\tlet response: unknown;\n\t\t\tif (upload?.method && upload?.url) {\n\t\t\t\tconst formData = new FormData();\n\t\t\t\tformData.append(\"dataURL\", iFile.drawingDataURL || iFile.dataURL || \"\");\n\t\t\t\tformData.append(\"sessionId\", upload?.sessionId || sessionId.current || \"\");\n\t\t\t\tformData.append(\"slot\", `${iFile.slot}`);\n\t\t\t\tresponse = await new AxiosApiClient(\"\", undefined, undefined, true)[upload.method](\n\t\t\t\t\tupload.url,\n\t\t\t\t\tformData,\n\t\t\t\t\t{\n\t\t\t\t\t\tonUploadProgress: (progressEvent) => {\n\t\t\t\t\t\t\tconst { loaded, total } = progressEvent;\n\t\t\t\t\t\t\tconst percent = Math.floor((loaded * 100) / total);\n\t\t\t\t\t\t\tsetImages((prev) => {\n\t\t\t\t\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\t\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t\t\t\t\t...prev[index],\n\t\t\t\t\t\t\t\t\tuploadProgress: percent,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\treturn updatedImages;\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t},\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t...prev[index],\n\t\t\t\t\tuploadResponse: response,\n\t\t\t\t\tstatus: EImageStatus.UPLOADED,\n\t\t\t\t};\n\t\t\t\treturn updatedImages;\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tsetImages((prev) => {\n\t\t\t\tconst updatedImages = [...prev];\n\t\t\t\tupdatedImages[index] = {\n\t\t\t\t\t...prev[index],\n\t\t\t\t\tstatus: EImageStatus.ERROR_GENERIC,\n\t\t\t\t};\n\t\t\t\treturn updatedImages;\n\t\t\t});\n\t\t}\n\t};\n\n\treturn null;\n};\n"],"names":["ImageManager","props","accepts","compress","crop","dimensions","editImage","id","maxSizeInKb","outputType","upload","filenameMatches","filenameMatchesErrorMessage","value","images","setImages","setErrorCount","setCurrentFileIds","useContext","ImageContext","previousImages","usePrevious","previousValue","setValue","useFormContext","sessionId","useRef","managerErrorCount","dispatchFieldEvent","addFieldEventListener","removeFieldEventListener","useFieldEvent","useEffect","handleUpdateImageStatus","e","prev","imageIndex","findIndex","image","detail","updatedImage","status","updatedStatus","customErrorMessage","errorMessage","newImages","splice","current","generateRandomId","forEach","index","previousFile","dataURL","EImageStatus","INJECTED","FileHelper","dataUrlToBlob","then","blob","updatedImages","file","File","name","NONE","catch","filter","i","pattern","resolveMatchesPattern","test","ERROR_FILENAME","getType","fileType","mimeType","mime","map","fileExtensionToMimeType","includes","deduplicateFileName","sanitizeFileName","type","addedFrom","UPLOADED","compressImage","convertImage","ERROR_FORMAT","TO_RECOMPRESS","recompressImage","COMPRESSED","CONVERTED","RECOMPRESSED","shouldPreventDefault","imageData","PENDING","UPLOAD_READY","uploadImage","TO_DELETE","join","updatedManagerErrorCount","ERROR_GENERIC","ERROR_SIZE","Math","max","uploadedImages","ERROR_CUSTOM_MUTED","notPrefilledImages","gotDeleteImages","length","hasNotPrefilledImages","shouldDirty","drawingDataURL","metadata","uploadResponse","fileId","fileName","shouldTouch","undefined","getScale","origWidth","origHeight","scale","width","height","matches","parsed","match","RegExp","async","ImageHelper","convertBlob","filesize","getFilesizeFromBase64","getMetadata","imageToCompress","dataUrlToImage","origDim","w","naturalWidth","h","naturalHeight","compressed","resampleImage","fileSize","size","fileToDataUrl","scaledFile","iFile","response","UPLOADING","method","url","formData","FormData","append","slot","AxiosApiClient","onUploadProgress","progressEvent","loaded","total","percent","floor","uploadProgress","err"],"mappings":"kwDAkCa,MAAAA,EAAgBC,IAI5B,MAAMC,QACLA,EAAOC,SACPA,EAAQC,KACRA,EAAIC,WACJA,EAAUC,UACVA,EAASC,GACTA,EAAEC,YACFA,EAAWC,WACXA,EAAUC,OACVA,EAAMC,gBACNA,EAAeC,4BACfA,EAA2BC,MAC3BA,GACGZ,GACEa,OAAEA,EAAMC,UAAEA,EAASC,cAAEA,EAAaC,kBAAEA,GAAsBC,EAAWC,GACrEC,EAAiBC,EAAYP,GAC7BQ,EAAgBD,EAAYR,IAC5BU,SAAEA,GAAaC,IACfC,EAAYC,IACZC,EAAoBD,EAAO,IAE3BE,mBAAEA,EAAkBC,sBAAEA,EAAqBC,yBAAEA,GAA6BC,IAKhFC,GAAU,KACT,MAAMC,EAA2BC,IAChCnB,GAAWoB,IACV,MAAMC,EAAaD,EAAKE,WAAWC,GAAUA,EAAM/B,KAAO2B,EAAEK,OAAOhC,KAC7DiC,EAAe,IAAKL,EAAKC,IAC/BI,EAAaC,OAASP,EAAEK,OAAOG,cAC/BF,EAAaG,mBAAqBT,EAAEK,OAAOK,aAE3C,MAAMC,EAAY,IAAIV,GAEtB,OADAU,EAAUC,OAAOV,EAAY,EAAGI,GACzBK,CAAS,GACf,EAIH,OADAhB,EAAsB,sBAAuBtB,EAAI0B,GAC1C,IAAMH,EAAyB,sBAAuBvB,EAAI0B,EAAwB,GACvF,CAACJ,EAAuBtB,EAAIuB,EAA0Bf,IAGzDiB,GAAU,KACTP,EAAUsB,QAAUC,GAAkB,GACpC,IAEHhB,GAAU,KACTlB,EAAOmC,SAAQ,CAACX,EAAOY,KACtB,MAAMC,EAAe/B,I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K1B,OAJAwB,EAAcT,GAAS,IACnBf,EAAKe,GACRT,OAAQY,EAAa+F,WAEfzF,CAAa,IAIjBjD,GAAQ2I,QAAU3I,GAAQ4I,IAAK,CAClC,MAAMC,EAAW,IAAIC,SACrBD,EAASE,OAAO,UAAWP,EAAMpC,gBAAkBoC,EAAM9F,SAAW,IACpEmG,EAASE,OAAO,YAAa/I,GAAQe,WAAaA,EAAUsB,SAAW,IACvEwG,EAASE,OAAO,OAAQ,GAAGP,EAAMQ,QACjCP,QAAiB,IAAIQ,EAAe,QAAIvC,OAAWA,GAAW,GAAM1G,EAAO2I,QAC1E3I,EAAO4I,IACPC,EACA,CACCK,iBAAmBC,IAClB,MAAMC,OAAEA,EAAMC,MAAEA,GAAUF,EACpBG,EAAU3D,KAAK4D,MAAgB,IAATH,EAAgBC,GAC5ChJ,GAAWoB,IACV,MAAMwB,EAAgB,IAAIxB,GAK1B,OAJAwB,EAAcT,GAAS,IACnBf,EAAKe,GACRgH,eAAgBF,GAEVrG,CAAa,GACnB,GAIL,CAED5C,GAAWoB,IACV,MAAMwB,EAAgB,IAAIxB,GAM1B,OALAwB,EAAcT,GAAS,IACnBf,EAAKe,GACR8D,eAAgBmC,EAChB1G,OAAQY,EAAa6B,UAEfvB,CAAa,GAErB,CAAC,MAAOwG,GACRpJ,GAAWoB,IACV,MAAMwB,EAAgB,IAAIxB,GAK1B,OAJAwB,EAAcT,GAAS,IACnBf,EAAKe,GACRT,OAAQY,EAAa8C,eAEfxC,CAAa,GAErB,GAGF,OAAO,IAAI"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{jsx as t,jsxs as e,Fragment as n}from"react/jsx-runtime";import{Modal as i}from"@lifesg/react-design-system/modal";import{Colour as o,Font as r,Spacing as s,MediaQuery as a,Breakpoint as l,Border as h,Motion as c}from"@lifesg/react-design-system/theme";import{R as u,h as d,b as p,L as m,T as f,O as _,i as g,j as v,G as y,l as x,S as b,P as w,k as P,D as T}from"./index.
|
|
1
|
+
import{jsx as t,jsxs as e,Fragment as n}from"react/jsx-runtime";import{Modal as i}from"@lifesg/react-design-system/modal";import{Colour as o,Font as r,Spacing as s,MediaQuery as a,Breakpoint as l,Border as h,Motion as c}from"@lifesg/react-design-system/theme";import{R as u,h as d,b as p,L as m,T as f,O as _,i as g,j as v,G as y,l as x,S as b,P as w,k as P,D as T}from"./index.2d28e824.js";import{useContext as M,useRef as z,useEffect as k,useState as C,useCallback as S}from"react";import E,{ThemeContext as I,keyframes as Z}from"styled-components";import"@lifesg/react-design-system/markup";import A from"react-dom/server";import{Typography as O}from"@lifesg/react-design-system/typography";import{NavigationIcon as B}from"@lifesg/react-icons/navigation";import{NavigationFillIcon as N}from"@lifesg/react-icons/navigation-fill";import{PinFillIcon as R}from"@lifesg/react-icons/pin-fill";import{Card as D}from"@lifesg/react-design-system/card";import{CrossIcon as j}from"@lifesg/react-icons/cross";import{MagnifierIcon as $}from"@lifesg/react-icons/magnifier";import{Button as F}from"@lifesg/react-design-system/button";import"@lifesg/react-design-system/alert";import"@lifesg/react-design-system/layout";import"@lifesg/react-design-system/box-container";import"@lifesg/react-design-system/divider";import"@lifesg/react-design-system/v2_media";import"@lifesg/react-design-system/text-list";import"@lifesg/react-design-system/popover-v2";import"@lifesg/react-icons";import"@lifesg/react-design-system/tab";import"@lifesg/react-icons/plus-circle-fill";import"@lifesg/react-design-system/button-with-icon";import"@lifesg/react-design-system/error-display";import"@lifesg/react-design-system/filter";import"@lifesg/react-design-system/uneditable-section";import"@lifesg/react-design-system/timeline";import"@lifesg/react-design-system/form";import"@lifesg/react-design-system/checkbox";import"@lifesg/react-design-system/toggle";import"@lifesg/react-design-system/file-upload";import"@lifesg/react-design-system/icon-button";import"@lifesg/react-icons/bin";import"@lifesg/react-icons/eraser";import"@lifesg/react-icons/pencil";import"@lifesg/react-icons/pencil-stroke";import"@lifesg/react-icons/plus";import"@lifesg/react-icons/exclamation-triangle";import"@lifesg/react-design-system/image-button";import"@lifesg/react-design-system/radio-button";import"@lifesg/react-design-system/v2_layout";const W="https://assets.life.gov.sg/web-frontend-engine/img/common/no-network.png";var H={exports:{}};
|
|
2
2
|
/* @preserve
|
|
3
3
|
* Leaflet 1.9.4, a JS library for interactive maps. https://leafletjs.com
|
|
4
4
|
* (c) 2010-2023 Vladimir Agafonkin, (c) 2010-2011 CloudMade
|
|
@@ -344,4 +344,4 @@ import{jsx as t,jsxs as e,Fragment as n}from"react/jsx-runtime";import{Modal as
|
|
|
344
344
|
max-width: 16.5rem;
|
|
345
345
|
}
|
|
346
346
|
`,Bt=({id:n,cachedImage:i,refreshNetwork:o})=>e(Et,{id:f.generateId(n,"no-internet-connectivity"),"data-testid":f.generateId(n,"no-internet-connectivity"),children:[t(It,{src:i,alt:"no-connectivity"}),t(Zt,{weight:"semibold",children:"No connection found"}),t(At,{children:"Check your internet connection and try again."}),t(Ot,{onClick:o,children:"Try again"})]}),Nt=({id:o="location-modal",className:r,formValues:s,showLocationModal:a,mapPanZoom:h,interactiveMapPinIconUrl:c,mapApi:u,gettingCurrentLocationFetchMessage:d,mapBannerText:m,mustHavePostalCode:g,locationModalStyles:v,onClose:b,onConfirm:L,updateFormValues:E,locationListTitle:Z,locationSelectionMode:A,disableSearch:O,addressFieldPlaceholder:B,searchBarIcon:N,bufferRadius:R,pinsOnlyIndicateCurrentLocation:D})=>{const[j,$]=C("double"),[F,H]=C({}),[V,U]=C([]),[G,q]=C(!0),[K,Y]=C(!1),{dispatchFieldEvent:X,addFieldEventListener:nt,removeFieldEventListener:it}=p(),[ot,rt]=C(!0),[st,at]=C(!1),[lt,ht]=C(!1),[ct,ut]=C(!1),[dt,pt]=C(),[mt,ft]=C(),_t=z(!0),gt=M(I),vt=S(((t,e)=>{$((n=>"double"===n||e&&n!==e?n:t))}),[]),yt=S((async()=>{Y(!0);if(!!X("get-current-location",o)){const t={};try{t.payload=await P.getCurrentLocation()}catch(e){t.errors=e}return X("set-current-location",o,t),t.payload}}),[X,o]),xt=S((()=>{H(s||{})}),[s]),bt=S((()=>{_t.current=!0,b()}),[b]),wt=S((()=>{xt(),bt()}),[bt,xt]),Lt=S((t=>{const e=x.exports.isEmpty(t?.detail)?F:t?.detail;L(e),bt()}),[bt,L,F]),Pt=()=>{!X("before-hide-permission-modal",o)||at(!1)};k((()=>{const t={"error-end":t=>{const e=t.detail?.payload?.errorType;if(e)switch(e){case"OneMapError":case"GetLocationTimeoutError":bt()}},"set-selectable-pins":t=>{const e=t.detail.pins;Array.isArray(e)?U(e):ht(!0)},"confirm-location":Lt,"hide-permission-modal":()=>{at(!1)},"dismiss-location-modal":wt,"trigger-get-current-location":yt};return Object.entries(t).forEach((([t,e])=>{nt(t,o,e)})),()=>{Object.entries(t).forEach((([t,e])=>{it(t,o,e)}))}}),[nt,yt,wt,bt,Lt,o,it]),k((()=>{if(!window)return;const t=matchMedia(`(max-width: ${l["lg-max"]({theme:gt})}px)`);$(t.matches?"map":"double");const e=()=>rt(!0),n=()=>rt(!1),i=t=>{$(t.matches?"map":"double")};return window.addEventListener("online",e),window.addEventListener("offline",n),t.addEventListener("change",i),()=>{window.removeEventListener("online",e),window.removeEventListener("offline",n),t.removeEventListener("change",i)}}),[]),k((()=>{if(!a)return void $((t=>"double"!==t?"map":t));(async()=>{const{lat:t,lng:e}=s||{};t||e?_t.current&&X("get-selectable-pins",o,{lat:t,lng:e}):await yt(),_t.current=!1})()}),[X,s,yt,o,a]),k((()=>{x.exports.isEmpty(F)||vt("map","search")}),[F,K,vt]),k((()=>{H(s||{})}),[s]);return e(n,{children:[t(et,{src:W,alt:"no internet connectivity"}),t(i,{id:f.generateId(o,"modal",a?"show":"hide"),className:`${r}-location-modal`,show:a,children:t(J,{id:f.generateId(o,"modal-box"),className:`${r}-modal-box`,showCloseButton:!1,locationModalStyles:v,children:ot?e(n,{children:[t(St,{id:o,className:r,onCancel:wt,onConfirm:()=>{!X("click-confirm-location",o,F)||Lt()},updateFormValues:E,gettingCurrentLocation:K,panelInputMode:j,selectedAddressInfo:F,mapPickedLatLng:dt,formValues:s,onChangeSelectedAddressInfo:H,handleApiErrors:t=>{const e=(e,n)=>{!X("error",o,{payload:{errorType:e},errors:t})||n()};Y(!1),t instanceof _?e("OneMapError",(()=>{xt(),ht(!0)})):(q(!1),t instanceof y&&t?.code?.toString()===GeolocationPositionError.TIMEOUT.toString()?e("GetLocationTimeoutError",(()=>{ut(!0)})):e("GetLocationError",(()=>{at(!0)})))},onGetLocationCallback:(t,e)=>{Y(!1),q(!0),ft({lat:t,lng:e})},setSinglePanelMode:vt,showLocationModal:a,mapApi:u,gettingCurrentLocationFetchMessage:d,mustHavePostalCode:g,locationListTitle:Z,restrictLocationSelection:"pins-only"===A,selectablePins:V,disableSearch:O,addressFieldPlaceholder:B,searchBarIcon:N,bufferRadius:R}),t(Q,{id:o,className:r,panelInputMode:j,locationAvailable:G,gettingCurrentLocation:K,showLocationModal:a,selectedLocationCoord:{lat:F.lat,lng:F.lng},getCurrentLocation:yt,onMapCenterChange:t=>{pt(t)},interactiveMapPinIconUrl:c,mapPanZoom:h,mapBannerText:m,disableSelectionFromMap:"pins-only"===A,disableSelectedLocationMarker:"pins-only"===A,selectablePins:V,pinsOnlyIndicateCurrentLocation:D&&"pins-only"===A,currentLocation:mt})]}):t(Bt,{id:o,cachedImage:W,refreshNetwork:()=>{try{navigator.onLine&&rt(!0)}catch(t){}}})})}),(()=>{if(ot&&a)return lt?t(w,{id:f.generateId(o,"onemap-error"),"data-testid":f.generateId(o,"onemap-error"),title:"Map not available",size:"large",show:!0,image:t(tt,{src:"https://assets.life.gov.sg/web-frontend-engine/img/common/error.svg"}),description:e(T,{weight:"regular",children:["Sorry, there was a problem with the map. You’ll not be able to enter the location right now. Please try again later.",t("br",{}),t("br",{}),"Do note that you’ll not be able to submit your report without entering the location."]}),buttons:[{id:"ok",title:"OK",onClick:()=>{ht(!1),bt()}}]}):st?t(w,{id:f.generateId(o,"get-location-error"),"data-testid":f.generateId(o,"get-location-error"),title:"Enable location settings",size:"large",show:!0,description:"We need your permission to determine your location. Enable location access in your browser and device settings, or enter your location manually.",buttons:[{id:"ok",title:"OK",onClick:Pt}]}):ct?t(w,{id:f.generateId(o,"get-location-timeout-error"),"data-testid":f.generateId(o,"get-location-timeout-error"),title:"Something went wrong",size:"large",show:!0,image:t(tt,{src:"https://assets.life.gov.sg/web-frontend-engine/img/icons/get-location-timeout.svg"}),description:t(T,{weight:"regular",children:"It’s taking longer than expected to retrieve your location. Please exit the map and try again."}),buttons:[{id:"ok",title:"OK",onClick:()=>{ut(!1),bt()}}]}):void 0})()]})};export{Nt as default};
|
|
347
|
-
//# sourceMappingURL=location-modal.
|
|
347
|
+
//# sourceMappingURL=location-modal.5e4e98fd.js.map
|