You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SelectionState.js 3.8 KiB

3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  13. var Immutable = require("immutable");
  14. var Record = Immutable.Record;
  15. var defaultRecord = {
  16. anchorKey: '',
  17. anchorOffset: 0,
  18. focusKey: '',
  19. focusOffset: 0,
  20. isBackward: false,
  21. hasFocus: false
  22. };
  23. /* $FlowFixMe This comment suppresses an error found when automatically adding
  24. * a type annotation with the codemod Komodo/Annotate_exports. To see the error
  25. * delete this comment and run Flow. */
  26. var SelectionStateRecord = Record(defaultRecord);
  27. var SelectionState =
  28. /*#__PURE__*/
  29. function (_SelectionStateRecord) {
  30. _inheritsLoose(SelectionState, _SelectionStateRecord);
  31. function SelectionState() {
  32. return _SelectionStateRecord.apply(this, arguments) || this;
  33. }
  34. var _proto = SelectionState.prototype;
  35. _proto.serialize = function serialize() {
  36. return 'Anchor: ' + this.getAnchorKey() + ':' + this.getAnchorOffset() + ', ' + 'Focus: ' + this.getFocusKey() + ':' + this.getFocusOffset() + ', ' + 'Is Backward: ' + String(this.getIsBackward()) + ', ' + 'Has Focus: ' + String(this.getHasFocus());
  37. };
  38. _proto.getAnchorKey = function getAnchorKey() {
  39. return this.get('anchorKey');
  40. };
  41. _proto.getAnchorOffset = function getAnchorOffset() {
  42. return this.get('anchorOffset');
  43. };
  44. _proto.getFocusKey = function getFocusKey() {
  45. return this.get('focusKey');
  46. };
  47. _proto.getFocusOffset = function getFocusOffset() {
  48. return this.get('focusOffset');
  49. };
  50. _proto.getIsBackward = function getIsBackward() {
  51. return this.get('isBackward');
  52. };
  53. _proto.getHasFocus = function getHasFocus() {
  54. return this.get('hasFocus');
  55. }
  56. /**
  57. * Return whether the specified range overlaps with an edge of the
  58. * SelectionState.
  59. */
  60. ;
  61. _proto.hasEdgeWithin = function hasEdgeWithin(blockKey, start, end) {
  62. var anchorKey = this.getAnchorKey();
  63. var focusKey = this.getFocusKey();
  64. if (anchorKey === focusKey && anchorKey === blockKey) {
  65. var selectionStart = this.getStartOffset();
  66. var selectionEnd = this.getEndOffset();
  67. return start <= selectionStart && selectionStart <= end || // selectionStart is between start and end, or
  68. start <= selectionEnd && selectionEnd <= end // selectionEnd is between start and end
  69. ;
  70. }
  71. if (blockKey !== anchorKey && blockKey !== focusKey) {
  72. return false;
  73. }
  74. var offsetToCheck = blockKey === anchorKey ? this.getAnchorOffset() : this.getFocusOffset();
  75. return start <= offsetToCheck && end >= offsetToCheck;
  76. };
  77. _proto.isCollapsed = function isCollapsed() {
  78. return this.getAnchorKey() === this.getFocusKey() && this.getAnchorOffset() === this.getFocusOffset();
  79. };
  80. _proto.getStartKey = function getStartKey() {
  81. return this.getIsBackward() ? this.getFocusKey() : this.getAnchorKey();
  82. };
  83. _proto.getStartOffset = function getStartOffset() {
  84. return this.getIsBackward() ? this.getFocusOffset() : this.getAnchorOffset();
  85. };
  86. _proto.getEndKey = function getEndKey() {
  87. return this.getIsBackward() ? this.getAnchorKey() : this.getFocusKey();
  88. };
  89. _proto.getEndOffset = function getEndOffset() {
  90. return this.getIsBackward() ? this.getAnchorOffset() : this.getFocusOffset();
  91. };
  92. SelectionState.createEmpty = function createEmpty(key) {
  93. return new SelectionState({
  94. anchorKey: key,
  95. anchorOffset: 0,
  96. focusKey: key,
  97. focusOffset: 0,
  98. isBackward: false,
  99. hasFocus: false
  100. });
  101. };
  102. return SelectionState;
  103. }(SelectionStateRecord);
  104. module.exports = SelectionState;