@whitesev/pops 2.0.13 → 2.1.0

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.
@@ -21030,6 +21030,8 @@ declare class Pops {
21030
21030
  callback: (clickEvent: PointerEvent, contextMenuEvent: PointerEvent, liElement: HTMLLIElement, menuListenerRootNode: HTMLElement) => boolean | void | Promise<boolean | void>;
21031
21031
  item: /*elided*/ any[] | null;
21032
21032
  }[];
21033
+ chileMenuLeftOrRightDistance: number;
21034
+ childMenuTopOrBottomDistance: number;
21033
21035
  className: string;
21034
21036
  isAnimation: boolean;
21035
21037
  preventDefault: boolean;
@@ -20644,6 +20644,8 @@ export declare const PopsRightClickMenu: {
20644
20644
  callback: (clickEvent: PointerEvent, contextMenuEvent: PointerEvent, liElement: HTMLLIElement, menuListenerRootNode: HTMLElement) => boolean | void | Promise<boolean | void>;
20645
20645
  item: /*elided*/ any[] | null;
20646
20646
  }[];
20647
+ chileMenuLeftOrRightDistance: number;
20648
+ childMenuTopOrBottomDistance: number;
20647
20649
  className: string;
20648
20650
  isAnimation: boolean;
20649
20651
  preventDefault: boolean;
@@ -5,13 +5,15 @@ import type { PopsIconType } from "../../types/icon";
5
5
  */
6
6
  export interface PopsRightClickMenuDataDetails {
7
7
  /**
8
- * svg图标
8
+ * svg图标,留空则是没图标
9
+ * @default ""
9
10
  */
10
- icon: PopsIconType | string;
11
+ icon?: PopsIconType | string;
11
12
  /**
12
13
  * 图标是否旋转
14
+ * @default false
13
15
  */
14
- iconIsLoading: boolean;
16
+ iconIsLoading?: boolean;
15
17
  /**
16
18
  * 文字
17
19
  */
@@ -50,6 +52,16 @@ export interface PopsRightClickMenuDetails extends Pick<PopsCommonConfig, "useSh
50
52
  * 右键菜单数据
51
53
  */
52
54
  data: PopsRightClickMenuDataDetails[];
55
+ /**
56
+ * 子菜单的左右偏移距离
57
+ * @default 0
58
+ */
59
+ chileMenuLeftOrRightDistance?: number;
60
+ /**
61
+ * 子菜单的上下偏移距离
62
+ * @default 0
63
+ */
64
+ childMenuTopOrBottomDistance?: number;
53
65
  /**
54
66
  * 自定义className,默认为空
55
67
  * @default ""
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/pops",
3
- "version": "2.0.13",
3
+ "version": "2.1.0",
4
4
  "description": "弹窗库",
5
5
  "$schema": "https://json.schemastore.org/package.json",
6
6
  "main": "dist/index.cjs.js",
@@ -116,6 +116,8 @@ export const rightClickMenuConfig =
116
116
  ],
117
117
  },
118
118
  ],
119
+ chileMenuLeftOrRightDistance: 0,
120
+ childMenuTopOrBottomDistance: 0,
119
121
  useShadowRoot: true,
120
122
  className: "",
121
123
  isAnimation: true,
@@ -59,7 +59,7 @@ i.pops-rightClickMenu-icon[is-loading="true"] {
59
59
  }
60
60
  .pops-rightClickMenu ul li {
61
61
  padding: 5px 10px;
62
- margin: 2.5px 5px;
62
+ margin: 5px 5px;
63
63
  border-radius: 3px;
64
64
  display: flex;
65
65
  width: -webkit-fill-available;
@@ -71,9 +71,3 @@ i.pops-rightClickMenu-icon[is-loading="true"] {
71
71
  -ms-user-select: none;
72
72
  align-items: center;
73
73
  }
74
- .pops-rightClickMenu ul li:first-child {
75
- margin-top: 5px;
76
- }
77
- .pops-rightClickMenu ul li:last-child {
78
- margin-bottom: 5px;
79
- }
@@ -297,34 +297,104 @@ export const PopsRightClickMenu = {
297
297
  },
298
298
  /**
299
299
  * 获取left、top偏移
300
- * @param menuElement 菜单元素
301
- * @param x
302
- * @param y
300
+ * @param menuElement 当前生成的菜单元素
301
+ * @param mousePosition 鼠标位置信息
302
+ * @param isMainMenu 是否是主菜单
303
303
  */
304
- getOffset(menuElement: HTMLElement, x: number, y: number) {
304
+ getOffset(
305
+ menuElement: HTMLElement,
306
+ mousePosition: { x: number; y: number },
307
+ parentInfo: {
308
+ $menu: HTMLElement;
309
+ $parentItem: HTMLElement;
310
+ } | null
311
+ ) {
312
+ let result = {
313
+ top: 0,
314
+ right: 0,
315
+ bottom: 0,
316
+ left: 0,
317
+ };
305
318
  let menuElementWidth = popsDOMUtils.width(menuElement);
306
319
  let menuElementHeight = popsDOMUtils.height(menuElement);
320
+ /**
321
+ * 限制的间隙距离
322
+ */
323
+ let limitDistance = 1;
324
+ let maxPageLeftOffset = popsDOMUtils.width(globalThis) - limitDistance;
325
+ let maxPageTopOffset = popsDOMUtils.height(globalThis) - limitDistance;
307
326
  /* left最大偏移 */
308
- let maxLeftOffset =
309
- popsDOMUtils.width(globalThis) - menuElementWidth - 1;
327
+ let maxLeftOffset = maxPageLeftOffset - menuElementWidth;
310
328
  /* top最大偏移 */
311
- let maxTopOffset =
312
- popsDOMUtils.height(globalThis) - menuElementHeight - 8;
329
+ let maxTopOffset = maxPageTopOffset - menuElementHeight;
313
330
 
314
- let currentLeftOffset = x;
315
- let currentTopOffset = y;
316
- /* 不允许超出left最大值 */
331
+ let chileMenuLeftOrRightDistance = config.chileMenuLeftOrRightDistance;
332
+ let childMenuTopOrBottomDistance = config.childMenuTopOrBottomDistance;
333
+ let currentLeftOffset = mousePosition.x;
334
+ let currentTopOffset = mousePosition.y;
317
335
  currentLeftOffset = currentLeftOffset < 0 ? 0 : currentLeftOffset;
318
- currentLeftOffset =
319
- currentLeftOffset < maxLeftOffset ? currentLeftOffset : maxLeftOffset;
320
- /* 不允许超出top最大值 */
336
+ // 不允许超出left最大值
337
+ if (currentLeftOffset + chileMenuLeftOrRightDistance >= maxLeftOffset) {
338
+ // 超过,那么子菜单将会在放在左边
339
+ // 偏移计算方式就是父菜单的右偏移+父菜单的宽度
340
+ if (parentInfo) {
341
+ // 子菜单
342
+ let mainMenuOffset = popsDOMUtils.offset(parentInfo.$menu);
343
+ currentLeftOffset =
344
+ maxPageLeftOffset -
345
+ mainMenuOffset.left -
346
+ chileMenuLeftOrRightDistance +
347
+ limitDistance;
348
+ } else {
349
+ // 主菜单 默认的
350
+ currentLeftOffset = limitDistance + chileMenuLeftOrRightDistance;
351
+ }
352
+ if (currentLeftOffset < 0) {
353
+ currentLeftOffset = 0;
354
+ } else if (currentLeftOffset > maxLeftOffset) {
355
+ currentLeftOffset = maxLeftOffset;
356
+ }
357
+ // 去除左偏移,变为右偏移
358
+ result.right = currentLeftOffset;
359
+ Reflect.deleteProperty(result, "left");
360
+ } else {
361
+ // 右边
362
+ currentLeftOffset = currentLeftOffset + chileMenuLeftOrRightDistance;
363
+ result.left = currentLeftOffset;
364
+ Reflect.deleteProperty(result, "right");
365
+ }
366
+ // 不允许超出top最大值
321
367
  currentTopOffset = currentTopOffset < 0 ? 0 : currentTopOffset;
322
- currentTopOffset =
323
- currentTopOffset < maxTopOffset ? currentTopOffset : maxTopOffset;
324
- return {
325
- left: currentLeftOffset,
326
- top: currentTopOffset,
327
- };
368
+ if (currentTopOffset + childMenuTopOrBottomDistance >= maxTopOffset) {
369
+ // 超过,那么子菜单将会在放在上面
370
+ if (parentInfo) {
371
+ // 以项的top偏移为基准
372
+ let parentItemOffset = popsDOMUtils.offset(
373
+ parentInfo.$parentItem,
374
+ false
375
+ );
376
+ currentTopOffset =
377
+ maxPageTopOffset -
378
+ parentItemOffset.bottom -
379
+ childMenuTopOrBottomDistance +
380
+ limitDistance;
381
+ } else {
382
+ currentTopOffset = limitDistance + childMenuTopOrBottomDistance;
383
+ }
384
+ if (currentTopOffset < 0) {
385
+ currentTopOffset = limitDistance;
386
+ } else if (currentTopOffset > maxTopOffset) {
387
+ currentTopOffset = maxTopOffset;
388
+ }
389
+ // 去除上偏移,变为下偏移
390
+ result.bottom = currentTopOffset;
391
+ Reflect.deleteProperty(result, "top");
392
+ } else {
393
+ currentTopOffset = currentTopOffset + childMenuTopOrBottomDistance;
394
+ result.top = currentTopOffset;
395
+ Reflect.deleteProperty(result, "bottom");
396
+ }
397
+ return result;
328
398
  },
329
399
  /**
330
400
  * 显示菜单
@@ -360,14 +430,16 @@ export const PopsRightClickMenu = {
360
430
  }
361
431
  popsDOMUtils.appendBody($shadowContainer);
362
432
  }
363
- let { left: menuLeftOffset, top: menuTopOffset } = this.getOffset(
433
+ let offset = this.getOffset(
364
434
  menuElement,
365
- menuEvent.clientX,
366
- menuEvent.clientY
435
+ {
436
+ x: menuEvent.clientX,
437
+ y: menuEvent.clientY,
438
+ },
439
+ null
367
440
  );
368
441
  popsDOMUtils.css(menuElement, {
369
- left: menuLeftOffset,
370
- top: menuTopOffset,
442
+ ...offset,
371
443
  display: "",
372
444
  });
373
445
  /* 过渡动画 */
@@ -417,16 +489,21 @@ export const PopsRightClickMenu = {
417
489
  });
418
490
  /* 添加到页面 */
419
491
  popsDOMUtils.append($shadowRoot, menuElement);
420
- let { left: menuLeftOffset, top: menuTopOffset } = this.getOffset(
492
+ let $parentMenu = targetLiElement.closest<HTMLElement>(
493
+ ".pops-rightClickMenu"
494
+ )!;
495
+ let offset = this.getOffset(
421
496
  menuElement,
422
- posInfo.clientX,
423
- posInfo.clientY
497
+ {
498
+ x: posInfo.clientX,
499
+ y: posInfo.clientY,
500
+ },
501
+ {
502
+ $menu: $parentMenu,
503
+ $parentItem: targetLiElement,
504
+ }
424
505
  );
425
- popsDOMUtils.css(menuElement, {
426
- left: menuLeftOffset,
427
- top: menuTopOffset,
428
- display: "",
429
- });
506
+ popsDOMUtils.css(menuElement, { ...offset, display: "" });
430
507
  /* 过渡动画 */
431
508
  if (config.isAnimation) {
432
509
  popsDOMUtils.addClassName(menuElement, `pops-${PopsType}-anim-show`);
@@ -457,7 +534,9 @@ export const PopsRightClickMenu = {
457
534
  if (typeof item.icon === "string" && item.icon.trim() !== "") {
458
535
  let iconSVGHTML = PopsIcon.getIcon(item.icon) ?? item.icon;
459
536
  let iconElement = popsDOMUtils.parseTextToDOM(
460
- /*html*/ `<i class="pops-${PopsType}-icon" is-loading="${item.iconIsLoading}">${iconSVGHTML}</i>`
537
+ /*html*/ `<i class="pops-${PopsType}-icon" is-loading="${
538
+ item.iconIsLoading ?? false
539
+ }">${iconSVGHTML}</i>`
461
540
  );
462
541
  menuLiElement.appendChild(iconElement);
463
542
  }
@@ -6,13 +6,15 @@ import type { PopsIconType } from "../../types/icon";
6
6
  */
7
7
  export interface PopsRightClickMenuDataDetails {
8
8
  /**
9
- * svg图标
9
+ * svg图标,留空则是没图标
10
+ * @default ""
10
11
  */
11
- icon: PopsIconType | string;
12
+ icon?: PopsIconType | string;
12
13
  /**
13
14
  * 图标是否旋转
15
+ * @default false
14
16
  */
15
- iconIsLoading: boolean;
17
+ iconIsLoading?: boolean;
16
18
  /**
17
19
  * 文字
18
20
  */
@@ -61,6 +63,16 @@ export interface PopsRightClickMenuDetails
61
63
  * 右键菜单数据
62
64
  */
63
65
  data: PopsRightClickMenuDataDetails[];
66
+ /**
67
+ * 子菜单的左右偏移距离
68
+ * @default 0
69
+ */
70
+ chileMenuLeftOrRightDistance?: number;
71
+ /**
72
+ * 子菜单的上下偏移距离
73
+ * @default 0
74
+ */
75
+ childMenuTopOrBottomDistance?: number;
64
76
  /**
65
77
  * 自定义className,默认为空
66
78
  * @default ""