index.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. const { Plugin } = require('@uppy/core')
  2. const Translator = require('@uppy/utils/lib/Translator')
  3. const DashboardUI = require('./components/Dashboard')
  4. const StatusBar = require('@uppy/status-bar')
  5. const Informer = require('@uppy/informer')
  6. const ThumbnailGenerator = require('@uppy/thumbnail-generator')
  7. const findAllDOMElements = require('@uppy/utils/lib/findAllDOMElements')
  8. const toArray = require('@uppy/utils/lib/toArray')
  9. const getDroppedFiles = require('@uppy/utils/lib/getDroppedFiles')
  10. const trapFocus = require('./utils/trapFocus')
  11. const cuid = require('cuid')
  12. const ResizeObserver = require('resize-observer-polyfill').default || require('resize-observer-polyfill')
  13. const createSuperFocus = require('./utils/createSuperFocus')
  14. const memoize = require('memoize-one').default || require('memoize-one')
  15. const TAB_KEY = 9
  16. const ESC_KEY = 27
  17. function createPromise () {
  18. const o = {}
  19. o.promise = new Promise((resolve, reject) => {
  20. o.resolve = resolve
  21. o.reject = reject
  22. })
  23. return o
  24. }
  25. function defaultPickerIcon () {
  26. return (
  27. <svg aria-hidden="true" focusable="false" width="30" height="30" viewBox="0 0 30 30">
  28. <path d="M15 30c8.284 0 15-6.716 15-15 0-8.284-6.716-15-15-15C6.716 0 0 6.716 0 15c0 8.284 6.716 15 15 15zm4.258-12.676v6.846h-8.426v-6.846H5.204l9.82-12.364 9.82 12.364H19.26z" />
  29. </svg>
  30. )
  31. }
  32. /**
  33. * Dashboard UI with previews, metadata editing, tabs for various services and more
  34. */
  35. module.exports = class Dashboard extends Plugin {
  36. static VERSION = require('../package.json').version
  37. constructor (uppy, opts) {
  38. super(uppy, opts)
  39. this.id = this.opts.id || 'Dashboard'
  40. this.title = 'Dashboard'
  41. this.type = 'orchestrator'
  42. this.modalName = `uppy-Dashboard-${cuid()}`
  43. this.defaultLocale = {
  44. strings: {
  45. closeModal: 'Close Modal',
  46. importFrom: 'Import from %{name}',
  47. addingMoreFiles: 'Adding more files',
  48. addMoreFiles: 'Add more files',
  49. dashboardWindowTitle: 'File Uploader Window (Press escape to close)',
  50. dashboardTitle: 'File Uploader',
  51. copyLinkToClipboardSuccess: 'Link copied to clipboard',
  52. copyLinkToClipboardFallback: 'Copy the URL below',
  53. copyLink: 'Copy link',
  54. fileSource: 'File source: %{name}',
  55. done: 'Done',
  56. back: 'Back',
  57. addMore: 'Add more',
  58. removeFile: 'Remove file',
  59. editFile: 'Edit file',
  60. editing: 'Editing %{file}',
  61. finishEditingFile: 'Finish editing file',
  62. saveChanges: 'Save changes',
  63. cancel: 'Cancel',
  64. myDevice: 'My Device',
  65. dropPasteFiles: 'Drop files here, paste or %{browseFiles}',
  66. dropPasteFolders: 'Drop files here, paste or %{browseFolders}',
  67. dropPasteBoth: 'Drop files here, paste, %{browseFiles} or %{browseFolders}',
  68. dropPasteImportFiles: 'Drop files here, paste, %{browseFiles} or import from:',
  69. dropPasteImportFolders: 'Drop files here, paste, %{browseFolders} or import from:',
  70. dropPasteImportBoth: 'Drop files here, paste, %{browseFiles}, %{browseFolders} or import from:',
  71. dropHint: 'Drop your files here',
  72. browseFiles: 'browse files',
  73. browseFolders: 'browse folders',
  74. uploadComplete: 'Upload complete',
  75. uploadPaused: 'Upload paused',
  76. resumeUpload: 'Resume upload',
  77. pauseUpload: 'Pause upload',
  78. retryUpload: 'Retry upload',
  79. cancelUpload: 'Cancel upload',
  80. xFilesSelected: {
  81. 0: '%{smart_count} file selected',
  82. 1: '%{smart_count} files selected'
  83. },
  84. uploadingXFiles: {
  85. 0: 'Uploading %{smart_count} file',
  86. 1: 'Uploading %{smart_count} files'
  87. },
  88. processingXFiles: {
  89. 0: 'Processing %{smart_count} file',
  90. 1: 'Processing %{smart_count} files'
  91. },
  92. // The default `poweredBy2` string only combines the `poweredBy` string (%{backwardsCompat}) with the size.
  93. // Locales can override `poweredBy2` to specify a different word order. This is for backwards compat with
  94. // Uppy 1.9.x and below which did a naive concatenation of `poweredBy2 + size` instead of using a locale-specific
  95. // substitution.
  96. // TODO: In 2.0 `poweredBy2` should be removed in and `poweredBy` updated to use substitution.
  97. poweredBy2: '%{backwardsCompat} %{uppy}',
  98. poweredBy: 'Powered by'
  99. }
  100. }
  101. // set default options
  102. const defaultOptions = {
  103. target: 'body',
  104. metaFields: [],
  105. trigger: '#uppy-select-files',
  106. inline: false,
  107. width: 750,
  108. height: 550,
  109. thumbnailWidth: 280,
  110. waitForThumbnailsBeforeUpload: false,
  111. defaultPickerIcon,
  112. showLinkToFileUploadResult: true,
  113. showProgressDetails: false,
  114. hideUploadButton: false,
  115. hideCancelButton: false,
  116. hideRetryButton: false,
  117. hidePauseResumeButton: false,
  118. hideProgressAfterFinish: false,
  119. note: null,
  120. closeModalOnClickOutside: false,
  121. closeAfterFinish: false,
  122. disableStatusBar: false,
  123. disableInformer: false,
  124. disableThumbnailGenerator: false,
  125. disablePageScrollWhenModalOpen: true,
  126. animateOpenClose: true,
  127. fileManagerSelectionType: 'files',
  128. proudlyDisplayPoweredByUppy: true,
  129. onRequestCloseModal: () => this.closeModal(),
  130. showSelectedFiles: true,
  131. showRemoveButtonAfterComplete: false,
  132. browserBackButtonClose: false,
  133. theme: 'light'
  134. }
  135. // merge default options with the ones set by user
  136. this.opts = { ...defaultOptions, ...opts }
  137. this.i18nInit()
  138. this.superFocus = createSuperFocus()
  139. this.ifFocusedOnUppyRecently = false
  140. // Timeouts
  141. this.makeDashboardInsidesVisibleAnywayTimeout = null
  142. this.removeDragOverClassTimeout = null
  143. }
  144. setOptions = (newOpts) => {
  145. super.setOptions(newOpts)
  146. this.i18nInit()
  147. }
  148. i18nInit = () => {
  149. this.translator = new Translator([this.defaultLocale, this.uppy.locale, this.opts.locale])
  150. this.i18n = this.translator.translate.bind(this.translator)
  151. this.i18nArray = this.translator.translateArray.bind(this.translator)
  152. this.setPluginState() // so that UI re-renders and we see the updated locale
  153. }
  154. removeTarget = (plugin) => {
  155. const pluginState = this.getPluginState()
  156. // filter out the one we want to remove
  157. const newTargets = pluginState.targets.filter(target => target.id !== plugin.id)
  158. this.setPluginState({
  159. targets: newTargets
  160. })
  161. }
  162. addTarget = (plugin) => {
  163. const callerPluginId = plugin.id || plugin.constructor.name
  164. const callerPluginName = plugin.title || callerPluginId
  165. const callerPluginType = plugin.type
  166. if (callerPluginType !== 'acquirer' &&
  167. callerPluginType !== 'progressindicator' &&
  168. callerPluginType !== 'editor') {
  169. const msg = 'Dashboard: can only be targeted by plugins of types: acquirer, progressindicator, editor'
  170. this.uppy.log(msg, 'error')
  171. return
  172. }
  173. const target = {
  174. id: callerPluginId,
  175. name: callerPluginName,
  176. type: callerPluginType
  177. }
  178. const state = this.getPluginState()
  179. const newTargets = state.targets.slice()
  180. newTargets.push(target)
  181. this.setPluginState({
  182. targets: newTargets
  183. })
  184. return this.el
  185. }
  186. hideAllPanels = () => {
  187. const update = {
  188. activePickerPanel: false,
  189. showAddFilesPanel: false,
  190. activeOverlayType: null,
  191. fileCardFor: null,
  192. showFileEditor: false
  193. }
  194. const current = this.getPluginState()
  195. if (current.activePickerPanel === update.activePickerPanel &&
  196. current.showAddFilesPanel === update.showAddFilesPanel &&
  197. current.showFileEditor === update.showFileEditor &&
  198. current.activeOverlayType === update.activeOverlayType) {
  199. // avoid doing a state update if nothing changed
  200. return
  201. }
  202. console.log(update)
  203. this.setPluginState(update)
  204. }
  205. showPanel = (id) => {
  206. const { targets } = this.getPluginState()
  207. const activePickerPanel = targets.filter((target) => {
  208. return target.type === 'acquirer' && target.id === id
  209. })[0]
  210. this.setPluginState({
  211. activePickerPanel: activePickerPanel,
  212. activeOverlayType: 'PickerPanel'
  213. })
  214. }
  215. canEditFile = (file) => {
  216. const { targets } = this.getPluginState()
  217. const editors = this._getEditors(targets)
  218. return editors.some((target) => (
  219. this.uppy.getPlugin(target.id).canEditFile(file)
  220. ))
  221. }
  222. openFileEditor = (file) => {
  223. const { targets } = this.getPluginState()
  224. const editors = this._getEditors(targets)
  225. this.setPluginState({
  226. showFileEditor: true,
  227. activeOverlayType: 'FileEditor'
  228. })
  229. editors.forEach((editor) => {
  230. this.uppy.getPlugin(editor.id).selectFile(file)
  231. })
  232. }
  233. openModal = () => {
  234. const { promise, resolve } = createPromise()
  235. // save scroll position
  236. this.savedScrollPosition = window.pageYOffset
  237. // save active element, so we can restore focus when modal is closed
  238. this.savedActiveElement = document.activeElement
  239. if (this.opts.disablePageScrollWhenModalOpen) {
  240. document.body.classList.add('uppy-Dashboard-isFixed')
  241. }
  242. if (this.opts.animateOpenClose && this.getPluginState().isClosing) {
  243. const handler = () => {
  244. this.setPluginState({
  245. isHidden: false
  246. })
  247. this.el.removeEventListener('animationend', handler, false)
  248. resolve()
  249. }
  250. this.el.addEventListener('animationend', handler, false)
  251. } else {
  252. this.setPluginState({
  253. isHidden: false
  254. })
  255. resolve()
  256. }
  257. if (this.opts.browserBackButtonClose) {
  258. this.updateBrowserHistory()
  259. }
  260. // handle ESC and TAB keys in modal dialog
  261. document.addEventListener('keydown', this.handleKeyDownInModal)
  262. this.uppy.emit('dashboard:modal-open')
  263. return promise
  264. }
  265. closeModal = (opts = {}) => {
  266. const {
  267. manualClose = true // Whether the modal is being closed by the user (`true`) or by other means (e.g. browser back button)
  268. } = opts
  269. const { isHidden, isClosing } = this.getPluginState()
  270. if (isHidden || isClosing) {
  271. // short-circuit if animation is ongoing
  272. return
  273. }
  274. const { promise, resolve } = createPromise()
  275. if (this.opts.disablePageScrollWhenModalOpen) {
  276. document.body.classList.remove('uppy-Dashboard-isFixed')
  277. }
  278. if (this.opts.animateOpenClose) {
  279. this.setPluginState({
  280. isClosing: true
  281. })
  282. const handler = () => {
  283. this.setPluginState({
  284. isHidden: true,
  285. isClosing: false
  286. })
  287. this.superFocus.cancel()
  288. this.savedActiveElement.focus()
  289. this.el.removeEventListener('animationend', handler, false)
  290. resolve()
  291. }
  292. this.el.addEventListener('animationend', handler, false)
  293. } else {
  294. this.setPluginState({
  295. isHidden: true
  296. })
  297. this.superFocus.cancel()
  298. this.savedActiveElement.focus()
  299. resolve()
  300. }
  301. // handle ESC and TAB keys in modal dialog
  302. document.removeEventListener('keydown', this.handleKeyDownInModal)
  303. if (manualClose) {
  304. if (this.opts.browserBackButtonClose) {
  305. // Make sure that the latest entry in the history state is our modal name
  306. if (history.state && history.state[this.modalName]) {
  307. // Go back in history to clear out the entry we created (ultimately closing the modal)
  308. history.go(-1)
  309. }
  310. }
  311. }
  312. this.uppy.emit('dashboard:modal-closed')
  313. return promise
  314. }
  315. isModalOpen = () => {
  316. return !this.getPluginState().isHidden || false
  317. }
  318. requestCloseModal = () => {
  319. if (this.opts.onRequestCloseModal) {
  320. return this.opts.onRequestCloseModal()
  321. }
  322. return this.closeModal()
  323. }
  324. setDarkModeCapability = (isDarkModeOn) => {
  325. const { capabilities } = this.uppy.getState()
  326. this.uppy.setState({
  327. capabilities: {
  328. ...capabilities,
  329. darkMode: isDarkModeOn
  330. }
  331. })
  332. }
  333. handleSystemDarkModeChange = (event) => {
  334. const isDarkModeOnNow = event.matches
  335. this.uppy.log(`[Dashboard] Dark mode is ${isDarkModeOnNow ? 'on' : 'off'}`)
  336. this.setDarkModeCapability(isDarkModeOnNow)
  337. }
  338. toggleFileCard = (fileId) => {
  339. if (fileId) {
  340. this.uppy.emit('dashboard:file-edit-start')
  341. } else {
  342. this.uppy.emit('dashboard:file-edit-complete')
  343. }
  344. this.setPluginState({
  345. fileCardFor: fileId || null,
  346. activeOverlayType: fileId ? 'FileCard' : null
  347. })
  348. }
  349. toggleAddFilesPanel = (show) => {
  350. this.setPluginState({
  351. showAddFilesPanel: show,
  352. activeOverlayType: show ? 'AddFiles' : null
  353. })
  354. }
  355. addFiles = (files) => {
  356. const descriptors = files.map((file) => ({
  357. source: this.id,
  358. name: file.name,
  359. type: file.type,
  360. data: file,
  361. meta: {
  362. // path of the file relative to the ancestor directory the user selected.
  363. // e.g. 'docs/Old Prague/airbnb.pdf'
  364. relativePath: file.relativePath || null
  365. }
  366. }))
  367. try {
  368. this.uppy.addFiles(descriptors)
  369. } catch (err) {
  370. this.uppy.log(err)
  371. }
  372. }
  373. // ___Why make insides of Dashboard invisible until first ResizeObserver event is emitted?
  374. // ResizeOberserver doesn't emit the first resize event fast enough, users can see the jump from one .uppy-size-- to another (e.g. in Safari)
  375. // ___Why not apply visibility property to .uppy-Dashboard-inner?
  376. // Because ideally, acc to specs, ResizeObserver should see invisible elements as of width 0. So even though applying invisibility to .uppy-Dashboard-inner works now, it may not work in the future.
  377. startListeningToResize = () => {
  378. // Watch for Dashboard container (`.uppy-Dashboard-inner`) resize
  379. // and update containerWidth/containerHeight in plugin state accordingly.
  380. // Emits first event on initialization.
  381. this.resizeObserver = new ResizeObserver((entries, observer) => {
  382. const uppyDashboardInnerEl = entries[0]
  383. const { width, height } = uppyDashboardInnerEl.contentRect
  384. this.uppy.log(`[Dashboard] resized: ${width} / ${height}`, 'debug')
  385. this.setPluginState({
  386. containerWidth: width,
  387. containerHeight: height,
  388. areInsidesReadyToBeVisible: true
  389. })
  390. })
  391. this.resizeObserver.observe(this.el.querySelector('.uppy-Dashboard-inner'))
  392. // If ResizeObserver fails to emit an event telling us what size to use - default to the mobile view
  393. this.makeDashboardInsidesVisibleAnywayTimeout = setTimeout(() => {
  394. const pluginState = this.getPluginState()
  395. const isModalAndClosed = !this.opts.inline && pluginState.isHidden
  396. if (
  397. // if ResizeObserver hasn't yet fired,
  398. !pluginState.areInsidesReadyToBeVisible &&
  399. // and it's not due to the modal being closed
  400. !isModalAndClosed
  401. ) {
  402. this.uppy.log("[Dashboard] resize event didn't fire on time: defaulted to mobile layout", 'debug')
  403. this.setPluginState({
  404. areInsidesReadyToBeVisible: true
  405. })
  406. }
  407. }, 1000)
  408. }
  409. stopListeningToResize = () => {
  410. this.resizeObserver.disconnect()
  411. clearTimeout(this.makeDashboardInsidesVisibleAnywayTimeout)
  412. }
  413. // Records whether we have been interacting with uppy right now, which is then used to determine whether state updates should trigger a refocusing.
  414. recordIfFocusedOnUppyRecently = (event) => {
  415. if (this.el.contains(event.target)) {
  416. this.ifFocusedOnUppyRecently = true
  417. } else {
  418. this.ifFocusedOnUppyRecently = false
  419. // ___Why run this.superFocus.cancel here when it already runs in superFocusOnEachUpdate?
  420. // Because superFocus is debounced, when we move from Uppy to some other element on the page,
  421. // previously run superFocus sometimes hits and moves focus back to Uppy.
  422. this.superFocus.cancel()
  423. }
  424. }
  425. updateBrowserHistory = () => {
  426. // Ensure history state does not already contain our modal name to avoid double-pushing
  427. if (!history.state || !history.state[this.modalName]) {
  428. // Push to history so that the page is not lost on browser back button press
  429. history.pushState({
  430. ...history.state,
  431. [this.modalName]: true
  432. }, '')
  433. }
  434. // Listen for back button presses
  435. window.addEventListener('popstate', this.handlePopState, false)
  436. }
  437. handlePopState = (event) => {
  438. // Close the modal if the history state no longer contains our modal name
  439. if (this.isModalOpen() && (!event.state || !event.state[this.modalName])) {
  440. this.closeModal({ manualClose: false })
  441. }
  442. // When the browser back button is pressed and uppy is now the latest entry in the history but the modal is closed, fix the history by removing the uppy history entry
  443. // This occurs when another entry is added into the history state while the modal is open, and then the modal gets manually closed
  444. // Solves PR #575 (https://github.com/transloadit/uppy/pull/575)
  445. if (!this.isModalOpen() && event.state && event.state[this.modalName]) {
  446. history.go(-1)
  447. }
  448. }
  449. handleKeyDownInModal = (event) => {
  450. // close modal on esc key press
  451. if (event.keyCode === ESC_KEY) this.requestCloseModal(event)
  452. // trap focus on tab key press
  453. if (event.keyCode === TAB_KEY) trapFocus.forModal(event, this.getPluginState().activeOverlayType, this.el)
  454. }
  455. handleClickOutside = () => {
  456. if (this.opts.closeModalOnClickOutside) this.requestCloseModal()
  457. }
  458. handlePaste = (event) => {
  459. // 1. Let any acquirer plugin (Url/Webcam/etc.) handle pastes to the root
  460. this.uppy.iteratePlugins((plugin) => {
  461. if (plugin.type === 'acquirer') {
  462. // Every Plugin with .type acquirer can define handleRootPaste(event)
  463. plugin.handleRootPaste && plugin.handleRootPaste(event)
  464. }
  465. })
  466. // 2. Add all dropped files
  467. const files = toArray(event.clipboardData.files)
  468. this.addFiles(files)
  469. }
  470. handleInputChange = (event) => {
  471. event.preventDefault()
  472. const files = toArray(event.target.files)
  473. this.addFiles(files)
  474. }
  475. handleDragOver = (event) => {
  476. event.preventDefault()
  477. event.stopPropagation()
  478. // 1. Add a small (+) icon on drop
  479. // (and prevent browsers from interpreting this as files being _moved_ into the browser, https://github.com/transloadit/uppy/issues/1978)
  480. event.dataTransfer.dropEffect = 'copy'
  481. clearTimeout(this.removeDragOverClassTimeout)
  482. this.setPluginState({ isDraggingOver: true })
  483. }
  484. handleDragLeave = (event) => {
  485. event.preventDefault()
  486. event.stopPropagation()
  487. clearTimeout(this.removeDragOverClassTimeout)
  488. // Timeout against flickering, this solution is taken from drag-drop library. Solution with 'pointer-events: none' didn't work across browsers.
  489. this.removeDragOverClassTimeout = setTimeout(() => {
  490. this.setPluginState({ isDraggingOver: false })
  491. }, 50)
  492. }
  493. handleDrop = (event, dropCategory) => {
  494. event.preventDefault()
  495. event.stopPropagation()
  496. clearTimeout(this.removeDragOverClassTimeout)
  497. // 2. Remove dragover class
  498. this.setPluginState({ isDraggingOver: false })
  499. // 3. Let any acquirer plugin (Url/Webcam/etc.) handle drops to the root
  500. this.uppy.iteratePlugins((plugin) => {
  501. if (plugin.type === 'acquirer') {
  502. // Every Plugin with .type acquirer can define handleRootDrop(event)
  503. plugin.handleRootDrop && plugin.handleRootDrop(event)
  504. }
  505. })
  506. // 4. Add all dropped files
  507. let executedDropErrorOnce = false
  508. const logDropError = (error) => {
  509. this.uppy.log(error, 'error')
  510. // In practice all drop errors are most likely the same, so let's just show one to avoid overwhelming the user
  511. if (!executedDropErrorOnce) {
  512. this.uppy.info(error.message, 'error')
  513. executedDropErrorOnce = true
  514. }
  515. }
  516. getDroppedFiles(event.dataTransfer, { logDropError })
  517. .then((files) => {
  518. if (files.length > 0) {
  519. this.uppy.log('[Dashboard] Files were dropped')
  520. this.addFiles(files)
  521. }
  522. })
  523. }
  524. handleRequestThumbnail = (file) => {
  525. if (!this.opts.waitForThumbnailsBeforeUpload) {
  526. this.uppy.emit('thumbnail:request', file)
  527. }
  528. }
  529. /**
  530. * We cancel thumbnail requests when a file item component unmounts to avoid clogging up the queue when the user scrolls past many elements.
  531. */
  532. handleCancelThumbnail = (file) => {
  533. if (!this.opts.waitForThumbnailsBeforeUpload) {
  534. this.uppy.emit('thumbnail:cancel', file)
  535. }
  536. }
  537. handleKeyDownInInline = (event) => {
  538. // Trap focus on tab key press.
  539. if (event.keyCode === TAB_KEY) trapFocus.forInline(event, this.getPluginState().activeOverlayType, this.el)
  540. }
  541. // ___Why do we listen to the 'paste' event on a document instead of onPaste={props.handlePaste} prop, or this.el.addEventListener('paste')?
  542. // Because (at least) Chrome doesn't handle paste if focus is on some button, e.g. 'My Device'.
  543. // => Therefore, the best option is to listen to all 'paste' events, and only react to them when we are focused on our particular Uppy instance.
  544. // ___Why do we still need onPaste={props.handlePaste} for the DashboardUi?
  545. // Because if we click on the 'Drop files here' caption e.g., `document.activeElement` will be 'body'. Which means our standard determination of whether we're pasting into our Uppy instance won't work.
  546. // => Therefore, we need a traditional onPaste={props.handlePaste} handler too.
  547. handlePasteOnBody = (event) => {
  548. const isFocusInOverlay = this.el.contains(document.activeElement)
  549. if (isFocusInOverlay) {
  550. this.handlePaste(event)
  551. }
  552. }
  553. handleComplete = ({ failed, uploadID }) => {
  554. if (this.opts.closeAfterFinish && failed.length === 0) {
  555. // All uploads are done
  556. this.requestCloseModal()
  557. }
  558. }
  559. initEvents = () => {
  560. // Modal open button
  561. if (this.opts.trigger && !this.opts.inline) {
  562. const showModalTrigger = findAllDOMElements(this.opts.trigger)
  563. if (showModalTrigger) {
  564. showModalTrigger.forEach(trigger => trigger.addEventListener('click', this.openModal))
  565. } else {
  566. this.uppy.log('Dashboard modal trigger not found. Make sure `trigger` is set in Dashboard options, unless you are planning to call `dashboard.openModal()` method yourself', 'warning')
  567. }
  568. }
  569. this.startListeningToResize()
  570. document.addEventListener('paste', this.handlePasteOnBody)
  571. this.uppy.on('plugin-remove', this.removeTarget)
  572. this.uppy.on('file-added', this.hideAllPanels)
  573. this.uppy.on('dashboard:modal-closed', this.hideAllPanels)
  574. this.uppy.on('file-editor:complete', this.hideAllPanels)
  575. this.uppy.on('complete', this.handleComplete)
  576. // ___Why fire on capture?
  577. // Because this.ifFocusedOnUppyRecently needs to change before onUpdate() fires.
  578. document.addEventListener('focus', this.recordIfFocusedOnUppyRecently, true)
  579. document.addEventListener('click', this.recordIfFocusedOnUppyRecently, true)
  580. if (this.opts.inline) {
  581. this.el.addEventListener('keydown', this.handleKeyDownInInline)
  582. }
  583. }
  584. removeEvents = () => {
  585. const showModalTrigger = findAllDOMElements(this.opts.trigger)
  586. if (!this.opts.inline && showModalTrigger) {
  587. showModalTrigger.forEach(trigger => trigger.removeEventListener('click', this.openModal))
  588. }
  589. this.stopListeningToResize()
  590. document.removeEventListener('paste', this.handlePasteOnBody)
  591. window.removeEventListener('popstate', this.handlePopState, false)
  592. this.uppy.off('plugin-remove', this.removeTarget)
  593. this.uppy.off('file-added', this.hideAllPanels)
  594. this.uppy.off('dashboard:modal-closed', this.hideAllPanels)
  595. this.uppy.off('complete', this.handleComplete)
  596. document.removeEventListener('focus', this.recordIfFocusedOnUppyRecently)
  597. document.removeEventListener('click', this.recordIfFocusedOnUppyRecently)
  598. if (this.opts.inline) {
  599. this.el.removeEventListener('keydown', this.handleKeyDownInInline)
  600. }
  601. }
  602. superFocusOnEachUpdate = () => {
  603. const isFocusInUppy = this.el.contains(document.activeElement)
  604. // When focus is lost on the page (== focus is on body for most browsers, or focus is null for IE11)
  605. const isFocusNowhere = document.activeElement === document.body || document.activeElement === null
  606. const isInformerHidden = this.uppy.getState().info.isHidden
  607. const isModal = !this.opts.inline
  608. if (
  609. // If update is connected to showing the Informer - let the screen reader calmly read it.
  610. isInformerHidden &&
  611. (
  612. // If we are in a modal - always superfocus without concern for other elements on the page (user is unlikely to want to interact with the rest of the page)
  613. isModal ||
  614. // If we are already inside of Uppy, or
  615. isFocusInUppy ||
  616. // If we are not focused on anything BUT we have already, at least once, focused on uppy
  617. // 1. We focus when isFocusNowhere, because when the element we were focused on disappears (e.g. an overlay), - focus gets lost. If user is typing something somewhere else on the page, - focus won't be 'nowhere'.
  618. // 2. We only focus when focus is nowhere AND this.ifFocusedOnUppyRecently, to avoid focus jumps if we do something else on the page.
  619. // [Practical check] Without '&& this.ifFocusedOnUppyRecently', in Safari, in inline mode, when file is uploading, - navigate via tab to the checkbox, try to press space multiple times. Focus will jump to Uppy.
  620. (isFocusNowhere && this.ifFocusedOnUppyRecently)
  621. )
  622. ) {
  623. this.superFocus(this.el, this.getPluginState().activeOverlayType)
  624. } else {
  625. this.superFocus.cancel()
  626. }
  627. }
  628. afterUpdate = () => {
  629. this.superFocusOnEachUpdate()
  630. }
  631. cancelUpload = (fileID) => {
  632. this.uppy.removeFile(fileID)
  633. }
  634. saveFileCard = (meta, fileID) => {
  635. this.uppy.setFileMeta(fileID, meta)
  636. this.toggleFileCard()
  637. }
  638. _attachRenderFunctionToTarget = (target) => {
  639. const plugin = this.uppy.getPlugin(target.id)
  640. return {
  641. ...target,
  642. icon: plugin.icon || this.opts.defaultPickerIcon,
  643. render: plugin.render
  644. }
  645. }
  646. _isTargetSupported = (target) => {
  647. const plugin = this.uppy.getPlugin(target.id)
  648. // If the plugin does not provide a `supported` check, assume the plugin works everywhere.
  649. if (typeof plugin.isSupported !== 'function') {
  650. return true
  651. }
  652. return plugin.isSupported()
  653. }
  654. _getAcquirers = memoize((targets) => {
  655. return targets
  656. .filter(target => target.type === 'acquirer' && this._isTargetSupported(target))
  657. .map(this._attachRenderFunctionToTarget)
  658. })
  659. _getProgressIndicators = memoize((targets) => {
  660. return targets
  661. .filter(target => target.type === 'progressindicator')
  662. .map(this._attachRenderFunctionToTarget)
  663. })
  664. _getEditors = memoize((targets) => {
  665. return targets
  666. .filter(target => target.type === 'editor')
  667. .map(this._attachRenderFunctionToTarget)
  668. })
  669. render = (state) => {
  670. const pluginState = this.getPluginState()
  671. const { files, capabilities, allowNewUpload } = state
  672. // TODO: move this to Core, to share between Status Bar and Dashboard
  673. // (and any other plugin that might need it, too)
  674. const newFiles = Object.keys(files).filter((file) => {
  675. return !files[file].progress.uploadStarted
  676. })
  677. const uploadStartedFiles = Object.keys(files).filter((file) => {
  678. return files[file].progress.uploadStarted
  679. })
  680. const pausedFiles = Object.keys(files).filter((file) => {
  681. return files[file].isPaused
  682. })
  683. const completeFiles = Object.keys(files).filter((file) => {
  684. return files[file].progress.uploadComplete
  685. })
  686. const erroredFiles = Object.keys(files).filter((file) => {
  687. return files[file].error
  688. })
  689. const inProgressFiles = Object.keys(files).filter((file) => {
  690. return !files[file].progress.uploadComplete &&
  691. files[file].progress.uploadStarted
  692. })
  693. const inProgressNotPausedFiles = inProgressFiles.filter((file) => {
  694. return !files[file].isPaused
  695. })
  696. const processingFiles = Object.keys(files).filter((file) => {
  697. return files[file].progress.preprocess || files[file].progress.postprocess
  698. })
  699. const isUploadStarted = uploadStartedFiles.length > 0
  700. const isAllComplete = state.totalProgress === 100 &&
  701. completeFiles.length === Object.keys(files).length &&
  702. processingFiles.length === 0
  703. const isAllErrored = isUploadStarted &&
  704. erroredFiles.length === uploadStartedFiles.length
  705. const isAllPaused = inProgressFiles.length !== 0 &&
  706. pausedFiles.length === inProgressFiles.length
  707. const acquirers = this._getAcquirers(pluginState.targets)
  708. const progressindicators = this._getProgressIndicators(pluginState.targets)
  709. const editors = this._getEditors(pluginState.targets)
  710. let theme
  711. if (this.opts.theme === 'auto') {
  712. theme = capabilities.darkMode ? 'dark' : 'light'
  713. } else {
  714. theme = this.opts.theme
  715. }
  716. if (['files', 'folders', 'both'].indexOf(this.opts.fileManagerSelectionType) < 0) {
  717. this.opts.fileManagerSelectionType = 'files'
  718. console.error(`Unsupported option for "fileManagerSelectionType". Using default of "${this.opts.fileManagerSelectionType}".`)
  719. }
  720. return DashboardUI({
  721. state,
  722. isHidden: pluginState.isHidden,
  723. files,
  724. newFiles,
  725. uploadStartedFiles,
  726. completeFiles,
  727. erroredFiles,
  728. inProgressFiles,
  729. inProgressNotPausedFiles,
  730. processingFiles,
  731. isUploadStarted,
  732. isAllComplete,
  733. isAllErrored,
  734. isAllPaused,
  735. totalFileCount: Object.keys(files).length,
  736. totalProgress: state.totalProgress,
  737. allowNewUpload,
  738. acquirers,
  739. theme,
  740. activePickerPanel: pluginState.activePickerPanel,
  741. showFileEditor: pluginState.showFileEditor,
  742. animateOpenClose: this.opts.animateOpenClose,
  743. isClosing: pluginState.isClosing,
  744. getPlugin: this.uppy.getPlugin,
  745. progressindicators: progressindicators,
  746. editors: editors,
  747. autoProceed: this.uppy.opts.autoProceed,
  748. id: this.id,
  749. closeModal: this.requestCloseModal,
  750. handleClickOutside: this.handleClickOutside,
  751. handleInputChange: this.handleInputChange,
  752. handlePaste: this.handlePaste,
  753. inline: this.opts.inline,
  754. showPanel: this.showPanel,
  755. hideAllPanels: this.hideAllPanels,
  756. log: this.uppy.log,
  757. i18n: this.i18n,
  758. i18nArray: this.i18nArray,
  759. removeFile: this.uppy.removeFile,
  760. uppy: this.uppy,
  761. info: this.uppy.info,
  762. note: this.opts.note,
  763. metaFields: pluginState.metaFields,
  764. resumableUploads: capabilities.resumableUploads || false,
  765. individualCancellation: capabilities.individualCancellation,
  766. isMobileDevice: capabilities.isMobileDevice,
  767. pauseUpload: this.uppy.pauseResume,
  768. retryUpload: this.uppy.retryUpload,
  769. cancelUpload: this.cancelUpload,
  770. cancelAll: this.uppy.cancelAll,
  771. fileCardFor: pluginState.fileCardFor,
  772. toggleFileCard: this.toggleFileCard,
  773. toggleAddFilesPanel: this.toggleAddFilesPanel,
  774. showAddFilesPanel: pluginState.showAddFilesPanel,
  775. saveFileCard: this.saveFileCard,
  776. openFileEditor: this.openFileEditor,
  777. canEditFile: this.canEditFile,
  778. width: this.opts.width,
  779. height: this.opts.height,
  780. showLinkToFileUploadResult: this.opts.showLinkToFileUploadResult,
  781. fileManagerSelectionType: this.opts.fileManagerSelectionType,
  782. proudlyDisplayPoweredByUppy: this.opts.proudlyDisplayPoweredByUppy,
  783. hideCancelButton: this.opts.hideCancelButton,
  784. hideRetryButton: this.opts.hideRetryButton,
  785. hidePauseResumeButton: this.opts.hidePauseResumeButton,
  786. showRemoveButtonAfterComplete: this.opts.showRemoveButtonAfterComplete,
  787. containerWidth: pluginState.containerWidth,
  788. containerHeight: pluginState.containerHeight,
  789. areInsidesReadyToBeVisible: pluginState.areInsidesReadyToBeVisible,
  790. isTargetDOMEl: this.isTargetDOMEl,
  791. parentElement: this.el,
  792. allowedFileTypes: this.uppy.opts.restrictions.allowedFileTypes,
  793. maxNumberOfFiles: this.uppy.opts.restrictions.maxNumberOfFiles,
  794. showSelectedFiles: this.opts.showSelectedFiles,
  795. handleRequestThumbnail: this.handleRequestThumbnail,
  796. handleCancelThumbnail: this.handleCancelThumbnail,
  797. // drag props
  798. isDraggingOver: pluginState.isDraggingOver,
  799. handleDragOver: this.handleDragOver,
  800. handleDragLeave: this.handleDragLeave,
  801. handleDrop: this.handleDrop
  802. })
  803. }
  804. discoverProviderPlugins = () => {
  805. this.uppy.iteratePlugins((plugin) => {
  806. if (plugin && !plugin.target && plugin.opts && plugin.opts.target === this.constructor) {
  807. this.addTarget(plugin)
  808. }
  809. })
  810. }
  811. install = () => {
  812. // Set default state for Dashboard
  813. this.setPluginState({
  814. isHidden: true,
  815. fileCardFor: null,
  816. activeOverlayType: null,
  817. showAddFilesPanel: false,
  818. activePickerPanel: false,
  819. showFileEditor: false,
  820. metaFields: this.opts.metaFields,
  821. targets: [],
  822. // We'll make them visible once .containerWidth is determined
  823. areInsidesReadyToBeVisible: false,
  824. isDraggingOver: false
  825. })
  826. const { inline, closeAfterFinish } = this.opts
  827. if (inline && closeAfterFinish) {
  828. throw new Error('[Dashboard] `closeAfterFinish: true` cannot be used on an inline Dashboard, because an inline Dashboard cannot be closed at all. Either set `inline: false`, or disable the `closeAfterFinish` option.')
  829. }
  830. const { allowMultipleUploads } = this.uppy.opts
  831. if (allowMultipleUploads && closeAfterFinish) {
  832. this.uppy.log('[Dashboard] When using `closeAfterFinish`, we recommended setting the `allowMultipleUploads` option to `false` in the Uppy constructor. See https://uppy.io/docs/uppy/#allowMultipleUploads-true', 'warning')
  833. }
  834. const { target } = this.opts
  835. if (target) {
  836. this.mount(target, this)
  837. }
  838. const plugins = this.opts.plugins || []
  839. plugins.forEach((pluginID) => {
  840. const plugin = this.uppy.getPlugin(pluginID)
  841. if (plugin) {
  842. plugin.mount(this, plugin)
  843. }
  844. })
  845. if (!this.opts.disableStatusBar) {
  846. this.uppy.use(StatusBar, {
  847. id: `${this.id}:StatusBar`,
  848. target: this,
  849. hideUploadButton: this.opts.hideUploadButton,
  850. hideRetryButton: this.opts.hideRetryButton,
  851. hidePauseResumeButton: this.opts.hidePauseResumeButton,
  852. hideCancelButton: this.opts.hideCancelButton,
  853. showProgressDetails: this.opts.showProgressDetails,
  854. hideAfterFinish: this.opts.hideProgressAfterFinish,
  855. locale: this.opts.locale
  856. })
  857. }
  858. if (!this.opts.disableInformer) {
  859. this.uppy.use(Informer, {
  860. id: `${this.id}:Informer`,
  861. target: this
  862. })
  863. }
  864. if (!this.opts.disableThumbnailGenerator) {
  865. this.uppy.use(ThumbnailGenerator, {
  866. id: `${this.id}:ThumbnailGenerator`,
  867. thumbnailWidth: this.opts.thumbnailWidth,
  868. waitForThumbnailsBeforeUpload: this.opts.waitForThumbnailsBeforeUpload,
  869. // If we don't block on thumbnails, we can lazily generate them
  870. lazy: !this.opts.waitForThumbnailsBeforeUpload
  871. })
  872. }
  873. // Dark Mode / theme
  874. this.darkModeMediaQuery = (typeof window !== 'undefined' && window.matchMedia)
  875. ? window.matchMedia('(prefers-color-scheme: dark)')
  876. : null
  877. const isDarkModeOnFromTheStart = this.darkModeMediaQuery ? this.darkModeMediaQuery.matches : false
  878. this.uppy.log(`[Dashboard] Dark mode is ${isDarkModeOnFromTheStart ? 'on' : 'off'}`)
  879. this.setDarkModeCapability(isDarkModeOnFromTheStart)
  880. if (this.opts.theme === 'auto') {
  881. this.darkModeMediaQuery.addListener(this.handleSystemDarkModeChange)
  882. }
  883. this.discoverProviderPlugins()
  884. this.initEvents()
  885. }
  886. uninstall = () => {
  887. if (!this.opts.disableInformer) {
  888. const informer = this.uppy.getPlugin(`${this.id}:Informer`)
  889. // Checking if this plugin exists, in case it was removed by uppy-core
  890. // before the Dashboard was.
  891. if (informer) this.uppy.removePlugin(informer)
  892. }
  893. if (!this.opts.disableStatusBar) {
  894. const statusBar = this.uppy.getPlugin(`${this.id}:StatusBar`)
  895. if (statusBar) this.uppy.removePlugin(statusBar)
  896. }
  897. if (!this.opts.disableThumbnailGenerator) {
  898. const thumbnail = this.uppy.getPlugin(`${this.id}:ThumbnailGenerator`)
  899. if (thumbnail) this.uppy.removePlugin(thumbnail)
  900. }
  901. const plugins = this.opts.plugins || []
  902. plugins.forEach((pluginID) => {
  903. const plugin = this.uppy.getPlugin(pluginID)
  904. if (plugin) plugin.unmount()
  905. })
  906. if (this.opts.theme === 'auto') {
  907. this.darkModeMediaQuery.removeListener(this.handleSystemDarkModeChange)
  908. }
  909. this.unmount()
  910. this.removeEvents()
  911. }
  912. }