@empoleon/solid-dropzone
Version:
Simple HTML5 drag-drop zone with SolidJS
2 lines (1 loc) • 2.57 kB
JavaScript
const e={FileInvalidType:"file-invalid-type",FileTooLarge:"file-too-large",FileTooSmall:"file-too-small",TooManyFiles:"too-many-files"},n={code:e.TooManyFiles,message:"Too many files"};function t(n,t){const i="application/x-moz-file"===n.type||o(n,t);return[i,i?null:{code:e.FileInvalidType,message:`File type must be ${t}`}]}function i(n,t,i){if(r(n.size))if(r(t)&&r(i)){if(n.size>i)return[!1,{code:e.FileTooLarge,message:`File is larger than ${i} bytes`}];if(n.size<t)return[!1,{code:e.FileTooSmall,message:`File is smaller than ${t} bytes`}]}else{if(r(t)&&n.size<t)return[!1,{code:e.FileTooSmall,message:`File is smaller than ${t} bytes`}];if(r(i)&&n.size>i)return[!1,{code:e.FileTooLarge,message:`File is larger than ${i} bytes`}]}return[!0,null]}function r(e){return null!=e}function o(e,n){if(e&&n){const t=Array.isArray(n)?n:n.split(","),i=e.name||"",r=(e.type||"").toLowerCase(),o=r.replace(/\/.*$/,"");return t.some(e=>{const n=e.trim().toLowerCase();return"."===n.charAt(0)?i.toLowerCase().endsWith(n):n.endsWith("/*")?o===n.replace(/\/.*$/,""):r===n})}return!0}function a({files:e,accept:n,minSize:r,maxSize:o,multiple:a,maxFiles:l,validator:s}){return!(!a&&e.length>1||a&&l>=1&&e.length>l)&&e.every(e=>{const[a]=t(e,n),[l]=i(e,r,o),u=s?s(e):null;return a&&l&&!u})}function l(e){if(void 0!==e)return Object.entries(e).reduce((e,[n,t])=>[...e,n,...t],[]).join(",")}function s(e){if(void 0!==e)return Object.entries(e).map(([e,n])=>({description:"Files",accept:{[e]:n}}))}function u(...e){return(n,...t)=>e.some(e=>(e&&e(n,...t),n.defaultPrevented||n.propagationStopped))}function c(e){return f(e)&&e.dataTransfer?Array.prototype.some.call(e.dataTransfer.types,e=>"Files"===e||"application/x-moz-file"===e):!(!e.target||!e.target.files)}function f(e){return"dataTransfer"in e}function p(e){return"function"==typeof e.isPropagationStopped?e.isPropagationStopped():void 0!==e.cancelBubble&&e.cancelBubble}const d=e=>{e.preventDefault()};function m(e=window.navigator.userAgent){return e.indexOf("MSIE ")>0||e.indexOf("Trident/")>0||e.indexOf("Edge/")>0}function g(){return"showOpenFilePicker"in window}function y(e){return"AbortError"===e.name}function F(e){return"SecurityError"===e.name}export{e as ErrorCode,n as TOO_MANY_FILES_REJECTION,l as acceptPropAsAcceptAttr,o as accepts,a as allFilesAccepted,g as canUseFileSystemAccessAPI,u as composeEventHandlers,t as fileAccepted,i as fileMatchSize,y as isAbort,f as isDragEvent,c as isEvtWithFiles,m as isIeOrEdge,p as isPropagationStopped,F as isSecurityError,d as onDocumentDragOver,s as pickerOptionsFromAccept};