index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const { UIPlugin } = require('@uppy/core')
  2. const { Provider } = require('@uppy/companion-client')
  3. const { h } = require('preact')
  4. const DriveProviderViews = require('./DriveProviderViews')
  5. module.exports = class GoogleDrive extends UIPlugin {
  6. static VERSION = require('../package.json').version
  7. constructor (uppy, opts) {
  8. super(uppy, opts)
  9. this.id = this.opts.id || 'GoogleDrive'
  10. this.title = this.opts.title || 'Google Drive'
  11. Provider.initPlugin(this, opts)
  12. this.title = this.opts.title || 'Google Drive'
  13. this.icon = () => (
  14. <svg
  15. aria-hidden="true"
  16. focusable="false"
  17. width="32"
  18. height="32"
  19. viewBox="0 0 32 32"
  20. >
  21. <g fill="none" fillRule="evenodd">
  22. <rect
  23. className="uppy-ProviderIconBg"
  24. fill="#4285F4"
  25. width="32"
  26. height="32"
  27. rx="16"
  28. />
  29. <path
  30. d="M25.216 17.736L19.043 7h-6.086l6.175 10.736h6.084zm-11.275.896L10.9 24h11.723l3.04-5.368H13.942zm-1.789-10.29l-5.816 10.29L9.38 24l5.905-10.29-3.132-5.369z"
  31. fill="#FFF"
  32. />
  33. </g>
  34. </svg>
  35. )
  36. this.provider = new Provider(uppy, {
  37. companionUrl: this.opts.companionUrl,
  38. companionHeaders: this.opts.companionHeaders,
  39. companionKeysParams: this.opts.companionKeysParams,
  40. companionCookiesRule: this.opts.companionCookiesRule,
  41. provider: 'drive',
  42. pluginId: this.id,
  43. })
  44. this.defaultLocale = {
  45. strings: {
  46. pluginNameGoogleDrive: 'Google Drive',
  47. },
  48. }
  49. this.i18nInit()
  50. this.title = this.i18n('pluginNameGoogleDrive')
  51. this.onFirstRender = this.onFirstRender.bind(this)
  52. this.render = this.render.bind(this)
  53. }
  54. install () {
  55. this.view = new DriveProviderViews(this, {
  56. provider: this.provider,
  57. })
  58. const { target } = this.opts
  59. if (target) {
  60. this.mount(target, this)
  61. }
  62. }
  63. uninstall () {
  64. this.view.tearDown()
  65. this.unmount()
  66. }
  67. onFirstRender () {
  68. return Promise.all([
  69. this.provider.fetchPreAuthToken(),
  70. this.view.getFolder('root', '/'),
  71. ])
  72. }
  73. render (state) {
  74. return this.view.render(state)
  75. }
  76. }