1
1
export type HTMLElementType = HTMLElement | null ;
2
+ export type TriggerEvent = 'click' | 'contextmenu' | 'hover' ;
2
3
export const select = document . querySelector . bind ( document ) ;
3
4
export const selectAll = document . querySelectorAll . bind ( document ) ;
4
5
@@ -37,17 +38,17 @@ export function getDocumentRect() {
37
38
}
38
39
39
40
/**
40
- * Returns the position of element relative anchor's position
41
+ * Returns the position of element relative to element position
41
42
* @param element target element
42
- * @param anchorPos anchor's position
43
+ * @param relatedPos the relative element position
43
44
*/
44
45
export function getRelativePosition (
45
46
element : HTMLElement ,
46
- anchorPos : IPosition
47
+ relativePos : IPosition
47
48
) {
48
49
const page = getDocumentRect ( ) ;
49
- const anchorX = anchorPos . x ;
50
- const anchorY = anchorPos . y ;
50
+ const anchorX = relativePos . x ;
51
+ const anchorY = relativePos . y ;
51
52
52
53
const marginRight = page . width - anchorX ;
53
54
const marginBottom = page . height - anchorY ;
@@ -63,3 +64,39 @@ export function getRelativePosition(
63
64
y,
64
65
} ;
65
66
}
67
+
68
+ export function getEventPosition ( e : React . MouseEvent ) {
69
+ return {
70
+ x : e . clientX ,
71
+ y : e . clientY
72
+ }
73
+ }
74
+
75
+ export function findParentByClassName < T > ( element , className ) : T | null {
76
+ try {
77
+ while ( element && element ) {
78
+ const classes = element . getAttribute ( 'class' ) ;
79
+ if ( classes && classes . indexOf ( className ) > - 1 ) {
80
+ return element ;
81
+ }
82
+ element = element . parentElement ;
83
+ }
84
+ } catch ( e ) {
85
+ throw new Error ( e ) ;
86
+ }
87
+ return null ;
88
+ }
89
+
90
+ export function triggerEvent ( trigger : TriggerEvent ) {
91
+ switch ( trigger ) {
92
+ case 'click' : {
93
+ return 'onClick' ;
94
+ }
95
+ case 'hover' : {
96
+ return 'onMouseOver' ;
97
+ }
98
+ default : {
99
+ return trigger ;
100
+ }
101
+ }
102
+ }
0 commit comments