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

@uppy/dashboard: auto discover and install plugins without target #4343

Merged
merged 18 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions packages/@uppy/core/src/Uppy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,8 @@ class Uppy {
}
plugin.install()

this.emit('plugin-added', plugin)
arturi marked this conversation as resolved.
Show resolved Hide resolved

return this
}

Expand Down
47 changes: 33 additions & 14 deletions packages/@uppy/dashboard/src/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ export default class Dashboard extends UIPlugin {
this.startListeningToResize()
document.addEventListener('paste', this.handlePasteOnBody)

this.uppy.on('plugin-added', this.#autoDiscoverPlugins)
arturi marked this conversation as resolved.
Show resolved Hide resolved
this.uppy.on('plugin-remove', this.removeTarget)
this.uppy.on('file-added', this.hideAllPanels)
this.uppy.on('dashboard:modal-closed', this.hideAllPanels)
Expand Down Expand Up @@ -783,6 +784,7 @@ export default class Dashboard extends UIPlugin {
document.removeEventListener('paste', this.handlePasteOnBody)

window.removeEventListener('popstate', this.handlePopState, false)
this.uppy.off('plugin-added', this.#autoDiscoverPlugins)
this.uppy.off('plugin-remove', this.removeTarget)
this.uppy.off('file-added', this.hideAllPanels)
this.uppy.off('dashboard:modal-closed', this.hideAllPanels)
Expand Down Expand Up @@ -1015,10 +1017,34 @@ export default class Dashboard extends UIPlugin {
})
}

discoverProviderPlugins = () => {
this.uppy.iteratePlugins((plugin) => {
if (plugin && !plugin.target && plugin.opts && plugin.opts.target === this.constructor) {
this.addTarget(plugin)
// discoverProviderPlugins = () => {
// this.uppy.iteratePlugins((plugin) => {
// if (plugin && !plugin.target && plugin.opts && plugin.opts.target === this.constructor) {
// console.log('plugin', plugin.id)
// this.addTarget(plugin)
// }
// })
// }

arturi marked this conversation as resolved.
Show resolved Hide resolved
#addSpecifiedPlugins = () => {
const plugins = this.opts.plugins || []

plugins.forEach((pluginID) => {
const plugin = this.uppy.getPlugin(pluginID)
if (plugin) {
plugin.mount(this, plugin)
}
})
}

#autoDiscoverPlugins = () => {
const typesAllowed = ['acquirer', 'editor']
arturi marked this conversation as resolved.
Show resolved Hide resolved
arturi marked this conversation as resolved.
Show resolved Hide resolved
this.uppy.iteratePlugins(plugin => {
if (plugin && !plugin.opts?.target && typesAllowed.includes(plugin.type)) {
const pluginAlreadyAdded = this.getPluginState().targets.some(
installedPlugin => plugin.id === installedPlugin.id,
)
if (!pluginAlreadyAdded) this.addTarget(plugin)
}
})
}
Expand Down Expand Up @@ -1055,15 +1081,6 @@ export default class Dashboard extends UIPlugin {
this.mount(target, this)
}

const plugins = this.opts.plugins || []

plugins.forEach((pluginID) => {
const plugin = this.uppy.getPlugin(pluginID)
if (plugin) {
plugin.mount(this, plugin)
}
})

if (!this.opts.disableStatusBar) {
this.uppy.use(StatusBar, {
id: `${this.id}:StatusBar`,
Expand Down Expand Up @@ -1111,7 +1128,9 @@ export default class Dashboard extends UIPlugin {
this.darkModeMediaQuery.addListener(this.handleSystemDarkModeChange)
}

this.discoverProviderPlugins()
// this.discoverProviderPlugins()
arturi marked this conversation as resolved.
Show resolved Hide resolved
this.#addSpecifiedPlugins()
this.#autoDiscoverPlugins()
arturi marked this conversation as resolved.
Show resolved Hide resolved
this.initEvents()
}

Expand Down
2 changes: 0 additions & 2 deletions packages/@uppy/remote-sources/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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'
Expand Down Expand Up @@ -38,7 +37,6 @@ export default class RemoteSources extends BasePlugin {

const defaultOptions = {
sources: Object.keys(availablePlugins),
target: Dashboard,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a breaking change, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for RemoteSources unfortunately. But not for the Dashboard. But if you update RemoteSources and not the Dashboard, it will stop working, true.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make that opt-in somehow? Maybe by adding an option ignoreTarget: true, and we would remove both options in the next major, wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope we can figure out something without adding more options. Either by letting plugins do breaking changes whenever they want or by skipping this feature in remote sources for now.

}
this.opts = { ...defaultOptions, ...opts }

Expand Down