/
ModalWorkflowSource.js
209 lines (177 loc) · 6.08 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
import PropTypes from 'prop-types';
import { Component } from 'react';
import { AtomicBlockUtils, Modifier, RichUtils, EditorState } from 'draft-js';
import { ENTITY_TYPE } from 'draftail';
import { STRINGS } from '../../../config/wagtailConfig';
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';
export const getChooserConfig = (entityType, entity) => {
const chooserURL = {};
chooserURL[ENTITY_TYPE.IMAGE] = `${global.chooserUrls.imageChooser}?select_format=true`;
chooserURL[EMBED] = global.chooserUrls.embedsChooser;
chooserURL[ENTITY_TYPE.LINK] = global.chooserUrls.pageChooser;
chooserURL[DOCUMENT] = global.chooserUrls.documentChooser;
let url = chooserURL[entityType.type];
let urlParams = {};
if (entityType.type === ENTITY_TYPE.LINK) {
urlParams = {
page_type: 'wagtailcore.page',
allow_external_link: true,
allow_email_link: true,
can_choose_root: 'false',
// This does not initialise the modal with the currently selected text.
// This will need to be implemented in the future.
// See https://github.com/jpuri/draftjs-utils/blob/e81c0ae19c3b0fdef7e0c1b70d924398956be126/js/block.js#L106.
link_text: '',
};
if (entity) {
const data = entity.getData();
if (data.id) {
url = `${global.chooserUrls.pageChooser}${data.parentId}/`;
} else if (data.url.startsWith('mailto:')) {
url = global.chooserUrls.emailLinkChooser;
urlParams.link_url = data.url.replace('mailto:', '');
} else {
url = global.chooserUrls.externalLinkChooser;
urlParams.link_url = data.url;
}
}
}
return {
url,
urlParams,
};
};
export const filterEntityData = (entityType, data) => {
switch (entityType.type) {
case ENTITY_TYPE.IMAGE:
return {
id: data.id,
src: data.preview.url,
alt: data.alt,
format: data.format,
};
case EMBED:
return {
embedType: data.embedType,
url: data.url,
providerName: data.providerName,
authorName: data.authorName,
thumbnail: data.thumbnail,
title: data.title,
};
case ENTITY_TYPE.LINK:
if (data.id) {
return {
url: data.url,
id: data.id,
parentId: data.parentId,
};
}
return {
url: data.url,
};
case DOCUMENT:
return {
url: data.url,
filename: data.filename,
id: data.id,
};
default:
return {};
}
};
/**
* 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);
}
componentDidMount() {
const { onClose, entityType, entity } = this.props;
const { url, urlParams } = getChooserConfig(entityType, entity);
$(document.body).on('hidden.bs.modal', this.onClose);
// eslint-disable-next-line new-cap
this.workflow = global.ModalWorkflow({
url,
urlParams,
responses: {
imageChosen: this.onChosen,
// Discard the first parameter (HTML) to only transmit the data.
embedChosen: (_, data) => this.onChosen(data),
documentChosen: this.onChosen,
pageChosen: this.onChosen,
},
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, entityType, onComplete } = this.props;
const content = editorState.getCurrentContent();
const selection = editorState.getSelection();
const entityData = filterEntityData(entityType, data);
const mutability = MUTABILITY[entityType.type];
const contentWithEntity = content.createEntity(entityType.type, mutability, entityData);
const entityKey = contentWithEntity.getLastCreatedEntityKey();
let nextState;
if (entityType.block) {
// Only supports adding entities at the moment, not editing existing ones.
// See https://github.com/springload/draftail/blob/cdc8988fe2e3ac32374317f535a5338ab97e8637/examples/sources/ImageSource.js#L44-L62.
// See https://github.com/springload/draftail/blob/cdc8988fe2e3ac32374317f535a5338ab97e8637/examples/sources/EmbedSource.js#L64-L91
nextState = AtomicBlockUtils.insertAtomicBlock(editorState, entityKey, ' ');
} else {
// 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, entityKey);
nextState = EditorState.push(editorState, newContent, 'insert-characters');
} else {
nextState = RichUtils.toggleLink(editorState, selection, entityKey);
}
}
// 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,
onComplete: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired,
};
ModalWorkflowSource.defaultProps = {
entity: null,
};
export default ModalWorkflowSource;