index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const { Plugin } = require('@uppy/core')
  2. const { Provider } = require('@uppy/companion-client')
  3. const { ProviderViews } = require('@uppy/provider-views')
  4. const { h } = require('preact')
  5. module.exports = class Facebook extends Plugin {
  6. static VERSION = require('../package.json').version
  7. constructor (uppy, opts) {
  8. super(uppy, opts)
  9. this.id = this.opts.id || 'Facebook'
  10. Provider.initPlugin(this, opts)
  11. this.title = this.opts.title || 'Facebook'
  12. this.icon = () => (
  13. <svg aria-hidden="true" focusable="false" width="32" height="32" viewBox="0 0 32 32">
  14. <g fill="none" fill-rule="evenodd">
  15. <rect width="32" height="32" rx="16" fill="#3C5A99" />
  16. <path d="M17.842 26v-8.667h2.653l.398-3.377h-3.051v-2.157c0-.978.248-1.644 1.527-1.644H21V7.132A19.914 19.914 0 0 0 18.623 7c-2.352 0-3.963 1.574-3.963 4.465v2.49H12v3.378h2.66V26h3.182z" fill="#FFF" fill-rule="nonzero" />
  17. </g>
  18. </svg>
  19. )
  20. this.provider = new Provider(uppy, {
  21. companionUrl: this.opts.companionUrl,
  22. companionHeaders: this.opts.companionHeaders || this.opts.serverHeaders,
  23. companionKeysParams: this.opts.companionKeysParams,
  24. companionCookiesRule: this.opts.companionCookiesRule,
  25. provider: 'facebook',
  26. pluginId: this.id
  27. })
  28. this.onFirstRender = this.onFirstRender.bind(this)
  29. this.render = this.render.bind(this)
  30. }
  31. install () {
  32. this.view = new ProviderViews(this, {
  33. provider: this.provider
  34. })
  35. const target = this.opts.target
  36. if (target) {
  37. this.mount(target, this)
  38. }
  39. }
  40. uninstall () {
  41. this.view.tearDown()
  42. this.unmount()
  43. }
  44. onFirstRender () {
  45. return Promise.all([
  46. this.provider.fetchPreAuthToken(),
  47. this.view.getFolder()
  48. ])
  49. }
  50. render (state) {
  51. const viewOptions = {}
  52. if (this.getPluginState().files.length && !this.getPluginState().folders.length) {
  53. viewOptions.viewType = 'grid'
  54. viewOptions.showFilter = false
  55. viewOptions.showTitles = false
  56. }
  57. return this.view.render(state, viewOptions)
  58. }
  59. }