main.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const Uppy = require('@uppy/core')
  2. const Dashboard = require('@uppy/dashboard')
  3. const Tus = require('@uppy/tus')
  4. const canvasToBlob = require('@uppy/utils/lib/canvasToBlob')
  5. const isOnTravis = !!(process.env.TRAVIS && process.env.CI)
  6. const endpoint = isOnTravis ? 'http://companion.test:1081' : 'http://localhost:1081'
  7. let id = 0
  8. window.setup = function (options) {
  9. id += 1
  10. // Initialise Uppy with Drag & Drop
  11. const uppy = new Uppy({ id: `uppy${id}`, debug: true })
  12. uppy.use(Dashboard, { inline: true, target: '#dash' })
  13. uppy.use(Tus, {
  14. endpoint: `${endpoint}/files/`,
  15. limit: options.limit,
  16. })
  17. uppy.on('file-added', (file) => {
  18. randomColorImage().then((blob) => {
  19. uppy.setFileState(file.id, {
  20. // eslint-disable-next-line compat/compat
  21. preview: URL.createObjectURL(blob),
  22. })
  23. })
  24. })
  25. return uppy
  26. }
  27. function randomColorImage () {
  28. const canvas = document.createElement('canvas')
  29. canvas.width = 140
  30. canvas.height = 140
  31. const context = canvas.getContext('2d')
  32. context.fillStyle = '#xxxxxx'.replace(/x/g, () => '0123456789ABCDEF'[Math.floor(Math.random() * 16)])
  33. context.fillRect(0, 0, 140, 140)
  34. return canvasToBlob(canvas)
  35. }