@duxweb/dvha-core 1.1.4 → 1.1.6
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/cjs/hooks/form.cjs +1 -1
- package/dist/cjs/hooks/formExtend.cjs +1 -1
- package/dist/cjs/hooks/upload.cjs +1 -1
- package/dist/esm/hooks/form.js +49 -29
- package/dist/esm/hooks/formExtend.js +28 -26
- package/dist/esm/hooks/upload.js +1 -1
- package/dist/types/hooks/form.d.ts +2 -0
- package/package.json +1 -1
package/dist/cjs/hooks/form.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("lodash-es"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("lodash-es"),u=require("vue"),l=require("./data.cjs");function R(a){const t=u.toRef(a,"form",{}),o=u.toRef(a,"id",void 0),i=u.toRef(a,"meta",{}),v=u.toRef(a,"params",{}),c=u.ref(r.cloneDeep(t.value||{})),d=u.ref(r.cloneDeep(t.value||{})),n=u.computed(()=>a.action==="edit"||!!o.value),{data:m,isLoading:b,refetch:g}=l.useOne({get path(){return a.path||""},get id(){return o.value},get meta(){return{params:{...v.value},...i.value}},options:{enabled:!1},providerName:a.providerName}),s=()=>{let e={};n.value?e=r.cloneDeep(c.value):e=r.cloneDeep(d.value),Object.assign(t.value,e)};u.watch([o,n],async()=>{if(!n.value){s();return}await g();const e=r.cloneDeep(m.value?.data||{});Object.assign(t.value,e),Object.assign(c.value,e)},{immediate:!0,deep:!0}),u.watch([v,i],async()=>{if(!n.value)return;await g();const e=r.cloneDeep(m.value?.data||{});Object.assign(t.value,e),Object.assign(c.value,e)},{deep:!0});const f=l.useCreate({path:a.path??"",data:t.value,get meta(){return i.value||{}},onSuccess:e=>{s(),a.onSuccess?.(e)},onError:e=>{a.onError?.(e)},providerName:a.providerName}),D=l.useUpdate({get path(){return a.path??""},get id(){return o.value},data:t.value,get meta(){return i.value||{}},onSuccess:e=>{a.onSuccess?.(e)},onError:e=>{a.onError?.(e)},providerName:a.providerName}),h=e=>{n.value?D.mutate({id:o.value,data:e||t.value}):f.mutate({data:e||t.value})},O=u.computed(()=>b.value||f.isLoading.value||D.isLoading.value);return{form:t,initData:n.value?c.value:d.value,isLoading:O,isEdit:n,onSubmit:h,onReset:s}}exports.useForm=R;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("vue"),d=require("./form.cjs"),c=require("./formValidate.cjs");function l(e){const o=m.toRef(e,"form",{}),{validate:r,reset:n}=c.useValidateForm({data:o,rules:e.rules}),t=d.useForm({id:e.id,path:e.path,form:o,action:e.action,onSuccess:e.onSuccess,onError:e.onError,providerName:e.providerName,meta:e.meta,params:e.params}),i=s=>{r().then(u=>{if(!u.valid){e.onError?.({message:"表单验证失败"});return}t.onSubmit(s)})},a=()=>{t.onReset(),n()};return{isLoading:t.isLoading,isEdit:t.isEdit,form:o,onSubmit:i,onReset:a}}exports.useExtendForm=l;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("@vueuse/core"),V=require("lodash-es"),c=require("vue"),_=require("./upload/local.cjs");function H(l){l=l||{};const v=c.ref(!1),o=c.ref([]),u=c.ref(-1),d=c.ref(new Map),S=l.driver||_.createLocalUploadDriver(),P=c.computed(()=>{const{onProgress:e,onDataCallback:t,onCancel:a,onComplete:n,maxFileSize:i,maxFileCount:s,accept:r,multiple:f,autoUpload:m,options:M,onSuccess:U,onError:Q,method:R,driver:W,params:X,...O}=l;return O}),A=c.computed(()=>l.method||"POST"),F=c.computed(()=>{const e=o.value.length,t=o.value.reduce((s,r)=>s+(r.filesize||0),0);let a=0,n=0;return o.value.forEach((s,r)=>{r<u.value?a+=s.filesize||0:r===u.value&&(a+=s.progress?.loaded||0)}),t>0&&(n=Math.round(a/t*100)),{index:u.value>=0?u.value+1:0,totalFiles:e,currentFile:o.value[u.value],totalPercent:n,totalLoaded:a,totalSize:t}}),w=e=>{if(e===0)return"0 Bytes";const t=1024,a=["Bytes","KB","MB","GB"],n=Math.floor(Math.log(e)/Math.log(t));return`${Number.parseFloat((e/t**n).toFixed(2))} ${a[n]}`},g=e=>{if(e===0)return"0 B/s";const t=1024,a=["B/s","KB/s","MB/s","GB/s"],n=Math.floor(Math.log(e)/Math.log(t));return`${Number.parseFloat((e/t**n).toFixed(2))} ${a[n]}`},b=()=>V.uniqueId("upload-"),$=async(e,t,a)=>{switch(t){case"file":{if(!(e instanceof File))throw new TypeError('Payload must be a File when type is "file"');return e}case"blob":{if(!(e instanceof Blob))throw new TypeError('Payload must be a Blob when type is "blob"');return new File([e],"blob-file",{type:e.type})}case"base64":{if(typeof e!="string")throw new TypeError('Payload must be a string when type is "base64"');const n=e.includes(",")?e.split(",")[1]:e,i=e.includes(",")?e.split(",")[0].split(":")[1].split(";")[0]:"application/octet-stream",s=atob(n),r=new Uint8Array(s.length);for(let m=0;m<s.length;m++)r[m]=s.charCodeAt(m);const f=new Blob([r],{type:i});return new File([f],"base64-file",{type:i})}case"arrayBuffer":{if(!(e instanceof ArrayBuffer))throw new TypeError('Payload must be ArrayBuffer when type is "arrayBuffer"');const n=new Blob([e]);return new File([n],"array-buffer-file",{type:"application/octet-stream"})}default:throw new Error(`Unsupported upload type: ${t}`)}},x=e=>{if(l.maxFileSize&&e.size>l.maxFileSize)throw new Error(`File size cannot exceed ${w(l.maxFileSize)}`);if(l.maxFileCount&&o.value.length
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("@vueuse/core"),V=require("lodash-es"),c=require("vue"),_=require("./upload/local.cjs");function H(l){l=l||{};const v=c.ref(!1),o=c.ref([]),u=c.ref(-1),d=c.ref(new Map),S=l.driver||_.createLocalUploadDriver(),P=c.computed(()=>{const{onProgress:e,onDataCallback:t,onCancel:a,onComplete:n,maxFileSize:i,maxFileCount:s,accept:r,multiple:f,autoUpload:m,options:M,onSuccess:U,onError:Q,method:R,driver:W,params:X,...O}=l;return O}),A=c.computed(()=>l.method||"POST"),F=c.computed(()=>{const e=o.value.length,t=o.value.reduce((s,r)=>s+(r.filesize||0),0);let a=0,n=0;return o.value.forEach((s,r)=>{r<u.value?a+=s.filesize||0:r===u.value&&(a+=s.progress?.loaded||0)}),t>0&&(n=Math.round(a/t*100)),{index:u.value>=0?u.value+1:0,totalFiles:e,currentFile:o.value[u.value],totalPercent:n,totalLoaded:a,totalSize:t}}),w=e=>{if(e===0)return"0 Bytes";const t=1024,a=["Bytes","KB","MB","GB"],n=Math.floor(Math.log(e)/Math.log(t));return`${Number.parseFloat((e/t**n).toFixed(2))} ${a[n]}`},g=e=>{if(e===0)return"0 B/s";const t=1024,a=["B/s","KB/s","MB/s","GB/s"],n=Math.floor(Math.log(e)/Math.log(t));return`${Number.parseFloat((e/t**n).toFixed(2))} ${a[n]}`},b=()=>V.uniqueId("upload-"),$=async(e,t,a)=>{switch(t){case"file":{if(!(e instanceof File))throw new TypeError('Payload must be a File when type is "file"');return e}case"blob":{if(!(e instanceof Blob))throw new TypeError('Payload must be a Blob when type is "blob"');return new File([e],"blob-file",{type:e.type})}case"base64":{if(typeof e!="string")throw new TypeError('Payload must be a string when type is "base64"');const n=e.includes(",")?e.split(",")[1]:e,i=e.includes(",")?e.split(",")[0].split(":")[1].split(";")[0]:"application/octet-stream",s=atob(n),r=new Uint8Array(s.length);for(let m=0;m<s.length;m++)r[m]=s.charCodeAt(m);const f=new Blob([r],{type:i});return new File([f],"base64-file",{type:i})}case"arrayBuffer":{if(!(e instanceof ArrayBuffer))throw new TypeError('Payload must be ArrayBuffer when type is "arrayBuffer"');const n=new Blob([e]);return new File([n],"array-buffer-file",{type:"application/octet-stream"})}default:throw new Error(`Unsupported upload type: ${t}`)}},x=e=>{if(l.maxFileSize&&e.size>l.maxFileSize)throw new Error(`File size cannot exceed ${w(l.maxFileSize)}`);if(l.maxFileCount&&o.value.length>l.maxFileCount)throw new Error(`File count cannot exceed ${l.maxFileCount}`)},I=async(e,t,a)=>{const n=await $(e,t);x(n);const i={id:b(),file:n,filename:n.name,filesize:n.size,filetype:n.type,filesizeText:w(n.size),status:"pending",progress:{loaded:0,total:n.size,percent:0,speed:0,speedText:g(0),remainingTime:0,uploadTime:0}};return o.value.push(i),i},h=(e,t)=>{const a=o.value.findIndex(n=>n.id===e);a!==-1&&(o.value[a]={...o.value[a],...t})},T=()=>{o.value.every(t=>t.status!=="uploading")&&l.onComplete?.(o.value)},p=e=>{const t=o.value.find(a=>a.id===e);if(!t||t.status!=="pending"&&t.status!=="uploading")return!1;if(t.status==="uploading"){const a=d.value.get(e);a&&(a.abort(),d.value.delete(e))}return h(e,{status:"cancelled"}),l.onCancel?.(e),T(),!0},k=e=>{e?e.forEach(t=>p(t)):o.value.forEach(t=>p(t.id))},q=async e=>{if(!e.file)throw new Error("File not found");x(e.file);const t=new AbortController;d.value.set(e.id,t);const a=Date.now();return h(e.id,{status:"uploading"}),S.upload(e.file,{...P.value,method:A.value,signal:t.signal,params:l.params,onUploadProgress:n=>{const r=(Date.now()-a)/1e3,f=r>0?n.loaded/r:0,m=(n.total||0)-n.loaded,M=f>0?m/f:0,U={loaded:n.loaded,total:n.total,percent:n.percent||0,speed:Math.round(f),speedText:g(f),uploadTime:Math.round(r),remainingTime:Math.round(M)};h(e.id,{status:"uploading",progress:U}),l.onProgress?.(F.value)}}).then(n=>{if(e.status==="cancelled")throw new Error("Upload cancelled");const i=l.onDataCallback?.(n,e)||n.data,s=Math.round((Date.now()-a)/1e3),r={...e,status:"success",data:n,...i,progress:{...e.progress,loaded:e.filesize||0,percent:100,speed:0,speedText:g(0),uploadTime:s,remainingTime:0}};return h(e.id,r),d.value.delete(e.id),l.onSuccess?.(n),r}).catch(n=>{throw n.message==="canceled"||e.status==="cancelled"?new Error("Upload cancelled"):(h(e.id,{status:"error",error:n.message}),d.value.delete(e.id),n)}).finally(()=>{T()})},z=async()=>{const e=o.value.filter(t=>t.status==="pending");if(e.length===0)return[];v.value=!0;try{const t=[];for(let a=0;a<e.length;a++){const n=e[a],i=o.value.findIndex(r=>r.id===n.id);u.value=i;const s=await q(n);t.push(s)}return t}catch(t){throw l.onError?.({status:t?.status||500,message:t?.message||"upload failed"}),t}finally{v.value=!1,u.value=-1}},y=()=>{o.value.forEach(e=>{p(e.id)}),d.value.clear(),o.value=[],u.value=-1},C=async(e,t="file")=>{try{if(!l.multiple&&e.length>1)throw new Error("Single file mode: only one file can be selected");if(!l.multiple&&o.value.length>0&&y(),l.maxFileCount&&o.value.length+e.length>l.maxFileCount)throw new Error(`Adding ${e.length} files would exceed the maximum limit of ${l.maxFileCount}`);for(const a of e)await I(a,t)}catch(a){throw l.onError?.({status:a?.status||400,message:a?.message||"Failed to add files"}),a}l.autoUpload&&z().catch(a=>{console.warn("Auto upload failed:",a)})},E=e=>{const t=o.value.find(n=>n.id===e);if(t&&t.status==="uploading"){h(e,{status:"cancelled"});const n=d.value.get(e);if(!n)return;n.abort(),d.value.delete(e)}const a=o.value.findIndex(n=>n.id===e);a!==-1&&o.value.splice(a,1)},D=e=>{e?e.forEach(t=>E(t)):o.value.forEach(t=>E(t.id))},{files:G,open:K,reset:B}=j.useFileDialog({accept:l.accept,multiple:l.multiple||!1}),L=e=>({id:b(),...e,filesizeText:e.filesize?w(e.filesize):void 0,status:e.url?"success":"pending",progress:{loaded:0,total:e.filesize||0,percent:e.url?100:0,speed:0,speedText:g(0),remainingTime:0,uploadTime:0}});c.watch(G,async e=>{if(e&&e.length>0){const t=Array.from(e);B(),await C(t,"file").catch(a=>{console.warn("Failed to add selected files:",a)})}});const N=c.computed(()=>o.value.filter(e=>e.status==="success").map(e=>({url:e.url,filename:e.filename,filesize:e.filesize,filetype:e.filetype})));return{isUploading:v,uploadFiles:o,dataFiles:N,progress:F,open:K,trigger:z,resetFiles:B,clearFiles:y,removeFiles:D,addFiles:C,addDataFiles:e=>{!l.multiple&&o.value.length>0&&y();const t=e.map(a=>L(a));o.value.push(...t)},cancelFiles:k}}exports.useUpload=H;
|
package/dist/esm/hooks/form.js
CHANGED
|
@@ -1,52 +1,72 @@
|
|
|
1
|
-
import { cloneDeep as
|
|
2
|
-
import { toRef as
|
|
3
|
-
import { useOne as E, useCreate as L, useUpdate as
|
|
4
|
-
function
|
|
5
|
-
const t =
|
|
1
|
+
import { cloneDeep as n } from "lodash-es";
|
|
2
|
+
import { toRef as s, ref as h, computed as b, watch as O } from "vue";
|
|
3
|
+
import { useOne as E, useCreate as L, useUpdate as S } from "./data.js";
|
|
4
|
+
function x(a) {
|
|
5
|
+
const t = s(a, "form", {}), r = s(a, "id", void 0), i = s(a, "meta", {}), l = s(a, "params", {}), o = h(n(t.value || {})), c = h(n(t.value || {})), u = b(() => a.action === "edit" || !!r.value), { data: d, isLoading: D, refetch: m } = E({
|
|
6
6
|
get path() {
|
|
7
7
|
return a.path || "";
|
|
8
8
|
},
|
|
9
9
|
get id() {
|
|
10
|
-
return
|
|
10
|
+
return r.value;
|
|
11
|
+
},
|
|
12
|
+
get meta() {
|
|
13
|
+
return {
|
|
14
|
+
params: {
|
|
15
|
+
...l.value
|
|
16
|
+
},
|
|
17
|
+
...i.value
|
|
18
|
+
};
|
|
11
19
|
},
|
|
12
20
|
options: {
|
|
13
21
|
enabled: !1
|
|
14
22
|
},
|
|
15
23
|
providerName: a.providerName
|
|
16
|
-
}),
|
|
24
|
+
}), v = () => {
|
|
17
25
|
let e = {};
|
|
18
|
-
|
|
26
|
+
u.value ? e = n(o.value) : e = n(c.value), Object.assign(t.value, e);
|
|
19
27
|
};
|
|
20
|
-
|
|
21
|
-
if (!
|
|
22
|
-
|
|
28
|
+
O([r, u], async () => {
|
|
29
|
+
if (!u.value) {
|
|
30
|
+
v();
|
|
23
31
|
return;
|
|
24
32
|
}
|
|
25
|
-
await
|
|
26
|
-
const e =
|
|
33
|
+
await m();
|
|
34
|
+
const e = n(d.value?.data || {});
|
|
27
35
|
Object.assign(t.value, e), Object.assign(o.value, e);
|
|
28
36
|
}, {
|
|
29
37
|
immediate: !0,
|
|
30
38
|
deep: !0
|
|
31
|
-
})
|
|
32
|
-
|
|
39
|
+
}), O([l, i], async () => {
|
|
40
|
+
if (!u.value)
|
|
41
|
+
return;
|
|
42
|
+
await m();
|
|
43
|
+
const e = n(d.value?.data || {});
|
|
44
|
+
Object.assign(t.value, e), Object.assign(o.value, e);
|
|
45
|
+
}, { deep: !0 });
|
|
46
|
+
const g = L({
|
|
33
47
|
path: a.path ?? "",
|
|
34
48
|
data: t.value,
|
|
49
|
+
get meta() {
|
|
50
|
+
return i.value || {};
|
|
51
|
+
},
|
|
35
52
|
onSuccess: (e) => {
|
|
36
|
-
|
|
53
|
+
v(), a.onSuccess?.(e);
|
|
37
54
|
},
|
|
38
55
|
onError: (e) => {
|
|
39
56
|
a.onError?.(e);
|
|
40
57
|
},
|
|
41
58
|
providerName: a.providerName
|
|
42
|
-
}),
|
|
59
|
+
}), f = S({
|
|
43
60
|
get path() {
|
|
44
61
|
return a.path ?? "";
|
|
45
62
|
},
|
|
46
63
|
get id() {
|
|
47
|
-
return
|
|
64
|
+
return r.value;
|
|
48
65
|
},
|
|
49
66
|
data: t.value,
|
|
67
|
+
get meta() {
|
|
68
|
+
return i.value || {};
|
|
69
|
+
},
|
|
50
70
|
onSuccess: (e) => {
|
|
51
71
|
a.onSuccess?.(e);
|
|
52
72
|
},
|
|
@@ -54,23 +74,23 @@ function w(a) {
|
|
|
54
74
|
a.onError?.(e);
|
|
55
75
|
},
|
|
56
76
|
providerName: a.providerName
|
|
57
|
-
}),
|
|
58
|
-
|
|
59
|
-
id:
|
|
77
|
+
}), N = (e) => {
|
|
78
|
+
u.value ? f.mutate({
|
|
79
|
+
id: r.value,
|
|
60
80
|
data: e || t.value
|
|
61
|
-
}) :
|
|
81
|
+
}) : g.mutate({
|
|
62
82
|
data: e || t.value
|
|
63
83
|
});
|
|
64
|
-
},
|
|
84
|
+
}, j = b(() => D.value || g.isLoading.value || f.isLoading.value);
|
|
65
85
|
return {
|
|
66
86
|
form: t,
|
|
67
|
-
initData:
|
|
68
|
-
isLoading:
|
|
69
|
-
isEdit:
|
|
70
|
-
onSubmit:
|
|
71
|
-
onReset:
|
|
87
|
+
initData: u.value ? o.value : c.value,
|
|
88
|
+
isLoading: j,
|
|
89
|
+
isEdit: u,
|
|
90
|
+
onSubmit: N,
|
|
91
|
+
onReset: v
|
|
72
92
|
};
|
|
73
93
|
}
|
|
74
94
|
export {
|
|
75
|
-
|
|
95
|
+
x as useForm
|
|
76
96
|
};
|
|
@@ -1,39 +1,41 @@
|
|
|
1
|
-
import { toRef as
|
|
2
|
-
import { useForm as
|
|
3
|
-
import { useValidateForm as
|
|
4
|
-
function
|
|
5
|
-
const
|
|
6
|
-
data:
|
|
7
|
-
rules:
|
|
8
|
-
}),
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
import { toRef as d } from "vue";
|
|
2
|
+
import { useForm as u } from "./form.js";
|
|
3
|
+
import { useValidateForm as c } from "./formValidate.js";
|
|
4
|
+
function v(t) {
|
|
5
|
+
const o = d(t, "form", {}), { validate: i, reset: n } = c({
|
|
6
|
+
data: o,
|
|
7
|
+
rules: t.rules
|
|
8
|
+
}), e = u({
|
|
9
|
+
id: t.id,
|
|
10
|
+
path: t.path,
|
|
11
|
+
form: o,
|
|
12
|
+
action: t.action,
|
|
13
|
+
onSuccess: t.onSuccess,
|
|
14
|
+
onError: t.onError,
|
|
15
|
+
providerName: t.providerName,
|
|
16
|
+
meta: t.meta,
|
|
17
|
+
params: t.params
|
|
18
|
+
}), r = (m) => {
|
|
19
|
+
i().then((s) => {
|
|
20
|
+
if (!s.valid) {
|
|
21
|
+
t.onError?.({
|
|
20
22
|
message: "表单验证失败"
|
|
21
23
|
});
|
|
22
24
|
return;
|
|
23
25
|
}
|
|
24
|
-
|
|
26
|
+
e.onSubmit(m);
|
|
25
27
|
});
|
|
26
28
|
}, a = () => {
|
|
27
|
-
|
|
29
|
+
e.onReset(), n();
|
|
28
30
|
};
|
|
29
31
|
return {
|
|
30
|
-
isLoading:
|
|
31
|
-
isEdit:
|
|
32
|
-
form:
|
|
33
|
-
onSubmit:
|
|
32
|
+
isLoading: e.isLoading,
|
|
33
|
+
isEdit: e.isEdit,
|
|
34
|
+
form: o,
|
|
35
|
+
onSubmit: r,
|
|
34
36
|
onReset: a
|
|
35
37
|
};
|
|
36
38
|
}
|
|
37
39
|
export {
|
|
38
|
-
|
|
40
|
+
v as useExtendForm
|
|
39
41
|
};
|
package/dist/esm/hooks/upload.js
CHANGED
|
@@ -63,7 +63,7 @@ function ne(l) {
|
|
|
63
63
|
}, T = (e) => {
|
|
64
64
|
if (l.maxFileSize && e.size > l.maxFileSize)
|
|
65
65
|
throw new Error(`File size cannot exceed ${p(l.maxFileSize)}`);
|
|
66
|
-
if (l.maxFileCount && o.value.length
|
|
66
|
+
if (l.maxFileCount && o.value.length > l.maxFileCount)
|
|
67
67
|
throw new Error(`File count cannot exceed ${l.maxFileCount}`);
|
|
68
68
|
}, k = async (e, t, n) => {
|
|
69
69
|
const a = await I(e, t);
|
|
@@ -9,6 +9,8 @@ export interface IUseFormProps {
|
|
|
9
9
|
onError?: (error: IDataProviderError) => void;
|
|
10
10
|
action?: 'create' | 'edit';
|
|
11
11
|
providerName?: string;
|
|
12
|
+
meta?: MaybeRef<Record<string, any>>;
|
|
13
|
+
params?: MaybeRef<Record<string, any>>;
|
|
12
14
|
}
|
|
13
15
|
export declare function useForm(props: IUseFormProps): {
|
|
14
16
|
form: import("vue").Ref<MaybeRef<Record<string, any>>, MaybeRef<Record<string, any>>>;
|