56 lines
1.6 KiB
Plaintext
56 lines
1.6 KiB
Plaintext
/**
|
|
* 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 moveSelectionBackward
|
|
* @format
|
|
* @flow
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import type EditorState from './EditorState';
|
|
import type SelectionState from './SelectionState';
|
|
|
|
/**
|
|
* Given a collapsed selection, move the focus `maxDistance` backward within
|
|
* the selected block. If the selection will go beyond the start of the block,
|
|
* move focus to the end of the previous block, but no further.
|
|
*
|
|
* This function is not Unicode-aware, so surrogate pairs will be treated
|
|
* as having length 2.
|
|
*/
|
|
function moveSelectionBackward(editorState: EditorState, maxDistance: number): SelectionState {
|
|
var selection = editorState.getSelection();
|
|
var content = editorState.getCurrentContent();
|
|
var key = selection.getStartKey();
|
|
var offset = selection.getStartOffset();
|
|
|
|
var focusKey = key;
|
|
var focusOffset = 0;
|
|
|
|
if (maxDistance > offset) {
|
|
var keyBefore = content.getKeyBefore(key);
|
|
if (keyBefore == null) {
|
|
focusKey = key;
|
|
} else {
|
|
focusKey = keyBefore;
|
|
var blockBefore = content.getBlockForKey(keyBefore);
|
|
focusOffset = blockBefore.getText().length;
|
|
}
|
|
} else {
|
|
focusOffset = offset - maxDistance;
|
|
}
|
|
|
|
return selection.merge({
|
|
focusKey,
|
|
focusOffset,
|
|
isBackward: true
|
|
});
|
|
}
|
|
|
|
module.exports = moveSelectionBackward; |