cmms/frontend/dist/assets/useFormAutoSave-Jaawgo91.js

2 lines
3.2 KiB
JavaScript

import{r,m as j,b4 as h,o as R}from"./index-BUdRbiwj.js";function M(l,S={}){const{storageKey:f="form-auto-save",saveInterval:F=2e3,excludeFields:D=[],onRestore:p=null,onSave:b=null,enabled:B=!0}=S,s=r(!1),c=r(null),i=r(null),o=r(B),u=r(!1),d=r(!0),I=e=>{if(!e||typeof e!="object")return e;const a={...e};return D.forEach(t=>{delete a[t]}),Object.keys(a).forEach(t=>{const n=a[t];(typeof n=="function"||typeof n=="symbol"||n===void 0)&&delete a[t]}),a.image&&(a.image instanceof File||a.image instanceof Blob)?a.image={_isFile:!0,name:a.image.name||"image",size:a.image.size||0,type:a.image.type||"image/*",lastModified:a.image.lastModified||Date.now()}:a.image&&a.image._isFile&&(a.image=null),a.audioRecordings&&Array.isArray(a.audioRecordings)&&(a.audioRecordings=a.audioRecordings.map(t=>{const n={...t};return n.audioBlob instanceof Blob&&(n.audioBlob={_isBlob:!0,size:n.audioBlob.size||0,type:n.audioBlob.type||"audio/webm"}),n.audioUrl&&n.audioUrl.startsWith("blob:")&&(n.audioUrl=null,n._hadBlobUrl=!0),n})),a.files&&Array.isArray(a.files)&&(a.files=a.files.map(t=>t instanceof File||t instanceof Blob?{_isFile:!0,name:t.name||"file",size:t.size||0,type:t.type||"application/octet-stream",lastModified:t.lastModified||Date.now()}:t&&t._isFile?null:t).filter(t=>t!==null)),a},m=()=>{if(!(!o.value||!l||!u.value))try{const e=l.value||l,t={data:I(e),timestamp:Date.now(),url:window.location.pathname,userAgent:navigator.userAgent.substring(0,100)};localStorage.setItem(f,JSON.stringify(t)),c.value=new Date,s.value=!1,b&&b(t)}catch(e){console.error("Failed to auto-save form data:",e)}},v=()=>{if(!o.value)return null;try{const e=localStorage.getItem(f);if(!e)return null;const a=JSON.parse(e);if(a.url!==window.location.pathname)return null;const t=24*60*60*1e3;return Date.now()-a.timestamp>t?(w(),null):a}catch(e){return console.error("Failed to load auto-saved form data:",e),null}},T=()=>{const e=v();if(!e||!e.data)return!1;try{const a=o.value;return o.value=!1,l.value?Object.assign(l.value,e.data):Object.assign(l,e.data),c.value=new Date(e.timestamp),s.value=!1,setTimeout(()=>{o.value=a},100),p&&p(e),!0}catch(a){return console.warn("Failed to restore form data:",a),o.value=!0,!1}},w=()=>{try{localStorage.removeItem(f),s.value=!1,c.value=null,u.value=!1,d.value=!0}catch(e){console.warn("Failed to clear auto-saved data:",e)}},z=()=>{const e=v();return e&&e.data&&Object.keys(e.data).length>0},A=()=>{const e=v();return e?{timestamp:e.timestamp,date:new Date(e.timestamp),age:Date.now()-e.timestamp,hasData:e.data&&Object.keys(e.data).length>0}:null},U=()=>{i.value&&clearTimeout(i.value),i.value=setTimeout(()=>{m()},F)},g=()=>{i.value&&clearTimeout(i.value),m()},E=()=>{u.value=!0,d.value=!1},O=()=>{d.value=!1};if(o.value&&l){const e=j(l,(a,t)=>{o.value&&!d.value&&(u.value=!0,s.value=!0,U())},{deep:!0,immediate:!1});h(()=>{e()})}const y=e=>{s.value&&g()};return R(()=>{o.value&&window.addEventListener("beforeunload",y)}),h(()=>{i.value&&clearTimeout(i.value),o.value&&(window.removeEventListener("beforeunload",y),s.value&&g())}),{hasUnsavedChanges:s,lastSaveTime:c,userHasInteracted:u,saveNow:g,restoreFormData:T,clearSavedData:w,hasSavedData:z,getSavedDataInfo:A,markUserInteraction:E,finishInitialization:O,loadFromStorage:v,saveToStorage:m}}export{M as u};