/
index.tsx
172 lines (157 loc) · 5.04 KB
/
index.tsx
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
import { h } from 'preact'
import { useEffect, useState, useCallback } from 'preact/hooks'
import classNames from 'classnames'
import { nanoid } from 'nanoid/non-secure'
import getFileTypeIcon from '../../utils/getFileTypeIcon.tsx'
import ignoreEvent from '../../utils/ignoreEvent.ts'
import FilePreview from '../FilePreview.tsx'
import RenderMetaFields from './RenderMetaFields.tsx'
type $TSFixMe = any
export default function FileCard(props: $TSFixMe) {
const {
files,
fileCardFor,
toggleFileCard,
saveFileCard,
metaFields,
requiredMetaFields,
openFileEditor,
i18n,
i18nArray,
className,
canEditFile,
} = props
const getMetaFields = () => {
return typeof metaFields === 'function' ?
metaFields(files[fileCardFor])
: metaFields
}
const file = files[fileCardFor]
const computedMetaFields = getMetaFields() ?? []
const showEditButton = canEditFile(file)
const storedMetaData: Record<string, string> = {}
computedMetaFields.forEach((field: $TSFixMe) => {
storedMetaData[field.id] = file.meta[field.id] ?? ''
})
const [formState, setFormState] = useState(storedMetaData)
const handleSave = useCallback(
(ev: $TSFixMe) => {
ev.preventDefault()
saveFileCard(formState, fileCardFor)
},
[saveFileCard, formState, fileCardFor],
)
const updateMeta = (newVal: $TSFixMe, name: $TSFixMe) => {
setFormState({
...formState,
[name]: newVal,
})
}
const handleCancel = () => {
toggleFileCard(false)
}
const [form] = useState(() => {
const formEl = document.createElement('form')
formEl.setAttribute('tabindex', '-1')
formEl.id = nanoid()
return formEl
})
useEffect(() => {
document.body.appendChild(form)
form.addEventListener('submit', handleSave)
return () => {
form.removeEventListener('submit', handleSave)
document.body.removeChild(form)
}
}, [form, handleSave])
return (
<div
className={classNames('uppy-Dashboard-FileCard', 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"
>
{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={form.id}
title={i18n('finishEditingFile')}
onClick={handleCancel}
>
{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: $TSFixMe) => {
// 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.
handleSave(event)
openFileEditor(file)
}}
>
{/* At the moment we only have one file editor - image editor.
If we get editors for other file formats (e.g. pdfs),
we can conditionally display i18n('editFile')/i18n('editImage'). */}
{i18n('editImage')}
</button>
)}
</div>
<div className="uppy-Dashboard-FileCard-info">
<RenderMetaFields
computedMetaFields={computedMetaFields}
requiredMetaFields={requiredMetaFields}
updateMeta={updateMeta}
form={form}
formState={formState}
/>
</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="submit"
form={form.id}
>
{i18n('saveChanges')}
</button>
<button
className="uppy-u-reset uppy-c-btn uppy-c-btn-link uppy-Dashboard-FileCard-actionsBtn"
type="button"
onClick={handleCancel}
form={form.id}
>
{i18n('cancel')}
</button>
</div>
</div>
</div>
)
}