@@ -11,14 +11,21 @@ import { Map } from 'maplibre-gl';
11
11
import { save } from 'reducers/editor' ;
12
12
import { ConfirmModal } from 'common/BootstrapSNCF/ModalSNCF' ;
13
13
import { NEW_ENTITY_ID } from 'applications/editor/data/utils' ;
14
+ import {
15
+ NULL_GEOMETRY ,
16
+ BufferStopEntity ,
17
+ DetectorEntity ,
18
+ SignalEntity ,
19
+ TrackSectionEntity ,
20
+ } from 'types' ;
14
21
import { LAYER_TO_EDITOAST_DICT , LayerType } from '../types' ;
15
22
import { getNearestPoint } from '../../../../utils/mapHelper' ;
16
23
import { getPointEditionLeftPanel , POINT_LAYER_ID , PointEditionMessages } from './components' ;
17
24
import { PointEditionState } from './types' ;
18
- import { NULL_GEOMETRY , BufferStopEntity , DetectorEntity , SignalEntity } from '../../../../types' ;
19
25
import { getEntity } from '../../data/api' ;
20
26
import { Tool } from '../editorContextTypes' ;
21
27
import { DEFAULT_COMMON_TOOL_STATE } from '../commonToolState' ;
28
+ import { approximateDistanceWithEditoastData } from '../utils' ;
22
29
23
30
type EditorPoint = BufferStopEntity | DetectorEntity | SignalEntity ;
24
31
interface PointEditionToolParams < T extends EditorPoint > {
@@ -29,6 +36,14 @@ interface PointEditionToolParams<T extends EditorPoint> {
29
36
requiresAngle ?: boolean ;
30
37
}
31
38
39
+ function calculateDistanceAlongTrack ( track : Feature < LineString > , point : Point ) {
40
+ const wrongPointOnTrack = nearestPointOnLine ( track . geometry , point , { units : 'meters' } ) ;
41
+ return approximateDistanceWithEditoastData (
42
+ track as TrackSectionEntity ,
43
+ wrongPointOnTrack . geometry
44
+ ) ;
45
+ }
46
+
32
47
function getPointEditionTool < T extends EditorPoint > ( {
33
48
layer,
34
49
icon,
@@ -61,20 +76,6 @@ function getPointEditionTool<T extends EditorPoint>({
61
76
getInitialState,
62
77
actions : [
63
78
[
64
- {
65
- id : 'reset-entity' ,
66
- icon : BiReset ,
67
- labelTranslationKey : `Editor.tools.${ id } -edition.actions.reset-entity` ,
68
- onClick ( { setState, state } ) {
69
- setState ( {
70
- ...getInitialState ( ) ,
71
- entity : state . initialEntity ,
72
- } ) ;
73
- } ,
74
- isDisabled ( { state } ) {
75
- return isEqual ( state . entity , state . initialEntity ) ;
76
- } ,
77
- } ,
78
79
{
79
80
id : 'new-entity' ,
80
81
icon : AiOutlinePlus ,
@@ -83,6 +84,19 @@ function getPointEditionTool<T extends EditorPoint>({
83
84
setState ( getInitialState ( ) ) ;
84
85
} ,
85
86
} ,
87
+ {
88
+ id : 'reset-entity' ,
89
+ icon : BiReset ,
90
+ labelTranslationKey : `Editor.tools.${ id } -edition.actions.reset-entity` ,
91
+ isDisabled ( { state : { entity, initialEntity } } ) {
92
+ return isEqual ( entity , initialEntity ) ;
93
+ } ,
94
+ onClick ( { setState, state : { initialEntity } } ) {
95
+ setState ( {
96
+ entity : cloneDeep ( initialEntity ) ,
97
+ } ) ;
98
+ } ,
99
+ } ,
86
100
] ,
87
101
[
88
102
{
@@ -116,6 +130,12 @@ function getPointEditionTool<T extends EditorPoint>({
116
130
] ,
117
131
118
132
// Interactions:
133
+ getCursor ( { state } , { isDragging } ) {
134
+ if ( isDragging || ! state . entity . geometry || isEqual ( state . entity . geometry , NULL_GEOMETRY ) )
135
+ return 'move' ;
136
+ if ( state . isHoveringTarget ) return 'pointer' ;
137
+ return 'default' ;
138
+ } ,
119
139
onClickMap ( _e , { setState, state, infraID, dispatch } ) {
120
140
const { isHoveringTarget, entity, nearestPoint } = state ;
121
141
if ( entity . geometry && ! isEqual ( entity . geometry , NULL_GEOMETRY ) && isHoveringTarget ) {
@@ -125,7 +145,6 @@ function getPointEditionTool<T extends EditorPoint>({
125
145
entity : omit ( entity , 'geometry' ) as T ,
126
146
} ) ;
127
147
}
128
-
129
148
if ( ( ! entity . geometry || isEqual ( entity . geometry , NULL_GEOMETRY ) ) && nearestPoint ) {
130
149
const newEntity = cloneDeep ( entity ) ;
131
150
newEntity . geometry = {
@@ -135,15 +154,13 @@ function getPointEditionTool<T extends EditorPoint>({
135
154
newEntity . properties = newEntity . properties || { } ;
136
155
newEntity . properties . track = nearestPoint . trackSectionID ;
137
156
138
- // retrieve the track section to be sure that the computation of the distance will be good
139
- // we can't trust maplibre, because the stored gemetry is not necessary the real one
140
157
getEntity ( infraID as number , newEntity . properties . track , 'TrackSection' , dispatch ) . then (
141
158
( track ) => {
142
- newEntity . properties . position = nearestPointOnLine (
143
- ( track as Feature < LineString > ) . geometry ,
144
- newEntity . geometry as Point ,
145
- { units : 'meters' }
146
- ) . properties ?. location ;
159
+ const distanceAlongTrack = calculateDistanceAlongTrack (
160
+ track as TrackSectionEntity ,
161
+ newEntity . geometry as Point
162
+ ) ;
163
+ newEntity . properties . position = distanceAlongTrack ;
147
164
148
165
setState ( {
149
166
...state ,
@@ -244,12 +261,6 @@ function getPointEditionTool<T extends EditorPoint>({
244
261
getInteractiveLayers ( ) {
245
262
return [ 'editor/geo/track-main' , POINT_LAYER_ID ] ;
246
263
} ,
247
- getCursor ( { state } , { isDragging } ) {
248
- if ( isDragging || ! state . entity . geometry || isEqual ( state . entity . geometry , NULL_GEOMETRY ) )
249
- return 'move' ;
250
- if ( state . isHoveringTarget ) return 'pointer' ;
251
- return 'default' ;
252
- } ,
253
264
254
265
layersComponent,
255
266
leftPanelComponent : getPointEditionLeftPanel ( LAYER_TO_EDITOAST_DICT [ layer ] ) ,
0 commit comments