@apollo-design/position 0.0.0-stage → 0.1.9

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.
@@ -0,0 +1,14 @@
1
+ function getUnitOffset(size, offset = 0) {
2
+ const offsetStr = `${offset}`;
3
+ const cells = offsetStr.match(/^(.*)%$/);
4
+ if (cells?.[1] !== void 0) {
5
+ return size * (parseFloat(cells[1]) / 100);
6
+ }
7
+ return parseFloat(offsetStr);
8
+ }
9
+ function getNumberOffset(rect, offset) {
10
+ const [offsetX, offsetY] = offset ?? [];
11
+ return [getUnitOffset(rect.width, offsetX), getUnitOffset(rect.height, offsetY)];
12
+ }
13
+
14
+ export { getNumberOffset, getUnitOffset };
@@ -0,0 +1,138 @@
1
+ const PLACEMENT_POINTS = {
2
+ left: ["cr", "cl"],
3
+ right: ["cl", "cr"],
4
+ top: ["bc", "tc"],
5
+ bottom: ["tc", "bc"],
6
+ topLeft: ["bl", "tl"],
7
+ leftTop: ["tr", "tl"],
8
+ topRight: ["br", "tr"],
9
+ rightTop: ["tl", "tr"],
10
+ bottomRight: ["tr", "br"],
11
+ rightBottom: ["bl", "br"],
12
+ bottomLeft: ["tl", "bl"],
13
+ leftBottom: ["br", "bl"]
14
+ };
15
+ const ARROW_CENTER_PLACEMENT_POINTS = {
16
+ topLeft: ["bl", "tc"],
17
+ leftTop: ["tr", "cl"],
18
+ topRight: ["br", "tc"],
19
+ rightTop: ["tl", "cr"],
20
+ bottomRight: ["tr", "bc"],
21
+ rightBottom: ["bl", "cr"],
22
+ bottomLeft: ["tl", "bc"],
23
+ leftBottom: ["br", "cl"]
24
+ };
25
+ const DISABLE_AUTO_ARROW = /* @__PURE__ */ new Set([
26
+ "topLeft",
27
+ "topRight",
28
+ "bottomLeft",
29
+ "bottomRight",
30
+ "leftTop",
31
+ "leftBottom",
32
+ "rightTop",
33
+ "rightBottom"
34
+ ]);
35
+ const MAX_VERTICAL_CONTENT_RADIUS = 8;
36
+ function getArrowOffsetToken(contentRadius) {
37
+ const arrowOffset = contentRadius > 12 ? contentRadius + 2 : 12;
38
+ return {
39
+ arrowOffsetHorizontal: arrowOffset,
40
+ arrowOffsetVertical: MAX_VERTICAL_CONTENT_RADIUS
41
+ };
42
+ }
43
+ function isPlainObject(value) {
44
+ return typeof value === "object" && value !== null && !Array.isArray(value);
45
+ }
46
+ function getOverflowOptions(placement, arrowWidth, borderRadius, autoAdjustOverflow) {
47
+ if (autoAdjustOverflow === false) {
48
+ return { adjustX: false, adjustY: false };
49
+ }
50
+ const overflow = isPlainObject(autoAdjustOverflow) ? autoAdjustOverflow : {};
51
+ const arrowOffset = getArrowOffsetToken(borderRadius);
52
+ const baseOverflow = {};
53
+ switch (placement) {
54
+ case "top":
55
+ case "bottom":
56
+ baseOverflow.shiftX = arrowOffset.arrowOffsetHorizontal * 2 + arrowWidth;
57
+ baseOverflow.shiftY = true;
58
+ baseOverflow.adjustY = true;
59
+ break;
60
+ case "left":
61
+ case "right":
62
+ baseOverflow.shiftY = arrowOffset.arrowOffsetVertical * 2 + arrowWidth;
63
+ baseOverflow.shiftX = true;
64
+ baseOverflow.adjustX = true;
65
+ break;
66
+ }
67
+ const merged = { ...baseOverflow, ...overflow };
68
+ if (!merged.shiftX) merged.adjustX = true;
69
+ if (!merged.shiftY) merged.adjustY = true;
70
+ return merged;
71
+ }
72
+ function getPlacements(config) {
73
+ const { arrowWidth, offset, borderRadius, autoAdjustOverflow, arrowPointAtCenter, visibleFirst } = config;
74
+ const halfArrowWidth = arrowWidth / 2;
75
+ const arrowOffset = getArrowOffsetToken(borderRadius);
76
+ const result = {};
77
+ for (const key of Object.keys(PLACEMENT_POINTS)) {
78
+ const template = (arrowPointAtCenter ? ARROW_CENTER_PLACEMENT_POINTS[key] : void 0) ?? PLACEMENT_POINTS[key];
79
+ const info = {
80
+ points: template,
81
+ offset: [0, 0],
82
+ dynamicInset: true
83
+ };
84
+ if (DISABLE_AUTO_ARROW.has(key)) {
85
+ info.autoArrow = false;
86
+ }
87
+ switch (key) {
88
+ case "top":
89
+ case "topLeft":
90
+ case "topRight":
91
+ info.offset[1] = -halfArrowWidth - offset;
92
+ break;
93
+ case "bottom":
94
+ case "bottomLeft":
95
+ case "bottomRight":
96
+ info.offset[1] = halfArrowWidth + offset;
97
+ break;
98
+ case "left":
99
+ case "leftTop":
100
+ case "leftBottom":
101
+ info.offset[0] = -halfArrowWidth - offset;
102
+ break;
103
+ case "right":
104
+ case "rightTop":
105
+ case "rightBottom":
106
+ info.offset[0] = halfArrowWidth + offset;
107
+ break;
108
+ }
109
+ if (arrowPointAtCenter) {
110
+ switch (key) {
111
+ case "topLeft":
112
+ case "bottomLeft":
113
+ info.offset[0] = -arrowOffset.arrowOffsetHorizontal - halfArrowWidth;
114
+ break;
115
+ case "topRight":
116
+ case "bottomRight":
117
+ info.offset[0] = arrowOffset.arrowOffsetHorizontal + halfArrowWidth;
118
+ break;
119
+ case "leftTop":
120
+ case "rightTop":
121
+ info.offset[1] = -arrowOffset.arrowOffsetHorizontal * 2 + halfArrowWidth;
122
+ break;
123
+ case "leftBottom":
124
+ case "rightBottom":
125
+ info.offset[1] = arrowOffset.arrowOffsetHorizontal * 2 - halfArrowWidth;
126
+ break;
127
+ }
128
+ }
129
+ info.overflow = getOverflowOptions(key, arrowWidth, borderRadius, autoAdjustOverflow);
130
+ if (visibleFirst) {
131
+ info.htmlRegion = "visibleFirst";
132
+ }
133
+ result[key] = info;
134
+ }
135
+ return result;
136
+ }
137
+
138
+ export { ARROW_CENTER_PLACEMENT_POINTS, PLACEMENT_POINTS, getArrowOffsetToken, getOverflowOptions, getPlacements };
package/dist/point.mjs ADDED
@@ -0,0 +1,31 @@
1
+ function splitPoints(point) {
2
+ const v = point[0];
3
+ const h = point[1];
4
+ return [v ?? "c", h ?? "c"];
5
+ }
6
+ function getAlignPoint(rect, point) {
7
+ const [vertical, horizontal] = point;
8
+ let y;
9
+ if (vertical === "t") y = rect.y;
10
+ else if (vertical === "b") y = rect.y + rect.height;
11
+ else y = rect.y + rect.height / 2;
12
+ let x;
13
+ if (horizontal === "l") x = rect.x;
14
+ else if (horizontal === "r") x = rect.x + rect.width;
15
+ else x = rect.x + rect.width / 2;
16
+ return { x, y };
17
+ }
18
+ function alignPointOf(rect, point) {
19
+ return getAlignPoint(rect, splitPoints(point));
20
+ }
21
+ const REVERSE_MAP = { t: "b", b: "t", l: "r", r: "l" };
22
+ function reversePoint(point, index) {
23
+ const current = point[index];
24
+ const reversed = REVERSE_MAP[current] ?? "c";
25
+ return index === 0 ? [reversed, point[1]] : [point[0], reversed];
26
+ }
27
+ function flatPoint(point) {
28
+ return `${point[0]}${point[1]}`;
29
+ }
30
+
31
+ export { alignPointOf, flatPoint, getAlignPoint, reversePoint, splitPoints };
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "@apollo-design/position",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.9",
4
+ "description": "浮层定位几何。替代 @rc-component/trigger 与 rc-tooltip 的定位部分。**只做几何与尺寸测量**,触发时机与生命周期在 @apollo-design/overlay。",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "main": "./dist/index.mjs",
9
+ "module": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.mjs"
15
+ },
16
+ "./package.json": "./package.json"
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "dependencies": {
22
+ "@apollo-design/utils": "0.1.9"
23
+ },
24
+ "peerDependencies": {
25
+ "vue": "^3.5.42"
26
+ },
27
+ "devDependencies": {
28
+ "@apollo-design/test-utils": "0.1.9"
29
+ },
30
+ "scripts": {
31
+ "build": "unbuild --config ../../scripts/unbuild-preserve-modules.mjs",
32
+ "test": "vitest run",
33
+ "lint": "vue-tsc --noEmit"
34
+ }
6
35
  }