Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

getVisibleSelectionRect.js 1.3 KiB

před 3 roky
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 getRangeBoundingClientRect = require("./getRangeBoundingClientRect");
  13. /**
  14. * Return the bounding ClientRect for the visible DOM selection, if any.
  15. * In cases where there are no selected ranges or the bounding rect is
  16. * temporarily invalid, return null.
  17. *
  18. * When using from an iframe, you should pass the iframe window object
  19. */
  20. function getVisibleSelectionRect(global) {
  21. var selection = global.getSelection();
  22. if (!selection.rangeCount) {
  23. return null;
  24. }
  25. var range = selection.getRangeAt(0);
  26. var boundingRect = getRangeBoundingClientRect(range);
  27. var top = boundingRect.top,
  28. right = boundingRect.right,
  29. bottom = boundingRect.bottom,
  30. left = boundingRect.left; // When a re-render leads to a node being removed, the DOM selection will
  31. // temporarily be placed on an ancestor node, which leads to an invalid
  32. // bounding rect. Discard this state.
  33. if (top === 0 && right === 0 && bottom === 0 && left === 0) {
  34. return null;
  35. }
  36. return boundingRect;
  37. }
  38. module.exports = getVisibleSelectionRect;