-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
import * as Figma from "figma-js";
import { CSSResultArray, LitElement } from "lit";
export type SizedNode = Extract<Figma.Node, { absoluteBoundingBox: any }>;
export interface Point2D {
x: number;
y: number;
}
export type DistanceGuide = {
/**
* Solid line
*/
points: [Point2D, Point2D];
/**
* Dashed line
*/
bisector?: [Point2D, Point2D];
};
interface AbsRect {
/**
* min y of the rect.
* y of the top line.
*/
top: number;
/**
* max x of the rect.
* x of the right line.
*/
right: number;
/**
* max y of the rect.
* y of the bottom line.
*/
bottom: number;
/**
* min x of the rect.
* x of the left line.
*/
left: number;
}
function absRect(rect: Figma.Rect): AbsRect {
return {
top: rect.y,
right: rect.x + rect.width,
bottom: rect.y + rect.height,
left: rect.x,
};
}
export function getDistanceGuides(
selected: Figma.Rect,
compared: Figma.Rect
): readonly DistanceGuide[] {
const a = absRect(selected);
const b = absRect(compared);
const isYIntersecting = !(a.top > b.bottom || a.bottom < b.top);
const isXIntersecting = !(a.left > b.right || a.right < b.left);
// Rects are intersecting.
if (isXIntersecting && isYIntersecting) {
// Center of intersecting region.
const intersectCenter: Point2D = {
x: (Math.max(a.left, b.left) + Math.min(a.right, b.right)) / 2,
y: (Math.max(a.top, b.top) + Math.min(a.bottom, b.bottom)) / 2,
};
return [
{
points: [
{ x: a.left, y: intersectCenter.y },
{ x: b.left, y: intersectCenter.y },
],
},
{
points: [
{
x: a.right,
y: intersectCenter.y,
},
{ x: b.right, y: intersectCenter.y },
],
},
{
points: [
{ y: a.top, x: intersectCenter.x },
{ y: b.top, x: intersectCenter.x },
],
},
{
points: [
{
y: a.bottom,
x: intersectCenter.x,
},
{ y: b.bottom, x: intersectCenter.x },
],
},
];
}
const isALeft = a.left > b.right;
const isABelow = a.top > b.bottom;
const selectedCenter: Point2D = {
x: selected.x + selected.width / 2,
y: selected.y + selected.height / 2,
};
const guides: readonly (DistanceGuide | null)[] = [
!isXIntersecting
? {
points: [
{ x: isALeft ? a.left : a.right, y: selectedCenter.y },
{ x: isALeft ? b.right : b.left, y: selectedCenter.y },
],
bisector: !isYIntersecting
? [
{ x: isALeft ? b.right : b.left, y: selectedCenter.y },
{
x: isALeft ? b.right : b.left,
y: isABelow ? b.bottom : b.top,
},
]
: void 0,
}
: null,
!isYIntersecting
? {
points: [
{ y: isABelow ? a.top : a.bottom, x: selectedCenter.x },
{ y: isABelow ? b.bottom : b.top, x: selectedCenter.x },
],
bisector: !isXIntersecting
? [
{ y: isABelow ? b.bottom : b.top, x: selectedCenter.x },
{
y: isABelow ? b.bottom : b.top,
x: isALeft ? b.right : b.left,
},
]
: void 0,
}
: null,
];
return guides.filter((x): x is DistanceGuide => !!x);
}
/**
* x.xxxxx... -> x.xx
*/
export function round(n: number) {
return Math.round(n * 100) / 100;
}
/**
* Utility type for creating constructor type from an interface.
* @example
* function FooMixin<T extends Constructor<LitElement>>(Base: T): T & Constructor<MixinInterface> {
* // ...
* }
*/
export type Constructor<T> = new (...args: any[]) => T;
export function extendStyles(
left: typeof LitElement.styles,
right: typeof LitElement.styles
): CSSResultArray {
return [...stylesToArray(left), ...stylesToArray(right)];
}
function stylesToArray(styles: typeof LitElement.styles): CSSResultArray {
if (!styles) {
return [];
}
if (styles instanceof Array) {
return styles;
}
return [styles];
}