/
index.tsx
169 lines (155 loc) · 3.22 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/** @module Context.Select */
import { createContext, Dispatch, ReactNode, useMemo, useReducer } from 'react'
/**
* Select interface
*/
export interface ISelect {
uuid: string
label: number
}
/**
* Select state interface
*/
export interface ISelectState {
enabled: boolean
type?: 'solids' | 'faces' | 'edges'
part?: string
highlighted?: ISelect
selected: ISelect[]
point?: ISelectPoint
data?: boolean
postProcessing?: boolean
dispatch: Dispatch<ISelectAction>
}
/**
* Select action interface
*/
export interface ISelectAction {
type: string
value?: boolean | string | ISelectPoint | ISelect | ISelect[]
}
/**
* Select point
*/
export interface ISelectPoint {
x: number
y: number
z: number
}
/**
* Select initial state
*/
export const initialState: ISelectState = {
enabled: false,
type: undefined,
part: undefined,
highlighted: undefined,
selected: [],
point: undefined,
data: undefined,
postProcessing: undefined,
dispatch: () => undefined
}
/**
* Action types
*/
export const actionTypes = {
ENABLE: 'ENABLE',
DISABLE: 'DISABLE',
CLEAR: 'CLEAR',
SETTYPE: 'SETTYPE',
SETPART: 'SETPART',
HIGHLIGHT: 'HIGHLIGHT',
SELECT: 'SELECT',
SETPOINT: 'SETPOINT',
SETDATA: 'SETDATA',
SETPOSTPROCESSING: 'SETPOSTPROCESSING'
}
/**
* Context
*/
export const SelectContext = createContext(initialState)
/**
* Reducer
* @param state State
* @param action Action
* @returns State
*/
export const selectReducer = (
state: ISelectState,
action: ISelectAction
): ISelectState => {
switch (action.type) {
case actionTypes.ENABLE:
return { ...state, enabled: true }
case actionTypes.DISABLE:
return {
...state,
enabled: false,
highlighted: undefined,
selected: []
}
case actionTypes.CLEAR:
return { ...initialState }
case actionTypes.SETTYPE:
return { ...state, type: action.value as 'solids' | 'faces' | 'edges' }
case actionTypes.SETPART:
return { ...state, part: action.value as string }
case actionTypes.HIGHLIGHT:
return { ...state, highlighted: action.value as ISelect | undefined }
case actionTypes.SELECT:
return {
...state,
selected: action.value as ISelect[]
}
case actionTypes.SETPOINT:
return {
...state,
point: action.value as ISelectPoint
}
case actionTypes.SETDATA:
return {
...state,
data: action.value as boolean
}
case actionTypes.SETPOSTPROCESSING:
return {
...state,
postProcessing: action.value as boolean
}
default:
return state
}
}
/**
* Props
*/
export interface IProps {
children: ReactNode
}
/**
* Select provider
* @param props Props
* @returns SelectProvider
*/
const SelectProvider: React.FunctionComponent<IProps> = ({ children }) => {
// Reducer
const [selectState, selectDispatch] = useReducer(selectReducer, initialState)
// Context
const contextValue = useMemo(
() => ({
...selectState,
dispatch: selectDispatch
}),
[selectState]
)
/**
* Render
*/
return (
<SelectContext.Provider value={contextValue}>
{children}
</SelectContext.Provider>
)
}
export default SelectProvider