Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

50 wiersze
1.3 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
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. import type { FakeClientRect } from "./getRangeBoundingClientRect";
  13. const getRangeBoundingClientRect = require("./getRangeBoundingClientRect");
  14. /**
  15. * Return the bounding ClientRect for the visible DOM selection, if any.
  16. * In cases where there are no selected ranges or the bounding rect is
  17. * temporarily invalid, return null.
  18. *
  19. * When using from an iframe, you should pass the iframe window object
  20. */
  21. function getVisibleSelectionRect(global: any): ?FakeClientRect {
  22. const selection = global.getSelection();
  23. if (!selection.rangeCount) {
  24. return null;
  25. }
  26. const range = selection.getRangeAt(0);
  27. const boundingRect = getRangeBoundingClientRect(range);
  28. const {
  29. top,
  30. right,
  31. bottom,
  32. left
  33. } = boundingRect; // When a re-render leads to a node being removed, the DOM selection will
  34. // temporarily be placed on an ancestor node, which leads to an invalid
  35. // bounding rect. Discard this state.
  36. if (top === 0 && right === 0 && bottom === 0 && left === 0) {
  37. return null;
  38. }
  39. return boundingRect;
  40. }
  41. module.exports = getVisibleSelectionRect;