76 lines
2.6 KiB
JavaScript
76 lines
2.6 KiB
JavaScript
/**
|
|
* 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 getUpdatedSelectionState
|
|
* @format
|
|
*
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var DraftOffsetKey = require('./DraftOffsetKey');
|
|
|
|
var nullthrows = require('fbjs/lib/nullthrows');
|
|
|
|
function getUpdatedSelectionState(editorState, anchorKey, anchorOffset, focusKey, focusOffset) {
|
|
var selection = nullthrows(editorState.getSelection());
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
if (!anchorKey || !focusKey) {
|
|
/*eslint-disable no-console */
|
|
console.warn('Invalid selection state.', arguments, editorState.toJS());
|
|
/*eslint-enable no-console */
|
|
return selection;
|
|
}
|
|
}
|
|
|
|
var anchorPath = DraftOffsetKey.decode(anchorKey);
|
|
var anchorBlockKey = anchorPath.blockKey;
|
|
var anchorLeaf = editorState.getBlockTree(anchorBlockKey).getIn([anchorPath.decoratorKey, 'leaves', anchorPath.leafKey]);
|
|
|
|
var focusPath = DraftOffsetKey.decode(focusKey);
|
|
var focusBlockKey = focusPath.blockKey;
|
|
var focusLeaf = editorState.getBlockTree(focusBlockKey).getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]);
|
|
|
|
var anchorLeafStart = anchorLeaf.get('start');
|
|
var focusLeafStart = focusLeaf.get('start');
|
|
|
|
var anchorBlockOffset = anchorLeaf ? anchorLeafStart + anchorOffset : null;
|
|
var focusBlockOffset = focusLeaf ? focusLeafStart + focusOffset : null;
|
|
|
|
var areEqual = selection.getAnchorKey() === anchorBlockKey && selection.getAnchorOffset() === anchorBlockOffset && selection.getFocusKey() === focusBlockKey && selection.getFocusOffset() === focusBlockOffset;
|
|
|
|
if (areEqual) {
|
|
return selection;
|
|
}
|
|
|
|
var isBackward = false;
|
|
if (anchorBlockKey === focusBlockKey) {
|
|
var anchorLeafEnd = anchorLeaf.get('end');
|
|
var focusLeafEnd = focusLeaf.get('end');
|
|
if (focusLeafStart === anchorLeafStart && focusLeafEnd === anchorLeafEnd) {
|
|
isBackward = focusOffset < anchorOffset;
|
|
} else {
|
|
isBackward = focusLeafStart < anchorLeafStart;
|
|
}
|
|
} else {
|
|
var startKey = editorState.getCurrentContent().getBlockMap().keySeq().skipUntil(function (v) {
|
|
return v === anchorBlockKey || v === focusBlockKey;
|
|
}).first();
|
|
isBackward = startKey === focusBlockKey;
|
|
}
|
|
|
|
return selection.merge({
|
|
anchorKey: anchorBlockKey,
|
|
anchorOffset: anchorBlockOffset,
|
|
focusKey: focusBlockKey,
|
|
focusOffset: focusBlockOffset,
|
|
isBackward: isBackward
|
|
});
|
|
}
|
|
|
|
module.exports = getUpdatedSelectionState; |