This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
DraftEditorDragHandler.js
159 lines (138 loc) · 4.06 KB
/
DraftEditorDragHandler.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
/**
* 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 DraftEditorDragHandler
* @format
* @flow
*/
'use strict';
import type DraftEditor from 'DraftEditor.react';
import type SelectionState from 'SelectionState';
const DataTransfer = require('DataTransfer');
const DraftModifier = require('DraftModifier');
const EditorState = require('EditorState');
const findAncestorOffsetKey = require('findAncestorOffsetKey');
const getTextContentFromFiles = require('getTextContentFromFiles');
const getUpdatedSelectionState = require('getUpdatedSelectionState');
const isEventHandled = require('isEventHandled');
const nullthrows = require('nullthrows');
/**
* Get a SelectionState for the supplied mouse event.
*/
function getSelectionForEvent(
event: Object,
editorState: EditorState,
): ?SelectionState {
let node: ?Node = null;
let offset: ?number = null;
/* $FlowFixMe(>=0.68.0 site=www,mobile) This comment suppresses an error
* found when Flow v0.68 was deployed. To see the error delete this comment
* and run Flow. */
if (typeof document.caretRangeFromPoint === 'function') {
var dropRange = document.caretRangeFromPoint(event.x, event.y);
node = dropRange.startContainer;
offset = dropRange.startOffset;
} else if (event.rangeParent) {
node = event.rangeParent;
offset = event.rangeOffset;
} else {
return null;
}
node = nullthrows(node);
offset = nullthrows(offset);
const offsetKey = nullthrows(findAncestorOffsetKey(node));
return getUpdatedSelectionState(
editorState,
offsetKey,
offset,
offsetKey,
offset,
);
}
var DraftEditorDragHandler = {
/**
* Drag originating from input terminated.
*/
onDragEnd: function(editor: DraftEditor): void {
editor.exitCurrentMode();
},
/**
* Handle data being dropped.
*/
onDrop: function(editor: DraftEditor, e: Object): void {
const data = new DataTransfer(e.nativeEvent.dataTransfer);
const editorState: EditorState = editor._latestEditorState;
const dropSelection: ?SelectionState = getSelectionForEvent(
e.nativeEvent,
editorState,
);
e.preventDefault();
editor.exitCurrentMode();
if (dropSelection == null) {
return;
}
const files = data.getFiles();
if (files.length > 0) {
if (
editor.props.handleDroppedFiles &&
isEventHandled(editor.props.handleDroppedFiles(dropSelection, files))
) {
return;
}
getTextContentFromFiles(files, fileText => {
fileText &&
editor.update(
insertTextAtSelection(editorState, dropSelection, fileText),
);
});
return;
}
const dragType = editor._internalDrag ? 'internal' : 'external';
if (
editor.props.handleDrop &&
isEventHandled(editor.props.handleDrop(dropSelection, data, dragType))
) {
return;
}
if (editor._internalDrag) {
editor.update(moveText(editorState, dropSelection));
return;
}
editor.update(
insertTextAtSelection(editorState, dropSelection, data.getText()),
);
},
};
function moveText(
editorState: EditorState,
targetSelection: SelectionState,
): EditorState {
const newContentState = DraftModifier.moveText(
editorState.getCurrentContent(),
editorState.getSelection(),
targetSelection,
);
return EditorState.push(editorState, newContentState, 'insert-fragment');
}
/**
* Insert text at a specified selection.
*/
function insertTextAtSelection(
editorState: EditorState,
selection: SelectionState,
text: string,
): EditorState {
const newContentState = DraftModifier.insertText(
editorState.getCurrentContent(),
selection,
text,
editorState.getCurrentInlineStyle(),
);
return EditorState.push(editorState, newContentState, 'insert-fragment');
}
module.exports = DraftEditorDragHandler;