-
Notifications
You must be signed in to change notification settings - Fork 2k
/
FileCard.js
111 lines (95 loc) · 3.52 KB
/
FileCard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const getFileTypeIcon = require('../utils/getFileTypeIcon')
const FilePreview = require('./FilePreview')
const ignoreEvent = require('../utils/ignoreEvent.js')
const { h, Component } = require('preact')
class FileCard extends Component {
constructor (props) {
super(props)
this.meta = {}
this.tempStoreMetaOrSubmit = this.tempStoreMetaOrSubmit.bind(this)
this.renderMetaFields = this.renderMetaFields.bind(this)
this.handleSave = this.handleSave.bind(this)
this.handleCancel = this.handleCancel.bind(this)
}
componentDidMount () {
setTimeout(() => {
if (!this.firstInput) return
this.firstInput.focus({ preventScroll: true })
}, 150)
}
tempStoreMetaOrSubmit (ev) {
const file = this.props.files[this.props.fileCardFor]
if (ev.keyCode === 13) {
ev.stopPropagation()
ev.preventDefault()
this.props.saveFileCard(this.meta, file.id)
return
}
const value = ev.target.value
const name = ev.target.dataset.name
this.meta[name] = value
}
renderMetaFields (file) {
const metaFields = this.props.metaFields || []
return metaFields.map((field, i) => {
return <fieldset class="uppy-DashboardFileCard-fieldset">
<label class="uppy-DashboardFileCard-label">{field.name}</label>
<input class="uppy-c-textInput uppy-DashboardFileCard-input"
type="text"
data-name={field.id}
value={file.meta[field.id]}
placeholder={field.placeholder}
onkeyup={this.tempStoreMetaOrSubmit}
onkeydown={this.tempStoreMetaOrSubmit}
onkeypress={this.tempStoreMetaOrSubmit}
ref={(el) => {
if (i === 0) this.firstInput = el
}} /></fieldset>
})
}
handleSave (ev) {
const fileID = this.props.fileCardFor
this.props.saveFileCard(this.meta, fileID)
}
handleCancel (ev) {
this.meta = {}
this.props.toggleFileCard()
}
render () {
const file = this.props.files[this.props.fileCardFor]
return (
<div class="uppy-DashboardFileCard"
onDragOver={ignoreEvent}
onDragLeave={ignoreEvent}
onDrop={ignoreEvent}
onPaste={ignoreEvent}>
<div class="uppy-DashboardContent-bar">
<div class="uppy-DashboardContent-title" role="heading" aria-level="h1">
{this.props.i18nArray('editing', {
file: <span class="uppy-DashboardContent-titleFile">{file.meta ? file.meta.name : file.name}</span>
})}
</div>
<button class="uppy-DashboardContent-back" type="button" title={this.props.i18n('finishEditingFile')}
onclick={this.handleSave}>{this.props.i18n('done')}</button>
</div>
<div class="uppy-DashboardFileCard-inner">
<div class="uppy-DashboardFileCard-preview" style={{ backgroundColor: getFileTypeIcon(file.type).color }}>
<FilePreview file={file} />
</div>
<div class="uppy-DashboardFileCard-info">
{this.renderMetaFields(file)}
</div>
<div class="uppy-Dashboard-actions">
<button class="uppy-u-reset uppy-c-btn uppy-c-btn-primary uppy-Dashboard-actionsBtn"
type="button"
onclick={this.handleSave}>{this.props.i18n('saveChanges')}</button>
<button class="uppy-u-reset uppy-c-btn uppy-c-btn-link uppy-Dashboard-actionsBtn"
type="button"
onclick={this.handleCancel}>{this.props.i18n('cancel')}</button>
</div>
</div>
</div>
)
}
}
module.exports = FileCard