Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

86 строки
3.1 KiB

  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. * @flow strict-local
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. declare var __DEV__: boolean;
  13. import type EditorState from "./EditorState";
  14. import type SelectionState from "./SelectionState";
  15. const DraftOffsetKey = require("./DraftOffsetKey");
  16. const nullthrows = require("fbjs/lib/nullthrows");
  17. function getUpdatedSelectionState(editorState: EditorState, anchorKey: string, anchorOffset: number, focusKey: string, focusOffset: number): SelectionState {
  18. const selection: SelectionState = nullthrows(editorState.getSelection());
  19. if (__DEV__) {
  20. if (!anchorKey || !focusKey) {
  21. /* eslint-disable-next-line */
  22. console.warn('Invalid selection state.', arguments, editorState.toJS());
  23. return selection;
  24. }
  25. }
  26. const anchorPath = DraftOffsetKey.decode(anchorKey);
  27. const anchorBlockKey = anchorPath.blockKey;
  28. const anchorLeafBlockTree = editorState.getBlockTree(anchorBlockKey);
  29. const anchorLeaf = anchorLeafBlockTree && anchorLeafBlockTree.getIn([anchorPath.decoratorKey, 'leaves', anchorPath.leafKey]);
  30. const focusPath = DraftOffsetKey.decode(focusKey);
  31. const focusBlockKey = focusPath.blockKey;
  32. const focusLeafBlockTree = editorState.getBlockTree(focusBlockKey);
  33. const focusLeaf = focusLeafBlockTree && focusLeafBlockTree.getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]);
  34. if (!anchorLeaf || !focusLeaf) {
  35. // If we cannot make sense of the updated selection state, stick to the current one.
  36. if (__DEV__) {
  37. /* eslint-disable-next-line */
  38. console.warn('Invalid selection state.', arguments, editorState.toJS());
  39. }
  40. return selection;
  41. }
  42. const anchorLeafStart: number = anchorLeaf.get('start');
  43. const focusLeafStart: number = focusLeaf.get('start');
  44. const anchorBlockOffset = anchorLeaf ? anchorLeafStart + anchorOffset : null;
  45. const focusBlockOffset = focusLeaf ? focusLeafStart + focusOffset : null;
  46. const areEqual = selection.getAnchorKey() === anchorBlockKey && selection.getAnchorOffset() === anchorBlockOffset && selection.getFocusKey() === focusBlockKey && selection.getFocusOffset() === focusBlockOffset;
  47. if (areEqual) {
  48. return selection;
  49. }
  50. let isBackward = false;
  51. if (anchorBlockKey === focusBlockKey) {
  52. const anchorLeafEnd: number = anchorLeaf.get('end');
  53. const focusLeafEnd: number = focusLeaf.get('end');
  54. if (focusLeafStart === anchorLeafStart && focusLeafEnd === anchorLeafEnd) {
  55. isBackward = focusOffset < anchorOffset;
  56. } else {
  57. isBackward = focusLeafStart < anchorLeafStart;
  58. }
  59. } else {
  60. const startKey = editorState.getCurrentContent().getBlockMap().keySeq().skipUntil(v => v === anchorBlockKey || v === focusBlockKey).first();
  61. isBackward = startKey === focusBlockKey;
  62. }
  63. return selection.merge({
  64. anchorKey: anchorBlockKey,
  65. anchorOffset: anchorBlockOffset,
  66. focusKey: focusBlockKey,
  67. focusOffset: focusBlockOffset,
  68. isBackward
  69. });
  70. }
  71. module.exports = getUpdatedSelectionState;