Dropzone: Polished image manager elements

- Added file placeholder for non-image uploads.
- Added use of upload limits.
- Removed upload timeout variable.
- Added pass-through and usage of filetypes.
- Extracted some view text to language files and made use of existing
  text.
This commit is contained in:
Dan Brown 2023-04-25 16:41:39 +01:00
parent b21a9007c5
commit 61d2ea6ac7
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
6 changed files with 43 additions and 36 deletions

View File

@ -7,6 +7,8 @@ return [
// Image Manager // Image Manager
'image_select' => 'Image Select', 'image_select' => 'Image Select',
'image_upload' => 'Upload Image', 'image_upload' => 'Upload Image',
'image_intro' => 'Here you can select and manage images that have been previously uploaded to the system.',
'image_intro_upload' => 'Upload a new image by dragging an image file into this window, or by using the "Upload Image" button above.',
'image_all' => 'All', 'image_all' => 'All',
'image_all_title' => 'View all images', 'image_all_title' => 'View all images',
'image_book_title' => 'View images uploaded to this book', 'image_book_title' => 'View images uploaded to this book',
@ -23,9 +25,9 @@ return [
'images_deleted' => 'Images Deleted', 'images_deleted' => 'Images Deleted',
'image_preview' => 'Image Preview', 'image_preview' => 'Image Preview',
'image_upload_success' => 'Image uploaded successfully', 'image_upload_success' => 'Image uploaded successfully',
'image_upload_failure' => 'Image failed to upload',
'image_update_success' => 'Image details successfully updated', 'image_update_success' => 'Image details successfully updated',
'image_delete_success' => 'Image successfully deleted', 'image_delete_success' => 'Image successfully deleted',
'image_upload_remove' => 'Remove',
// Code Editor // Code Editor
'code_editor' => 'Edit Code', 'code_editor' => 'Edit Code',

View File

@ -45,7 +45,6 @@ return [
'cannot_create_thumbs' => 'The server cannot create thumbnails. Please check you have the GD PHP extension installed.', 'cannot_create_thumbs' => 'The server cannot create thumbnails. Please check you have the GD PHP extension installed.',
'server_upload_limit' => 'The server does not allow uploads of this size. Please try a smaller file size.', 'server_upload_limit' => 'The server does not allow uploads of this size. Please try a smaller file size.',
'uploaded' => 'The server does not allow uploads of this size. Please try a smaller file size.', 'uploaded' => 'The server does not allow uploads of this size. Please try a smaller file size.',
'file_upload_timeout' => 'The file upload has timed out.',
// Drawing & Images // Drawing & Images
'image_upload_error' => 'An error occurred uploading the image', 'image_upload_error' => 'An error occurred uploading the image',

View File

@ -1,4 +1 @@
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M6 2c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6H6zm7 7V3.5L18.5 9H13z"/></svg>
<path d="M6 2c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6H6zm7 7V3.5L18.5 9H13z"/>
<path d="M0 0h24v24H0z" fill="none"/>
</svg>

Before

Width:  |  Height:  |  Size: 221 B

After

Width:  |  Height:  |  Size: 173 B

View File

@ -14,21 +14,28 @@ export class Dropzone extends Component {
this.url = this.$opts.url; this.url = this.$opts.url;
this.successMessage = this.$opts.successMessage; this.successMessage = this.$opts.successMessage;
this.removeMessage = this.$opts.removeMessage; this.errorMessage = this.$opts.errorMessage;
this.uploadLimit = Number(this.$opts.uploadLimit); // TODO - Use this.uploadLimitMb = Number(this.$opts.uploadLimit);
this.uploadLimitMessage = this.$opts.uploadLimitMessage; // TODO - Use this.uploadLimitMessage = this.$opts.uploadLimitMessage;
this.timeoutMessage = this.$opts.timeoutMessage; // TODO - Use
this.zoneText = this.$opts.zoneText; this.zoneText = this.$opts.zoneText;
// window.uploadTimeout // TODO - Use this.fileAcceptTypes = this.$opts.fileAccept;
this.setupListeners(); this.setupListeners();
} }
setupListeners() { setupListeners() {
onSelect(this.selectButtons, this.manualSelectHandler.bind(this)); onSelect(this.selectButtons, this.manualSelectHandler.bind(this));
this.setupDropTargetHandlers();
}
setupDropTargetHandlers() {
let depth = 0; let depth = 0;
const reset = () => {
this.hideOverlay();
depth = 0;
};
this.dropTarget.addEventListener('dragenter', event => { this.dropTarget.addEventListener('dragenter', event => {
event.preventDefault(); event.preventDefault();
depth += 1; depth += 1;
@ -42,22 +49,13 @@ export class Dropzone extends Component {
event.preventDefault(); event.preventDefault();
}); });
const reset = () => { this.dropTarget.addEventListener('dragend', reset);
this.hideOverlay(); this.dropTarget.addEventListener('dragleave', () => {
depth = 0;
};
this.dropTarget.addEventListener('dragend', event => {
reset();
});
this.dropTarget.addEventListener('dragleave', event => {
depth -= 1; depth -= 1;
if (depth === 0) { if (depth === 0) {
reset(); reset();
} }
}); });
this.dropTarget.addEventListener('drop', event => { this.dropTarget.addEventListener('drop', event => {
event.preventDefault(); event.preventDefault();
reset(); reset();
@ -70,10 +68,10 @@ export class Dropzone extends Component {
} }
manualSelectHandler() { manualSelectHandler() {
const input = elem('input', {type: 'file', style: 'left: -400px; visibility: hidden; position: fixed;'}); const input = elem('input', {type: 'file', style: 'left: -400px; visibility: hidden; position: fixed;', accept: this.fileAcceptTypes});
this.container.append(input); this.container.append(input);
input.click(); input.click();
input.addEventListener('change', event => { input.addEventListener('change', () => {
for (const file of input.files) { for (const file of input.files) {
this.createUploadFromFile(file); this.createUploadFromFile(file);
} }
@ -101,7 +99,9 @@ export class Dropzone extends Component {
* @return {Upload} * @return {Upload}
*/ */
createUploadFromFile(file) { createUploadFromFile(file) {
const {dom, status, progress, dismiss} = this.createDomForFile(file); const {
dom, status, progress, dismiss,
} = this.createDomForFile(file);
this.statusArea.append(dom); this.statusArea.append(dom);
const component = this; const component = this;
@ -116,6 +116,7 @@ export class Dropzone extends Component {
status.setAttribute('data-status', 'error'); status.setAttribute('data-status', 'error');
status.textContent = message; status.textContent = message;
removeLoading(dom); removeLoading(dom);
this.updateProgress(100);
}, },
markSuccess(message) { markSuccess(message) {
status.setAttribute('data-status', 'success'); status.setAttribute('data-status', 'success');
@ -128,6 +129,12 @@ export class Dropzone extends Component {
}, },
}; };
// Enforce early upload filesize limit
if (file.size > (this.uploadLimitMb * 1000000)) {
upload.markError(this.uploadLimitMessage);
return upload;
}
this.startXhrForUpload(upload); this.startXhrForUpload(upload);
return upload; return upload;
@ -139,14 +146,15 @@ export class Dropzone extends Component {
startXhrForUpload(upload) { startXhrForUpload(upload) {
const formData = new FormData(); const formData = new FormData();
formData.append('file', upload.file, upload.file.name); formData.append('file', upload.file, upload.file.name);
const component = this;
const req = window.$http.createXMLHttpRequest('POST', this.url, { const req = window.$http.createXMLHttpRequest('POST', this.url, {
error() { error() {
upload.markError('Upload failed'); // TODO - Update text upload.markError(component.errorMessage);
}, },
readystatechange() { readystatechange() {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
upload.markSuccess('Finished upload!'); upload.markSuccess(component.successMessage);
} else if (this.readyState === XMLHttpRequest.DONE && this.status >= 400) { } else if (this.readyState === XMLHttpRequest.DONE && this.status >= 400) {
const content = this.responseText; const content = this.responseText;
const data = content.startsWith('{') ? JSON.parse(content) : {message: content}; const data = content.startsWith('{') ? JSON.parse(content) : {message: content};
@ -170,7 +178,7 @@ export class Dropzone extends Component {
* @return {{image: Element, dom: Element, progress: Element, status: Element, dismiss: function}} * @return {{image: Element, dom: Element, progress: Element, status: Element, dismiss: function}}
*/ */
createDomForFile(file) { createDomForFile(file) {
const image = elem('img', {src: ''}); const image = elem('img', {src: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M9.224 7.373a.924.924 0 0 0-.92.925l-.006 7.404c0 .509.412.925.921.925h5.557a.928.928 0 0 0 .926-.925v-5.553l-2.777-2.776Zm3.239 3.239V8.067l2.545 2.545z' style='fill:%23000;fill-opacity:.75'/%3E%3C/svg%3E"});
const status = elem('div', {class: 'dropzone-file-item-status'}, []); const status = elem('div', {class: 'dropzone-file-item-status'}, []);
const progress = elem('div', {class: 'dropzone-file-item-progress'}); const progress = elem('div', {class: 'dropzone-file-item-progress'});
const imageWrap = elem('div', {class: 'dropzone-file-item-image-wrap'}, [image]); const imageWrap = elem('div', {class: 'dropzone-file-item-image-wrap'}, [image]);
@ -191,7 +199,7 @@ export class Dropzone extends Component {
const dismiss = () => { const dismiss = () => {
dom.classList.add('dismiss'); dom.classList.add('dismiss');
dom.addEventListener('animationend', event => { dom.addEventListener('animationend', () => {
dom.remove(); dom.remove();
}); });
}; };

View File

@ -145,6 +145,7 @@
box-shadow: none; box-shadow: none;
color: #FFF; color: #FFF;
padding: $-xs $-m; padding: $-xs $-m;
cursor: pointer;
} }
.popup-header button:not(.popup-header-close) { .popup-header button:not(.popup-header-close) {
@ -292,6 +293,7 @@ body.flexbox-support #entity-selector-wrap .popup-body .form-group {
.dropzone-file-item-image-wrap { .dropzone-file-item-image-wrap {
width: 80px; width: 80px;
position: relative; position: relative;
background-color: var(--color-primary-light);
img { img {
object-fit: cover; object-fit: cover;
width: 100%; width: 100%;
@ -322,7 +324,7 @@ body.flexbox-support #entity-selector-wrap .popup-body .form-group {
} }
.dropzone-file-item-status[data-status] { .dropzone-file-item-status[data-status] {
display: flex; display: flex;
font-size: .8rem; font-size: .6rem;
font-weight: 500; font-weight: 500;
line-height: 1.2; line-height: 1.2;
} }
@ -407,7 +409,7 @@ body.flexbox-support #entity-selector-wrap .popup-body .form-group {
min-height: auto; min-height: auto;
padding: $-m; padding: $-m;
} }
img { .image-manager-viewer img {
max-width: 100%; max-width: 100%;
max-height: 180px; max-height: 180px;
display: block; display: block;

View File

@ -1,11 +1,11 @@
<div components="image-manager dropzone" <div components="image-manager dropzone"
option:dropzone:url="{{ url('/images/gallery?' . http_build_query(['uploaded_to' => $uploaded_to ?? 0])) }}" option:dropzone:url="{{ url('/images/gallery?' . http_build_query(['uploaded_to' => $uploaded_to ?? 0])) }}"
option:dropzone:success-message="{{ trans('components.image_upload_success') }}" option:dropzone:success-message="{{ trans('components.image_upload_success') }}"
option:dropzone:remove-message="{{ trans('components.image_upload_remove') }}" option:dropzone:error-message="{{ trans('components.image_upload_failure') }}"
option:dropzone:upload-limit="{{ config('app.upload_limit') }}" option:dropzone:upload-limit="{{ config('app.upload_limit') }}"
option:dropzone:upload-limit-message="{{ trans('errors.server_upload_limit') }}" option:dropzone:upload-limit-message="{{ trans('errors.server_upload_limit') }}"
option:dropzone:timeout-message="{{ trans('errors.file_upload_timeout') }}"
option:dropzone:zone-text="{{ trans('components.image_dropzone_drop') }}" option:dropzone:zone-text="{{ trans('components.image_dropzone_drop') }}"
option:dropzone:file-accept="image/*"
option:image-manager:uploaded-to="{{ $uploaded_to ?? 0 }}" option:image-manager:uploaded-to="{{ $uploaded_to ?? 0 }}"
class="image-manager"> class="image-manager">
@ -66,9 +66,8 @@
</div> </div>
<div refs="image-manager@form-container-placeholder" class="p-m text-small text-muted"> <div refs="image-manager@form-container-placeholder" class="p-m text-small text-muted">
<p>Here you can manage or select images that have been previously uploaded to the system.</p> <p>{{ trans('components.image_intro') }}</p>
<p>Upload a new image by dragging an image file into this window, <p>{{ trans('components.image_intro_upload') }}</p>
or by using the "Upload Image" button above.</p>
</div> </div>
<div refs="image-manager@formContainer" class="inner flex"> <div refs="image-manager@formContainer" class="inner flex">