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.
 
 
 
 

69 rader
1.8 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 getRangeClientRects = require("./getRangeClientRects");
  13. /**
  14. * Like range.getBoundingClientRect() but normalizes for browser bugs.
  15. */
  16. function getRangeBoundingClientRect(range) {
  17. // "Return a DOMRect object describing the smallest rectangle that includes
  18. // the first rectangle in list and all of the remaining rectangles of which
  19. // the height or width is not zero."
  20. // http://www.w3.org/TR/cssom-view/#dom-range-getboundingclientrect
  21. var rects = getRangeClientRects(range);
  22. var top = 0;
  23. var right = 0;
  24. var bottom = 0;
  25. var left = 0;
  26. if (rects.length) {
  27. // If the first rectangle has 0 width, we use the second, this is needed
  28. // because Chrome renders a 0 width rectangle when the selection contains
  29. // a line break.
  30. if (rects.length > 1 && rects[0].width === 0) {
  31. var _rects$ = rects[1];
  32. top = _rects$.top;
  33. right = _rects$.right;
  34. bottom = _rects$.bottom;
  35. left = _rects$.left;
  36. } else {
  37. var _rects$2 = rects[0];
  38. top = _rects$2.top;
  39. right = _rects$2.right;
  40. bottom = _rects$2.bottom;
  41. left = _rects$2.left;
  42. }
  43. for (var ii = 1; ii < rects.length; ii++) {
  44. var rect = rects[ii];
  45. if (rect.height !== 0 && rect.width !== 0) {
  46. top = Math.min(top, rect.top);
  47. right = Math.max(right, rect.right);
  48. bottom = Math.max(bottom, rect.bottom);
  49. left = Math.min(left, rect.left);
  50. }
  51. }
  52. }
  53. return {
  54. top: top,
  55. right: right,
  56. bottom: bottom,
  57. left: left,
  58. width: right - left,
  59. height: bottom - top
  60. };
  61. }
  62. module.exports = getRangeBoundingClientRect;