25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

55 satır
1.6 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. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. var warning = require("fbjs/lib/warning");
  13. /**
  14. * Given a collapsed selection, move the focus `maxDistance` backward within
  15. * the selected block. If the selection will go beyond the start of the block,
  16. * move focus to the end of the previous block, but no further.
  17. *
  18. * This function is not Unicode-aware, so surrogate pairs will be treated
  19. * as having length 2.
  20. */
  21. function moveSelectionBackward(editorState, maxDistance) {
  22. var selection = editorState.getSelection(); // Should eventually make this an invariant
  23. process.env.NODE_ENV !== "production" ? warning(selection.isCollapsed(), 'moveSelectionBackward should only be called with a collapsed SelectionState') : void 0;
  24. var content = editorState.getCurrentContent();
  25. var key = selection.getStartKey();
  26. var offset = selection.getStartOffset();
  27. var focusKey = key;
  28. var focusOffset = 0;
  29. if (maxDistance > offset) {
  30. var keyBefore = content.getKeyBefore(key);
  31. if (keyBefore == null) {
  32. focusKey = key;
  33. } else {
  34. focusKey = keyBefore;
  35. var blockBefore = content.getBlockForKey(keyBefore);
  36. focusOffset = blockBefore.getText().length;
  37. }
  38. } else {
  39. focusOffset = offset - maxDistance;
  40. }
  41. return selection.merge({
  42. focusKey: focusKey,
  43. focusOffset: focusOffset,
  44. isBackward: true
  45. });
  46. }
  47. module.exports = moveSelectionBackward;