|
59 | 59 | Action |
60 | 60 | </button> |
61 | 61 | <div class="dropdown-menu"> |
62 | | - <a :class="{'dropdown-item': true, disabled: file.active || file.success}" href="#" @click.prevent="file.active || file.success ? false : onEditFileShow(file)">Edit</a> |
| 62 | + <a :class="{'dropdown-item': true, disabled: file.active || file.success || file.error === 'compressing'}" href="#" @click.prevent="file.active || file.success || file.error === 'compressing' ? false : onEditFileShow(file)">Edit</a> |
63 | 63 | <a :class="{'dropdown-item': true, disabled: !file.active}" href="#" @click.prevent="file.active ? $refs.upload.update(file, {error: 'cancel'}) : false">Cancel</a> |
64 | 64 |
|
65 | 65 | <a class="dropdown-item" href="#" v-if="file.active" @click.prevent="$refs.upload.update(file, {active: false})">Abort</a> |
66 | | - <a class="dropdown-item" href="#" v-else-if="file.error && $refs.upload.features.html5" @click.prevent="$refs.upload.update(file, {active: true, error: '', progress: '0.00'})">Retry upload</a> |
67 | | - <a :class="{'dropdown-item': true, disabled: file.success}" href="#" v-else @click.prevent="file.success ? false : $refs.upload.update(file, {active: true})">Upload</a> |
| 66 | + <a class="dropdown-item" href="#" v-else-if="file.error && file.error !== 'compressing' && $refs.upload.features.html5" @click.prevent="$refs.upload.update(file, {active: true, error: '', progress: '0.00'})">Retry upload</a> |
| 67 | + <a :class="{'dropdown-item': true, disabled: file.success || file.error === 'compressing'}" href="#" v-else @click.prevent="file.success || file.error === 'compressing' ? false : $refs.upload.update(file, {active: true})">Upload</a> |
68 | 68 |
|
69 | 69 | <div class="dropdown-divider"></div> |
70 | 70 | <a class="dropdown-item" href="#" @click.prevent="$refs.upload.remove(file)">Remove</a> |
|
164 | 164 | <label for="minSize">Min size:</label> |
165 | 165 | <input type="number" min="0" id="minSize" class="form-control" v-model.number="minSize"> |
166 | 166 | </div> |
| 167 | + <div class="form-group"> |
| 168 | + <label for="autoCompress">Automatically compress:</label> |
| 169 | + <input type="number" min="0" id="autoCompress" class="form-control" v-model.number="autoCompress"> |
| 170 | + <small class="form-text text-muted" v-if="autoCompress > 0">More than {{autoCompress | formatSize}} files are automatically compressed</small> |
| 171 | + <small class="form-text text-muted" v-else>Set up automatic compression</small> |
| 172 | + </div> |
| 173 | + |
167 | 174 | <div class="form-group"> |
168 | 175 | <div class="form-check"> |
169 | 176 | <label class="form-check-label"> |
|
172 | 179 | </div> |
173 | 180 | <small class="form-text text-muted">Add a file list to start the location to add</small> |
174 | 181 | </div> |
| 182 | + |
175 | 183 | <div class="form-group"> |
176 | 184 | <div class="form-check"> |
177 | 185 | <label class="form-check-label"> |
|
374 | 382 |
|
375 | 383 | <script> |
376 | 384 | import Cropper from 'cropperjs' |
| 385 | +import ImageCompressor from '@xkeshi/image-compressor' |
377 | 386 | import FileUpload from 'vue-upload-component' |
378 | 387 | export default { |
379 | 388 | components: { |
@@ -405,7 +414,7 @@ export default { |
405 | 414 | '_csrf_token': 'xxxxxx', |
406 | 415 | }, |
407 | 416 |
|
408 | | -
|
| 417 | + autoCompress: 1024 * 1024, |
409 | 418 | uploadAuto: false, |
410 | 419 | isOption: false, |
411 | 420 |
|
@@ -473,6 +482,24 @@ export default { |
473 | 482 | if (/\.(php5?|html?|jsx?)$/i.test(newFile.name)) { |
474 | 483 | return prevent() |
475 | 484 | } |
| 485 | +
|
| 486 | + // Automatic compression |
| 487 | + // 自动压缩 |
| 488 | + if (newFile.file && newFile.type.substr(0, 6) === 'image/' && this.autoCompress > 0 && this.autoCompress < newFile.size) { |
| 489 | + newFile.error = 'compressing' |
| 490 | + const imageCompressor = new ImageCompressor(null, { |
| 491 | + convertSize: Infinity, |
| 492 | + maxWidth: 512, |
| 493 | + maxHeight: 512, |
| 494 | + }) |
| 495 | + imageCompressor.compress(newFile.file) |
| 496 | + .then((file) => { |
| 497 | + this.$refs.upload.update(newFile, { error: '', file, size: file.size, type: file.type }) |
| 498 | + }) |
| 499 | + .catch((err) => { |
| 500 | + this.$refs.upload.update(newFile, { error: err.message || 'compress' }) |
| 501 | + }) |
| 502 | + } |
476 | 503 | } |
477 | 504 |
|
478 | 505 |
|
@@ -535,8 +562,10 @@ export default { |
535 | 562 |
|
536 | 563 |
|
537 | 564 | // Automatically activate upload |
538 | | - if (newFile && !oldFile && this.uploadAuto && !this.$refs.upload.active) { |
539 | | - this.$refs.upload.active = true |
| 565 | + if (Boolean(newFile) !== Boolean(oldFile) || oldFile.error !== newFile.error) { |
| 566 | + if (this.uploadAuto && !this.$refs.upload.active) { |
| 567 | + this.$refs.upload.active = true |
| 568 | + } |
540 | 569 | } |
541 | 570 | }, |
542 | 571 |
|
|
0 commit comments