forked from OpenCatalogi/opencatalogi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
324 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { useDropZone, useFileDialog } from '@vueuse/core' | ||
import { ref, computed } from 'vue' | ||
import { publicationStore } from './../store/store.js' | ||
|
||
/** | ||
* File selection composable | ||
* @param options | ||
* | ||
* Special thanks to Github user adamreisnz for creating most of this file | ||
* https://github.com/adamreisnz | ||
* https://github.com/vueuse/vueuse/issues/4085 | ||
* | ||
*/ | ||
export function useFileSelection(options) { | ||
|
||
// Extract options | ||
const { | ||
dropzone, | ||
allowMultiple, | ||
allowedFileTypes, | ||
onFileDrop, | ||
} = options | ||
|
||
// Data types computed ref | ||
const dataTypes = computed(() => { | ||
if (allowedFileTypes?.value) { | ||
if (!Array.isArray(allowedFileTypes.value)) { | ||
return [allowedFileTypes.value] | ||
Check failure on line 28 in src/composables/UseFileSelection.js
|
||
} | ||
return allowedFileTypes.value | ||
} | ||
return null | ||
}) | ||
|
||
Check failure on line 34 in src/composables/UseFileSelection.js
|
||
// Accept string computed ref | ||
const accept = computed(() => { | ||
if (Array.isArray(dataTypes.value)) { | ||
return dataTypes.value.join(',') | ||
} | ||
return '*' | ||
}) | ||
Check failure on line 41 in src/composables/UseFileSelection.js
|
||
|
||
// Handling of files drop | ||
const onDrop = files => { | ||
Check failure on line 44 in src/composables/UseFileSelection.js
|
||
if (!files || files.length === 0) { | ||
return | ||
} | ||
if (files instanceof FileList) { | ||
files = Array.from(files) | ||
} | ||
if (files.length > 1 && !allowMultiple.value) { | ||
files = [files[0]] | ||
} | ||
filesList.value = files | ||
onFileDrop && onFileDrop() | ||
} | ||
|
||
const reset = () => { | ||
filesList.value = null | ||
} | ||
|
||
// const onLeave = () => { | ||
// let timer | ||
// document.addEventListener('mousemove', () => { | ||
// clearTimeout(timer) | ||
// timer = setTimeout(isOverDropZone.value = false, 300) | ||
// }) | ||
// } | ||
|
||
const setFiles = (files) => { | ||
filesList.value = files | ||
publicationStore.setAttachmentFile(null) | ||
} | ||
|
||
// Setup dropzone and file dialog composables | ||
const { isOverDropZone } = useDropZone(dropzone, { dataTypes, onDrop }) | ||
const { onChange, open } = useFileDialog({ | ||
accept: accept.value, | ||
multiple: allowMultiple?.value, | ||
}) | ||
|
||
const filesList = ref(null) | ||
|
||
// Use onChange handler | ||
onChange(fileList => onDrop(fileList)) | ||
|
||
// Expose interface | ||
return { | ||
isOverDropZone, | ||
openFileUpload: open, | ||
files: filesList, | ||
reset, | ||
setFiles, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.