bounds.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseDocumentSize = exports.parseBounds = exports.Bounds = void 0;
  4. var Bounds = /** @class */ (function () {
  5. function Bounds(left, top, width, height) {
  6. this.left = left;
  7. this.top = top;
  8. this.width = width;
  9. this.height = height;
  10. }
  11. Bounds.prototype.add = function (x, y, w, h) {
  12. return new Bounds(this.left + x, this.top + y, this.width + w, this.height + h);
  13. };
  14. Bounds.fromClientRect = function (context, clientRect) {
  15. return new Bounds(clientRect.left + context.windowBounds.left, clientRect.top + context.windowBounds.top, clientRect.width, clientRect.height);
  16. };
  17. Bounds.fromDOMRectList = function (context, domRectList) {
  18. var domRect = Array.from(domRectList).find(function (rect) { return rect.width !== 0; });
  19. return domRect
  20. ? new Bounds(domRect.left + context.windowBounds.left, domRect.top + context.windowBounds.top, domRect.width, domRect.height)
  21. : Bounds.EMPTY;
  22. };
  23. Bounds.EMPTY = new Bounds(0, 0, 0, 0);
  24. return Bounds;
  25. }());
  26. exports.Bounds = Bounds;
  27. var parseBounds = function (context, node) {
  28. return Bounds.fromClientRect(context, node.getBoundingClientRect());
  29. };
  30. exports.parseBounds = parseBounds;
  31. var parseDocumentSize = function (document) {
  32. var body = document.body;
  33. var documentElement = document.documentElement;
  34. if (!body || !documentElement) {
  35. throw new Error("Unable to get document size");
  36. }
  37. var width = Math.max(Math.max(body.scrollWidth, documentElement.scrollWidth), Math.max(body.offsetWidth, documentElement.offsetWidth), Math.max(body.clientWidth, documentElement.clientWidth));
  38. var height = Math.max(Math.max(body.scrollHeight, documentElement.scrollHeight), Math.max(body.offsetHeight, documentElement.offsetHeight), Math.max(body.clientHeight, documentElement.clientHeight));
  39. return new Bounds(0, 0, width, height);
  40. };
  41. exports.parseDocumentSize = parseDocumentSize;
  42. //# sourceMappingURL=bounds.js.map