UNPKG

1.75 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 *
9 * @emails oncall+draft_js
10 */
11'use strict';
12
13var EditorState = require("./EditorState");
14
15var expandRangeToStartOfLine = require("./expandRangeToStartOfLine");
16
17var getDraftEditorSelectionWithNodes = require("./getDraftEditorSelectionWithNodes");
18
19var moveSelectionBackward = require("./moveSelectionBackward");
20
21var removeTextWithStrategy = require("./removeTextWithStrategy");
22
23function keyCommandBackspaceToStartOfLine(editorState, e) {
24 var afterRemoval = removeTextWithStrategy(editorState, function (strategyState) {
25 var selection = strategyState.getSelection();
26
27 if (selection.isCollapsed() && selection.getAnchorOffset() === 0) {
28 return moveSelectionBackward(strategyState, 1);
29 }
30
31 var ownerDocument = e.currentTarget.ownerDocument;
32 var domSelection = ownerDocument.defaultView.getSelection(); // getRangeAt can technically throw if there's no selection, but we know
33 // there is one here because text editor has focus (the cursor is a
34 // selection of length 0). Therefore, we don't need to wrap this in a
35 // try-catch block.
36
37 var range = domSelection.getRangeAt(0);
38 range = expandRangeToStartOfLine(range);
39 return getDraftEditorSelectionWithNodes(strategyState, null, range.endContainer, range.endOffset, range.startContainer, range.startOffset).selectionState;
40 }, 'backward');
41
42 if (afterRemoval === editorState.getCurrentContent()) {
43 return editorState;
44 }
45
46 return EditorState.push(editorState, afterRemoval, 'remove-range');
47}
48
49module.exports = keyCommandBackspaceToStartOfLine;
\No newline at end of file