index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const { h } = require('preact')
  2. const copyToClipboard = require('../../../utils/copyToClipboard')
  3. const { iconPencil, iconCross, iconCopyLink } = require('../../icons')
  4. function EditButton ({
  5. file,
  6. uploadInProgressOrComplete,
  7. metaFields,
  8. i18n,
  9. onClick
  10. }) {
  11. if (!uploadInProgressOrComplete &&
  12. metaFields &&
  13. metaFields.length > 0) {
  14. return (
  15. <button
  16. class="uppy-u-reset uppy-DashboardItem-action uppy-DashboardItem-action--edit"
  17. type="button"
  18. aria-label={i18n('editFile') + ' ' + file.meta.name}
  19. title={i18n('editFile')}
  20. onclick={() => onClick()}
  21. >
  22. {iconPencil()}
  23. </button>
  24. )
  25. }
  26. return null
  27. }
  28. function RemoveButton ({ i18n, onClick }) {
  29. return (
  30. <button
  31. class="uppy-u-reset uppy-DashboardItem-action uppy-DashboardItem-action--remove"
  32. type="button"
  33. aria-label={i18n('removeFile')}
  34. title={i18n('removeFile')}
  35. onclick={() => onClick()}
  36. >
  37. {iconCross()}
  38. </button>
  39. )
  40. }
  41. const copyLinkToClipboard = (event, props) =>
  42. copyToClipboard(props.file.uploadURL, props.i18n('copyLinkToClipboardFallback'))
  43. .then(() => {
  44. props.log('Link copied to clipboard.')
  45. props.info(props.i18n('copyLinkToClipboardSuccess'), 'info', 3000)
  46. })
  47. .catch(props.log)
  48. // avoid losing focus
  49. .then(() => event.target.focus({ preventScroll: true }))
  50. function CopyLinkButton (props) {
  51. return (
  52. <button
  53. class="uppy-u-reset uppy-DashboardItem-action uppy-DashboardItem-action--copyLink"
  54. type="button"
  55. aria-label={props.i18n('copyLink')}
  56. title={props.i18n('copyLink')}
  57. onclick={(event) => copyLinkToClipboard(event, props)}
  58. >
  59. {iconCopyLink()}
  60. </button>
  61. )
  62. }
  63. module.exports = function Buttons (props) {
  64. const {
  65. file,
  66. uploadInProgressOrComplete,
  67. metaFields,
  68. showLinkToFileUploadResult,
  69. showRemoveButton,
  70. i18n,
  71. removeFile,
  72. toggleFileCard
  73. } = props
  74. return (
  75. <div className="uppy-DashboardItem-actionWrapper">
  76. <EditButton
  77. i18n={i18n}
  78. file={file}
  79. uploadInProgressOrComplete={uploadInProgressOrComplete}
  80. metaFields={metaFields}
  81. onClick={() => toggleFileCard(file.id)}
  82. />
  83. {showLinkToFileUploadResult && file.uploadURL ? (
  84. <CopyLinkButton i18n={i18n} />
  85. ) : null}
  86. {showRemoveButton ? (
  87. <RemoveButton
  88. i18n={i18n}
  89. info={props.info}
  90. log={props.log}
  91. onClick={() => removeFile(file.id)}
  92. />
  93. ) : null}
  94. </div>
  95. )
  96. }