forked from transloadit/uppy
/
index.js
200 lines (179 loc) · 6.85 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const { h, Component } = require('preact')
const classNames = require('classnames')
const { nanoid } = require('nanoid/non-secure')
const getFileTypeIcon = require('../../utils/getFileTypeIcon')
const ignoreEvent = require('../../utils/ignoreEvent.js')
const FilePreview = require('../FilePreview')
class FileCard extends Component {
form = document.createElement('form')
constructor (props) {
super(props)
const file = this.props.files[this.props.fileCardFor]
const metaFields = this.getMetaFields() || []
const storedMetaData = {}
metaFields.forEach((field) => {
storedMetaData[field.id] = file.meta[field.id] || ''
})
this.state = {
formState: storedMetaData,
}
this.form.id = nanoid()
}
// TODO(aduh95): move this to `UNSAFE_componentWillMount` when updating to Preact X+.
componentWillMount () { // eslint-disable-line react/no-deprecated
this.form.addEventListener('submit', this.handleSave)
document.body.appendChild(this.form)
}
componentWillUnmount () {
this.form.removeEventListener('submit', this.handleSave)
document.body.removeChild(this.form)
}
getMetaFields () {
return typeof this.props.metaFields === 'function'
? this.props.metaFields(this.props.files[this.props.fileCardFor])
: this.props.metaFields
}
updateMeta = (newVal, name) => {
this.setState(({ formState }) => ({
formState: {
...formState,
[name]: newVal,
},
}))
}
handleSave = (e) => {
e.preventDefault()
const fileID = this.props.fileCardFor
this.props.saveFileCard(this.state.formState, fileID)
}
handleCancel = () => {
this.props.toggleFileCard(false)
}
saveOnEnter = (ev) => {
if (ev.keyCode === 13) {
ev.stopPropagation()
ev.preventDefault()
const file = this.props.files[this.props.fileCardFor]
this.props.saveFileCard(this.state.formState, file.id)
}
}
renderMetaFields = () => {
const metaFields = this.getMetaFields() || []
const fieldCSSClasses = {
text: 'uppy-u-reset uppy-c-textInput uppy-Dashboard-FileCard-input',
}
return metaFields.map((field) => {
const id = `uppy-Dashboard-FileCard-input-${field.id}`
const required = this.props.requiredMetaFields.includes(field.id)
return (
<fieldset key={field.id} className="uppy-Dashboard-FileCard-fieldset">
<label className="uppy-Dashboard-FileCard-label" htmlFor={id}>{field.name}</label>
{field.render !== undefined
? field.render({
value: this.state.formState[field.id],
onChange: (newVal) => this.updateMeta(newVal, field.id),
fieldCSSClasses,
required,
form: this.form.id,
}, h)
: (
<input
className={fieldCSSClasses.text}
id={id}
form={this.form.id}
type={field.type || 'text'}
required={required}
value={this.state.formState[field.id]}
placeholder={field.placeholder}
// If `form` attribute is not supported, we need to capture pressing Enter to avoid bubbling in case Uppy is
// embedded inside a <form>.
onKeyUp={'form' in HTMLInputElement.prototype ? undefined : this.saveOnEnter}
onKeyDown={'form' in HTMLInputElement.prototype ? undefined : this.saveOnEnter}
onKeyPress={'form' in HTMLInputElement.prototype ? undefined : this.saveOnEnter}
onInput={ev => this.updateMeta(ev.target.value, field.id)}
data-uppy-super-focusable
/>
)}
</fieldset>
)
})
}
render () {
const file = this.props.files[this.props.fileCardFor]
const showEditButton = this.props.canEditFile(file)
return (
<div
className={classNames('uppy-Dashboard-FileCard', this.props.className)}
data-uppy-panelType="FileCard"
onDragOver={ignoreEvent}
onDragLeave={ignoreEvent}
onDrop={ignoreEvent}
onPaste={ignoreEvent}
>
<div className="uppy-DashboardContent-bar">
<div className="uppy-DashboardContent-title" role="heading" aria-level="1">
{this.props.i18nArray('editing', {
file: <span className="uppy-DashboardContent-titleFile">{file.meta ? file.meta.name : file.name}</span>,
})}
</div>
<button
className="uppy-DashboardContent-back"
type="button"
form={this.form.id}
title={this.props.i18n('finishEditingFile')}
onClick={this.handleCancel}
>
{this.props.i18n('cancel')}
</button>
</div>
<div className="uppy-Dashboard-FileCard-inner">
<div className="uppy-Dashboard-FileCard-preview" style={{ backgroundColor: getFileTypeIcon(file.type).color }}>
<FilePreview file={file} />
{showEditButton
&& (
<button
type="button"
className="uppy-u-reset uppy-c-btn uppy-Dashboard-FileCard-edit"
onClick={(event) => {
// When opening the image editor we want to save any meta fields changes.
// Otherwise it's confusing for the user to click save in the editor,
// but the changes here are discarded. This bypasses validation,
// but we are okay with that.
this.handleSave(event)
this.props.openFileEditor(file)
}}
form={this.form.id}
>
{this.props.i18n('editFile')}
</button>
)}
</div>
<div className="uppy-Dashboard-FileCard-info">
{this.renderMetaFields()}
</div>
<div className="uppy-Dashboard-FileCard-actions">
<button
className="uppy-u-reset uppy-c-btn uppy-c-btn-primary uppy-Dashboard-FileCard-actionsBtn"
// If `form` attribute is supported, we want a submit button to trigger the form validation.
// Otherwise, fallback to a classic button with a onClick event handler.
type={'form' in HTMLButtonElement.prototype ? 'submit' : 'button'}
onClick={'form' in HTMLButtonElement.prototype ? undefined : this.handleSave}
form={this.form.id}
>
{this.props.i18n('saveChanges')}
</button>
<button
className="uppy-u-reset uppy-c-btn uppy-c-btn-link uppy-Dashboard-FileCard-actionsBtn"
type="button"
onClick={this.handleCancel}
form={this.form.id}
>
{this.props.i18n('cancel')}
</button>
</div>
</div>
</div>
)
}
}
module.exports = FileCard