in packages/shared/src/coordinate.ts [387:443]
export function calcSpaceBlockOfRect(
target: IRect,
source: IRect,
type?: string
) {
const targetRect = new Rect(target.x, target.y, target.width, target.height)
const sourceRect = new Rect(source.x, source.y, source.width, source.height)
if (sourceRect.bottom < targetRect.top && sourceRect.left > targetRect.right)
return
if (sourceRect.top > targetRect.bottom && sourceRect.left > targetRect.right)
return
if (sourceRect.bottom < targetRect.top && sourceRect.right < targetRect.left)
return
if (sourceRect.top > targetRect.bottom && sourceRect.right < targetRect.left)
return
if (sourceRect.bottom < targetRect.top) {
const distance = targetRect.top - sourceRect.bottom
const left = Math.min(sourceRect.left, targetRect.left)
const right = Math.max(sourceRect.right, targetRect.right)
if (type && type !== 'top') return
return {
type: 'top',
distance,
rect: new Rect(left, sourceRect.bottom, right - left, distance),
}
} else if (sourceRect.top > targetRect.bottom) {
const distance = sourceRect.top - targetRect.bottom
const left = Math.min(sourceRect.left, targetRect.left)
const right = Math.max(sourceRect.right, targetRect.right)
if (type && type !== 'bottom') return
return {
type: 'bottom',
distance,
rect: new Rect(left, targetRect.bottom, right - left, distance),
}
} else if (sourceRect.right < targetRect.left) {
const distance = targetRect.left - sourceRect.right
const top = Math.min(sourceRect.top, targetRect.top)
const bottom = Math.max(sourceRect.bottom, targetRect.bottom)
if (type && type !== 'left') return
return {
type: 'left',
distance,
rect: new Rect(sourceRect.right, top, distance, bottom - top),
}
} else if (sourceRect.left > targetRect.right) {
const distance = sourceRect.left - targetRect.right
const top = Math.min(sourceRect.top, targetRect.top)
const bottom = Math.max(sourceRect.bottom, targetRect.bottom)
if (type && type !== 'right') return
return {
type: 'right',
distance,
rect: new Rect(targetRect.right, top, distance, bottom - top),
}
}
}