-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
ModalWorkflowSource.js
326 lines (288 loc) · 8.33 KB
/
ModalWorkflowSource.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import PropTypes from 'prop-types';
import { Component } from 'react';
import { AtomicBlockUtils, Modifier, RichUtils, EditorState } from 'draft-js';
import { ENTITY_TYPE, DraftUtils } from 'draftail';
import { STRINGS } from '../../../config/wagtailConfig';
import { getSelectionText } from '../DraftUtils';
const $ = global.jQuery;
const EMBED = 'EMBED';
const DOCUMENT = 'DOCUMENT';
const MUTABILITY = {};
MUTABILITY[ENTITY_TYPE.LINK] = 'MUTABLE';
MUTABILITY[DOCUMENT] = 'MUTABLE';
MUTABILITY[ENTITY_TYPE.IMAGE] = 'IMMUTABLE';
MUTABILITY[EMBED] = 'IMMUTABLE';
/**
* Interfaces with Wagtail's ModalWorkflow to open the chooser,
* and create new content in Draft.js based on the data.
*/
class ModalWorkflowSource extends Component {
constructor(props) {
super(props);
this.onChosen = this.onChosen.bind(this);
this.onClose = this.onClose.bind(this);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getChooserConfig(entity, selectedText) {
throw new TypeError(
'Subclasses of ModalWorkflowSource must implement getChooserConfig',
);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
filterEntityData(data) {
throw new TypeError(
'Subclasses of ModalWorkflowSource must implement filterEntityData',
);
}
componentDidMount() {
const { onClose, entity, editorState } = this.props;
const selectedText = getSelectionText(editorState);
const { url, urlParams, onload, responses } = this.getChooserConfig(
entity,
selectedText,
);
$(document.body).on('hidden.bs.modal', this.onClose);
this.workflow = global.ModalWorkflow({
url,
urlParams,
onload,
responses,
onError: () => {
// eslint-disable-next-line no-alert
window.alert(STRINGS.SERVER_ERROR);
onClose();
},
});
}
componentWillUnmount() {
this.workflow = null;
$(document.body).off('hidden.bs.modal', this.onClose);
}
onChosen(data) {
const { editorState, entity, entityKey, entityType, onComplete } =
this.props;
const content = editorState.getCurrentContent();
const selection = editorState.getSelection();
const entityData = this.filterEntityData(data);
const mutability = MUTABILITY[entityType.type];
let nextState;
if (entityType.block) {
if (entity && entityKey) {
// Replace the data for the currently selected block
const blockKey = selection.getAnchorKey();
const block = content.getBlockForKey(blockKey);
nextState = DraftUtils.updateBlockEntity(
editorState,
block,
entityData,
);
} else {
// Add new entity if there is none selected
const contentWithEntity = content.createEntity(
entityType.type,
mutability,
entityData,
);
const newEntityKey = contentWithEntity.getLastCreatedEntityKey();
nextState = AtomicBlockUtils.insertAtomicBlock(
editorState,
newEntityKey,
' ',
);
}
} else {
const contentWithEntity = content.createEntity(
entityType.type,
mutability,
entityData,
);
const newEntityKey = contentWithEntity.getLastCreatedEntityKey();
// Replace text if the chooser demands it, or if there is no selected text in the first place.
const shouldReplaceText =
data.prefer_this_title_as_link_text || selection.isCollapsed();
if (shouldReplaceText) {
// If there is a title attribute, use it. Otherwise we inject the URL.
const newText = data.title || data.url;
const newContent = Modifier.replaceText(
content,
selection,
newText,
null,
newEntityKey,
);
nextState = EditorState.push(
editorState,
newContent,
'insert-characters',
);
} else {
nextState = RichUtils.toggleLink(editorState, selection, newEntityKey);
}
}
// IE11 crashes when rendering the new entity in contenteditable if the modal is still open.
// Other browsers do not mind. This is probably a focus management problem.
// From the user's perspective, this is all happening too fast to notice either way.
this.workflow.close();
onComplete(nextState);
}
onClose(e) {
const { onClose } = this.props;
e.preventDefault();
onClose();
}
render() {
return null;
}
}
ModalWorkflowSource.propTypes = {
editorState: PropTypes.object.isRequired,
entityType: PropTypes.object.isRequired,
entity: PropTypes.object,
entityKey: PropTypes.string,
onComplete: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired,
};
ModalWorkflowSource.defaultProps = {
entity: null,
};
class ImageModalWorkflowSource extends ModalWorkflowSource {
getChooserConfig(entity) {
let url;
let urlParams;
if (entity) {
const data = entity.getData();
url = `${global.chooserUrls.imageChooser}${data.id}/select_format/`;
urlParams = {
format: data.format,
alt_text: data.alt,
};
} else {
url = `${global.chooserUrls.imageChooser}?select_format=true`;
urlParams = {};
}
return {
url,
urlParams,
onload: global.IMAGE_CHOOSER_MODAL_ONLOAD_HANDLERS,
responses: {
imageChosen: this.onChosen,
},
};
}
filterEntityData(data) {
return {
id: data.id,
src: data.preview.url,
alt: data.alt,
format: data.format,
};
}
}
class EmbedModalWorkflowSource extends ModalWorkflowSource {
getChooserConfig(entity) {
const urlParams = {};
if (entity) {
urlParams.url = entity.getData().url;
}
return {
url: global.chooserUrls.embedsChooser,
urlParams,
onload: global.EMBED_CHOOSER_MODAL_ONLOAD_HANDLERS,
responses: {
// Discard the first parameter (HTML) to only transmit the data.
embedChosen: (_, data) => this.onChosen(data),
},
};
}
filterEntityData(data) {
return {
embedType: data.embedType,
url: data.url,
providerName: data.providerName,
authorName: data.authorName,
thumbnail: data.thumbnail,
title: data.title,
};
}
}
class LinkModalWorkflowSource extends ModalWorkflowSource {
getChooserConfig(entity, selectedText) {
let url = global.chooserUrls.pageChooser;
const urlParams = {
page_type: 'wagtailcore.page',
allow_external_link: true,
allow_email_link: true,
allow_phone_link: true,
allow_anchor_link: true,
link_text: selectedText,
};
if (entity) {
const data = entity.getData();
if (data.id) {
if (data.parentId !== null) {
url = `${global.chooserUrls.pageChooser}${data.parentId}/`;
} else {
url = global.chooserUrls.pageChooser;
}
} else if (data.url.startsWith('mailto:')) {
url = global.chooserUrls.emailLinkChooser;
urlParams.link_url = data.url.replace('mailto:', '');
} else if (data.url.startsWith('tel:')) {
url = global.chooserUrls.phoneLinkChooser;
urlParams.link_url = data.url.replace('tel:', '');
} else if (data.url.startsWith('#')) {
url = global.chooserUrls.anchorLinkChooser;
urlParams.link_url = data.url.replace('#', '');
} else {
url = global.chooserUrls.externalLinkChooser;
urlParams.link_url = data.url;
}
}
return {
url,
urlParams,
onload: global.PAGE_CHOOSER_MODAL_ONLOAD_HANDLERS,
responses: {
pageChosen: this.onChosen,
},
};
}
filterEntityData(data) {
if (data.id) {
return {
url: data.url,
id: data.id,
parentId: data.parentId,
};
}
return {
url: data.url,
};
}
}
class DocumentModalWorkflowSource extends ModalWorkflowSource {
getChooserConfig() {
return {
url: global.chooserUrls.documentChooser,
urlParams: {},
onload: global.DOCUMENT_CHOOSER_MODAL_ONLOAD_HANDLERS,
responses: {
documentChosen: this.onChosen,
},
};
}
filterEntityData(data) {
return {
url: data.url,
filename: data.filename,
id: data.id,
};
}
}
export {
ModalWorkflowSource,
ImageModalWorkflowSource,
EmbedModalWorkflowSource,
LinkModalWorkflowSource,
DocumentModalWorkflowSource,
};