-
-
Notifications
You must be signed in to change notification settings - Fork 356
[Dropzone] Add multiple file preview #2747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
phasdev
wants to merge
1
commit into
symfony:2.x
Choose a base branch
from
phasdev:dropzone-multiple
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+909
−163
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 +1,9 @@ | ||
# CHANGELOG | ||
|
||
## 2.24 | ||
|
||
- Support multiple files preview | ||
|
||
## 2.20 | ||
|
||
- Enable file replacement via "drag-and-drop" | ||
|
This file contains hidden or 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,19 +1,48 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
export default class extends Controller { | ||
readonly inputTarget: HTMLInputElement; | ||
readonly placeholderTarget: HTMLDivElement; | ||
readonly previewTarget: HTMLDivElement; | ||
readonly previewClearButtonTarget: HTMLButtonElement; | ||
readonly previewFilenameTarget: HTMLDivElement; | ||
readonly previewImageTarget: HTMLDivElement; | ||
readonly placeholderTarget: HTMLElement; | ||
readonly previewTargets: HTMLElement[]; | ||
readonly previewContainerTarget: HTMLElement; | ||
readonly previewTemplateTarget: HTMLTemplateElement; | ||
readonly optionsValue: any; | ||
static values: { | ||
options: { | ||
type: ObjectConstructor; | ||
default: { | ||
preview: { | ||
style: string; | ||
can_open_file_picker: boolean; | ||
can_toggle_placeholder: boolean; | ||
}; | ||
}; | ||
}; | ||
}; | ||
static targets: string[]; | ||
files: Map<string, File>; | ||
initialize(): void; | ||
connect(): void; | ||
disconnect(): void; | ||
clear(): void; | ||
onInputChange(event: any): void; | ||
_populateImagePreview(file: Blob): void; | ||
onDragEnter(): void; | ||
onDragLeave(event: any): void; | ||
onDragOver(event: any): void; | ||
onDrop(event: any): void; | ||
onPreviewContainerClick(event: any): void; | ||
onPreviewButtonClick(event: any): void; | ||
private dispatchEvent; | ||
private addFiles; | ||
private buildPreview; | ||
private refreshPreview; | ||
private isImage; | ||
private get isMultiple(); | ||
private updateFileInput; | ||
private formatBytes; | ||
private get firstFile(); | ||
private get isLegacy(); | ||
private refreshLegacyPreview; | ||
private showLegacyPreview; | ||
private hideLegacyPreview; | ||
private showLegacyFileInput; | ||
private hideLegacyFileInput; | ||
} |
This file contains hidden or 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,79 +1,265 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
class default_1 extends Controller { | ||
constructor() { | ||
super(...arguments); | ||
this.files = new Map(); | ||
} | ||
initialize() { | ||
this.clear = this.clear.bind(this); | ||
this.onInputChange = this.onInputChange.bind(this); | ||
this.onDragEnter = this.onDragEnter.bind(this); | ||
this.onDragLeave = this.onDragLeave.bind(this); | ||
this.onDragOver = this.onDragOver.bind(this); | ||
this.onDrop = this.onDrop.bind(this); | ||
this.onPreviewButtonClick = this.onPreviewButtonClick.bind(this); | ||
this.onPreviewContainerClick = this.onPreviewContainerClick.bind(this); | ||
} | ||
connect() { | ||
this.clear(); | ||
this.previewClearButtonTarget.addEventListener('click', this.clear); | ||
this.inputTarget.addEventListener('change', this.onInputChange); | ||
this.element.addEventListener('dragenter', this.onDragEnter); | ||
this.element.addEventListener('dragleave', this.onDragLeave); | ||
this.element.addEventListener('dragover', this.onDragOver); | ||
this.element.addEventListener('drop', this.onDrop); | ||
if (!this.isLegacy && this.optionsValue.preview.can_open_file_picker) { | ||
this.previewContainerTarget.addEventListener('click', this.onPreviewContainerClick); | ||
} | ||
this.dispatchEvent('connect'); | ||
} | ||
disconnect() { | ||
this.previewClearButtonTarget.removeEventListener('click', this.clear); | ||
this.clear(); | ||
this.inputTarget.removeEventListener('change', this.onInputChange); | ||
this.element.removeEventListener('dragenter', this.onDragEnter); | ||
this.element.removeEventListener('dragleave', this.onDragLeave); | ||
this.element.removeEventListener('dragover', this.onDragOver); | ||
this.element.removeEventListener('drop', this.onDrop); | ||
if (!this.isLegacy && this.optionsValue.preview.can_open_file_picker) { | ||
this.previewContainerTarget.removeEventListener('click', this.onPreviewContainerClick); | ||
} | ||
} | ||
clear() { | ||
this.inputTarget.value = ''; | ||
this.inputTarget.style.display = 'block'; | ||
this.placeholderTarget.style.display = 'block'; | ||
this.previewTarget.style.display = 'none'; | ||
this.previewImageTarget.style.display = 'none'; | ||
this.previewImageTarget.style.backgroundImage = 'none'; | ||
this.previewFilenameTarget.textContent = ''; | ||
this.files.clear(); | ||
this.updateFileInput(); | ||
this.refreshPreview(); | ||
this.element.classList.remove('dropzone-active'); | ||
if (this.isLegacy) { | ||
this.showLegacyFileInput(); | ||
} | ||
this.dispatchEvent('clear'); | ||
} | ||
onInputChange(event) { | ||
const file = event.target.files[0]; | ||
if (typeof file === 'undefined') { | ||
const files = Array.from(event.target.files).filter((file) => typeof file !== 'undefined'); | ||
if (files.length === 0) { | ||
return; | ||
} | ||
this.inputTarget.style.display = 'none'; | ||
this.placeholderTarget.style.display = 'none'; | ||
this.previewFilenameTarget.textContent = file.name; | ||
this.previewTarget.style.display = 'flex'; | ||
this.previewImageTarget.style.display = 'none'; | ||
if (file.type && file.type.indexOf('image') !== -1) { | ||
this._populateImagePreview(file); | ||
this.files.clear(); | ||
this.addFiles(files); | ||
this.refreshPreview(); | ||
this.dispatchEvent('change', this.isLegacy ? this.firstFile : Array.from(this.files.values())); | ||
} | ||
onDragLeave(event) { | ||
event.preventDefault(); | ||
if (!this.element.contains(event.relatedTarget)) { | ||
this.element.classList.remove('dropzone-active'); | ||
if (this.isLegacy) { | ||
this.hideLegacyFileInput(); | ||
this.showLegacyPreview(); | ||
} | ||
} | ||
} | ||
onDragOver(event) { | ||
event.preventDefault(); | ||
this.element.classList.add('dropzone-active'); | ||
if (this.isLegacy) { | ||
this.hideLegacyPreview(); | ||
this.showLegacyFileInput(); | ||
} | ||
this.dispatchEvent('change', file); | ||
} | ||
_populateImagePreview(file) { | ||
if (typeof FileReader === 'undefined') { | ||
onDrop(event) { | ||
event.preventDefault(); | ||
const files = Array.from(event.dataTransfer.files).filter((file) => typeof file !== 'undefined'); | ||
if (files.length === 0) { | ||
return; | ||
} | ||
const reader = new FileReader(); | ||
reader.addEventListener('load', (event) => { | ||
this.previewImageTarget.style.display = 'block'; | ||
this.previewImageTarget.style.backgroundImage = `url("${event.target.result}")`; | ||
}); | ||
reader.readAsDataURL(file); | ||
if (!this.isMultiple) { | ||
this.files.clear(); | ||
} | ||
this.addFiles(files); | ||
this.updateFileInput(); | ||
this.refreshPreview(); | ||
this.element.classList.remove('dropzone-active'); | ||
this.dispatchEvent('change', Array.from(this.files.values())); | ||
} | ||
onDragEnter() { | ||
this.inputTarget.style.display = 'block'; | ||
this.placeholderTarget.style.display = 'block'; | ||
this.previewTarget.style.display = 'none'; | ||
onPreviewContainerClick(event) { | ||
event.stopPropagation(); | ||
this.inputTarget.click(); | ||
} | ||
onDragLeave(event) { | ||
event.preventDefault(); | ||
if (!this.element.contains(event.relatedTarget)) { | ||
this.inputTarget.style.display = 'none'; | ||
this.placeholderTarget.style.display = 'none'; | ||
this.previewTarget.style.display = 'block'; | ||
onPreviewButtonClick(event) { | ||
event.stopPropagation(); | ||
if (this.isLegacy) { | ||
return this.clear(); | ||
} | ||
const button = event.currentTarget; | ||
button.removeEventListener('click', this.onPreviewButtonClick); | ||
const preview = button.closest('.dropzone-preview'); | ||
preview.remove(); | ||
if (!button.dataset.filename) { | ||
return; | ||
} | ||
this.files.delete(button.dataset.filename); | ||
this.updateFileInput(); | ||
this.refreshPreview(); | ||
} | ||
dispatchEvent(name, payload = {}) { | ||
this.dispatch(name, { detail: payload, prefix: 'dropzone' }); | ||
} | ||
addFiles(files) { | ||
for (const file of files) { | ||
this.files.set(file.name, file); | ||
} | ||
} | ||
buildPreview(file, el) { | ||
if (!el) { | ||
el = this.previewTemplateTarget.content.firstElementChild?.cloneNode(true); | ||
} | ||
const button = el.querySelector('.dropzone-preview-button'); | ||
if (button) { | ||
button.dataset.filename = file.name; | ||
button.addEventListener('click', this.onPreviewButtonClick); | ||
} | ||
const filename = el.querySelector('.dropzone-preview-filename'); | ||
if (filename) { | ||
filename.textContent = file.name; | ||
} | ||
const size = el.querySelector('.dropzone-preview-file-size'); | ||
if (size) { | ||
size.textContent = this.formatBytes(file.size); | ||
} | ||
const image = el.querySelector('.dropzone-preview-image'); | ||
if (image && this.isImage(file) && typeof FileReader !== 'undefined') { | ||
const reader = new FileReader(); | ||
image.classList.add('dropzone-preview-image-hidden'); | ||
reader.addEventListener('load', (event) => { | ||
image.querySelector('.dropzone-preview-image-placeholder')?.remove(); | ||
image.style.backgroundImage = `url('${event.target.result}')`; | ||
image.classList.remove('dropzone-preview-image-hidden'); | ||
}); | ||
reader.readAsDataURL(file); | ||
} | ||
return el; | ||
} | ||
refreshPreview() { | ||
if (this.isLegacy) { | ||
return this.refreshLegacyPreview(); | ||
} | ||
this.element.classList.add('dropzone-preview-container-hidden'); | ||
for (const preview of this.previewTargets) { | ||
preview.querySelector('.dropzone-preview-button')?.removeEventListener('click', this.onPreviewButtonClick); | ||
preview.remove(); | ||
} | ||
for (const file of this.files.values()) { | ||
const preview = this.buildPreview(file); | ||
this.previewContainerTarget.appendChild(preview); | ||
} | ||
if (this.previewTargets.length > 0) { | ||
this.element.classList.remove('dropzone-preview-container-hidden'); | ||
} | ||
const canToggle = this.optionsValue.preview.can_toggle_placeholder; | ||
if (canToggle) { | ||
const hide = this.previewTargets.length > 0 && | ||
(canToggle === true || (canToggle === 'auto' && this.previewTargets.length < 2)); | ||
this.element.classList.toggle('dropzone-placeholder-hidden', hide); | ||
} | ||
} | ||
isImage(file) { | ||
return typeof file.type !== 'undefined' && file.type.indexOf('image') !== -1; | ||
} | ||
get isMultiple() { | ||
return this.inputTarget.multiple; | ||
} | ||
updateFileInput() { | ||
const dataTransfer = new DataTransfer(); | ||
for (const file of this.files.values()) { | ||
dataTransfer.items.add(file); | ||
} | ||
this.inputTarget.files = dataTransfer.files; | ||
} | ||
formatBytes(bytes, decimals = 2) { | ||
if (bytes === 0) | ||
return '0 Bytes'; | ||
const k = 1024; | ||
const dm = decimals || 2; | ||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
return `${Number.parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`; | ||
} | ||
get firstFile() { | ||
return this.files.values().next().value; | ||
} | ||
get isLegacy() { | ||
return this.optionsValue.preview.style === 'legacy'; | ||
} | ||
refreshLegacyPreview() { | ||
const preview = this.previewTargets[0]; | ||
const image = preview.querySelector('.dropzone-preview-image'); | ||
const filename = preview.querySelector('.dropzone-preview-filename'); | ||
const file = this.firstFile; | ||
if (!file) { | ||
this.hideLegacyPreview(); | ||
if (filename) { | ||
filename.textContent = ''; | ||
} | ||
if (image) { | ||
image.style.display = 'none'; | ||
image.style.backgroundImage = 'none'; | ||
} | ||
return; | ||
} | ||
this.buildPreview(file, preview); | ||
const fileCount = this.files.size; | ||
if (filename && fileCount > 1) { | ||
filename.textContent += ` +${fileCount - 1}`; | ||
filename.title = Array.from(this.files.values()) | ||
.map((file) => file.name) | ||
.join('\n'); | ||
} | ||
if (image) { | ||
if (this.isImage(file)) { | ||
image.style.display = 'block'; | ||
} | ||
else { | ||
image.style.display = 'none'; | ||
image.style.backgroundImage = 'none'; | ||
} | ||
} | ||
this.showLegacyPreview(); | ||
this.hideLegacyFileInput(); | ||
} | ||
showLegacyPreview() { | ||
this.previewTargets[0].style.display = 'flex'; | ||
} | ||
hideLegacyPreview() { | ||
this.previewTargets[0].style.display = 'none'; | ||
} | ||
showLegacyFileInput() { | ||
this.inputTarget.style.display = 'block'; | ||
this.placeholderTarget.style.display = 'block'; | ||
} | ||
hideLegacyFileInput() { | ||
this.inputTarget.style.display = 'none'; | ||
this.placeholderTarget.style.display = 'none'; | ||
} | ||
} | ||
default_1.targets = ['input', 'placeholder', 'preview', 'previewClearButton', 'previewFilename', 'previewImage']; | ||
default_1.values = { | ||
options: { | ||
type: Object, | ||
default: { | ||
preview: { | ||
style: 'legacy', | ||
can_open_file_picker: true, | ||
can_toggle_placeholder: true, | ||
}, | ||
}, | ||
}, | ||
}; | ||
default_1.targets = ['input', 'placeholder', 'preview', 'previewContainer', 'previewTemplate']; | ||
|
||
export { default_1 as default }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible breaking change? Some controller targets have been removed and I've changed types on
placeholderTarget
andpreviewTarget
so they're more generic. For example, the new placeholder is a<label>
not a<div>
, so I changedplaceholderTarget
type fromHTMLDivElement
to the more genericHTMLElement
.Since the documentation suggests using event listeners to extend behaviour I assumed the controller class wouldn't be extended directly, so perhaps these aren't breaking changes.