Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

getRangeClientRects.js.flow 2.0 KiB

3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 UserAgent = require("fbjs/lib/UserAgent");
  13. const invariant = require("fbjs/lib/invariant");
  14. const isChrome = UserAgent.isBrowser('Chrome'); // In Chrome, the client rects will include the entire bounds of all nodes that
  15. // begin (have a start tag) within the selection, even if the selection does
  16. // not overlap the entire node. To resolve this, we split the range at each
  17. // start tag and join the client rects together.
  18. // https://code.google.com/p/chromium/issues/detail?id=324437
  19. /* eslint-disable consistent-return */
  20. function getRangeClientRectsChrome(range: Range): Array<ClientRect> {
  21. const tempRange = range.cloneRange();
  22. const clientRects = [];
  23. for (let ancestor = range.endContainer; ancestor != null; ancestor = ancestor.parentNode) {
  24. // If we've climbed up to the common ancestor, we can now use the
  25. // original start point and stop climbing the tree.
  26. const atCommonAncestor = ancestor === range.commonAncestorContainer;
  27. if (atCommonAncestor) {
  28. tempRange.setStart(range.startContainer, range.startOffset);
  29. } else {
  30. tempRange.setStart(tempRange.endContainer, 0);
  31. }
  32. const rects = Array.from(tempRange.getClientRects());
  33. clientRects.push(rects);
  34. if (atCommonAncestor) {
  35. clientRects.reverse();
  36. return [].concat(...clientRects);
  37. }
  38. tempRange.setEndBefore(ancestor);
  39. }
  40. invariant(false, 'Found an unexpected detached subtree when getting range client rects.');
  41. }
  42. /* eslint-enable consistent-return */
  43. /**
  44. * Like range.getClientRects() but normalizes for browser bugs.
  45. */
  46. const getRangeClientRects = (isChrome ? getRangeClientRectsChrome : function (range: Range): Array<ClientRect> {
  47. return Array.from(range.getClientRects());
  48. }: (range: Range) => Array<ClientRect>);
  49. module.exports = getRangeClientRects;