Skip to content
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

Fix rendering preview for uploading image #705

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ class WidgetFile {
this.progress = this.progress.bind(this)
this.callback = callbacks('memory')
this.ctrl = new CancelController()
this.progressState = 'pending'
this.file = uploadFile(data, {
...build(settings || {}),
onProgress: info => this.callback.fire(info),
onProgress: info => this.callback.fire(info, data),
cancel: this.ctrl
})
}

state() {
return this.progressState
}

fail(callback) {
this.progressState = 'error'

return this.file.catch(error => {
if (!error.isCancel) {
return callback(error)
Expand All @@ -25,10 +32,14 @@ class WidgetFile {
}

done(callback) {
this.progressState = 'ready'

return this.file.then(callback)
}

progress(callback) {
this.progressState = 'uploading'

this.callback.add(callback)
}

Expand All @@ -37,6 +48,8 @@ class WidgetFile {
}

cancel() {
this.progressState = 'cancelled'

this.ctrl.cancel()
}
}
Expand Down
55 changes: 25 additions & 30 deletions src/widget/tabs/preview-tab.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { URL } from '../../utils/abilities'
// import { Blob } from '../../utils/abilities'
import { URL, Blob } from '../../utils/abilities'
import { imageLoader, videoLoader } from '../../utils/image-loader.ts'
import {
defer,
gcd as calcGCD,
// once,
once,
fitSize,
// readableFileSize,
readableFileSize,
parseHTML,
canvasToBlob
} from '../../utils'
Expand Down Expand Up @@ -52,26 +51,23 @@ class PreviewTab extends BasePreviewTab {
}
}

// const tryToLoadImagePreview = once(this.__tryToLoadImagePreview.bind(this))
// const tryToLoadVideoPreview = once(this.__tryToLoadVideoPreview.bind(this))
const tryToLoadImagePreview = once(this.__tryToLoadImagePreview.bind(this))
const tryToLoadVideoPreview = once(this.__tryToLoadVideoPreview.bind(this))
this.__setState('unknown', {})

// this.file.progress(
// ifCur(info => {
// info = info.incompleteFileInfo
// const label = (info.name || '') + readableFileSize(info.size, '', ', ')
// this.container.querySelector(
// '.uploadcare--preview__file-name'
// ).textContent = label
// const source = info.sourceInfo
// const blob = Blob
// if (source.file && blob && source.file instanceof blob) {
// return tryToLoadImagePreview(file, source.file).catch(() => {
// return tryToLoadVideoPreview(file, source.file)
// })
// }
// })
// )
this.file.progress(
ifCur((info, data) => {
const label = (data.name || '') + readableFileSize(data.size, '', ', ')
this.container.querySelector(
'.uploadcare--preview__file-name'
).textContent = label
if (data instanceof Blob) {
return tryToLoadImagePreview(file, data).catch(() => {
return tryToLoadVideoPreview(file, data)
})
}
})
)
this.file.done(
ifCur(info => {
var imgInfo, src
Expand Down Expand Up @@ -125,7 +121,7 @@ class PreviewTab extends BasePreviewTab {
rej = reject
})
if (
file.state() !== 'pending' ||
file.state() !== 'ready' ||
!blob.size ||
blob.size >= this.settings.multipartMinSize
) {
Expand Down Expand Up @@ -304,13 +300,12 @@ class PreviewTab extends BasePreviewTab {
)
$cropSizes.setAttribute('aria-disabled', 'true')
$cropSizes.setAttribute('tabindex', -1)
return imgLoader
.then(function() {
// Often IE 11 doesn't do reflow after image.onLoad
// and actual image remains 28x30 (broken image placeholder).
// Looks like defer always fixes it.
return defer(startCrop)
})
return imgLoader.then(function() {
// Often IE 11 doesn't do reflow after image.onLoad
// and actual image remains 28x30 (broken image placeholder).
// Looks like defer always fixes it.
return defer(startCrop)
})
}
}

Expand Down