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
/
editOnSelect.js
72 lines (64 loc) · 2.09 KB
/
editOnSelect.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
/**
* 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 editOnSelect
* @format
* @flow
*/
'use strict';
import type DraftEditor from 'DraftEditor.react';
const DraftJsDebugLogging = require('DraftJsDebugLogging');
var EditorState = require('EditorState');
var ReactDOM = require('ReactDOM');
var getDraftEditorSelection = require('getDraftEditorSelection');
const invariant = require('invariant');
function editOnSelect(editor: DraftEditor): void {
if (
editor._blockSelectEvents ||
editor._latestEditorState !== editor.props.editorState
) {
if (editor._blockSelectEvents) {
const editorState = editor.props.editorState;
const selectionState = editorState.getSelection();
DraftJsDebugLogging.logBlockedSelectionEvent({
// For now I don't think we need any other info
anonymizedDom: 'N/A',
extraParams: JSON.stringify({stacktrace: new Error().stack}),
selectionState: JSON.stringify(selectionState.toJS()),
});
}
return;
}
var editorState = editor.props.editorState;
const editorNode = ReactDOM.findDOMNode(editor.editorContainer);
invariant(editorNode, 'Missing editorNode');
invariant(
editorNode.firstChild instanceof HTMLElement,
'editorNode.firstChild is not an HTMLElement',
);
var documentSelection = getDraftEditorSelection(
editorState,
editorNode.firstChild,
);
var updatedSelectionState = documentSelection.selectionState;
if (updatedSelectionState !== editorState.getSelection()) {
if (documentSelection.needsRecovery) {
editorState = EditorState.forceSelection(
editorState,
updatedSelectionState,
);
} else {
editorState = EditorState.acceptSelection(
editorState,
updatedSelectionState,
);
}
editor.update(editorState);
}
}
module.exports = editOnSelect;