forked from transloadit/uppy
/
index.js
77 lines (67 loc) · 2.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { BasePlugin } from '@uppy/core'
import Dashboard from '@uppy/dashboard'
import Dropbox from '@uppy/dropbox'
import GoogleDrive from '@uppy/google-drive'
import Instagram from '@uppy/instagram'
import Facebook from '@uppy/facebook'
import OneDrive from '@uppy/onedrive'
import Box from '@uppy/box'
import Unsplash from '@uppy/unsplash'
import Url from '@uppy/url'
import Zoom from '@uppy/zoom'
import packageJson from '../package.json'
const availablePlugins = {
__proto__: null,
Box,
Dropbox,
Facebook,
GoogleDrive,
Instagram,
OneDrive,
Unsplash,
Url,
Zoom,
}
export default class RemoteSources extends BasePlugin {
static VERSION = packageJson.version
#installedPlugins = new Set()
constructor (uppy, opts) {
super(uppy, opts)
this.id = this.opts.id || 'RemoteSources'
this.type = 'acquirer'
const defaultOptions = {
sources: Object.keys(availablePlugins),
target: Dashboard,
}
this.opts = { ...defaultOptions, ...opts }
if (this.opts.companionUrl == null) {
throw new Error('Please specify companionUrl for RemoteSources to work, see https://uppy.io/docs/remote-sources#companionUrl')
}
}
setOptions (newOpts) {
this.uninstall()
super.setOptions(newOpts)
this.install()
}
install () {
this.opts.sources.forEach((pluginId) => {
const optsForRemoteSourcePlugin = { ...this.opts, sources: undefined }
const plugin = availablePlugins[pluginId]
if (plugin == null) {
const pluginNames = Object.keys(availablePlugins)
const formatter = new Intl.ListFormat('en', { style: 'long', type: 'disjunction' })
throw new Error(`Invalid plugin: "${pluginId}" is not one of: ${formatter.format(pluginNames)}.`)
}
this.uppy.use(plugin, optsForRemoteSourcePlugin)
// `plugin` is a class, but we want to track the instance object
// so we have to do `getPlugin` here.
this.#installedPlugins.add(this.uppy.getPlugin(pluginId))
})
}
uninstall () {
for (const plugin of this.#installedPlugins) {
this.uppy.removePlugin(plugin)
}
this.#installedPlugins.clear()
}
}