@vxe-ui/core 3.4.30 → 3.4.32

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,194 @@
1
+ import XEUtils from 'xe-utils'
2
+
3
+ import type { VxePopperUtils, VxePopperOptions } from '../../types'
4
+
5
+ /**
6
+ * 获取包裹容器的元素
7
+ */
8
+ function getWrapperElement (panelElem: HTMLElement | null | undefined) {
9
+ if (!panelElem) {
10
+ return null
11
+ }
12
+ const bodyEl = document.body
13
+ const parentEl = panelElem.parentElement
14
+ return (parentEl === bodyEl ? document.documentElement : parentEl) || null
15
+ }
16
+
17
+ /**
18
+ * 获取插入到指定容器的元素或字符串
19
+ */
20
+ function getAppendSelector (appendTo: string | HTMLElement | ((params: any) => string | HTMLElement) | undefined) {
21
+ const selectElem = appendTo && XEUtils.isFunction(appendTo) ? appendTo({}) : appendTo
22
+ return selectElem || 'body'
23
+ }
24
+
25
+ /**
26
+ * 获取插入到指定容器的元素
27
+ */
28
+ function getAppendElement (appendTo: string | HTMLElement | ((params: any) => string | HTMLElement) | undefined) {
29
+ const selectElem = getAppendSelector(appendTo)
30
+ if (XEUtils.isString(selectElem)) {
31
+ return document.querySelector<HTMLElement>(selectElem) || document.body
32
+ }
33
+ return selectElem
34
+ }
35
+
36
+ export const popperUtils: VxePopperUtils = {
37
+ /**
38
+ * 更新位置
39
+ */
40
+ updatePlacement (targetElem: HTMLElement | null | undefined, panelElem: HTMLElement | null | undefined, options: VxePopperOptions) {
41
+ const { defaultTop, defaultLeft, placement, defaultPlacement, appendTo, teleportTo, marginSize, isMinWidth } = Object.assign({
42
+ appendTo: '',
43
+ teleportTo: false,
44
+ marginSize: 18,
45
+ isMinWidth: true
46
+ }, options)
47
+ let panelPlacement: 'top' | 'bottom' = 'bottom'
48
+ let top: number | '' = ''
49
+ let bottom: number | '' = ''
50
+ let left = 0
51
+ let minWidth: number | '' = ''
52
+ let arrowLeft: number | '' = ''
53
+ const stys: Record<string, string> = {}
54
+ if (panelElem && targetElem) {
55
+ const bodyEl = document.body
56
+ const parentWrapperEl = getWrapperElement(teleportTo ? getAppendElement(appendTo) : panelElem)
57
+ if (parentWrapperEl) {
58
+ const targetWidth = targetElem.offsetWidth
59
+ const targetHeight = targetElem.offsetHeight
60
+ const panelWidth = panelElem.offsetWidth
61
+ const panelHeight = panelElem.offsetHeight
62
+
63
+ const parentWrapperRect = parentWrapperEl.getBoundingClientRect()
64
+ const panelRect = panelElem.getBoundingClientRect()
65
+ const targetRect = targetElem.getBoundingClientRect()
66
+ const visibleHeight = bodyEl.clientHeight
67
+ const visibleWidth = bodyEl.clientWidth
68
+
69
+ const parentWrapperScrollLeft = parentWrapperEl.scrollLeft
70
+ const parentWrapperScrollTop = parentWrapperEl.scrollTop
71
+ const parentWrapperOffsetLeft = parentWrapperRect.left
72
+ const parentWrapperOffsetTop = parentWrapperRect.top
73
+ const targetLeft = targetRect.left
74
+ const targetTop = targetRect.top
75
+
76
+ minWidth = targetElem.offsetWidth
77
+
78
+ if (teleportTo) {
79
+ left = (defaultLeft || (targetLeft - (panelWidth - targetWidth) / 2)) - parentWrapperScrollLeft
80
+ top = (defaultTop || (targetTop + targetHeight)) - parentWrapperScrollTop
81
+ if (placement === 'top') {
82
+ panelPlacement = 'top'
83
+ top = targetTop - panelHeight
84
+ } else if (!placement) {
85
+ if (defaultPlacement === 'top') {
86
+ panelPlacement = 'top'
87
+ if (!defaultTop) {
88
+ top = targetTop - panelHeight
89
+ }
90
+ // 如果上面不够放,则向下
91
+ if (top < marginSize) {
92
+ panelPlacement = 'bottom'
93
+ top = targetTop + targetHeight
94
+ }
95
+ // 如果下面不够放,则向上(优先)
96
+ if (top + panelHeight + marginSize > visibleHeight) {
97
+ panelPlacement = 'top'
98
+ top = targetTop - panelHeight
99
+ }
100
+ } else {
101
+ // 如果下面不够放,则向上
102
+ if (top + panelHeight + marginSize > visibleHeight) {
103
+ panelPlacement = 'top'
104
+ top = targetTop - panelHeight
105
+ }
106
+ // 如果上面不够放,则向下(优先)
107
+ if (top < marginSize) {
108
+ panelPlacement = 'bottom'
109
+ top = targetTop + targetHeight
110
+ }
111
+ }
112
+ }
113
+ // 如果溢出右边
114
+ if (left + panelWidth + marginSize > visibleWidth) {
115
+ left -= left + panelWidth + marginSize - visibleWidth
116
+ }
117
+ // 如果溢出左边
118
+ if (left < marginSize) {
119
+ left = marginSize
120
+ }
121
+
122
+ // 偏移
123
+ top -= parentWrapperOffsetTop
124
+ left -= parentWrapperOffsetLeft
125
+ } else {
126
+ if (placement === 'top') {
127
+ panelPlacement = 'top'
128
+ bottom = targetHeight
129
+ } else if (!placement) {
130
+ // 如果下面不够放,则向上
131
+ top = targetHeight
132
+ if (targetTop + targetHeight + panelHeight + marginSize > visibleHeight) {
133
+ // 如果上面不够放,则向下(优先)
134
+ if (targetTop - targetHeight - panelHeight > marginSize) {
135
+ panelPlacement = 'top'
136
+ top = ''
137
+ bottom = targetHeight
138
+ }
139
+ }
140
+ }
141
+ // 是否超出右侧
142
+ if (panelRect.left + panelRect.width + marginSize > visibleWidth) {
143
+ left = -(panelRect.left + panelRect.width + marginSize - visibleWidth)
144
+ }
145
+ }
146
+ if (XEUtils.isNumber(top)) {
147
+ stys.top = top + 'px'
148
+ }
149
+ if (XEUtils.isNumber(bottom)) {
150
+ stys.bottom = bottom + 'px'
151
+ }
152
+ if (XEUtils.isNumber(left)) {
153
+ stys.left = left + 'px'
154
+ }
155
+ if (isMinWidth && XEUtils.isNumber(minWidth)) {
156
+ stys.minWidth = minWidth + 'px'
157
+ }
158
+
159
+ // 箭头
160
+ if (left === targetLeft) {
161
+ if (targetWidth <= panelWidth) {
162
+ arrowLeft = targetWidth / 2
163
+ }
164
+ } else if (left < targetLeft) {
165
+ if (left + panelWidth > targetLeft + targetWidth) {
166
+ arrowLeft = (targetLeft - left) + targetWidth / 2
167
+ } else {
168
+ arrowLeft = (targetLeft - left) + (panelWidth - (targetLeft - left)) / 2
169
+ }
170
+ }
171
+ }
172
+ }
173
+ return {
174
+ top: top || 0,
175
+ bottom: bottom || 0,
176
+ left: left || 0,
177
+ arrowLeft: arrowLeft || 0,
178
+ style: stys,
179
+ placement: panelPlacement
180
+ }
181
+ },
182
+ /**
183
+ * 获取包裹容器的元素
184
+ */
185
+ getWrapperElement,
186
+ /**
187
+ * 获取插入到指定容器的元素或字符串
188
+ */
189
+ getAppendSelector,
190
+ /**
191
+ * 获取插入到指定容器的元素
192
+ */
193
+ getAppendElement
194
+ }
@@ -0,0 +1,58 @@
1
+ import { VxeScrollbarUtils } from '../../types'
2
+
3
+ export const scrollbarUtils: VxeScrollbarUtils = {
4
+ /**
5
+ * 计算滚动条滑块的长度
6
+ */
7
+ getThumbSize (viewportSize: number, contentSize: number, trackSize: number) {
8
+ if (contentSize <= viewportSize) {
9
+ return 0
10
+ }
11
+ const ratio = viewportSize / contentSize
12
+ const ideal = trackSize * ratio
13
+ return Math.max(20, Math.min(ideal, trackSize))
14
+ },
15
+ /**
16
+ * 计算滚动条滑块的偏移位置
17
+ */
18
+ getThumbOffsetSize (offsetSize: number, viewportSize: number, contentSize: number, trackSize: number, thumbSize: number) {
19
+ const maxScroll = contentSize - viewportSize
20
+ if (maxScroll <= 0) {
21
+ return 0
22
+ }
23
+ const maxThumbOffset = trackSize - thumbSize
24
+ if (maxThumbOffset <= 0) {
25
+ return 0
26
+ }
27
+ const clamped = Math.min(Math.max(offsetSize, 0), maxScroll)
28
+ const ratio = clamped / maxScroll
29
+ return ratio * maxThumbOffset
30
+ },
31
+ /**
32
+ * 根据点击轨道的位置,计算滑块的偏移位置
33
+ */
34
+ getScrollFromClick (clickPosition: number, viewportSize: number, contentSize: number, trackSize: number, thumbSize: number) {
35
+ const maxThumbOffset = trackSize - thumbSize
36
+ const maxScroll = contentSize - viewportSize
37
+ if (maxScroll <= 0 || maxThumbOffset <= 0) {
38
+ return 0
39
+ }
40
+ let thumbOffset = clickPosition - thumbSize / 2
41
+ thumbOffset = Math.min(Math.max(thumbOffset, 0), maxThumbOffset)
42
+ const scrollOffset = (thumbOffset / maxThumbOffset) * maxScroll
43
+ return scrollOffset
44
+ },
45
+ /**
46
+ * 拖拽 thumb 时,根据鼠标位置计算滑块的偏移位置
47
+ */
48
+ getScrollFromDrag (pointerPosition: number, dragOffset: number, viewportSize: number, contentSize: number, trackSize: number, thumbSize: number) {
49
+ const maxScroll = contentSize - viewportSize
50
+ const maxThumbOffset = trackSize - thumbSize
51
+ if (maxScroll <= 0 || maxThumbOffset <= 0) {
52
+ return 0
53
+ }
54
+ let thumbOffset = pointerPosition - dragOffset
55
+ thumbOffset = Math.min(Math.max(thumbOffset, 0), maxThumbOffset)
56
+ return (thumbOffset / maxThumbOffset) * maxScroll
57
+ }
58
+ }
@@ -17,6 +17,8 @@ import { VxeGlobalClipboard } from './clipboard'
17
17
  import { VxeGlobalPermission } from './permission'
18
18
  import { VxeBaseUtils } from './base-utils'
19
19
  import { VxeDomUtils } from './dom-utils'
20
+ import { VxeScrollbarUtils } from './scrollbar-utils'
21
+ import { VxePopperUtils } from './popper-utils'
20
22
  import { VxeGlobalComponentMethod, VxeGlobalGetComponentMethod, VxeGlobalHasComponentMethod } from './components'
21
23
  import { VxeGlobalUseMixins } from './mixins'
22
24
  import { VxeGlobalLog } from './log'
@@ -74,6 +76,8 @@ export const permission: VxeGlobalPermission
74
76
 
75
77
  export const baseUtils: VxeBaseUtils
76
78
  export const domUtils: VxeDomUtils
79
+ export const scrollbarUtils: VxeScrollbarUtils
80
+ export const popperUtils: VxePopperUtils
77
81
 
78
82
  export const globalEvents: VxeGlobalEvents
79
83
 
@@ -215,6 +219,10 @@ export interface VxeUIExport {
215
219
  * Document 工具函数
216
220
  */
217
221
  domUtils: VxeDomUtils
222
+ /**
223
+ * 滚动条工具函数
224
+ */
225
+ scrollbarUtils: VxeScrollbarUtils
218
226
 
219
227
  /**
220
228
  * 全局事件管理
@@ -295,6 +303,8 @@ export * from './permission'
295
303
 
296
304
  export * from './base-utils'
297
305
  export * from './dom-utils'
306
+ export * from './scrollbar-utils'
307
+ export * from './popper-utils'
298
308
 
299
309
  export * from './log'
300
310
 
@@ -0,0 +1,44 @@
1
+ export interface VxePopperOptions {
2
+ defaultTop?: number
3
+ defaultLeft?: number
4
+ placement?: '' | 'top' | 'bottom' | null
5
+ defaultPlacement?: '' | 'top' | 'bottom' | null
6
+ appendTo?: string | HTMLElement | ((params: any) => string | HTMLElement)
7
+ teleportTo?: boolean
8
+ marginSize?: number
9
+ isMinWidth?: boolean
10
+ }
11
+
12
+ export interface VxePopperPlacementResult {
13
+ top: number
14
+ bottom: number
15
+ left: number
16
+ arrowLeft: number
17
+ style: Record<string, string>
18
+ placement: 'top' | 'bottom'
19
+ }
20
+
21
+ export interface VxePopperUtils {
22
+ /**
23
+ * 更新位置
24
+ * @param targetElem
25
+ * @param panelElem
26
+ * @param options
27
+ */
28
+ updatePlacement (targetElem: HTMLElement | null | undefined, panelElem: HTMLElement | null | undefined, options: VxePopperOptions): VxePopperPlacementResult
29
+ /**
30
+ * 获取包裹容器的元素
31
+ * @param panelElem
32
+ */
33
+ getWrapperElement (panelElem: HTMLElement | null | undefined): HTMLElement | null
34
+ /**
35
+ * 获取插入到指定容器的元素或字符串
36
+ * @param appendTo
37
+ */
38
+ getAppendSelector (appendTo: string | HTMLElement | ((params: any) => string | HTMLElement) | undefined): HTMLElement | string
39
+ /**
40
+ * 获取插入到指定容器的元素
41
+ * @param appendTo
42
+ */
43
+ getAppendElement (appendTo: string | HTMLElement | ((params: any) => string | HTMLElement) | undefined): HTMLElement | null
44
+ }
@@ -0,0 +1,38 @@
1
+
2
+ export interface VxeScrollbarUtils {
3
+ /**
4
+ * 计算滚动条滑块的长度
5
+ * @param viewportSize
6
+ * @param contentSize
7
+ * @param trackSize
8
+ */
9
+ getThumbSize (viewportSize: number, contentSize: number, trackSize: number): number
10
+ /**
11
+ * 计算滚动条滑块的偏移位置
12
+ * @param offsetSize
13
+ * @param viewportSize
14
+ * @param contentSize
15
+ * @param trackSize
16
+ * @param thumbSize
17
+ */
18
+ getThumbOffsetSize (offsetSize: number, viewportSize: number, contentSize: number, trackSize: number, thumbSize: number): number
19
+ /**
20
+ * 根据点击轨道的位置,计算滑块的偏移位置
21
+ * @param clickPosition
22
+ * @param viewportSize
23
+ * @param contentSize
24
+ * @param trackSize
25
+ * @param thumbSize
26
+ */
27
+ getScrollFromClick (clickPosition: number, viewportSize: number, contentSize: number, trackSize: number, thumbSize: number): number
28
+ /**
29
+ * 拖拽 thumb 时,根据鼠标位置计算滑块的偏移位置
30
+ * @param pointerPosition
31
+ * @param dragOffset
32
+ * @param viewportSize
33
+ * @param contentSize
34
+ * @param trackSize
35
+ * @param thumbSize
36
+ */
37
+ getScrollFromDrag (pointerPosition: number, dragOffset: number, viewportSize: number, contentSize: number, trackSize: number, thumbSize: number): number
38
+ }