index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const { h, Component } = require('preact')
  2. const getFileTypeIcon = require('../../utils/getFileTypeIcon')
  3. const ignoreEvent = require('../../utils/ignoreEvent.js')
  4. const FilePreview = require('../FilePreview')
  5. class FileCard extends Component {
  6. constructor (props) {
  7. super(props)
  8. const file = this.props.files[this.props.fileCardFor]
  9. const metaFields = this.props.metaFields || []
  10. const storedMetaData = {}
  11. metaFields.forEach((field) => {
  12. storedMetaData[field.id] = file.meta[field.id] || ''
  13. })
  14. this.state = {
  15. formState: storedMetaData
  16. }
  17. }
  18. saveOnEnter = (ev) => {
  19. if (ev.keyCode === 13) {
  20. ev.stopPropagation()
  21. ev.preventDefault()
  22. const file = this.props.files[this.props.fileCardFor]
  23. this.props.saveFileCard(this.state.formState, file.id)
  24. }
  25. }
  26. updateMeta = (newVal, name) => {
  27. this.setState({
  28. formState: {
  29. ...this.state.formState,
  30. [name]: newVal
  31. }
  32. })
  33. }
  34. handleSave = () => {
  35. const fileID = this.props.fileCardFor
  36. this.props.saveFileCard(this.state.formState, fileID)
  37. }
  38. handleCancel = () => {
  39. this.props.toggleFileCard()
  40. }
  41. renderMetaFields = () => {
  42. const metaFields = this.props.metaFields || []
  43. const fieldCSSClasses = {
  44. text: 'uppy-u-reset uppy-c-textInput uppy-Dashboard-FileCard-input'
  45. }
  46. return metaFields.map((field) => {
  47. const id = `uppy-Dashboard-FileCard-input-${field.id}`
  48. return (
  49. <fieldset key={field.id} class="uppy-Dashboard-FileCard-fieldset">
  50. <label class="uppy-Dashboard-FileCard-label" for={id}>{field.name}</label>
  51. {field.render !== undefined
  52. ? field.render({
  53. value: this.state.formState[field.id],
  54. onChange: (newVal) => this.updateMeta(newVal, field.id),
  55. fieldCSSClasses: fieldCSSClasses
  56. }, h)
  57. : (
  58. <input
  59. class={fieldCSSClasses.text}
  60. id={id}
  61. type={field.type || 'text'}
  62. value={this.state.formState[field.id]}
  63. placeholder={field.placeholder}
  64. onkeyup={this.saveOnEnter}
  65. onkeydown={this.saveOnEnter}
  66. onkeypress={this.saveOnEnter}
  67. oninput={ev => this.updateMeta(ev.target.value, field.id)}
  68. data-uppy-super-focusable
  69. />
  70. )}
  71. </fieldset>
  72. )
  73. })
  74. }
  75. render () {
  76. const file = this.props.files[this.props.fileCardFor]
  77. return (
  78. <div
  79. class="uppy-Dashboard-FileCard"
  80. data-uppy-panelType="FileCard"
  81. onDragOver={ignoreEvent}
  82. onDragLeave={ignoreEvent}
  83. onDrop={ignoreEvent}
  84. onPaste={ignoreEvent}
  85. >
  86. <div class="uppy-DashboardContent-bar">
  87. <div class="uppy-DashboardContent-title" role="heading" aria-level="1">
  88. {this.props.i18nArray('editing', {
  89. file: <span class="uppy-DashboardContent-titleFile">{file.meta ? file.meta.name : file.name}</span>
  90. })}
  91. </div>
  92. <button
  93. class="uppy-DashboardContent-back" type="button" title={this.props.i18n('finishEditingFile')}
  94. onclick={this.handleSave}
  95. >
  96. {this.props.i18n('done')}
  97. </button>
  98. </div>
  99. <div class="uppy-Dashboard-FileCard-inner">
  100. <div class="uppy-Dashboard-FileCard-preview" style={{ backgroundColor: getFileTypeIcon(file.type).color }}>
  101. <FilePreview file={file} />
  102. </div>
  103. <div class="uppy-Dashboard-FileCard-info">
  104. {this.renderMetaFields()}
  105. </div>
  106. <div class="uppy-Dashboard-FileCard-actions">
  107. <button
  108. class="uppy-u-reset uppy-c-btn uppy-c-btn-primary uppy-Dashboard-FileCard-actionsBtn"
  109. type="button"
  110. onclick={this.handleSave}
  111. >
  112. {this.props.i18n('saveChanges')}
  113. </button>
  114. <button
  115. class="uppy-u-reset uppy-c-btn uppy-c-btn-link uppy-Dashboard-FileCard-actionsBtn"
  116. type="button"
  117. onclick={this.handleCancel}
  118. >
  119. {this.props.i18n('cancel')}
  120. </button>
  121. </div>
  122. </div>
  123. </div>
  124. )
  125. }
  126. }
  127. module.exports = FileCard