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

58 строки
1.7 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. import type EditorState from "./EditorState";
  13. import type SelectionState from "./SelectionState";
  14. const warning = require("fbjs/lib/warning");
  15. /**
  16. * Given a collapsed selection, move the focus `maxDistance` backward within
  17. * the selected block. If the selection will go beyond the start of the block,
  18. * move focus to the end of the previous block, but no further.
  19. *
  20. * This function is not Unicode-aware, so surrogate pairs will be treated
  21. * as having length 2.
  22. */
  23. function moveSelectionBackward(editorState: EditorState, maxDistance: number): SelectionState {
  24. const selection = editorState.getSelection(); // Should eventually make this an invariant
  25. warning(selection.isCollapsed(), 'moveSelectionBackward should only be called with a collapsed SelectionState');
  26. const content = editorState.getCurrentContent();
  27. const key = selection.getStartKey();
  28. const offset = selection.getStartOffset();
  29. let focusKey = key;
  30. let focusOffset = 0;
  31. if (maxDistance > offset) {
  32. const keyBefore = content.getKeyBefore(key);
  33. if (keyBefore == null) {
  34. focusKey = key;
  35. } else {
  36. focusKey = keyBefore;
  37. const blockBefore = content.getBlockForKey(keyBefore);
  38. focusOffset = blockBefore.getText().length;
  39. }
  40. } else {
  41. focusOffset = offset - maxDistance;
  42. }
  43. return selection.merge({
  44. focusKey,
  45. focusOffset,
  46. isBackward: true
  47. });
  48. }
  49. module.exports = moveSelectionBackward;