Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

uppy library fix for image-editor plugin (#12865)

Szymon Chorobski0f95598ce8b7
diff --git a/admin/app/javascript/spree/admin/controllers/active_storage_upload_controller.js b/admin/app/javascript/spree/admin/controllers/active_storage_upload_controller.js
index 299f0e28a2..f8d57752fe 100644
--- a/admin/app/javascript/spree/admin/controllers/active_storage_upload_controller.js
+++ b/admin/app/javascript/spree/admin/controllers/active_storage_upload_controller.js
@@ -29,7 +29,8 @@ export default class extends Controller {
})
this.uppy.use(ActiveStorageUpload, {
- directUploadUrl: document.querySelector("meta[name='direct-upload-url']").getAttribute('content')
+ directUploadUrl: document.querySelector("meta[name='direct-upload-url']").getAttribute('content'),
+ crop: this.cropValue
})
let dashboardOptions = {}
@@ -55,16 +56,6 @@ export default class extends Controller {
this.uppy.on('file-editor:complete', (updatedFile) => {
console.log('File editing complete:', updatedFile)
- // Remove the old file from Uppy's file list
- this.uppy.removeFile(updatedFile.id)
- // Add the updated file to Uppy
- this.uppy.addFile({
- name: updatedFile.name,
- type: updatedFile.type,
- data: updatedFile.data,
- source: 'local',
- isRemote: false
- })
this.handleUI(updatedFile)
diff --git a/admin/app/javascript/spree/admin/helpers/uppy_active_storage.js b/admin/app/javascript/spree/admin/helpers/uppy_active_storage.js
index aa8d510f67..731f2cd980 100644
--- a/admin/app/javascript/spree/admin/helpers/uppy_active_storage.js
+++ b/admin/app/javascript/spree/admin/helpers/uppy_active_storage.js
@@ -20,7 +20,8 @@ export default class ActiveStorageUpload extends BasePlugin {
limit: 0,
timeout: 30 * 1000,
directUploadUrl: null,
- headers: {}
+ headers: {},
+ crop: false
}
this.opts = Object.assign({}, defaultOptions, opts)
@@ -37,10 +38,19 @@ export default class ActiveStorageUpload extends BasePlugin {
install() {
this.uppy.addUploader(this.handleUpload)
+ this.uppy.on('file-editor:complete', this.onEditorComplete)
}
uninstall() {
this.uppy.removeUploader(this.handleUpload)
+ this.uppy.off('file-editor:complete', this.onEditorComplete)
+ }
+
+ onEditorComplete = (updatedFile) => {
+ this.handleUpload([updatedFile.id])
+
+ // call directly upload method after editing image
+ return this.uploadFiles([updatedFile])
}
handleUpload(fileIDs) {
@@ -48,6 +58,10 @@ export default class ActiveStorageUpload extends BasePlugin {
this.uppy.log("[ActiveStorage] No files to upload!")
return Promise.resolve()
}
+ // do not upload before editing is done
+ if (this.opts.crop) {
+ return Promise.resolve()
+ }
this.uppy.log("[ActiveStorage] Uploading...")
const files = fileIDs.map(fileID => this.uppy.getFile(fileID))
@@ -115,8 +129,6 @@ export default class ActiveStorageUpload extends BasePlugin {
directUploadSignedId: blob.signed_id,
}
- this.uppy.setFileState(file.id, { response })
-
this.uppy.emit("upload-success", file, blob)
return resolve(file)