1
- import { filterNullable } from "array-utils-ts"
2
1
import clsx from "clsx"
3
2
import { useAtom , useAtomValue } from "jotai"
4
- import { range } from "lodash-es"
5
3
import { FC , useCallback , useEffect , useRef } from "react"
6
4
import { ActiveTab , ActualDate , TlSelected } from "../store"
7
5
import { useOnClickOutside } from "../utils/useOnClickOutside"
8
6
9
- const updateSelection = ( els : [ HTMLElement , HTMLElement ] | null ) => {
10
- document . querySelectorAll ( "[data-tl-idx]" ) . forEach ( ( x ) => x . classList . remove ( "tl-active" ) )
11
-
12
- const abc = filterNullable ( els ?. map ( ( x ) => x . getAttribute ( "data-tl-idx" ) ) ?? [ ] )
13
- . map ( ( x ) => parseInt ( x , 10 ) )
14
- . filter ( ( x ) => ! isNaN ( x ) )
15
- . sort ( ( a , b ) => a - b )
16
-
17
- if ( abc . length !== 2 ) return
18
-
19
- for ( const x of range ( 24 ) . filter ( ( x ) => x < abc [ 0 ] || abc [ 1 ] < x ) ) {
20
- document . querySelectorAll ( `[data-tl-idx="${ x } "]` ) . forEach ( ( x ) => x . classList . add ( "tl-active" ) )
21
- }
22
- }
23
-
24
7
// prettier-ignore
25
8
const getIdx = ( idx : string ) => document . querySelector ( `[data-tl-home=true] [data-tl-idx="${ idx } "]` ) as HTMLElement
26
9
const getNow = ( ) => document . querySelector ( "[data-tl-home=true] [data-tl-now=true]" ) as HTMLElement
@@ -35,70 +18,76 @@ export const BoardLine: FC<BoardLineProps> = ({ rtRef, tlRef }) => {
35
18
const date = useAtomValue ( ActualDate )
36
19
37
20
const [ selected , setSelected ] = useAtom ( TlSelected )
38
- const cRef = useRef < HTMLDivElement > ( null )
39
- // const lRef = useRef<HTMLDivElement>(null)
40
- // const rRef = useRef<HTMLDivElement>(null)
21
+ const cRef = useRef < HTMLDivElement > ( null ) // center line
22
+ const lRef = useRef < HTMLDivElement > ( null ) // left glass
23
+ const rRef = useRef < HTMLDivElement > ( null ) // right glass
41
24
42
25
const selectedRef = useRef ( selected )
43
26
selectedRef . current = selected
44
27
45
28
// root ref here to work click on event buttons
46
29
useOnClickOutside ( rtRef , ( ) => setSelected ( null ) )
47
30
48
- const setLine = useCallback ( ( x : number , width : number ) => {
49
- if ( ! tlRef . current || ! cRef . current ) return
31
+ const resetLine = useCallback ( ( ) => {
32
+ if ( ! cRef . current || ! lRef . current || ! rRef . current ) return
33
+
34
+ cRef . current . style . opacity = "0"
35
+ lRef . current . style . opacity = "0"
36
+ rRef . current . style . opacity = "0"
37
+ } , [ ] )
38
+
39
+ const setLine = useCallback ( ( x : number , width : number , glass = false ) => {
40
+ if ( ! tlRef . current || ! cRef . current || ! lRef . current || ! rRef . current ) return
50
41
51
- const el = tlRef . current
52
- const rt = el . getBoundingClientRect ( )
53
- const xx = rt . left + el . scrollLeft
42
+ const hh = tlRef . current . getBoundingClientRect ( ) . height
43
+ const pl = tlRef . current . getBoundingClientRect ( ) . left - tlRef . current . scrollLeft
44
+ const cx = x - pl
54
45
46
+ cRef . current . style . top = `${ hh / 2 } px`
47
+ cRef . current . style . height = `${ hh - 16 } px`
55
48
cRef . current . style . opacity = "1"
56
- cRef . current . style . top = `${ rt . height / 2 } px`
57
- cRef . current . style . height = `${ rt . height - 16 } px`
58
- cRef . current . style . left = `${ x - xx } px`
49
+ cRef . current . style . left = `${ cx } px`
59
50
cRef . current . style . width = `${ width } px`
60
51
61
- // const [head, tail] = [getIdx("0"), getIdx("23")]
62
- // if (!lRef.current || !rRef.current || !head || !tail) return
63
-
64
- // const x1 = head.getBoundingClientRect().left - xx
65
- // lRef.current.style.top = `${rt.height / 2}px`
66
- // lRef.current.style.height = `${rt.height - 16}px`
67
- // lRef.current.style.opacity = glass ? "1" : "0"
68
- // lRef.current.style.left = `${x1}px`
69
- // lRef.current.style.width = `${x - xx - x1}px`
70
-
71
- // const x2 = x - xx + width
72
- // rRef.current.style.top = `${rt.height / 2}px`
73
- // rRef.current.style.height = `${rt.height - 16}px`
74
- // rRef.current.style.opacity = glass ? "1" : "0"
75
- // rRef.current.style.left = `${x2}px`
76
- // rRef.current.style.width = `${tail.getBoundingClientRect().right - x2 - 58.5}px`
52
+ const [ head , tail ] = [ getIdx ( "0" ) , getIdx ( "23" ) ]
53
+ if ( ! head || ! tail ) return
54
+
55
+ const lx = head . getBoundingClientRect ( ) . left - pl
56
+ const rx = tail . getBoundingClientRect ( ) . right - pl
57
+
58
+ lRef . current . style . top = `${ hh / 2 } px`
59
+ lRef . current . style . height = `${ hh - 16 } px`
60
+ lRef . current . style . opacity = glass ? "1" : "0"
61
+ lRef . current . style . left = `${ lx } px`
62
+ lRef . current . style . width = `${ cx - lx } px`
63
+
64
+ rRef . current . style . top = `${ hh / 2 } px`
65
+ rRef . current . style . height = `${ hh - 16 } px`
66
+ rRef . current . style . opacity = glass ? "1" : "0"
67
+ rRef . current . style . left = `${ cx + width } px`
68
+ rRef . current . style . width = `${ rx - cx - width } px`
77
69
} , [ ] )
78
70
79
71
const calc = useCallback ( ( ) => {
80
72
if ( ! tlRef . current ) return
81
73
82
74
const selected = selectedRef . current
83
75
if ( ! selected ) {
84
- updateSelection ( null )
85
-
86
76
const el = getNow ( )
87
- if ( ! el && cRef . current ) cRef . current . style . opacity = "0"
77
+ if ( ! el && cRef . current ) resetLine ( )
88
78
else {
89
79
const rc = el . getBoundingClientRect ( )
90
80
setLine ( rc . left , rc . width )
91
81
}
92
82
} else {
93
83
const ae = getIdx ( selected [ 0 ] )
94
84
const be = getIdx ( selected [ 1 ] )
95
- updateSelection ( [ ae , be ] )
96
85
97
86
const a = ae . getBoundingClientRect ( )
98
87
const b = be . getBoundingClientRect ( )
99
88
const l = Math . min ( b . left , a . left )
100
89
const w = b . left < a . left ? a . right - b . left : b . right - a . left
101
- setLine ( l , w )
90
+ setLine ( l , w , true )
102
91
}
103
92
} , [ ] )
104
93
@@ -114,19 +103,20 @@ export const BoardLine: FC<BoardLineProps> = ({ rtRef, tlRef }) => {
114
103
setSelected ( null )
115
104
} , [ home ] )
116
105
117
- const cls = "absolute pointer-events-none select-none box-border -translate-y-1/2"
106
+ const cls = "absolute z-[10] box-border -translate-y-1/2 pointer-events-none select-none"
107
+ const sideCls = clsx ( cls , "z-[14] bg-blue-100/50" )
118
108
119
109
return (
120
110
< >
121
- { /* <div ref={lRef} className={clsx(cls, "z-[14] h-full w-full bg-red-100/50") } /> */ }
111
+ < div ref = { lRef } className = { sideCls } />
122
112
< div
123
113
ref = { cRef }
124
114
className = { clsx (
125
115
cls ,
126
116
"z-[15] w-[32px] rounded-md border-2 border-red-500/50 dark:border-red-500/80" ,
127
117
) }
128
118
/>
129
- { /* <div ref={rRef} className={clsx(cls, "z-[14] h-full w-full bg-green-100/50") } /> */ }
119
+ < div ref = { rRef } className = { sideCls } />
130
120
</ >
131
121
)
132
122
}
0 commit comments