forked from decaporg/decap-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwithFileControl.js
261 lines (229 loc) · 6.97 KB
/
withFileControl.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import styled from '@emotion/styled';
import { Map, List } from 'immutable';
import { once } from 'lodash';
import uuid from 'uuid/v4';
import { oneLine } from 'common-tags';
import { lengths, components, buttons, borders, effects, shadows } from 'netlify-cms-ui-default';
const MAX_DISPLAY_LENGTH = 50;
const ImageWrapper = styled.div`
flex-basis: 155px;
width: 155px;
height: 100px;
margin-right: 20px;
margin-bottom: 20px;
border: ${borders.textField};
border-radius: ${lengths.borderRadius};
${effects.checkerboard};
${shadows.inset};
`;
const Image = styled.img`
width: 100%;
height: 100%;
object-fit: contain;
`;
const MultiImageWrapper = styled.div`
display: flex;
flex-wrap: wrap;
`;
const FileLink = styled.a`
margin-bottom: 20px;
font-weight: normal;
color: inherit;
&:hover,
&:active,
&:focus {
text-decoration: underline;
}
`;
const FileLinks = styled.div`
margin-bottom: 12px;
`;
const FileLinkList = styled.ul`
list-style-type: none;
`;
const FileWidgetButton = styled.button`
${buttons.button};
${components.badge};
`;
const FileWidgetButtonRemove = styled.button`
${buttons.button};
${components.badgeDanger};
margin-top: 12px;
`;
function isMultiple(value) {
return Array.isArray(value) || List.isList(value);
}
const warnDeprecatedOptions = once(field =>
console.warn(oneLine`
Netlify CMS config: ${field.get('name')} field: property "options" has been deprecated for the
${field.get('widget')} widget and will be removed in the next major release. Rather than
\`field.options.media_library\`, apply media library options for this widget under
\`field.media_library\`.
`),
);
export default function withFileControl({ forImage } = {}) {
return class FileControl extends React.Component {
static propTypes = {
field: PropTypes.object.isRequired,
getAsset: PropTypes.func.isRequired,
mediaPaths: ImmutablePropTypes.map.isRequired,
onAddAsset: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
onRemoveInsertedMedia: PropTypes.func.isRequired,
onOpenMediaLibrary: PropTypes.func.isRequired,
onClearMediaControl: PropTypes.func.isRequired,
onRemoveMediaControl: PropTypes.func.isRequired,
classNameWrapper: PropTypes.string.isRequired,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
};
static defaultProps = {
value: '',
};
constructor(props) {
super(props);
this.controlID = uuid();
}
shouldComponentUpdate(nextProps) {
/**
* Always update if the value changes.
*/
if (this.props.value !== nextProps.value) {
return true;
}
/**
* If there is a media path for this control in the state object, and that
* path is different than the value in `nextProps`, update.
*/
const mediaPath = nextProps.mediaPaths.get(this.controlID);
if (mediaPath && nextProps.value !== mediaPath) {
return true;
}
return false;
}
componentDidUpdate() {
const { mediaPaths, value, onRemoveInsertedMedia, onChange } = this.props;
const mediaPath = mediaPaths.get(this.controlID);
if (mediaPath && mediaPath !== value) {
onChange(mediaPath);
} else if (mediaPath && mediaPath === value) {
onRemoveInsertedMedia(this.controlID);
}
}
componentWillUnmount() {
this.props.onRemoveMediaControl(this.controlID);
}
handleChange = e => {
const { field, onOpenMediaLibrary, value } = this.props;
e.preventDefault();
let mediaLibraryFieldOptions;
/**
* `options` hash as a general field property is deprecated, only used
* when external media libraries were first introduced. Not to be
* confused with `options` for the select widget, which serves a different
* purpose.
*/
if (field.hasIn(['options', 'media_library'])) {
warnDeprecatedOptions(field);
mediaLibraryFieldOptions = field.getIn(['options', 'media_library'], Map());
} else {
mediaLibraryFieldOptions = field.get('media_library', Map());
}
return onOpenMediaLibrary({
controlID: this.controlID,
forImage,
privateUpload: field.get('private'),
value,
allowMultiple: !!mediaLibraryFieldOptions.get('allow_multiple', true),
config: mediaLibraryFieldOptions.get('config'),
});
};
handleRemove = e => {
e.preventDefault();
this.props.onClearMediaControl(this.controlID);
return this.props.onChange('');
};
renderFileLink = value => {
const size = MAX_DISPLAY_LENGTH;
if (!value || value.length <= size) {
return value;
}
const text = `${value.substring(0, size / 2)}\u2026${value.substring(
value.length - size / 2 + 1,
value.length,
)}`;
return (
<FileLink href={value} rel="noopener" target="_blank">
{text}
</FileLink>
);
};
renderFileLinks = () => {
const { value } = this.props;
if (isMultiple(value)) {
return (
<FileLinks>
<FileLinkList>
{value.map(val => (
<li key={val}>{this.renderFileLink(val)}</li>
))}
</FileLinkList>
</FileLinks>
);
}
return <FileLinks>{this.renderFileLink(value)}</FileLinks>;
};
renderImages = () => {
const { getAsset, value } = this.props;
if (isMultiple(value)) {
return (
<MultiImageWrapper>
{value.map(val => (
<ImageWrapper key={val}>
<Image src={getAsset(val)} />
</ImageWrapper>
))}
</MultiImageWrapper>
);
}
return (
<ImageWrapper>
<Image src={getAsset(value)} />
</ImageWrapper>
);
};
renderSelection = subject => (
<div>
{forImage ? this.renderImages() : null}
<div>
{forImage ? null : this.renderFileLinks()}
<FileWidgetButton onClick={this.handleChange}>
Choose different {subject}
</FileWidgetButton>
<FileWidgetButtonRemove onClick={this.handleRemove}>
Remove {subject}
</FileWidgetButtonRemove>
</div>
</div>
);
renderNoSelection = (subject, article) => (
<FileWidgetButton onClick={this.handleChange}>
Choose {article} {subject}
</FileWidgetButton>
);
render() {
const { value, classNameWrapper } = this.props;
const subject = forImage ? 'image' : 'file';
const article = forImage ? 'an' : 'a';
return (
<div className={classNameWrapper}>
<span>
{value ? this.renderSelection(subject) : this.renderNoSelection(subject, article)}
</span>
</div>
);
}
};
}