@zag-js/rect-utils
Advanced tools
Comparing version 0.0.0-dev-20220704151659 to 0.0.0-dev-20220704163406
@@ -0,3 +1,3 @@ | ||
import { Rect } from "./rect"; | ||
import type { Point } from "./types"; | ||
import { Rect } from "./rect"; | ||
export declare function getRectFromPoints(...pts: Point[]): Rect; |
@@ -0,5 +1,5 @@ | ||
import { Rect } from "./rect"; | ||
import type { Point } from "./types"; | ||
import { Rect } from "./rect"; | ||
export declare function toRad(d: number): number; | ||
export declare function rotate(a: Point, d: number, c: Point): Point; | ||
export declare function getRotationRect(r: Rect, deg: number): Rect; |
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __export = (target, all) => { | ||
@@ -23,3 +40,2 @@ for (var name in all) | ||
__export(src_exports, { | ||
Rect: () => Rect, | ||
alignRect: () => alignRect, | ||
@@ -33,2 +49,3 @@ closest: () => closest, | ||
containsRect: () => containsRect, | ||
createRect: () => createRect, | ||
debugPolygon: () => debugPolygon, | ||
@@ -42,2 +59,5 @@ distance: () => distance, | ||
getElementRect: () => getElementRect, | ||
getRectCenters: () => getRectCenters, | ||
getRectCorners: () => getRectCorners, | ||
getRectEdges: () => getRectEdges, | ||
getRectFromPoints: () => getRectFromPoints, | ||
@@ -50,2 +70,3 @@ getRotationRect: () => getRotationRect, | ||
intersects: () => intersects, | ||
isRect: () => isRect, | ||
isSymmetric: () => isSymmetric, | ||
@@ -64,27 +85,37 @@ rotate: () => rotate, | ||
let x = ref.minX; | ||
if (h === "left-inside") | ||
if (h === "left-inside") { | ||
x = ref.minX; | ||
if (h === "left-outside") | ||
} | ||
if (h === "left-outside") { | ||
x = ref.minX - ref.width; | ||
if (h === "right-inside") | ||
} | ||
if (h === "right-inside") { | ||
x = ref.maxX - ref.width; | ||
if (h === "right-outside") | ||
} | ||
if (h === "right-outside") { | ||
x = ref.maxX; | ||
if (h === "center") | ||
} | ||
if (h === "center") { | ||
x = ref.midX - ref.width / 2; | ||
return a.clone().set({ x }); | ||
} | ||
return __spreadProps(__spreadValues({}, a), { x }); | ||
} | ||
function vAlign(a, ref, v) { | ||
let y = ref.minY; | ||
if (v === "top-inside") | ||
if (v === "top-inside") { | ||
y = ref.minY; | ||
if (v === "top-outside") | ||
} | ||
if (v === "top-outside") { | ||
y = ref.minY - a.height; | ||
if (v === "bottom-inside") | ||
} | ||
if (v === "bottom-inside") { | ||
y = ref.maxY - a.height; | ||
if (v === "bottom-outside") | ||
} | ||
if (v === "bottom-outside") { | ||
y = ref.maxY; | ||
if (v === "center") | ||
} | ||
if (v === "center") { | ||
y = ref.midY - a.height / 2; | ||
return a.clone().set({ y }); | ||
} | ||
return __spreadProps(__spreadValues({}, a), { y }); | ||
} | ||
@@ -96,73 +127,45 @@ function alignRect(a, ref, options) { | ||
// ../core/dist/index.mjs | ||
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop); | ||
// src/rect.ts | ||
var point = (x, y) => ({ x, y }); | ||
var Rect = class { | ||
constructor(v) { | ||
this.v = v; | ||
} | ||
static create(v) { | ||
return new Rect(v); | ||
} | ||
set(n) { | ||
return new Rect(Object.assign({}, this.v, n)); | ||
} | ||
clone() { | ||
return new Rect(this.v); | ||
} | ||
get x() { | ||
return this.v.x; | ||
} | ||
get y() { | ||
return this.v.y; | ||
} | ||
get width() { | ||
return this.v.width; | ||
} | ||
get height() { | ||
return this.v.height; | ||
} | ||
get minX() { | ||
return this.v.x; | ||
} | ||
get midX() { | ||
return this.v.x + this.v.width / 2; | ||
} | ||
get maxX() { | ||
return this.v.x + this.v.width; | ||
} | ||
get minY() { | ||
return this.v.y; | ||
} | ||
get midY() { | ||
return this.v.y + this.v.height / 2; | ||
} | ||
get maxY() { | ||
return this.v.y + this.v.height; | ||
} | ||
get center() { | ||
return point(this.midX, this.midY); | ||
} | ||
get centers() { | ||
const top = point(this.midX, this.minY); | ||
const right = point(this.maxX, this.midY); | ||
const bottom = point(this.midX, this.maxY); | ||
const left = point(this.minX, this.midY); | ||
return { top, right, bottom, left }; | ||
} | ||
get corners() { | ||
const top = point(this.minX, this.minY); | ||
const right = point(this.maxX, this.minY); | ||
const bottom = point(this.maxX, this.maxY); | ||
const left = point(this.minX, this.maxY); | ||
return { top, right, bottom, left }; | ||
} | ||
get edges() { | ||
const c = this.corners; | ||
const top = [c.top, c.right]; | ||
const right = [c.right, c.bottom]; | ||
const bottom = [c.left, c.bottom]; | ||
const left = [c.top, c.left]; | ||
return { top, right, bottom, left }; | ||
} | ||
}; | ||
function createRect(v) { | ||
const midX = v.x + v.width / 2; | ||
const midY = v.y + v.height / 2; | ||
return __spreadProps(__spreadValues({}, v), { | ||
minX: v.x, | ||
minY: v.y, | ||
maxX: v.x + v.width, | ||
maxY: v.y + v.height, | ||
midX, | ||
midY, | ||
center: point(midX, midY) | ||
}); | ||
} | ||
function isRect(v) { | ||
return hasProp(v, "x") && hasProp(v, "y") && hasProp(v, "width") && hasProp(v, "height"); | ||
} | ||
function getRectCenters(v) { | ||
const top = point(v.midX, v.minY); | ||
const right = point(v.maxX, v.midY); | ||
const bottom = point(v.midX, v.maxY); | ||
const left = point(v.minX, v.midY); | ||
return { top, right, bottom, left }; | ||
} | ||
function getRectCorners(v) { | ||
const top = point(v.minX, v.minY); | ||
const right = point(v.maxX, v.minY); | ||
const bottom = point(v.maxX, v.maxY); | ||
const left = point(v.minX, v.maxY); | ||
return { top, right, bottom, left }; | ||
} | ||
function getRectEdges(v) { | ||
const c = getRectCorners(v); | ||
const top = [c.top, c.right]; | ||
const right = [c.right, c.bottom]; | ||
const bottom = [c.left, c.bottom]; | ||
const left = [c.top, c.left]; | ||
return { top, right, bottom, left }; | ||
} | ||
@@ -178,3 +181,3 @@ // src/intersection.ts | ||
const y2 = Math.min(a.y + a.height, b.y + b.height); | ||
return Rect.create({ x, y, width: x2 - x, height: y2 - y }); | ||
return createRect({ x, y, width: x2 - x, height: y2 - y }); | ||
} | ||
@@ -238,10 +241,14 @@ function collisions(a, b) { | ||
function closestSideToRect(ref, r) { | ||
if (r.maxX <= ref.minX) | ||
if (r.maxX <= ref.minX) { | ||
return "left"; | ||
if (r.minX >= ref.maxX) | ||
} | ||
if (r.minX >= ref.maxX) { | ||
return "right"; | ||
if (r.maxY <= ref.minY) | ||
} | ||
if (r.maxY <= ref.minY) { | ||
return "top"; | ||
if (r.minY >= ref.maxY) | ||
} | ||
if (r.minY >= ref.maxY) { | ||
return "bottom"; | ||
} | ||
return "left"; | ||
@@ -276,6 +283,6 @@ } | ||
function containsRect(a, b) { | ||
return Object.values(b.corners).every((c) => containsPoint(a, c)); | ||
return Object.values(getRectCorners(b)).every((c) => containsPoint(a, c)); | ||
} | ||
function contains(r, v) { | ||
return v instanceof Rect ? containsRect(r, v) : containsPoint(r, v); | ||
return isRect(v) ? containsRect(r, v) : containsPoint(r, v); | ||
} | ||
@@ -286,4 +293,4 @@ | ||
const g = globalThis; | ||
g.__styleCache__ = g.__styleCache__ || /* @__PURE__ */ new WeakMap(); | ||
return g.__styleCache__; | ||
g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap(); | ||
return g.__styleCache; | ||
} | ||
@@ -306,3 +313,3 @@ function getComputedStyle(el) { | ||
function getElementRect(el, opts = {}) { | ||
return Rect.create(getClientRect(el, opts)); | ||
return createRect(getClientRect(el, opts)); | ||
} | ||
@@ -342,3 +349,3 @@ function getClientRect(el, opts = {}) { | ||
const height = Math.max(...ys) - y; | ||
return Rect.create({ x, y, width, height }); | ||
return createRect({ x, y, width, height }); | ||
} | ||
@@ -365,14 +372,13 @@ | ||
if (rects.length) { | ||
rs = rs.concat(rects.map((r) => Rect.create(r))); | ||
} else { | ||
let start = range.startContainer; | ||
if (start.nodeType === Node.TEXT_NODE) { | ||
start = start.parentNode; | ||
} | ||
if (start instanceof HTMLElement) { | ||
let r = getElementRect(start); | ||
r.clone().set({ x: r.maxX, width: 0 }); | ||
rs.push(r); | ||
} | ||
rs = rs.concat(rects.map(createRect)); | ||
return union.apply(void 0, rs); | ||
} | ||
let start = range.startContainer; | ||
if (start.nodeType === Node.TEXT_NODE) { | ||
start = start.parentNode; | ||
} | ||
if (start instanceof HTMLElement) { | ||
const r = getElementRect(start); | ||
rs.push(__spreadProps(__spreadValues({}, r), { x: r.maxX, width: 0 })); | ||
} | ||
return union.apply(void 0, rs); | ||
@@ -391,6 +397,9 @@ } | ||
const y = a.y - c.y; | ||
return { x: c.x + x * cos - y * sin, y: c.y + x * sin + y * cos }; | ||
return { | ||
x: c.x + x * cos - y * sin, | ||
y: c.y + x * sin + y * cos | ||
}; | ||
} | ||
function getRotationRect(r, deg) { | ||
const rr = Object.values(r.corners).map((p) => rotate(p, deg, r.center)); | ||
const rr = Object.values(getRectCorners(r)).map((p) => rotate(p, deg, r.center)); | ||
const xs = rr.map((p) => p.x); | ||
@@ -402,3 +411,8 @@ const ys = rr.map((p) => p.y); | ||
const maxY = Math.max(...ys); | ||
return Rect.create({ x: minX, y: minY, width: maxX - minX, height: maxY - minY }); | ||
return createRect({ | ||
x: minX, | ||
y: minY, | ||
width: maxX - minX, | ||
height: maxY - minY | ||
}); | ||
} | ||
@@ -408,3 +422,3 @@ | ||
function getWindowRect(win, opts = {}) { | ||
return Rect.create(getViewportRect(win, opts)); | ||
return createRect(getViewportRect(win, opts)); | ||
} | ||
@@ -431,3 +445,8 @@ function getViewportRect(win, opts) { | ||
const { top = 0, right = 0, bottom = 0, left = 0 } = v; | ||
return Rect.create({ x: r.x + left, y: r.y + top, width: r.width - left - right, height: r.height - top - bottom }); | ||
return createRect({ | ||
x: r.x + left, | ||
y: r.y + top, | ||
width: r.width - left - right, | ||
height: r.height - top - bottom | ||
}); | ||
} | ||
@@ -444,3 +463,8 @@ function expand(r, v) { | ||
const { x = 0, y = 0 } = o; | ||
return Rect.create({ x: r.x + x, y: r.y + y, width: r.width, height: r.height }); | ||
return createRect({ | ||
x: r.x + x, | ||
y: r.y + y, | ||
width: r.width, | ||
height: r.height | ||
}); | ||
} | ||
@@ -447,0 +471,0 @@ |
import type { RectEdge, RectValue } from "./types"; | ||
export declare class Rect { | ||
private v; | ||
static create(v: RectValue): Rect; | ||
protected constructor(v: RectValue); | ||
set(n: Partial<RectValue>): Rect; | ||
clone(): Rect; | ||
get x(): number; | ||
get y(): number; | ||
get width(): number; | ||
get height(): number; | ||
get minX(): number; | ||
get midX(): number; | ||
get maxX(): number; | ||
get minY(): number; | ||
get midY(): number; | ||
get maxY(): number; | ||
get center(): { | ||
export declare function createRect(v: RectValue): { | ||
minX: number; | ||
minY: number; | ||
maxX: number; | ||
maxY: number; | ||
midX: number; | ||
midY: number; | ||
center: { | ||
x: number; | ||
y: number; | ||
}; | ||
get centers(): { | ||
top: { | ||
x: number; | ||
y: number; | ||
}; | ||
right: { | ||
x: number; | ||
y: number; | ||
}; | ||
bottom: { | ||
x: number; | ||
y: number; | ||
}; | ||
left: { | ||
x: number; | ||
y: number; | ||
}; | ||
x: number; | ||
y: number; | ||
width: number; | ||
height: number; | ||
}; | ||
export declare type Rect = ReturnType<typeof createRect>; | ||
export declare function isRect(v: any): v is Rect; | ||
export declare function getRectCenters(v: Rect): { | ||
top: { | ||
x: number; | ||
y: number; | ||
}; | ||
get corners(): { | ||
top: { | ||
x: number; | ||
y: number; | ||
}; | ||
right: { | ||
x: number; | ||
y: number; | ||
}; | ||
bottom: { | ||
x: number; | ||
y: number; | ||
}; | ||
left: { | ||
x: number; | ||
y: number; | ||
}; | ||
right: { | ||
x: number; | ||
y: number; | ||
}; | ||
get edges(): { | ||
top: RectEdge; | ||
right: RectEdge; | ||
bottom: RectEdge; | ||
left: RectEdge; | ||
bottom: { | ||
x: number; | ||
y: number; | ||
}; | ||
} | ||
left: { | ||
x: number; | ||
y: number; | ||
}; | ||
}; | ||
export declare function getRectCorners(v: Rect): { | ||
top: { | ||
x: number; | ||
y: number; | ||
}; | ||
right: { | ||
x: number; | ||
y: number; | ||
}; | ||
bottom: { | ||
x: number; | ||
y: number; | ||
}; | ||
left: { | ||
x: number; | ||
y: number; | ||
}; | ||
}; | ||
export declare function getRectEdges(v: Rect): { | ||
top: RectEdge; | ||
right: RectEdge; | ||
bottom: RectEdge; | ||
left: RectEdge; | ||
}; |
{ | ||
"name": "@zag-js/rect-utils", | ||
"version": "0.0.0-dev-20220704151659", | ||
"version": "0.0.0-dev-20220704163406", | ||
"description": "", | ||
@@ -28,3 +28,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@zag-js/dom-utils": "0.0.0-dev-20220704151659", | ||
"@zag-js/dom-utils": "0.0.0-dev-20220704163406", | ||
"@zag-js/utils": "0.1.2" | ||
@@ -31,0 +31,0 @@ }, |
Sorry, the diff of this file is not supported yet
34791
1140
+ Added@zag-js/dom-utils@0.0.0-dev-20220704163406(transitive)
+ Added@zag-js/types@0.0.0-dev-20220704163406(transitive)
- Removed@zag-js/dom-utils@0.0.0-dev-20220704151659(transitive)
- Removed@zag-js/types@0.0.0-dev-20220704151659(transitive)