Browse Source

transloadit.modal

Renée Kooi 6 years ago
parent
commit
385542d1ee

+ 10 - 5
packages/@uppy/transloadit-preset/package.json

@@ -28,10 +28,15 @@
     "url": "git+https://github.com/transloadit/uppy.git"
   },
   "dependencies": {
-    "@uppy/core": "0.27.0",
-    "@uppy/dashboard": "0.27.2",
-    "@uppy/form": "0.27.1",
-    "@uppy/transloadit": "0.27.2",
-    "@uppy/utils": "0.27.0"
+    "@uppy/core": "0.27.3",
+    "@uppy/dashboard": "0.27.5",
+    "@uppy/dropbox": "0.27.4",
+    "@uppy/form": "0.27.4",
+    "@uppy/google-drive": "0.27.5",
+    "@uppy/instagram": "0.27.5",
+    "@uppy/transloadit": "0.27.5",
+    "@uppy/url": "0.27.5",
+    "@uppy/utils": "0.27.1",
+    "@uppy/webcam": "0.27.4"
   }
 }

+ 70 - 0
packages/@uppy/transloadit-preset/src/addProviders.js

@@ -0,0 +1,70 @@
+const remoteProviders = {
+  dropbox: require('@uppy/dropbox'),
+  'google-drive': require('@uppy/google-drive'),
+  instagram: require('@uppy/instagram'),
+  url: require('@uppy/url')
+}
+
+const localProviders = {
+  webcam: require('@uppy/webcam')
+}
+
+const remoteProviderOptionNames = [
+  'serverUrl',
+  'serverPattern',
+  'serverHeaders',
+  'target'
+]
+
+// No shared options.
+const localProviderOptionNames = [
+  'target'
+]
+
+function addRemoteProvider (uppy, name, opts) {
+  const Provider = remoteProviders[name]
+  const providerOptions = {}
+  remoteProviderOptionNames.forEach((name) => {
+    if (opts.hasOwnProperty(name)) providerOptions[name] = opts[name]
+  })
+  // Apply overrides for a specific provider plugin.
+  if (typeof opts[name] === 'object') {
+    Object.assign(providerOptions, opts[name])
+  }
+  uppy.use(Provider, providerOptions)
+}
+
+function addLocalProvider (uppy, name, opts) {
+  const Provider = localProviders[name]
+  const providerOptions = {}
+  localProviderOptionNames.forEach((name) => {
+    if (opts.hasOwnProperty(name)) providerOptions[name] = opts[name]
+  })
+  // Apply overrides for a specific provider plugin.
+  if (typeof opts[name] === 'object') {
+    Object.assign(providerOptions, opts[name])
+  }
+  uppy.use(Provider, providerOptions)
+}
+
+function addProviders (uppy, names, opts = {}) {
+  names.forEach((name) => {
+    if (remoteProviders.hasOwnProperty(name)) {
+      addRemoteProvider(uppy, name, opts)
+    } else if (localProviders.hasOwnProperty(name)) {
+      addLocalProvider(uppy, name, opts)
+    } else {
+      const validNames = [
+        ...Object.keys(remoteProviders),
+        ...Object.keys(localProviders)
+      ]
+      const expectedNameString = validNames
+        .sort()
+        .map((validName) => `'${validName}'`)
+        .join(', ')
+      throw new Error(`Unexpected provider '${name}', expected one of [${expectedNameString}]`)
+    }
+  })
+}
+
+module.exports = addProviders

+ 19 - 0
packages/@uppy/transloadit-preset/src/createUppy.js

@@ -0,0 +1,19 @@
+const Uppy = require('@uppy/core')
+
+const uppyOptionNames = [
+  'restrictions',
+  'meta',
+  'onBeforeFileAdded',
+  'onBeforeUpload'
+]
+function createUppy (opts, overrides = {}) {
+  const uppyOptions = {}
+  uppyOptionNames.forEach((name) => {
+    if (opts.hasOwnProperty(name)) uppyOptions[name] = opts[name]
+  })
+  Object.assign(uppyOptions, overrides)
+
+  return Uppy(uppyOptions)
+}
+
+module.exports = createUppy

+ 36 - 8
packages/@uppy/transloadit-preset/src/modal.js

@@ -1,23 +1,51 @@
-const Uppy = require('@uppy/core')
 const Dashboard = require('@uppy/dashboard')
+const createUppy = require('./createUppy')
 const addTransloaditPlugin = require('./addTransloaditPlugin')
+const addProviders = require('./addProviders')
 
-function modal (target, opts) {
-  const uppy = Uppy({
+const CANCEL = {}
+
+function modal (target, opts = {}) {
+  const pluginId = 'modal'
+  const uppy = createUppy(opts, {
     allowMultipleUploads: false
   })
   addTransloaditPlugin(uppy, opts)
   uppy.use(Dashboard, {
+    id: pluginId,
     target,
-    autoClose: true
+    closeAfterFinish: true
   })
 
+  window.u = uppy
+
+  if (Array.isArray(opts.providers)) {
+    addProviders(uppy, opts.providers, {
+      ...opts,
+      // Install providers into the Dashboard.
+      target: uppy.getPlugin(pluginId)
+    })
+  }
+
   return new Promise((resolve, reject) => {
-    uppy.on('complete', resolve)
-    uppy.getPlugin('Dashboard').openModal()
+    uppy.on('complete', (result) => {
+      if (result.failed.length === 0) {
+        resolve(result)
+      }
+    })
+    uppy.on('error', reject)
+    uppy.on('cancel-all', () => reject(CANCEL))
+    uppy.getPlugin(pluginId)
+      .openModal()
   }).then((result) => {
-    return uppy.getPlugin('Dashboard').closeModal()
-      .then(() => result)
+    return result
+  }, (err) => {
+    if (err === CANCEL) {
+      uppy.getPlugin(pluginId)
+        .requestCloseModal()
+      return null
+    }
+    throw err
   })
 }