-
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.
add filepond, refactor savePictures to support progress callback.
- Loading branch information
1 parent
b3d70ba
commit d2ed101
Showing
4 changed files
with
217 additions
and
69 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
183 changes: 125 additions & 58 deletions
183
client/views/components/robot-display/UploadTeamPicture.svelte
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 |
---|---|---|
@@ -1,62 +1,129 @@ | ||
<script lang="ts"> | ||
import UploadPicture from '../main/UploadPicture.svelte'; | ||
import { FIRSTTeam } from '../../../models/FIRST/team'; | ||
import { onMount } from 'svelte'; | ||
import { Modal } from '../../../utilities/modals'; | ||
import { alert } from '../../../utilities/notifications'; | ||
export let team: FIRSTTeam | undefined = undefined; | ||
let fileList: FileList; | ||
let me: HTMLDivElement; | ||
const open = () => { | ||
if (!team) return alert('No team selected!'); | ||
const m = new Modal(); | ||
const upload = new UploadPicture({ | ||
target: m.target.querySelector('.modal-body') as HTMLElement | ||
}); | ||
upload.$on('change', (e: CustomEvent) => { | ||
fileList = e.detail.fileList; | ||
}); | ||
m.show(); | ||
const close = document.createElement('button'); | ||
close.classList.add('btn', 'btn-secondary'); | ||
close.textContent = 'Close'; | ||
close.addEventListener('click', () => m.hide()); | ||
const submit = document.createElement('button'); | ||
submit.classList.add('btn', 'btn-success'); | ||
submit.textContent = 'Submit'; | ||
submit.addEventListener('click', () => { | ||
if (fileList.length === 0) return; | ||
if (!team) return console.error('No team'); | ||
team.savePictures(fileList); | ||
m.hide(); | ||
}); | ||
const group = document.createElement('div'); | ||
group.classList.add('btn-group', 'd-flex', 'justify-content-between'); | ||
group.appendChild(close); | ||
group.appendChild(submit); | ||
m.setFooter(group); | ||
m.on('hide', () => { | ||
m.destroy(); | ||
upload.$destroy(); | ||
}); | ||
}; | ||
import { attemptAsync } from '../../../../shared/check'; | ||
import FilePond, { registerPlugin } from 'svelte-filepond'; | ||
import FilePondPluginImageExifOrientation from 'filepond-plugin-image-exif-orientation'; | ||
import FilePondPluginImagePreview from 'filepond-plugin-image-preview'; | ||
import FilePondPluginImageCrop from 'filepond-plugin-image-crop'; | ||
import FilePondPluginImageResize from 'filepond-plugin-image-resize'; | ||
import FilePondPluginImageTransform from 'filepond-plugin-image-transform'; | ||
import { FIRSTTeam } from '../../../models/FIRST/team'; | ||
import { alert } from '../../../utilities/notifications'; | ||
import { getContext } from 'svelte'; | ||
import { ActualFileObject } from 'filepond'; | ||
import { onMount } from 'svelte'; | ||
registerPlugin( | ||
FilePondPluginImageExifOrientation, | ||
FilePondPluginImagePreview, | ||
FilePondPluginImageTransform, | ||
FilePondPluginImageCrop, | ||
FilePondPluginImageResize | ||
); | ||
export let team: FIRSTTeam | undefined = undefined; | ||
let pond: FilePond; | ||
let name = 'team-pictures'; | ||
const handleInit = async () => { | ||
console.log('FilePond has initialised'); | ||
}; | ||
const uploadFile = async (file: ActualFileObject) => { | ||
if (!team) { | ||
alert('No team selected!'); | ||
return; | ||
} | ||
const fileList = new DataTransfer(); | ||
const realFile = new File([file], file.name, { | ||
type: file.type, | ||
lastModified: file.lastModified | ||
}); | ||
fileList.items.add(realFile); | ||
const result = await team.savePictures(fileList.files, progress => {}); | ||
if (result.isErr()) { | ||
alert('Failed to upload picture. Please try again.'); | ||
console.error(result.error); | ||
return; | ||
} | ||
if (result.isOk()) { | ||
console.log(`Picture ${file.name} uploaded successfully`); | ||
return true; | ||
} | ||
}; | ||
const handleAddFile = async (err: any, file: ActualFileObject) => { | ||
console.log('A file has been added', file); | ||
await uploadFile(file); | ||
}; | ||
onMount(() => { | ||
const creditsElements = document.querySelectorAll('.filepond--credits'); | ||
creditsElements.forEach(element => element.remove()); | ||
// the credits don't want to style correctly, will add if I can figure out the styles | ||
}); | ||
// TODO: https://pqina.nl/pintura/docs/v8/installation/filepond/ | ||
// filepond image editor, should be limitable to just cropping and rotation | ||
</script> | ||
|
||
<div bind:this="{me}"> | ||
<button | ||
class="btn btn-primary" | ||
type="button" | ||
on:click="{open}" | ||
>Upload Pictures</button> | ||
<div class="bg-dark"> | ||
<FilePond | ||
bind:this="{pond}" | ||
{name} | ||
allowMultiple="{true}" | ||
labelIdle="Drag & Drop your team pictures or <span class='filepond--label-action'>Browse</span>" | ||
oninit="{handleInit}" | ||
allowImageTransform="{true}" | ||
imageTransformOutputQuality="{25}" | ||
imageTransformOutputMimeType="image/jpeg" | ||
instantUpload="{true}" | ||
server="{{ | ||
process: async ( | ||
fieldName, | ||
file, | ||
metadata, | ||
load, | ||
error, | ||
progress, | ||
abort, | ||
transfer, | ||
options | ||
) => { | ||
try { | ||
const result = await uploadFile(file); | ||
if (result) { | ||
load(file); | ||
} else { | ||
error('Failed to upload picture. Please try again.'); | ||
} | ||
} catch (err) { | ||
console.error('Error during upload:', err); | ||
error('Failed to upload picture. Please try again.'); | ||
} | ||
} | ||
}}" | ||
/> | ||
</div> | ||
|
||
<style> | ||
@import 'filepond/dist/filepond.css'; | ||
@import 'filepond-plugin-image-preview/dist/filepond-plugin-image-preview.css'; | ||
:global(.filepond--panel-root) { | ||
background-color: var(--bs-dark); | ||
border: 1px solid var(--bs-gray-dark); | ||
border-radius: var(--bs-border-radius); | ||
padding: var(--bs-spacing); | ||
box-shadow: var(--bs-box-shadow); | ||
} | ||
:global(.filepond--drop-label) { | ||
color: var(--bs-light); | ||
} | ||
</style> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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