UNPKG

1 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
13function isSelectionAtLeafStart(editorState) {
14 var selection = editorState.getSelection();
15 var anchorKey = selection.getAnchorKey();
16 var blockTree = editorState.getBlockTree(anchorKey);
17 var offset = selection.getStartOffset();
18 var isAtStart = false;
19 blockTree.some(function (leafSet) {
20 if (offset === leafSet.get('start')) {
21 isAtStart = true;
22 return true;
23 }
24
25 if (offset < leafSet.get('end')) {
26 return leafSet.get('leaves').some(function (leaf) {
27 var leafStart = leaf.get('start');
28
29 if (offset === leafStart) {
30 isAtStart = true;
31 return true;
32 }
33
34 return false;
35 });
36 }
37
38 return false;
39 });
40 return isAtStart;
41}
42
43module.exports = isSelectionAtLeafStart;
\No newline at end of file