import {iAllRects, iPlainPopperConfig, tDirection} from "./type.base"; import {handleDirection} from "./handleDirection"; export function limitBoundary({ name, value, rects, config, direction }: { direction: tDirection, name: tDirection, value: number, rects: iAllRects, config: iPlainPopperConfig }): number { const { boundary, body } = rects; if (!boundary) {return value;} /*同方向的时候,才会限制的时候算上offset*/ const isDirectionVertical = direction === 'top' || direction === 'bottom'; const isNameVertical = name === 'top' || name === 'bottom'; const offset = isDirectionVertical === isNameVertical ? config.offset : 0; return handleDirection(name, { top: () => { return Math.max(boundary.top + offset, Math.min(boundary.top + boundary.height - body.height - offset, value)); }, bottom: () => { return Math.max(boundary.bottom, Math.min(boundary.bottom + boundary.height - body.height, value)); }, left: () => { return Math.max(boundary.left + offset, Math.min(boundary.left + boundary.width - body.width - offset, value)); }, right: () => { return Math.max(boundary.right, Math.min(boundary.right + boundary.width - body.width, value)); }, }); }