Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

getRangeBoundingClientRect.js.flow 1.9 KiB

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 strict-local
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. const getRangeClientRects = require("./getRangeClientRects");
  13. export type FakeClientRect = {
  14. left: number,
  15. width: number,
  16. right: number,
  17. top: number,
  18. bottom: number,
  19. height: number,
  20. ...
  21. };
  22. /**
  23. * Like range.getBoundingClientRect() but normalizes for browser bugs.
  24. */
  25. function getRangeBoundingClientRect(range: Range): FakeClientRect {
  26. // "Return a DOMRect object describing the smallest rectangle that includes
  27. // the first rectangle in list and all of the remaining rectangles of which
  28. // the height or width is not zero."
  29. // http://www.w3.org/TR/cssom-view/#dom-range-getboundingclientrect
  30. const rects = getRangeClientRects(range);
  31. let top = 0;
  32. let right = 0;
  33. let bottom = 0;
  34. let left = 0;
  35. if (rects.length) {
  36. // If the first rectangle has 0 width, we use the second, this is needed
  37. // because Chrome renders a 0 width rectangle when the selection contains
  38. // a line break.
  39. if (rects.length > 1 && rects[0].width === 0) {
  40. ({
  41. top,
  42. right,
  43. bottom,
  44. left
  45. } = rects[1]);
  46. } else {
  47. ({
  48. top,
  49. right,
  50. bottom,
  51. left
  52. } = rects[0]);
  53. }
  54. for (let ii = 1; ii < rects.length; ii++) {
  55. const rect = rects[ii];
  56. if (rect.height !== 0 && rect.width !== 0) {
  57. top = Math.min(top, rect.top);
  58. right = Math.max(right, rect.right);
  59. bottom = Math.max(bottom, rect.bottom);
  60. left = Math.min(left, rect.left);
  61. }
  62. }
  63. }
  64. return {
  65. top,
  66. right,
  67. bottom,
  68. left,
  69. width: right - left,
  70. height: bottom - top
  71. };
  72. }
  73. module.exports = getRangeBoundingClientRect;