FileCard.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const getFileTypeIcon = require('./getFileTypeIcon')
  2. const FilePreview = require('./FilePreview')
  3. const { h, Component } = require('preact')
  4. module.exports = class FileCard extends Component {
  5. constructor (props) {
  6. super(props)
  7. this.meta = {}
  8. this.tempStoreMetaOrSubmit = this.tempStoreMetaOrSubmit.bind(this)
  9. this.renderMetaFields = this.renderMetaFields.bind(this)
  10. this.handleSave = this.handleSave.bind(this)
  11. this.handleCancel = this.handleCancel.bind(this)
  12. }
  13. tempStoreMetaOrSubmit (ev) {
  14. const file = this.props.files[this.props.fileCardFor]
  15. if (ev.keyCode === 13) {
  16. ev.stopPropagation()
  17. ev.preventDefault()
  18. this.props.saveFileCard(this.meta, file.id)
  19. return
  20. }
  21. const value = ev.target.value
  22. const name = ev.target.dataset.name
  23. this.meta[name] = value
  24. }
  25. renderMetaFields (file) {
  26. const metaFields = this.props.metaFields || []
  27. return metaFields.map((field) => {
  28. return <fieldset class="uppy-DashboardFileCard-fieldset">
  29. <label class="uppy-DashboardFileCard-label">{field.name}</label>
  30. <input class="uppy-c-textInput uppy-DashboardFileCard-input"
  31. type="text"
  32. data-name={field.id}
  33. value={file.meta[field.id]}
  34. placeholder={field.placeholder}
  35. onkeyup={this.tempStoreMetaOrSubmit}
  36. onkeydown={this.tempStoreMetaOrSubmit}
  37. onkeypress={this.tempStoreMetaOrSubmit} /></fieldset>
  38. })
  39. }
  40. handleSave (ev) {
  41. const fileID = this.props.fileCardFor
  42. this.props.saveFileCard(this.meta, fileID)
  43. }
  44. handleCancel (ev) {
  45. this.meta = {}
  46. this.props.toggleFileCard()
  47. }
  48. render () {
  49. if (!this.props.fileCardFor) {
  50. return <div class="uppy-DashboardFileCard" aria-hidden />
  51. }
  52. const file = this.props.files[this.props.fileCardFor]
  53. console.log({
  54. what: this.props.i18n('editing', {
  55. file: 'test'
  56. }),
  57. ever: this.props.i18nArray('editing', {
  58. file: <span class="uppy-DashboardContent-titleFile">{file.meta ? file.meta.name : file.name}</span>
  59. })
  60. })
  61. return (
  62. <div class="uppy-DashboardFileCard" aria-hidden={!this.props.fileCardFor}>
  63. <div style={{ width: '100%', height: '100%' }}>
  64. <div class="uppy-DashboardContent-bar">
  65. <h2 class="uppy-DashboardContent-title">
  66. {this.props.i18nArray('editing', {
  67. file: <span class="uppy-DashboardContent-titleFile">{file.meta ? file.meta.name : file.name}</span>
  68. })}
  69. </h2>
  70. <button class="uppy-DashboardContent-back" type="button" title={this.props.i18n('finishEditingFile')}
  71. onclick={this.handleSave}>{this.props.i18n('done')}</button>
  72. </div>
  73. <div class="uppy-DashboardFileCard-inner">
  74. <div class="uppy-DashboardFileCard-preview" style={{ backgroundColor: getFileTypeIcon(file.type).color }}>
  75. <FilePreview file={file} />
  76. </div>
  77. <div class="uppy-DashboardFileCard-info">
  78. {this.renderMetaFields(file)}
  79. </div>
  80. <div class="uppy-Dashboard-actions">
  81. <button class="uppy-u-reset uppy-c-btn uppy-c-btn-primary uppy-Dashboard-actionsBtn"
  82. type="button"
  83. onclick={this.handleSave}>{this.props.i18n('saveChanges')}</button>
  84. <button class="uppy-u-reset uppy-c-btn uppy-c-btn-link uppy-Dashboard-actionsBtn"
  85. type="button"
  86. onclick={this.handleCancel}>{this.props.i18n('cancel')}</button>
  87. </div>
  88. </div>
  89. </div>
  90. </div>
  91. )
  92. }
  93. }