Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

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