@v-c/trigger 0.0.10 → 0.0.12

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 Re } from "@v-c/util/dist/Dom/findDOMNode";
2
- import Xe from "@v-c/util/dist/Dom/isVisible";
3
- import { rafDebounce as Ye } from "@v-c/util/dist/raf";
4
- import { ref as Pt, shallowRef as Pe, computed as y, watch as Bt, nextTick as Be, watchEffect as Me } from "vue";
5
- import { collectScroller as Oe, getWin as Ve, getVisibleArea as Mt, toNum as Ot } from "../util.js";
6
- function Vt(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 Ct(o, a) {
11
- const [f, c] = a || [];
14
+ function getNumberOffset(rect, offset) {
15
+ const [offsetX, offsetY] = offset || [];
12
16
  return [
13
- Vt(o.width, f),
14
- Vt(o.height, c)
17
+ getUnitOffset(rect.width, offsetX),
18
+ getUnitOffset(rect.height, offsetY)
15
19
  ];
16
20
  }
17
- function Lt(o = "") {
18
- return [o[0], o[1]];
21
+ function splitPoints(points = "") {
22
+ return [points[0], points[1]];
19
23
  }
20
- function C(o, a) {
21
- const f = a[0], c = a[1];
22
- let d, Y;
23
- return f === "t" ? Y = o.y : f === "b" ? Y = o.y + o.height : Y = 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: Y };
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 A(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 We(o, a, f, c, d, Y, It, k) {
35
- const g = Pt({
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,221 +67,433 @@ function We(o, a, f, c, d, Y, It, k) {
42
67
  arrowY: 0,
43
68
  scaleX: 1,
44
69
  scaleY: 1,
45
- align: d.value[c.value] || {}
46
- }), U = Pe(0), K = y(() => !a.value || k?.value ? [] : Oe(a.value)), p = Pt({}), Tt = () => {
47
- p.value = {};
48
- }, Ft = Ye(() => {
49
- if (a.value && f.value && o.value && !k?.value) {
50
- let u = function(t, h, m = rt) {
51
- const Xt = s.x + t, Yt = s.y + h, ye = Xt + B, de = Yt + P, xe = Math.max(Xt, m.left), be = Math.max(Yt, m.top), we = Math.min(ye, m.right), Ae = Math.min(de, m.bottom);
52
- return Math.max(0, (we - xe) * (Ae - be));
53
- }, tt = function() {
54
- j = s.y + r, D = j + P, N = s.x + l, _ = N + B;
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, et = e.ownerDocument, ot = Ve(e), { position: St } = ot.getComputedStyle(e), jt = e.style.left, Nt = e.style.top, Wt = e.style.right, $t = e.style.bottom, Ht = e.style.overflow, L = {
57
- ...d.value[c.value],
58
- ...Y?.value
59
- }, R = et.createElement("div");
60
- e.parentElement?.appendChild(R), R.style.left = `${e.offsetLeft}px`, R.style.top = `${e.offsetTop}px`, R.style.position = St, R.style.height = `${e.offsetHeight}px`, R.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: Et, width: Dt } = ot.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: _t,
82
- clientHeight: kt,
83
- scrollWidth: Ut,
84
- scrollHeight: qt,
85
- scrollTop: nt,
86
- scrollLeft: st
87
- } = et.documentElement, P = s.height, B = s.width, I = i.height, T = i.width, zt = {
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: _t,
91
- bottom: kt
92
- }, Gt = {
93
- left: -st,
94
- top: -nt,
95
- right: Ut - st,
96
- bottom: qt - nt
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: F } = L;
99
- const it = "visible", lt = "visibleFirst";
100
- F !== "scroll" && F !== lt && (F = it);
101
- const S = F === lt, Jt = Mt(Gt, K.value), n = Mt(zt, K.value), rt = F === it ? n : Jt, H = S ? 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 = Nt, e.style.right = Wt, e.style.bottom = $t, e.style.overflow = Ht, e.parentElement?.removeChild(R);
105
- const M = Ot(
106
- Math.round(B / parseFloat(Dt) * 1e3) / 1e3
107
- ), O = Ot(
108
- Math.round(P / parseFloat(Et) * 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 (M === 0 || O === 0 || Re(f) && !Xe(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: Kt, targetOffset: Qt } = L;
113
- let [x, b] = Ct(s, Kt);
114
- const [Zt, te] = Ct(
115
- i,
116
- Qt
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 -= Zt, i.y -= te;
119
- const [ee, oe] = L.points || [], X = Lt(oe), v = Lt(ee), at = C(i, X), ct = C(s, v), V = {
120
- ...L
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 w = u(
124
- l,
125
- r
126
- ), E = u(
127
- l,
128
- r,
129
- n
130
- ), ut = C(i, ["t", "l"]), pt = C(s, ["t", "l"]), ht = C(i, ["b", "r"]), gt = C(s, ["b", "r"]), ne = L.overflow || {}, { adjustX: se, adjustY: ie, shiftX: mt, shiftY: vt } = ne, yt = (t) => typeof t == "boolean" ? t : t >= 0;
131
- let j, D, N, _;
132
- tt();
133
- const dt = yt(ie), xt = v[0] === X[0];
134
- if (dt && v[0] === "t" && (D > H.bottom || p.value.bt)) {
135
- let t = r;
136
- xt ? t -= P - I : t = ut.y - gt.y - b;
137
- const h = u(
138
- l,
139
- t
140
- ), m = 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
- // Of course use larger one
146
- h > w || h === w && (!S || m >= E) ? (p.value.bt = !0, r = t, b = -b, V.points = [
147
- A(v, 0),
148
- A(X, 0)
149
- ]) : 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
+ }
150
280
  }
151
- if (dt && v[0] === "b" && (j < H.top || p.value.tb)) {
152
- let t = r;
153
- xt ? t += P - I : t = ht.y - pt.y - b;
154
- const h = u(
155
- l,
156
- t
157
- ), m = u(
158
- l,
159
- t,
160
- 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
161
291
  );
162
- // Of course use larger one
163
- h > w || h === w && (!S || m >= E) ? (p.value.tb = !0, r = t, b = -b, V.points = [
164
- A(v, 0),
165
- A(X, 0)
166
- ]) : 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
+ }
167
311
  }
168
- const bt = yt(se), wt = v[1] === X[1];
169
- if (bt && v[1] === "l" && (_ > H.right || p.value.rl)) {
170
- let t = l;
171
- wt ? t -= B - T : t = ut.x - gt.x - x;
172
- const h = u(
173
- t,
174
- r
175
- ), m = u(
176
- t,
177
- r,
178
- 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
179
329
  );
180
- // Of course use larger one
181
- h > w || h === w && (!S || m >= E) ? (p.value.rl = !0, l = t, x = -x, V.points = [
182
- A(v, 1),
183
- A(X, 1)
184
- ]) : 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
+ }
185
344
  }
186
- if (bt && v[1] === "r" && (N < H.left || p.value.lr)) {
187
- let t = l;
188
- wt ? t += B - T : t = ht.x - pt.x - x;
189
- const h = u(
190
- t,
191
- r
192
- ), m = u(
193
- t,
194
- r,
195
- 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
196
360
  );
197
- // Of course use larger one
198
- h > w || h === w && (!S || m >= E) ? (p.value.lr = !0, l = t, x = -x, V.points = [
199
- A(v, 1),
200
- A(X, 1)
201
- ]) : 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);
202
429
  }
203
- tt();
204
- const W = mt === !0 ? 0 : mt;
205
- typeof W == "number" && (N < n.left && (l -= N - n.left - x, i.x + T < n.left + W && (l += i.x - n.left + T - W)), _ > n.right && (l -= _ - n.right - x, i.x > n.right - W && (l += i.x - n.right + W)));
206
- const $ = vt === !0 ? 0 : vt;
207
- typeof $ == "number" && (j < n.top && (r -= j - n.top - b, i.y + I < n.top + $ && (r += i.y - n.top + I - $)), D > n.bottom && (r -= D - n.bottom - b, i.y > n.bottom - $ && (r += i.y - n.bottom + $)));
208
- const q = s.x + l, le = q + B, z = s.y + r, re = z + P, At = i.x, fe = At + T, Rt = i.y, ae = Rt + I, ce = Math.max(q, At), ue = Math.min(le, fe), pe = (ce + ue) / 2 - q, he = Math.max(z, Rt), ge = Math.min(re, ae), me = (he + ge) / 2 - z;
209
- It?.(a.value, V);
210
- let G = ft.right - s.x - (l + s.width), J = ft.bottom - s.y - (r + s.height);
211
- M === 1 && (l = Math.round(l), G = Math.round(G)), O === 1 && (r = Math.round(r), J = Math.round(J));
212
- const ve = {
213
- ready: !0,
214
- offsetX: l / M,
215
- offsetY: r / O,
216
- offsetR: G / M,
217
- offsetB: J / O,
218
- arrowX: pe / M,
219
- arrowY: me / O,
220
- scaleX: M,
221
- scaleY: O,
222
- align: V
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
223
445
  };
224
- g.value = ve;
446
+ Object.assign(offsetInfo, nextOffsetInfo);
225
447
  }
226
- }), Q = () => {
227
- U.value += 1;
228
- const u = U.value;
448
+ };
449
+ const onAlign = rafDebounce(_onAlign);
450
+ const triggerAlign = () => {
451
+ alignCountRef.value += 1;
452
+ const id = alignCountRef.value;
229
453
  Promise.resolve().then(() => {
230
- U.value === u && Ft();
454
+ if (alignCountRef.value === id) {
455
+ onAlign();
456
+ }
231
457
  });
232
458
  };
233
- Bt(
234
- a,
235
- async (u) => {
236
- u && o.value && !k?.value && (await Be(), Q());
459
+ watch(
460
+ popupEle,
461
+ async (ele) => {
462
+ if (ele && open.value && !mobile?.value) {
463
+ await nextTick();
464
+ triggerAlign();
465
+ }
237
466
  }
238
467
  );
239
- const Z = () => {
240
- g.value.ready = !1;
468
+ const resetReady = () => {
469
+ offsetInfo.ready = false;
241
470
  };
242
- return Bt(c, () => {
243
- Z();
244
- }), Me(() => {
245
- o.value || (Tt(), Z());
246
- }), [
247
- y(() => g.value?.ready),
248
- y(() => g.value?.offsetX),
249
- y(() => g.value?.offsetY),
250
- y(() => g.value?.offsetR),
251
- y(() => g.value?.offsetB),
252
- y(() => g.value?.arrowX),
253
- y(() => g.value?.arrowY),
254
- y(() => g.value?.scaleX),
255
- y(() => g.value?.scaleY),
256
- y(() => g.value?.align),
257
- Q
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
258
495
  ];
259
496
  }
260
497
  export {
261
- We as default
498
+ useAlign as default
262
499
  };