@v-c/trigger 0.0.9 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,39 +1,64 @@
1
- import { isDOM as Ye } from "@v-c/util/dist/Dom/findDOMNode";
2
- import Pe from "@v-c/util/dist/Dom/isVisible";
3
- import { rafDebounce as Be } from "@v-c/util/dist/raf";
4
- import { ref as Bt, shallowRef as Me, computed as y, watch as Mt, nextTick as G, watchEffect as Oe } from "vue";
5
- import { collectScroller as Ve, getWin as Ce, getVisibleArea as Ot, toNum as Vt } from "../util.js";
6
- function Ct(o, a = 0) {
7
- const f = `${a}`, c = f.match(/^(.*)\%$/);
8
- return c ? o * (parseFloat(c[1]) / 100) : parseFloat(f);
1
+ import { isDOM } from "@v-c/util/dist/Dom/findDOMNode";
2
+ import isVisible from "@v-c/util/dist/Dom/isVisible";
3
+ import { rafDebounce } from "@v-c/util/dist/raf";
4
+ import { reactive, shallowRef, computed, ref, watch, nextTick, watchEffect, toRefs } from "vue";
5
+ import { collectScroller, getWin, getVisibleArea, toNum } from "../util.js";
6
+ function getUnitOffset(size, offset = 0) {
7
+ const offsetStr = `${offset}`;
8
+ const cells = offsetStr.match(/^(.*)\%$/);
9
+ if (cells) {
10
+ return size * (parseFloat(cells[1]) / 100);
11
+ }
12
+ return parseFloat(offsetStr);
9
13
  }
10
- function Lt(o, a) {
11
- const [f, c] = a || [];
14
+ function getNumberOffset(rect, offset) {
15
+ const [offsetX, offsetY] = offset || [];
12
16
  return [
13
- Ct(o.width, f),
14
- Ct(o.height, c)
17
+ getUnitOffset(rect.width, offsetX),
18
+ getUnitOffset(rect.height, offsetY)
15
19
  ];
16
20
  }
17
- function It(o = "") {
18
- return [o[0], o[1]];
21
+ function splitPoints(points = "") {
22
+ return [points[0], points[1]];
19
23
  }
20
- function V(o, a) {
21
- const f = a[0], c = a[1];
22
- let d, X;
23
- return f === "t" ? X = o.y : f === "b" ? X = o.y + o.height : X = o.y + o.height / 2, c === "l" ? d = o.x : c === "r" ? d = o.x + o.width : d = o.x + o.width / 2, { x: d, y: X };
24
+ function getAlignPoint(rect, points) {
25
+ const topBottom = points[0];
26
+ const leftRight = points[1];
27
+ let x;
28
+ let y;
29
+ if (topBottom === "t") {
30
+ y = rect.y;
31
+ } else if (topBottom === "b") {
32
+ y = rect.y + rect.height;
33
+ } else {
34
+ y = rect.y + rect.height / 2;
35
+ }
36
+ if (leftRight === "l") {
37
+ x = rect.x;
38
+ } else if (leftRight === "r") {
39
+ x = rect.x + rect.width;
40
+ } else {
41
+ x = rect.x + rect.width / 2;
42
+ }
43
+ return { x, y };
24
44
  }
25
- function w(o, a) {
26
- const f = {
45
+ function reversePoints(points, index) {
46
+ const reverseMap = {
27
47
  t: "b",
28
48
  b: "t",
29
49
  l: "r",
30
50
  r: "l"
31
51
  };
32
- return o.map((c, d) => d === a ? f[c] || "c" : c).join("");
52
+ return points.map((point, i) => {
53
+ if (i === index) {
54
+ return reverseMap[point] || "c";
55
+ }
56
+ return point;
57
+ }).join("");
33
58
  }
34
- function $e(o, a, f, c, d, X, Tt, D) {
35
- const h = Bt({
36
- ready: !1,
59
+ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAlign, onPopupAlign, mobile) {
60
+ const offsetInfo = reactive({
61
+ ready: false,
37
62
  offsetX: 0,
38
63
  offsetY: 0,
39
64
  offsetR: 0,
@@ -42,217 +67,433 @@ function $e(o, a, f, c, d, X, Tt, D) {
42
67
  arrowY: 0,
43
68
  scaleX: 1,
44
69
  scaleY: 1,
45
- align: d.value[c.value] || {}
46
- }), _ = Me(0), J = y(() => !a.value || D?.value ? [] : Ve(a.value)), p = Bt({}), Ft = () => {
47
- p.value = {};
48
- }, St = Be(() => {
49
- if (a.value && f.value && o.value && !D?.value) {
50
- let u = function(t, v, g = rt) {
51
- const Yt = s.x + t, Pt = s.y + v, xe = Yt + P, be = Pt + Y, we = Math.max(Yt, g.left), Ae = Math.max(Pt, g.top), Re = Math.min(xe, g.right), Xe = Math.min(be, g.bottom);
52
- return Math.max(0, (Re - we) * (Xe - Ae));
53
- }, Z = function() {
54
- F = s.y + r, H = F + Y, S = s.x + l, E = S + P;
70
+ align: builtinPlacements.value[placement.value] || {}
71
+ });
72
+ const alignCountRef = shallowRef(0);
73
+ const scrollerList = computed(() => {
74
+ if (!popupEle.value || mobile?.value) {
75
+ return [];
76
+ }
77
+ return collectScroller(popupEle.value);
78
+ });
79
+ const prevFlipRef = ref({});
80
+ const resetFlipCache = () => {
81
+ prevFlipRef.value = {};
82
+ };
83
+ const _onAlign = () => {
84
+ if (popupEle.value && target.value && open.value && !mobile?.value) {
85
+ let getIntersectionVisibleArea = function(offsetX2, offsetY2, area = visibleArea) {
86
+ const l = popupRect.x + offsetX2;
87
+ const t = popupRect.y + offsetY2;
88
+ const r = l + popupWidth;
89
+ const b = t + popupHeight;
90
+ const visibleL = Math.max(l, area.left);
91
+ const visibleT = Math.max(t, area.top);
92
+ const visibleR = Math.min(r, area.right);
93
+ const visibleB = Math.min(b, area.bottom);
94
+ return Math.max(0, (visibleR - visibleL) * (visibleB - visibleT));
95
+ }, syncNextPopupPosition = function() {
96
+ nextPopupY = popupRect.y + nextOffsetY;
97
+ nextPopupBottom = nextPopupY + popupHeight;
98
+ nextPopupX = popupRect.x + nextOffsetX;
99
+ nextPopupRight = nextPopupX + popupWidth;
55
100
  };
56
- const e = a.value, tt = e.ownerDocument, et = Ce(e), { position: Nt } = et.getComputedStyle(e), jt = e.style.left, Wt = e.style.top, $t = e.style.right, Ht = e.style.bottom, Et = e.style.overflow, C = {
57
- ...d.value[c.value],
58
- ...X?.value
59
- }, A = tt.createElement("div");
60
- e.parentElement?.appendChild(A), A.style.left = `${e.offsetLeft}px`, A.style.top = `${e.offsetTop}px`, A.style.position = Nt, A.style.height = `${e.offsetHeight}px`, A.style.width = `${e.offsetWidth}px`, e.style.left = "0", e.style.top = "0", e.style.right = "auto", e.style.bottom = "auto", e.style.overflow = "hidden";
61
- let i;
62
- if (Array.isArray(f.value))
63
- i = {
64
- x: f.value[0],
65
- y: f.value[1],
101
+ const popupElement = popupEle.value;
102
+ const doc = popupElement.ownerDocument;
103
+ const win = getWin(popupElement);
104
+ const { position: popupPosition } = win.getComputedStyle(popupElement);
105
+ const originLeft = popupElement.style.left;
106
+ const originTop = popupElement.style.top;
107
+ const originRight = popupElement.style.right;
108
+ const originBottom = popupElement.style.bottom;
109
+ const originOverflow = popupElement.style.overflow;
110
+ const placementInfo = {
111
+ ...builtinPlacements.value[placement.value],
112
+ ...popupAlign?.value
113
+ };
114
+ const placeholderElement = doc.createElement("div");
115
+ popupElement.parentElement?.appendChild(placeholderElement);
116
+ placeholderElement.style.left = `${popupElement.offsetLeft}px`;
117
+ placeholderElement.style.top = `${popupElement.offsetTop}px`;
118
+ placeholderElement.style.position = popupPosition;
119
+ placeholderElement.style.height = `${popupElement.offsetHeight}px`;
120
+ placeholderElement.style.width = `${popupElement.offsetWidth}px`;
121
+ popupElement.style.left = "0";
122
+ popupElement.style.top = "0";
123
+ popupElement.style.right = "auto";
124
+ popupElement.style.bottom = "auto";
125
+ popupElement.style.overflow = "hidden";
126
+ let targetRect;
127
+ if (Array.isArray(target.value)) {
128
+ targetRect = {
129
+ x: target.value[0],
130
+ y: target.value[1],
66
131
  width: 0,
67
132
  height: 0
68
133
  };
69
- else {
70
- const t = f.value.getBoundingClientRect();
71
- t.x = t.x ?? t.left, t.y = t.y ?? t.top, i = {
72
- x: t.x,
73
- y: t.y,
74
- width: t.width,
75
- height: t.height
134
+ } else {
135
+ const rect = target.value.getBoundingClientRect();
136
+ rect.x = rect.x ?? rect.left;
137
+ rect.y = rect.y ?? rect.top;
138
+ targetRect = {
139
+ x: rect.x,
140
+ y: rect.y,
141
+ width: rect.width,
142
+ height: rect.height
76
143
  };
77
144
  }
78
- const s = e.getBoundingClientRect(), { height: Dt, width: _t } = et.getComputedStyle(e);
79
- s.x = s.x ?? s.left, s.y = s.y ?? s.top;
145
+ const popupRect = popupElement.getBoundingClientRect();
146
+ const { height, width } = win.getComputedStyle(popupElement);
147
+ popupRect.x = popupRect.x ?? popupRect.left;
148
+ popupRect.y = popupRect.y ?? popupRect.top;
80
149
  const {
81
- clientWidth: kt,
82
- clientHeight: Ut,
83
- scrollWidth: qt,
84
- scrollHeight: zt,
85
- scrollTop: ot,
86
- scrollLeft: nt
87
- } = tt.documentElement, Y = s.height, P = s.width, L = i.height, I = i.width, Gt = {
150
+ clientWidth,
151
+ clientHeight,
152
+ scrollWidth,
153
+ scrollHeight,
154
+ scrollTop,
155
+ scrollLeft
156
+ } = doc.documentElement;
157
+ const popupHeight = popupRect.height;
158
+ const popupWidth = popupRect.width;
159
+ const targetHeight = targetRect.height;
160
+ const targetWidth = targetRect.width;
161
+ const visibleRegion = {
88
162
  left: 0,
89
163
  top: 0,
90
- right: kt,
91
- bottom: Ut
92
- }, Jt = {
93
- left: -nt,
94
- top: -ot,
95
- right: qt - nt,
96
- bottom: zt - ot
164
+ right: clientWidth,
165
+ bottom: clientHeight
166
+ };
167
+ const scrollRegion = {
168
+ left: -scrollLeft,
169
+ top: -scrollTop,
170
+ right: scrollWidth - scrollLeft,
171
+ bottom: scrollHeight - scrollTop
97
172
  };
98
- let { htmlRegion: T } = C;
99
- const st = "visible", it = "visibleFirst";
100
- T !== "scroll" && T !== it && (T = st);
101
- const lt = T === it, Kt = Ot(Jt, J.value), n = Ot(Gt, J.value), rt = T === st ? n : Kt, W = lt ? n : rt;
102
- e.style.left = "auto", e.style.top = "auto", e.style.right = "0", e.style.bottom = "0";
103
- const ft = e.getBoundingClientRect();
104
- e.style.left = jt, e.style.top = Wt, e.style.right = $t, e.style.bottom = Ht, e.style.overflow = Et, e.parentElement?.removeChild(A);
105
- const B = Vt(
106
- Math.round(P / parseFloat(_t) * 1e3) / 1e3
107
- ), M = Vt(
108
- Math.round(Y / parseFloat(Dt) * 1e3) / 1e3
173
+ let { htmlRegion } = placementInfo;
174
+ const VISIBLE = "visible";
175
+ const VISIBLE_FIRST = "visibleFirst";
176
+ if (htmlRegion !== "scroll" && htmlRegion !== VISIBLE_FIRST) {
177
+ htmlRegion = VISIBLE;
178
+ }
179
+ const isVisibleFirst = htmlRegion === VISIBLE_FIRST;
180
+ const scrollRegionArea = getVisibleArea(scrollRegion, scrollerList.value);
181
+ const visibleRegionArea = getVisibleArea(visibleRegion, scrollerList.value);
182
+ const visibleArea = htmlRegion === VISIBLE ? visibleRegionArea : scrollRegionArea;
183
+ const adjustCheckVisibleArea = isVisibleFirst ? visibleRegionArea : visibleArea;
184
+ popupElement.style.left = "auto";
185
+ popupElement.style.top = "auto";
186
+ popupElement.style.right = "0";
187
+ popupElement.style.bottom = "0";
188
+ const popupMirrorRect = popupElement.getBoundingClientRect();
189
+ popupElement.style.left = originLeft;
190
+ popupElement.style.top = originTop;
191
+ popupElement.style.right = originRight;
192
+ popupElement.style.bottom = originBottom;
193
+ popupElement.style.overflow = originOverflow;
194
+ popupElement.parentElement?.removeChild(placeholderElement);
195
+ const scaleX2 = toNum(
196
+ Math.round(popupWidth / parseFloat(width) * 1e3) / 1e3
109
197
  );
110
- if (B === 0 || M === 0 || Ye(f) && !Pe(f))
198
+ const scaleY2 = toNum(
199
+ Math.round(popupHeight / parseFloat(height) * 1e3) / 1e3
200
+ );
201
+ if (scaleX2 === 0 || scaleY2 === 0 || isDOM(target) && !isVisible(target)) {
111
202
  return;
112
- const { offset: Qt, targetOffset: Zt } = C;
113
- let [x, b] = Lt(s, Qt);
114
- const [te, ee] = Lt(
115
- i,
116
- Zt
203
+ }
204
+ const { offset, targetOffset } = placementInfo;
205
+ let [popupOffsetX, popupOffsetY] = getNumberOffset(popupRect, offset);
206
+ const [targetOffsetX, targetOffsetY] = getNumberOffset(
207
+ targetRect,
208
+ targetOffset
209
+ );
210
+ targetRect.x -= targetOffsetX;
211
+ targetRect.y -= targetOffsetY;
212
+ const [popupPoint, targetPoint] = placementInfo.points || [];
213
+ const targetPoints = splitPoints(targetPoint);
214
+ const popupPoints = splitPoints(popupPoint);
215
+ const targetAlignPoint = getAlignPoint(targetRect, targetPoints);
216
+ const popupAlignPoint = getAlignPoint(popupRect, popupPoints);
217
+ const nextAlignInfo = {
218
+ ...placementInfo
219
+ };
220
+ let nextOffsetX = targetAlignPoint.x - popupAlignPoint.x + popupOffsetX;
221
+ let nextOffsetY = targetAlignPoint.y - popupAlignPoint.y + popupOffsetY;
222
+ const originIntersectionVisibleArea = getIntersectionVisibleArea(
223
+ nextOffsetX,
224
+ nextOffsetY
225
+ );
226
+ const originIntersectionRecommendArea = getIntersectionVisibleArea(
227
+ nextOffsetX,
228
+ nextOffsetY,
229
+ visibleRegionArea
117
230
  );
118
- i.x -= te, i.y -= ee;
119
- const [oe, ne] = C.points || [], R = It(ne), m = It(oe), at = V(i, R), ct = V(s, m), O = {
120
- ...C
231
+ const targetAlignPointTL = getAlignPoint(targetRect, ["t", "l"]);
232
+ const popupAlignPointTL = getAlignPoint(popupRect, ["t", "l"]);
233
+ const targetAlignPointBR = getAlignPoint(targetRect, ["b", "r"]);
234
+ const popupAlignPointBR = getAlignPoint(popupRect, ["b", "r"]);
235
+ const overflow = placementInfo.overflow || {};
236
+ const { adjustX, adjustY, shiftX, shiftY } = overflow;
237
+ const supportAdjust = (val) => {
238
+ if (typeof val === "boolean") {
239
+ return val;
240
+ }
241
+ return val >= 0;
121
242
  };
122
- let l = at.x - ct.x + x, r = at.y - ct.y + b;
123
- const ut = u(
124
- l,
125
- r
126
- ), se = u(
127
- l,
128
- r,
129
- n
130
- ), $ = (t, v) => !!(t > ut || lt && t === ut && v >= se), pt = V(i, ["t", "l"]), ht = V(s, ["t", "l"]), gt = V(i, ["b", "r"]), mt = V(s, ["b", "r"]), ie = C.overflow || {}, { adjustX: le, adjustY: re, shiftX: vt, shiftY: yt } = ie, dt = (t) => typeof t == "boolean" ? t : t >= 0;
131
- let F, H, S, E;
132
- Z();
133
- const xt = dt(re), bt = m[0] === R[0];
134
- if (xt && m[0] === "t" && (H > W.bottom || p.value.bt)) {
135
- let t = r;
136
- bt ? t -= Y - L : t = pt.y - mt.y - b;
137
- const v = u(
138
- l,
139
- t
140
- ), g = u(
141
- l,
142
- t,
143
- n
243
+ let nextPopupY;
244
+ let nextPopupBottom;
245
+ let nextPopupX;
246
+ let nextPopupRight;
247
+ syncNextPopupPosition();
248
+ const needAdjustY = supportAdjust(adjustY);
249
+ const sameTB = popupPoints[0] === targetPoints[0];
250
+ if (needAdjustY && popupPoints[0] === "t" && (nextPopupBottom > adjustCheckVisibleArea.bottom || prevFlipRef.value.bt)) {
251
+ let tmpNextOffsetY = nextOffsetY;
252
+ if (sameTB) {
253
+ tmpNextOffsetY -= popupHeight - targetHeight;
254
+ } else {
255
+ tmpNextOffsetY = targetAlignPointTL.y - popupAlignPointBR.y - popupOffsetY;
256
+ }
257
+ const newVisibleArea = getIntersectionVisibleArea(
258
+ nextOffsetX,
259
+ tmpNextOffsetY
260
+ );
261
+ const newVisibleRecommendArea = getIntersectionVisibleArea(
262
+ nextOffsetX,
263
+ tmpNextOffsetY,
264
+ visibleRegionArea
144
265
  );
145
- $(v, g) ? (p.value.bt = !0, r = t, b = -b, O.points = [
146
- w(m, 0),
147
- w(R, 0)
148
- ]) : p.value.bt = !1;
266
+ if (
267
+ // Of course use larger one
268
+ newVisibleArea > originIntersectionVisibleArea || newVisibleArea === originIntersectionVisibleArea && (!isVisibleFirst || newVisibleRecommendArea >= originIntersectionRecommendArea)
269
+ ) {
270
+ prevFlipRef.value.bt = true;
271
+ nextOffsetY = tmpNextOffsetY;
272
+ popupOffsetY = -popupOffsetY;
273
+ nextAlignInfo.points = [
274
+ reversePoints(popupPoints, 0),
275
+ reversePoints(targetPoints, 0)
276
+ ];
277
+ } else {
278
+ prevFlipRef.value.bt = false;
279
+ }
149
280
  }
150
- if (xt && m[0] === "b" && (F < W.top || p.value.tb)) {
151
- let t = r;
152
- bt ? t += Y - L : t = gt.y - ht.y - b;
153
- const v = u(
154
- l,
155
- t
156
- ), g = u(
157
- l,
158
- t,
159
- n
281
+ if (needAdjustY && popupPoints[0] === "b" && (nextPopupY < adjustCheckVisibleArea.top || prevFlipRef.value.tb)) {
282
+ let tmpNextOffsetY = nextOffsetY;
283
+ if (sameTB) {
284
+ tmpNextOffsetY += popupHeight - targetHeight;
285
+ } else {
286
+ tmpNextOffsetY = targetAlignPointBR.y - popupAlignPointTL.y - popupOffsetY;
287
+ }
288
+ const newVisibleArea = getIntersectionVisibleArea(
289
+ nextOffsetX,
290
+ tmpNextOffsetY
160
291
  );
161
- $(v, g) ? (p.value.tb = !0, r = t, b = -b, O.points = [
162
- w(m, 0),
163
- w(R, 0)
164
- ]) : p.value.tb = !1;
292
+ const newVisibleRecommendArea = getIntersectionVisibleArea(
293
+ nextOffsetX,
294
+ tmpNextOffsetY,
295
+ visibleRegionArea
296
+ );
297
+ if (
298
+ // Of course use larger one
299
+ newVisibleArea > originIntersectionVisibleArea || newVisibleArea === originIntersectionVisibleArea && (!isVisibleFirst || newVisibleRecommendArea >= originIntersectionRecommendArea)
300
+ ) {
301
+ prevFlipRef.value.tb = true;
302
+ nextOffsetY = tmpNextOffsetY;
303
+ popupOffsetY = -popupOffsetY;
304
+ nextAlignInfo.points = [
305
+ reversePoints(popupPoints, 0),
306
+ reversePoints(targetPoints, 0)
307
+ ];
308
+ } else {
309
+ prevFlipRef.value.tb = false;
310
+ }
165
311
  }
166
- const wt = dt(le), At = m[1] === R[1];
167
- if (wt && m[1] === "l" && (E > W.right || p.value.rl)) {
168
- let t = l;
169
- At ? t -= P - I : t = pt.x - mt.x - x;
170
- const v = u(
171
- t,
172
- r
173
- ), g = u(
174
- t,
175
- r,
176
- n
312
+ const needAdjustX = supportAdjust(adjustX);
313
+ const sameLR = popupPoints[1] === targetPoints[1];
314
+ if (needAdjustX && popupPoints[1] === "l" && (nextPopupRight > adjustCheckVisibleArea.right || prevFlipRef.value.rl)) {
315
+ let tmpNextOffsetX = nextOffsetX;
316
+ if (sameLR) {
317
+ tmpNextOffsetX -= popupWidth - targetWidth;
318
+ } else {
319
+ tmpNextOffsetX = targetAlignPointTL.x - popupAlignPointBR.x - popupOffsetX;
320
+ }
321
+ const newVisibleArea = getIntersectionVisibleArea(
322
+ tmpNextOffsetX,
323
+ nextOffsetY
324
+ );
325
+ const newVisibleRecommendArea = getIntersectionVisibleArea(
326
+ tmpNextOffsetX,
327
+ nextOffsetY,
328
+ visibleRegionArea
177
329
  );
178
- $(v, g) ? (p.value.rl = !0, l = t, x = -x, O.points = [
179
- w(m, 1),
180
- w(R, 1)
181
- ]) : p.value.rl = !1;
330
+ if (
331
+ // Of course use larger one
332
+ newVisibleArea > originIntersectionVisibleArea || newVisibleArea === originIntersectionVisibleArea && (!isVisibleFirst || newVisibleRecommendArea >= originIntersectionRecommendArea)
333
+ ) {
334
+ prevFlipRef.value.rl = true;
335
+ nextOffsetX = tmpNextOffsetX;
336
+ popupOffsetX = -popupOffsetX;
337
+ nextAlignInfo.points = [
338
+ reversePoints(popupPoints, 1),
339
+ reversePoints(targetPoints, 1)
340
+ ];
341
+ } else {
342
+ prevFlipRef.value.rl = false;
343
+ }
182
344
  }
183
- if (wt && m[1] === "r" && (S < W.left || p.value.lr)) {
184
- let t = l;
185
- At ? t += P - I : t = gt.x - ht.x - x;
186
- const v = u(
187
- t,
188
- r
189
- ), g = u(
190
- t,
191
- r,
192
- n
345
+ if (needAdjustX && popupPoints[1] === "r" && (nextPopupX < adjustCheckVisibleArea.left || prevFlipRef.value.lr)) {
346
+ let tmpNextOffsetX = nextOffsetX;
347
+ if (sameLR) {
348
+ tmpNextOffsetX += popupWidth - targetWidth;
349
+ } else {
350
+ tmpNextOffsetX = targetAlignPointBR.x - popupAlignPointTL.x - popupOffsetX;
351
+ }
352
+ const newVisibleArea = getIntersectionVisibleArea(
353
+ tmpNextOffsetX,
354
+ nextOffsetY
355
+ );
356
+ const newVisibleRecommendArea = getIntersectionVisibleArea(
357
+ tmpNextOffsetX,
358
+ nextOffsetY,
359
+ visibleRegionArea
193
360
  );
194
- $(v, g) ? (p.value.lr = !0, l = t, x = -x, O.points = [
195
- w(m, 1),
196
- w(R, 1)
197
- ]) : p.value.lr = !1;
361
+ if (
362
+ // Of course use larger one
363
+ newVisibleArea > originIntersectionVisibleArea || newVisibleArea === originIntersectionVisibleArea && (!isVisibleFirst || newVisibleRecommendArea >= originIntersectionRecommendArea)
364
+ ) {
365
+ prevFlipRef.value.lr = true;
366
+ nextOffsetX = tmpNextOffsetX;
367
+ popupOffsetX = -popupOffsetX;
368
+ nextAlignInfo.points = [
369
+ reversePoints(popupPoints, 1),
370
+ reversePoints(targetPoints, 1)
371
+ ];
372
+ } else {
373
+ prevFlipRef.value.lr = false;
374
+ }
375
+ }
376
+ syncNextPopupPosition();
377
+ const numShiftX = shiftX === true ? 0 : shiftX;
378
+ if (typeof numShiftX === "number") {
379
+ if (nextPopupX < visibleRegionArea.left) {
380
+ nextOffsetX -= nextPopupX - visibleRegionArea.left - popupOffsetX;
381
+ if (targetRect.x + targetWidth < visibleRegionArea.left + numShiftX) {
382
+ nextOffsetX += targetRect.x - visibleRegionArea.left + targetWidth - numShiftX;
383
+ }
384
+ }
385
+ if (nextPopupRight > visibleRegionArea.right) {
386
+ nextOffsetX -= nextPopupRight - visibleRegionArea.right - popupOffsetX;
387
+ if (targetRect.x > visibleRegionArea.right - numShiftX) {
388
+ nextOffsetX += targetRect.x - visibleRegionArea.right + numShiftX;
389
+ }
390
+ }
391
+ }
392
+ const numShiftY = shiftY === true ? 0 : shiftY;
393
+ if (typeof numShiftY === "number") {
394
+ if (nextPopupY < visibleRegionArea.top) {
395
+ nextOffsetY -= nextPopupY - visibleRegionArea.top - popupOffsetY;
396
+ if (targetRect.y + targetHeight < visibleRegionArea.top + numShiftY) {
397
+ nextOffsetY += targetRect.y - visibleRegionArea.top + targetHeight - numShiftY;
398
+ }
399
+ }
400
+ if (nextPopupBottom > visibleRegionArea.bottom) {
401
+ nextOffsetY -= nextPopupBottom - visibleRegionArea.bottom - popupOffsetY;
402
+ if (targetRect.y > visibleRegionArea.bottom - numShiftY) {
403
+ nextOffsetY += targetRect.y - visibleRegionArea.bottom + numShiftY;
404
+ }
405
+ }
406
+ }
407
+ const popupLeft = popupRect.x + nextOffsetX;
408
+ const popupRight = popupLeft + popupWidth;
409
+ const popupTop = popupRect.y + nextOffsetY;
410
+ const popupBottom = popupTop + popupHeight;
411
+ const targetLeft = targetRect.x;
412
+ const targetRight = targetLeft + targetWidth;
413
+ const targetTop = targetRect.y;
414
+ const targetBottom = targetTop + targetHeight;
415
+ const maxLeft = Math.max(popupLeft, targetLeft);
416
+ const minRight = Math.min(popupRight, targetRight);
417
+ const xCenter = (maxLeft + minRight) / 2;
418
+ const nextArrowX = xCenter - popupLeft;
419
+ const maxTop = Math.max(popupTop, targetTop);
420
+ const minBottom = Math.min(popupBottom, targetBottom);
421
+ const yCenter = (maxTop + minBottom) / 2;
422
+ const nextArrowY = yCenter - popupTop;
423
+ onPopupAlign?.(popupEle.value, nextAlignInfo);
424
+ let offsetX4Right = popupMirrorRect.right - popupRect.x - (nextOffsetX + popupRect.width);
425
+ let offsetY4Bottom = popupMirrorRect.bottom - popupRect.y - (nextOffsetY + popupRect.height);
426
+ if (scaleX2 === 1) {
427
+ nextOffsetX = Math.round(nextOffsetX);
428
+ offsetX4Right = Math.round(offsetX4Right);
198
429
  }
199
- Z();
200
- const N = vt === !0 ? 0 : vt;
201
- typeof N == "number" && (S < n.left && (l -= S - n.left - x, i.x + I < n.left + N && (l += i.x - n.left + I - N)), E > n.right && (l -= E - n.right - x, i.x > n.right - N && (l += i.x - n.right + N)));
202
- const j = yt === !0 ? 0 : yt;
203
- typeof j == "number" && (F < n.top && (r -= F - n.top - b, i.y + L < n.top + j && (r += i.y - n.top + L - j)), H > n.bottom && (r -= H - n.bottom - b, i.y > n.bottom - j && (r += i.y - n.bottom + j)));
204
- const k = s.x + l, fe = k + P, U = s.y + r, ae = U + Y, Rt = i.x, ce = Rt + I, Xt = i.y, ue = Xt + L, pe = Math.max(k, Rt), he = Math.min(fe, ce), ge = (pe + he) / 2 - k, me = Math.max(U, Xt), ve = Math.min(ae, ue), ye = (me + ve) / 2 - U;
205
- Tt?.(a.value, O);
206
- let q = ft.right - s.x - (l + s.width), z = ft.bottom - s.y - (r + s.height);
207
- B === 1 && (l = Math.round(l), q = Math.round(q)), M === 1 && (r = Math.round(r), z = Math.round(z));
208
- const de = {
209
- ready: !0,
210
- offsetX: l / B,
211
- offsetY: r / M,
212
- offsetR: q / B,
213
- offsetB: z / M,
214
- arrowX: ge / B,
215
- arrowY: ye / M,
216
- scaleX: B,
217
- scaleY: M,
218
- align: O
430
+ if (scaleY2 === 1) {
431
+ nextOffsetY = Math.round(nextOffsetY);
432
+ offsetY4Bottom = Math.round(offsetY4Bottom);
433
+ }
434
+ const nextOffsetInfo = {
435
+ ready: true,
436
+ offsetX: nextOffsetX / scaleX2,
437
+ offsetY: nextOffsetY / scaleY2,
438
+ offsetR: offsetX4Right / scaleX2,
439
+ offsetB: offsetY4Bottom / scaleY2,
440
+ arrowX: nextArrowX / scaleX2,
441
+ arrowY: nextArrowY / scaleY2,
442
+ scaleX: scaleX2,
443
+ scaleY: scaleY2,
444
+ align: nextAlignInfo
219
445
  };
220
- h.value = de;
446
+ Object.assign(offsetInfo, nextOffsetInfo);
221
447
  }
222
- }), K = () => {
223
- _.value += 1;
224
- const u = _.value;
448
+ };
449
+ const onAlign = rafDebounce(_onAlign);
450
+ const triggerAlign = () => {
451
+ alignCountRef.value += 1;
452
+ const id = alignCountRef.value;
225
453
  Promise.resolve().then(() => {
226
- _.value === u && St();
454
+ if (alignCountRef.value === id) {
455
+ onAlign();
456
+ }
227
457
  });
228
458
  };
229
- Mt(
230
- a,
231
- async (u) => {
232
- u && o.value && !D?.value && (await G(), K());
459
+ watch(
460
+ popupEle,
461
+ async (ele) => {
462
+ if (ele && open.value && !mobile?.value) {
463
+ await nextTick();
464
+ triggerAlign();
465
+ }
233
466
  }
234
467
  );
235
- const Q = () => {
236
- h.value.ready = !1;
468
+ const resetReady = () => {
469
+ offsetInfo.ready = false;
237
470
  };
238
- return Mt(c, async () => {
239
- await G(), Q();
240
- }), Oe(async () => {
241
- await G(), o.value || (Ft(), Q());
242
- }), [
243
- y(() => h.value?.ready),
244
- y(() => h.value?.offsetX),
245
- y(() => h.value?.offsetY),
246
- y(() => h.value?.offsetR),
247
- y(() => h.value?.offsetB),
248
- y(() => h.value?.arrowX),
249
- y(() => h.value?.arrowY),
250
- y(() => h.value?.scaleX),
251
- y(() => h.value?.scaleY),
252
- y(() => h.value?.align),
253
- K
471
+ watch(placement, async () => {
472
+ await nextTick();
473
+ resetReady();
474
+ });
475
+ watchEffect(async () => {
476
+ if (!open.value) {
477
+ resetFlipCache();
478
+ await nextTick();
479
+ resetReady();
480
+ }
481
+ });
482
+ const { ready, offsetX, offsetR, offsetY, offsetB, align, arrowY, arrowX, scaleY, scaleX } = toRefs(offsetInfo);
483
+ return [
484
+ ready,
485
+ offsetX,
486
+ offsetY,
487
+ offsetR,
488
+ offsetB,
489
+ arrowX,
490
+ arrowY,
491
+ scaleX,
492
+ scaleY,
493
+ align,
494
+ triggerAlign
254
495
  ];
255
496
  }
256
497
  export {
257
- $e as default
498
+ useAlign as default
258
499
  };