11import {
22 SnapInfo , SnappableProps , SnappableState ,
33 SnapGuideline , ResizableProps , ScalableProps ,
4- SnapOffsetInfo , MoveableManagerInterface , SnapDirectionPoses ,
4+ SnapOffsetInfo , MoveableManagerInterface , SnapDirectionPoses , SnapDirectionInfo ,
55} from "../../types" ;
66import {
77 selectValue , getTinyDist , abs ,
@@ -17,6 +17,8 @@ export function checkMoveableSnapPoses(
1717 moveable : MoveableManagerInterface < SnappableProps , SnappableState > ,
1818 posesX : number [ ] ,
1919 posesY : number [ ] ,
20+ dirXs : string [ ] = [ ] ,
21+ dirYs : string [ ] = [ ] ,
2022 customSnapThreshold ?: number ,
2123) {
2224 const props = moveable . props ;
@@ -26,6 +28,8 @@ export function checkMoveableSnapPoses(
2628 moveable . state . guidelines ,
2729 posesX ,
2830 posesY ,
31+ dirXs ,
32+ dirYs ,
2933 snapThreshold ,
3034 ) ;
3135}
@@ -34,11 +38,13 @@ export function checkSnapPoses(
3438 guidelines : SnapGuideline [ ] ,
3539 posesX : number [ ] ,
3640 posesY : number [ ] ,
41+ dirXs : string [ ] ,
42+ dirYs : string [ ] ,
3743 snapThreshold : number ,
3844) {
3945 return {
40- vertical : checkSnap ( guidelines , "vertical" , posesX , snapThreshold ) ,
41- horizontal : checkSnap ( guidelines , "horizontal" , posesY , snapThreshold ) ,
46+ vertical : checkSnap ( guidelines , "vertical" , posesX , snapThreshold , dirXs ) ,
47+ horizontal : checkSnap ( guidelines , "horizontal" , posesY , snapThreshold , dirYs ) ,
4248 } ;
4349}
4450export function checkSnapKeepRatio (
@@ -142,19 +148,49 @@ export function checkSnapKeepRatio(
142148 } ;
143149}
144150
151+
152+ function getStringDirection ( dir : number | string ) {
153+ let stringDirection = "" ;
154+
155+ if ( dir === - 1 || dir === "top" || dir === "left" ) {
156+ stringDirection = "start" ;
157+ } else if ( dir === 0 || dir === "center" || dir === "middle" ) {
158+ stringDirection = "center" ;
159+ } else if ( dir === 1 || dir === "right" || dir === "bottom" ) {
160+ stringDirection = "end" ;
161+ }
162+ return stringDirection ;
163+ }
164+
165+
145166export function checkSnaps (
146167 moveable : MoveableManagerInterface < SnappableProps , SnappableState > ,
147168 rect : SnapDirectionPoses ,
148169 customSnapThreshold ?: number ,
149- ) {
170+ ) : { vertical : SnapDirectionInfo ; horizontal : SnapDirectionInfo } {
150171 const poses = splitSnapDirectionPoses ( moveable . props . snapDirections , rect ) ;
151172
152- return checkMoveableSnapPoses (
173+ const result = checkMoveableSnapPoses (
153174 moveable ,
154175 poses . vertical ,
155176 poses . horizontal ,
177+ poses . verticalNames . map ( name => getStringDirection ( name ) ) ,
178+ poses . horizontalNames . map ( name => getStringDirection ( name ) ) ,
156179 customSnapThreshold ,
157180 ) ;
181+ const horizontalDirection = getStringDirection ( poses . horizontalNames [ result . horizontal . index ] ) ;
182+ const verticalDirection = getStringDirection ( poses . verticalNames [ result . vertical . index ] ) ;
183+
184+ return {
185+ vertical : {
186+ ...result . vertical ,
187+ direction : verticalDirection ,
188+ } ,
189+ horizontal : {
190+ ...result . horizontal ,
191+ direction : horizontalDirection ,
192+ } ,
193+ } ;
158194}
159195
160196export function getNearestSnapGuidelineInfo (
@@ -191,18 +227,22 @@ function checkSnap(
191227 targetType : "horizontal" | "vertical" ,
192228 targetPoses : number [ ] ,
193229 snapThreshold : number ,
230+ dirs : string [ ] = [ ] ,
194231) : SnapInfo {
195232 if ( ! guidelines || ! guidelines . length ) {
196233 return {
197234 isSnap : false ,
198235 index : - 1 ,
236+ direction : "" ,
199237 posInfos : [ ] ,
200238 } ;
201239 }
202240 const isVertical = targetType === "vertical" ;
203241 const posType = isVertical ? 0 : 1 ;
204242
205243 const snapPosInfos = targetPoses . map ( ( targetPos , index ) => {
244+ const direction = dirs [ index ] || "" ;
245+
206246 const guidelineInfos = guidelines . map ( guideline => {
207247 const { pos } = guideline ;
208248 const offset = targetPos - pos [ posType ] ;
@@ -211,6 +251,7 @@ function checkSnap(
211251 offset,
212252 dist : abs ( offset ) ,
213253 guideline,
254+ direction,
214255 } ;
215256 } ) . filter ( ( { guideline, dist } ) => {
216257 const { type } = guideline ;
@@ -230,6 +271,7 @@ function checkSnap(
230271 pos : targetPos ,
231272 index,
232273 guidelineInfos,
274+ direction,
233275 } ;
234276 } ) . filter ( snapPosInfo => {
235277 return snapPosInfo . guidelineInfos . length > 0 ;
@@ -241,54 +283,88 @@ function checkSnap(
241283 return {
242284 isSnap,
243285 index : isSnap ? snapPosInfos [ 0 ] . index : - 1 ,
286+ direction : snapPosInfos [ 0 ] ?. direction ?? "" ,
244287 posInfos : snapPosInfos ,
245288 } ;
246289}
247290
248291export function getSnapInfosByDirection (
249292 moveable : MoveableManagerInterface < SnappableProps & ( ResizableProps | ScalableProps ) , SnappableState > ,
293+ // pos1 pos2 pos3 pos4
250294 poses : number [ ] [ ] ,
251295 snapDirection : number [ ] ,
252296 snapThreshold = 1 ,
253- ) {
254- let nextPoses : number [ ] [ ] = [ ] ;
297+ ) : { vertical : SnapDirectionInfo ; horizontal : SnapDirectionInfo } {
298+ let dirs : number [ ] [ ] = [ ] ;
299+
255300 if ( snapDirection [ 0 ] && snapDirection [ 1 ] ) {
256- nextPoses = [
301+ dirs = [
257302 snapDirection ,
258303 [ - snapDirection [ 0 ] , snapDirection [ 1 ] ] ,
259304 [ snapDirection [ 0 ] , - snapDirection [ 1 ] ] ,
260- ] . map ( direction => getPosByDirection ( poses , direction ) ) ;
305+ ] ;
261306 } else if ( ! snapDirection [ 0 ] && ! snapDirection [ 1 ] ) {
262- const alignPoses = [ poses [ 0 ] , poses [ 1 ] , poses [ 3 ] , poses [ 2 ] , poses [ 0 ] ] ;
263-
264- for ( let i = 0 ; i < 4 ; ++ i ) {
265- nextPoses . push ( alignPoses [ i ] ) ;
266- nextPoses . push ( [
267- ( alignPoses [ i ] [ 0 ] + alignPoses [ i + 1 ] [ 0 ] ) / 2 ,
268- ( alignPoses [ i ] [ 1 ] + alignPoses [ i + 1 ] [ 1 ] ) / 2 ,
307+ [
308+ [ - 1 , - 1 ] ,
309+ [ 1 , - 1 ] ,
310+ [ 1 , 1 ] ,
311+ [ - 1 , 1 ] ,
312+ ] . forEach ( ( dir , i , arr ) => {
313+ const nextDir = ( arr [ i + 1 ] || arr [ 0 ] ) ;
314+ dirs . push ( dir ) ;
315+ dirs . push ( [
316+ ( dir [ 0 ] + nextDir [ 0 ] ) / 2 ,
317+ ( dir [ 1 ] + nextDir [ 1 ] ) / 2 ,
269318 ] ) ;
270- }
319+ } ) ;
271320 } else {
272321 if ( moveable . props . keepRatio ) {
273- nextPoses = [
322+ dirs . push (
274323 [ - 1 , - 1 ] ,
275324 [ - 1 , 1 ] ,
276325 [ 1 , - 1 ] ,
277326 [ 1 , 1 ] ,
278327 snapDirection ,
279- ] . map ( dir => getPosByDirection ( poses , dir ) ) ;
328+ ) ;
280329 } else {
281- nextPoses = getPosesByDirection ( poses , snapDirection ) ;
330+ dirs . push ( ...getPosesByDirection ( [
331+ [ - 1 , - 1 ] ,
332+ [ 1 , - 1 ] ,
333+ [ - 1 , - 1 ] ,
334+ [ 1 , 1 ] ,
335+ ] , snapDirection ) ) ;
282336
283- if ( nextPoses . length > 1 ) {
284- nextPoses . push ( [
285- ( nextPoses [ 0 ] [ 0 ] + nextPoses [ 1 ] [ 0 ] ) / 2 ,
286- ( nextPoses [ 0 ] [ 1 ] + nextPoses [ 1 ] [ 1 ] ) / 2 ,
337+ if ( dirs . length > 1 ) {
338+ dirs . push ( [
339+ ( dirs [ 0 ] [ 0 ] + dirs [ 1 ] [ 0 ] ) / 2 ,
340+ ( dirs [ 0 ] [ 1 ] + dirs [ 1 ] [ 1 ] ) / 2 ,
287341 ] ) ;
288342 }
289343 }
290344 }
291- return checkMoveableSnapPoses ( moveable , nextPoses . map ( pos => pos [ 0 ] ) , nextPoses . map ( pos => pos [ 1 ] ) , snapThreshold ) ;
345+ const nextPoses = dirs . map ( dir => getPosByDirection ( poses , dir ) ) ;
346+ const xs = nextPoses . map ( pos => pos [ 0 ] ) ;
347+ const ys = nextPoses . map ( pos => pos [ 1 ] ) ;
348+ const result = checkMoveableSnapPoses (
349+ moveable ,
350+ xs , ys ,
351+ dirs . map ( dir => getStringDirection ( dir [ 0 ] ) ) ,
352+ dirs . map ( dir => getStringDirection ( dir [ 1 ] ) ) ,
353+ snapThreshold
354+ ) ;
355+ const verticalDirection = getStringDirection ( dirs . map ( dir => dir [ 0 ] ) [ result . vertical . index ] ) ;
356+ const horizontalDirection = getStringDirection ( dirs . map ( dir => dir [ 1 ] ) [ result . horizontal . index ] ) ;
357+
358+ return {
359+ vertical : {
360+ ...result . vertical ,
361+ direction : verticalDirection ,
362+ } ,
363+ horizontal : {
364+ ...result . horizontal ,
365+ direction : horizontalDirection ,
366+ } ,
367+ } ;
292368}
293369
294370export function checkSnapBoundPriority (
0 commit comments