@@ -795,65 +795,76 @@ class GeometryEditor extends Eventable(Class) {
795
795
const verticeLimit = shadow instanceof Polygon ? 3 : 2 ;
796
796
const propertyOfVertexRefreshFn = 'maptalks--editor-refresh-fn' ,
797
797
propertyOfVertexIndex = 'maptalks--editor-vertex-index' ;
798
- const vertexHandles = [ ] ,
799
- newVertexHandles = [ ] ;
798
+ //{ ringIndex:ring }
799
+ const vertexHandles = { 0 : [ ] } ,
800
+ newVertexHandles = { 0 : [ ] } ;
800
801
801
- function getVertexCoordinates ( ) {
802
+ function getVertexCoordinates ( ringIndex = 0 ) {
802
803
if ( shadow instanceof Polygon ) {
803
- const coordinates = shadow . getCoordinates ( ) [ 0 ] ;
804
+ const coordinates = shadow . getCoordinates ( ) [ ringIndex ] || [ ] ;
804
805
return coordinates . slice ( 0 , coordinates . length - 1 ) ;
805
806
} else {
806
807
return shadow . getCoordinates ( ) ;
807
808
}
808
809
809
810
}
810
811
811
- function getVertexPrjCoordinates ( ) {
812
- return shadow . _getPrjCoordinates ( ) ;
812
+ function getVertexPrjCoordinates ( ringIndex = 0 ) {
813
+ if ( ringIndex === 0 ) {
814
+ return shadow . _getPrjCoordinates ( ) ;
815
+ }
816
+ return shadow . _getPrjHoles ( ) [ ringIndex - 1 ] ;
813
817
}
814
818
815
819
function onVertexAddOrRemove ( ) {
816
820
//restore index property of each handles.
817
- for ( let i = vertexHandles . length - 1 ; i >= 0 ; i -- ) {
818
- vertexHandles [ i ] [ propertyOfVertexIndex ] = i ;
819
- }
820
- for ( let i = newVertexHandles . length - 1 ; i >= 0 ; i -- ) {
821
- newVertexHandles [ i ] [ propertyOfVertexIndex ] = i ;
821
+ for ( const ringIndex in vertexHandles ) {
822
+ for ( let i = vertexHandles [ ringIndex ] . length - 1 ; i >= 0 ; i -- ) {
823
+ vertexHandles [ ringIndex ] [ i ] [ propertyOfVertexIndex ] = i ;
824
+ }
825
+ for ( let i = newVertexHandles [ ringIndex ] . length - 1 ; i >= 0 ; i -- ) {
826
+ newVertexHandles [ ringIndex ] [ i ] [ propertyOfVertexIndex ] = i ;
827
+ }
822
828
}
823
829
me . _updateCoordFromShadow ( ) ;
824
830
}
825
831
826
832
function removeVertex ( param ) {
827
833
const handle = param [ 'target' ] ,
828
834
index = handle [ propertyOfVertexIndex ] ;
829
- const prjCoordinates = getVertexPrjCoordinates ( ) ;
835
+ const ringIndex = isNumber ( handle . _ringIndex ) ? handle . _ringIndex : 0 ;
836
+ const prjCoordinates = getVertexPrjCoordinates ( ringIndex ) ;
830
837
if ( prjCoordinates . length <= verticeLimit ) {
831
838
return ;
832
839
}
833
840
prjCoordinates . splice ( index , 1 ) ;
834
- shadow . _setPrjCoordinates ( prjCoordinates ) ;
841
+ if ( ringIndex > 0 ) {
842
+ shadow . _prjHoles [ ringIndex - 1 ] = prjCoordinates ;
843
+ } else {
844
+ shadow . _setPrjCoordinates ( prjCoordinates ) ;
845
+ }
835
846
shadow . _updateCache ( ) ;
836
847
//remove vertex handle
837
- vertexHandles . splice ( index , 1 ) [ 0 ] . remove ( ) ;
848
+ vertexHandles [ ringIndex ] . splice ( index , 1 ) [ 0 ] . remove ( ) ;
838
849
//remove two neighbor "new vertex" handles
839
- if ( index < newVertexHandles . length ) {
840
- newVertexHandles . splice ( index , 1 ) [ 0 ] . remove ( ) ;
850
+ if ( index < newVertexHandles [ ringIndex ] . length ) {
851
+ newVertexHandles [ ringIndex ] . splice ( index , 1 ) [ 0 ] . remove ( ) ;
841
852
}
842
853
let nextIndex ;
843
854
if ( index === 0 ) {
844
- nextIndex = newVertexHandles . length - 1 ;
855
+ nextIndex = newVertexHandles [ ringIndex ] . length - 1 ;
845
856
} else {
846
857
nextIndex = index - 1 ;
847
858
}
848
- newVertexHandles . splice ( nextIndex , 1 ) [ 0 ] . remove ( ) ;
859
+ newVertexHandles [ ringIndex ] . splice ( nextIndex , 1 ) [ 0 ] . remove ( ) ;
849
860
//add a new "new vertex" handle.
850
- newVertexHandles . splice ( nextIndex , 0 , createNewVertexHandle . call ( me , nextIndex ) ) ;
861
+ newVertexHandles [ ringIndex ] . splice ( nextIndex , 0 , createNewVertexHandle . call ( me , nextIndex , ringIndex ) ) ;
851
862
onVertexAddOrRemove ( ) ;
852
863
me . _refresh ( ) ;
853
864
}
854
865
855
- function moveVertexHandle ( handleViewPoint , index ) {
856
- const vertice = getVertexPrjCoordinates ( ) ;
866
+ function moveVertexHandle ( handleViewPoint , index , ringIndex = 0 ) {
867
+ const vertice = getVertexPrjCoordinates ( ringIndex ) ;
857
868
const nVertex = map . _viewPointToPrj ( handleViewPoint ) ;
858
869
const pVertex = vertice [ index ] ;
859
870
pVertex . x = nVertex . x ;
@@ -863,30 +874,30 @@ class GeometryEditor extends Eventable(Class) {
863
874
me . _updateCoordFromShadow ( true ) ;
864
875
let nextIndex ;
865
876
if ( index === 0 ) {
866
- nextIndex = newVertexHandles . length - 1 ;
877
+ nextIndex = newVertexHandles [ ringIndex ] . length - 1 ;
867
878
} else {
868
879
nextIndex = index - 1 ;
869
880
}
870
881
//refresh two neighbor "new vertex" handles.
871
- if ( newVertexHandles [ index ] ) {
872
- newVertexHandles [ index ] [ propertyOfVertexRefreshFn ] ( ) ;
882
+ if ( newVertexHandles [ ringIndex ] [ index ] ) {
883
+ newVertexHandles [ ringIndex ] [ index ] [ propertyOfVertexRefreshFn ] ( ) ;
873
884
}
874
- if ( newVertexHandles [ nextIndex ] ) {
875
- newVertexHandles [ nextIndex ] [ propertyOfVertexRefreshFn ] ( ) ;
885
+ if ( newVertexHandles [ ringIndex ] [ nextIndex ] ) {
886
+ newVertexHandles [ ringIndex ] [ nextIndex ] [ propertyOfVertexRefreshFn ] ( ) ;
876
887
}
877
888
}
878
889
879
- function createVertexHandle ( index ) {
880
- let vertex = getVertexCoordinates ( ) [ index ] ;
890
+ function createVertexHandle ( index , ringIndex = 0 ) {
891
+ let vertex = getVertexCoordinates ( ringIndex ) [ index ] ;
881
892
const handle = me . createHandle ( vertex , {
882
893
'symbol' : me . options [ 'vertexHandleSymbol' ] ,
883
894
'cursor' : 'pointer' ,
884
895
'axis' : null ,
885
896
onMove : function ( handleViewPoint ) {
886
- moveVertexHandle ( handleViewPoint , handle [ propertyOfVertexIndex ] ) ;
897
+ moveVertexHandle ( handleViewPoint , handle [ propertyOfVertexIndex ] , ringIndex ) ;
887
898
} ,
888
899
onRefresh : function ( ) {
889
- vertex = getVertexCoordinates ( ) [ handle [ propertyOfVertexIndex ] ] ;
900
+ vertex = getVertexCoordinates ( ringIndex ) [ handle [ propertyOfVertexIndex ] ] ;
890
901
handle . setCoordinates ( vertex ) ;
891
902
} ,
892
903
onUp : function ( ) {
@@ -900,12 +911,13 @@ class GeometryEditor extends Eventable(Class) {
900
911
}
901
912
} ) ;
902
913
handle [ propertyOfVertexIndex ] = index ;
914
+ handle . _ringIndex = ringIndex ;
903
915
handle . on ( me . options [ 'removeVertexOn' ] , removeVertex ) ;
904
916
return handle ;
905
917
}
906
918
907
- function createNewVertexHandle ( index ) {
908
- let vertexCoordinates = getVertexCoordinates ( ) ;
919
+ function createNewVertexHandle ( index , ringIndex = 0 ) {
920
+ let vertexCoordinates = getVertexCoordinates ( ringIndex ) ;
909
921
let nextVertex ;
910
922
if ( index + 1 >= vertexCoordinates . length ) {
911
923
nextVertex = vertexCoordinates [ 0 ] ;
@@ -921,42 +933,46 @@ class GeometryEditor extends Eventable(Class) {
921
933
if ( e && e . domEvent && e . domEvent . button === 2 ) {
922
934
return ;
923
935
}
924
- const prjCoordinates = getVertexPrjCoordinates ( ) ;
936
+ const prjCoordinates = getVertexPrjCoordinates ( ringIndex ) ;
925
937
const vertexIndex = handle [ propertyOfVertexIndex ] ;
926
938
//add a new vertex
927
939
const pVertex = projection . project ( handle . getCoordinates ( ) ) ;
928
940
//update shadow's vertice
929
941
prjCoordinates . splice ( vertexIndex + 1 , 0 , pVertex ) ;
930
- shadow . _setPrjCoordinates ( prjCoordinates ) ;
942
+ if ( ringIndex > 0 ) {
943
+ //update hole
944
+ shadow . _prjHoles [ ringIndex - 1 ] = prjCoordinates ;
945
+ } else {
946
+ shadow . _setPrjCoordinates ( prjCoordinates ) ;
947
+ }
931
948
shadow . _updateCache ( ) ;
932
949
933
950
const symbol = handle . getSymbol ( ) ;
934
951
delete symbol [ 'opacity' ] ;
935
952
handle . setSymbol ( symbol ) ;
936
953
937
954
//add two "new vertex" handles
938
- newVertexHandles . splice ( vertexIndex , 0 , createNewVertexHandle . call ( me , vertexIndex ) , createNewVertexHandle . call ( me , vertexIndex + 1 ) ) ;
939
-
955
+ newVertexHandles [ ringIndex ] . splice ( vertexIndex , 0 , createNewVertexHandle . call ( me , vertexIndex , ringIndex ) , createNewVertexHandle . call ( me , vertexIndex + 1 , ringIndex ) ) ;
940
956
} ,
941
957
onMove : function ( handleViewPoint ) {
942
- moveVertexHandle ( handleViewPoint , handle [ propertyOfVertexIndex ] + 1 ) ;
958
+ moveVertexHandle ( handleViewPoint , handle [ propertyOfVertexIndex ] + 1 , ringIndex ) ;
943
959
} ,
944
960
onUp : function ( e ) {
945
961
if ( e && e . domEvent && e . domEvent . button === 2 ) {
946
962
return ;
947
963
}
948
964
const vertexIndex = handle [ propertyOfVertexIndex ] ;
949
965
//remove this handle
950
- removeFromArray ( handle , newVertexHandles ) ;
966
+ removeFromArray ( handle , newVertexHandles [ ringIndex ] ) ;
951
967
handle . remove ( ) ;
952
968
//add a new vertex handle
953
- vertexHandles . splice ( vertexIndex + 1 , 0 , createVertexHandle . call ( me , vertexIndex + 1 ) ) ;
969
+ vertexHandles [ ringIndex ] . splice ( vertexIndex + 1 , 0 , createVertexHandle . call ( me , vertexIndex + 1 , ringIndex ) ) ;
954
970
onVertexAddOrRemove ( ) ;
955
971
me . _updateCoordFromShadow ( ) ;
956
972
me . _refresh ( ) ;
957
973
} ,
958
974
onRefresh : function ( ) {
959
- vertexCoordinates = getVertexCoordinates ( ) ;
975
+ vertexCoordinates = getVertexCoordinates ( ringIndex ) ;
960
976
const vertexIndex = handle [ propertyOfVertexIndex ] ;
961
977
let nextIndex ;
962
978
if ( vertexIndex === vertexCoordinates . length - 1 ) {
@@ -971,23 +987,42 @@ class GeometryEditor extends Eventable(Class) {
971
987
handle [ propertyOfVertexIndex ] = index ;
972
988
return handle ;
973
989
}
974
- const vertexCoordinates = getVertexCoordinates ( ) ;
975
- for ( let i = 0 , len = vertexCoordinates . length ; i < len ; i ++ ) {
976
- vertexHandles . push ( createVertexHandle . call ( this , i ) ) ;
977
- if ( i < len - 1 ) {
978
- newVertexHandles . push ( createNewVertexHandle . call ( this , i ) ) ;
979
- }
980
- }
981
990
if ( shadow instanceof Polygon ) {
982
- //1 more vertex handle for polygon
983
- newVertexHandles . push ( createNewVertexHandle . call ( this , vertexCoordinates . length - 1 ) ) ;
991
+ const rings = shadow . getHoles ( ) . length + 1 ;
992
+ for ( let ringIndex = 0 ; ringIndex < rings ; ringIndex ++ ) {
993
+ vertexHandles [ ringIndex ] = [ ] ;
994
+ newVertexHandles [ ringIndex ] = [ ] ;
995
+ const vertexCoordinates = getVertexCoordinates ( ringIndex ) ;
996
+ for ( let i = 0 , len = vertexCoordinates . length ; i < len ; i ++ ) {
997
+ vertexHandles [ ringIndex ] . push ( createVertexHandle . call ( this , i , ringIndex ) ) ;
998
+ if ( i < len - 1 ) {
999
+ newVertexHandles [ ringIndex ] . push ( createNewVertexHandle . call ( this , i , ringIndex ) ) ;
1000
+ }
1001
+ }
1002
+ //1 more vertex handle for polygon
1003
+ newVertexHandles [ ringIndex ] . push ( createNewVertexHandle . call ( this , vertexCoordinates . length - 1 , ringIndex ) ) ;
1004
+ }
1005
+
1006
+ } else {
1007
+ const ringIndex = 0 ;
1008
+ const vertexCoordinates = getVertexCoordinates ( ringIndex ) ;
1009
+ for ( let i = 0 , len = vertexCoordinates . length ; i < len ; i ++ ) {
1010
+ vertexHandles [ ringIndex ] . push ( createVertexHandle . call ( this , i , ringIndex ) ) ;
1011
+ if ( i < len - 1 ) {
1012
+ newVertexHandles [ ringIndex ] . push ( createNewVertexHandle . call ( this , i , ringIndex ) ) ;
1013
+ }
1014
+ }
984
1015
}
985
1016
this . _addRefreshHook ( ( ) => {
986
- for ( let i = newVertexHandles . length - 1 ; i >= 0 ; i -- ) {
987
- newVertexHandles [ i ] [ propertyOfVertexRefreshFn ] ( ) ;
1017
+ for ( const ringIndex in newVertexHandles ) {
1018
+ for ( let i = newVertexHandles [ ringIndex ] . length - 1 ; i >= 0 ; i -- ) {
1019
+ newVertexHandles [ ringIndex ] [ i ] [ propertyOfVertexRefreshFn ] ( ringIndex ) ;
1020
+ }
988
1021
}
989
- for ( let i = vertexHandles . length - 1 ; i >= 0 ; i -- ) {
990
- vertexHandles [ i ] [ propertyOfVertexRefreshFn ] ( ) ;
1022
+ for ( const ringIndex in vertexHandles ) {
1023
+ for ( let i = vertexHandles [ ringIndex ] . length - 1 ; i >= 0 ; i -- ) {
1024
+ vertexHandles [ ringIndex ] [ i ] [ propertyOfVertexRefreshFn ] ( ringIndex ) ;
1025
+ }
991
1026
}
992
1027
} ) ;
993
1028
}
0 commit comments