1
1
import React , {
2
- useEffect ,
3
- useState
2
+ useEffect
4
3
} from 'react' ;
5
4
6
5
import OlParser from 'geostyler-openlayers-parser' ;
7
6
8
7
import {
9
- Style as GsStyle
8
+ VectorData
9
+ } from 'geostyler-data' ;
10
+
11
+ import {
12
+ Style
10
13
} from 'geostyler-style' ;
11
14
12
15
import CardStyle , {
@@ -30,80 +33,98 @@ import {
30
33
DigitizeUtil
31
34
} from '@terrestris/react-geo/dist/Util/DigitizeUtil' ;
32
35
36
+ import useAppDispatch from '../../../../../hooks/useAppDispatch' ;
37
+ import useAppSelector from '../../../../../hooks/useAppSelector' ;
38
+
39
+ import {
40
+ ClientTools ,
41
+ DrawToolConfig ,
42
+ setDrawStyle
43
+ } from '../../../../../store/toolConfig' ;
44
+
33
45
import {
34
46
parseStyle
35
47
} from '../../../../../utils/parseStyle' ;
36
48
37
- const defaultStyle : GsStyle = {
38
- name : 'Default Style' ,
39
- rules : [ {
40
- name : 'Area' ,
41
- symbolizers : [ {
42
- kind : 'Fill' ,
43
- color : '#00b72b' ,
44
- outlineOpacity : 0.8 ,
45
- opacity : 0.5 ,
46
- fillOpacity : 0.8 ,
47
- outlineWidth : 2 ,
48
- outlineColor : '#00b72b'
49
- } ]
50
- } , {
51
- name : 'Line' ,
52
- symbolizers : [ {
53
- kind : 'Line' ,
54
- color : '#00b72b' ,
55
- width : 2 ,
56
- opacity : 0.8
57
- } ]
58
- } , {
59
- name : 'Point' ,
60
- symbolizers : [ {
61
- kind : 'Mark' ,
62
- wellKnownName : 'circle' ,
63
- color : '#00b72b' ,
64
- strokeColor : '#00b72b' ,
65
- strokeOpacity : 0.8 ,
66
- opacity : 0.5 ,
67
- radius : 7
68
- } ] ,
69
- filter : [
70
- '==' ,
71
- 'label' ,
72
- 'undefined'
73
- ]
74
- } , {
75
- name : 'Text' ,
76
- symbolizers : [ {
77
- kind : 'Text' ,
78
- label : '{{label}}' ,
79
- size : 12 ,
80
- font : [
81
- 'sans-serif'
82
- ] ,
83
- opacity : 0.8 ,
84
- color : '#00b72b' ,
85
- offset : [
86
- 5 ,
87
- 5
88
- ] ,
89
- haloColor : '#00b72b' ,
90
- haloWidth : 1
91
- } ] ,
92
- filter : [
93
- '!=' ,
94
- 'label' ,
95
- 'undefined'
96
- ]
97
- } ]
98
- } ;
99
-
100
49
export type StylingComponentProps = CardStyleProps & { } ;
101
50
102
51
export const StylingComponent : React . FC < StylingComponentProps > = ( {
103
52
...passThroughProps
104
53
} ) : JSX . Element => {
105
54
106
- const [ style , setStyle ] = useState < GsStyle > ( defaultStyle ) ;
55
+ const style = useAppSelector ( state => {
56
+ const drawToolConfig = state . toolConfig . find ( config => config . name === ClientTools . DRAW_TOOLS ) ;
57
+
58
+ if ( ! drawToolConfig ) {
59
+ return ;
60
+ }
61
+
62
+ return ( drawToolConfig as DrawToolConfig ) . config . style ;
63
+ } ) ;
64
+
65
+ const data = useAppSelector ( state => {
66
+ const drawToolConfig = state . toolConfig . find ( config => config . name === ClientTools . DRAW_TOOLS ) ;
67
+
68
+ if ( ! drawToolConfig ) {
69
+ return ;
70
+ }
71
+
72
+ const features = ( drawToolConfig as DrawToolConfig ) . config . features ;
73
+
74
+ if ( ! features ) {
75
+ return ;
76
+ }
77
+
78
+ const d : VectorData = {
79
+ exampleFeatures : {
80
+ type : 'FeatureCollection' ,
81
+ features : [ ]
82
+ } ,
83
+ schema : {
84
+ properties : { } ,
85
+ type : 'FeatureCollection'
86
+ }
87
+ } ;
88
+
89
+ features . features . forEach ( feature => {
90
+ d . exampleFeatures . features . push ( feature ) ;
91
+ const properties = feature . properties ;
92
+
93
+ if ( ! properties ) {
94
+ return ;
95
+ }
96
+
97
+ Object . entries ( properties ) . forEach ( ( [ key , value ] ) => {
98
+ let type : 'string' | 'number' | 'boolean' | null = null ;
99
+
100
+ if ( typeof value === 'string' ) {
101
+ type = 'string' ;
102
+ }
103
+
104
+ if ( typeof value === 'number' ) {
105
+ type = 'number' ;
106
+ }
107
+
108
+ if ( typeof value === 'boolean' ) {
109
+ type = 'boolean' ;
110
+ }
111
+
112
+ if ( ! type ) {
113
+ return ;
114
+ }
115
+
116
+ d . schema . properties [ key ] = {
117
+ type : type ,
118
+ minimum : d . schema . properties [ key ] < value ? d . schema . properties [ key ] : value ,
119
+ maximum : d . schema . properties [ key ] > value ? d . schema . properties [ key ] : value
120
+ } ;
121
+ } ) ;
122
+ } ) ;
123
+
124
+ return d ;
125
+ } ) ;
126
+
127
+ const dispatch = useAppDispatch ( ) ;
107
128
108
129
const map = useMap ( ) ;
109
130
@@ -117,18 +138,21 @@ export const StylingComponent: React.FC<StylingComponentProps> = ({
117
138
118
139
const drawLayerStyleFunction = await parseStyle ( style ) ;
119
140
120
- drawVectorLayer . set ( 'gsStyle' , style ) ;
121
-
122
141
drawVectorLayer . setStyle ( drawLayerStyleFunction as OlStyleFunction ) ;
123
142
} ;
124
143
125
144
setStyleToLayer ( ) ;
126
145
} , [ style , map ] ) ;
127
146
147
+ const onStyleChange = ( gsStyle : Style ) => {
148
+ dispatch ( setDrawStyle ( gsStyle ) ) ;
149
+ } ;
150
+
128
151
return (
129
152
< CardStyle
130
153
style = { style }
131
- onStyleChange = { setStyle }
154
+ data = { data }
155
+ onStyleChange = { onStyleChange }
132
156
{ ...passThroughProps }
133
157
/>
134
158
) ;
0 commit comments