forked from facebookarchive/draft-js
/
editOnPaste.js
266 lines (235 loc) · 8.03 KB
/
editOnPaste.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
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule editOnPaste
* @flow
*/
'use strict';
var BlockMapBuilder = require('BlockMapBuilder');
var CharacterMetadata = require('CharacterMetadata');
var DataTransfer = require('DataTransfer');
var DraftModifier = require('DraftModifier');
var DraftPasteProcessor = require('DraftPasteProcessor');
var EditorState = require('EditorState');
var getEntityKeyForSelection = require('getEntityKeyForSelection');
var getTextContentFromFiles = require('getTextContentFromFiles');
var splitTextIntoTextBlocks = require('splitTextIntoTextBlocks');
import type {BlockMap} from 'BlockMap';
const isEventHandled = require('isEventHandled');
const ReactDOM = require('ReactDOM');
const UserAgent = require('UserAgent');
const doesNotSupportHTMLFromClipboard =
UserAgent.isBrowser('IE')
|| UserAgent.isBrowser('Edge')
|| UserAgent.isBrowser('Safari');
/**
* Paste content.
*/
function editOnPaste(e: SyntheticClipboardEvent): void {
var data = new DataTransfer(e.clipboardData);
// Get files, unless this is likely to be a string the user wants inline.
if (!data.isRichText()) {
var files = data.getFiles();
var defaultFileText = data.getText();
if (files.length > 0) {
e.preventDefault();
// Allow customized paste handling for images, etc. Otherwise, fall
// through to insert text contents into the editor.
if (
this.props.handlePastedFiles &&
isEventHandled(this.props.handlePastedFiles(files))
) {
return;
}
getTextContentFromFiles(files, (/*string*/ fileText) => {
fileText = fileText || defaultFileText;
if (!fileText) {
return;
}
var {editorState} = this.props;
var blocks = splitTextIntoTextBlocks(fileText);
var character = CharacterMetadata.create({
style: editorState.getCurrentInlineStyle(),
entity: getEntityKeyForSelection(
editorState.getCurrentContent(),
editorState.getSelection()
),
});
var text = DraftPasteProcessor.processText(blocks, character);
var fragment = BlockMapBuilder.createFromArray(text);
var withInsertedText = DraftModifier.replaceWithFragment(
editorState.getCurrentContent(),
editorState.getSelection(),
fragment
);
this.update(
EditorState.push(
editorState,
withInsertedText,
'insert-fragment'
)
);
});
return;
}
}
const text = data.getText();
let html = data.getHTML();
if (html && text && html.replace(/\r\n/g, '\n') == text) html = null;
// Some browsers (IE/Edge) not support getting HTML from the clipboard,
// but it is possible to get the HTML
// if we allow native paste behaviour to occur.
// To do so, we take the following steps:
// - Create a new (off-screen) contenteditable DOM element
// and redirect focus to it.
// - Let native paste happen in the focused element.
// - Grab the HTML.
// - Remove the extra contenteditable.
// - Handle the pasted text in the normal way.
if (doesNotSupportHTMLFromClipboard) {
let contentContainer = ReactDOM
.findDOMNode(this)
.getElementsByClassName('public-DraftEditor-content')[0];
let clone = contentContainer.cloneNode();
clone.setAttribute('class', '');
clone.setAttribute(
'style',
'position: fixed; left: -9999px');
document.body.appendChild(clone);
clone.focus();
this.setRenderGuard();
this.setMode('paste');
// Let native paste behaviour occur, then get what was pasted from the DOM.
setTimeout(() => {
html = clone.innerHTML;
clone.parentNode.removeChild(clone);
this.exitCurrentMode();
this.removeRenderGuard();
this.focus();
handlePastedText.call(this, data, text, html);
}, 0);
} else {
e.preventDefault();
handlePastedText.call(this, data, text, html);
}
}
function handlePastedText(
data: DataTransfer,
text: ?string,
html: ?string
): void {
if (
this.props.handlePastedText &&
isEventHandled(this.props.handlePastedText(text, html))
) {
return;
}
let textBlocks: Array<string> = [];
if (text) {
textBlocks = splitTextIntoTextBlocks(text);
}
if (!this.props.stripPastedStyles) {
// If the text from the paste event is rich content that matches what we
// already have on the internal clipboard, assume that we should just use
// the clipboard fragment for the paste. This will allow us to preserve
// styling and entities, if any are present. Note that newlines are
// stripped during comparison -- this is because copy/paste within the
// editor in Firefox and IE will not include empty lines. The resulting
// paste will preserve the newlines correctly.
const internalClipboard = this.getClipboard();
if (data.isRichText() && html && internalClipboard) {
if (
// If the editorKey is present in the pasted HTML, it should be safe to
// assume this is an internal paste.
html.indexOf(this.getEditorKey()) !== -1 ||
// The copy may have been made within a single block, in which case the
// editor key won't be part of the paste. In this case, just check
// whether the pasted text matches the internal clipboard.
(
textBlocks.length === 1 &&
internalClipboard.size === 1 &&
internalClipboard.first().getText() === text
)
) {
this.update(
insertFragment(this.props.editorState, internalClipboard)
);
return;
}
} else if (
internalClipboard &&
data.types.includes('com.apple.webarchive') &&
!data.types.includes('text/html') &&
areTextBlocksAndClipboardEqual(textBlocks, internalClipboard)
) {
// Safari does not properly store text/html in some cases.
// Use the internalClipboard if present and equal to what is on
// the clipboard. See https://bugs.webkit.org/show_bug.cgi?id=19893.
this.update(
insertFragment(this.props.editorState, internalClipboard)
);
return;
}
// If there is html paste data, try to parse that.
if (html) {
var htmlFragment = DraftPasteProcessor.processHTML(
html,
this.props.blockRenderMap
);
if (htmlFragment) {
var htmlMap = BlockMapBuilder.createFromArray(htmlFragment);
this.update(insertFragment(this.props.editorState, htmlMap));
return;
}
}
// Otherwise, create a new fragment from our pasted text. Also
// empty the internal clipboard, since it's no longer valid.
this.setClipboard(null);
}
if (textBlocks) {
var {editorState} = this.props;
var character = CharacterMetadata.create({
style: editorState.getCurrentInlineStyle(),
entity: getEntityKeyForSelection(
editorState.getCurrentContent(),
editorState.getSelection()
),
});
var textFragment = DraftPasteProcessor.processText(
textBlocks,
character
);
var textMap = BlockMapBuilder.createFromArray(textFragment);
this.update(insertFragment(this.props.editorState, textMap));
}
}
function insertFragment(
editorState: EditorState,
fragment: BlockMap
): EditorState {
var newContent = DraftModifier.replaceWithFragment(
editorState.getCurrentContent(),
editorState.getSelection(),
fragment
);
return EditorState.push(
editorState,
newContent,
'insert-fragment'
);
}
function areTextBlocksAndClipboardEqual(
textBlocks: Array<string>,
blockMap: BlockMap
): boolean {
return (
textBlocks.length === blockMap.size &&
blockMap.valueSeq().every((block, ii) => block.getText() === textBlocks[ii])
);
}
module.exports = editOnPaste;