2020-01-06 20:24:54 +04:00
|
|
|
import { ExcalidrawElement } from "../element/types";
|
2020-01-26 20:15:08 +01:00
|
|
|
import { getCommonBounds } from "../element";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
|
|
|
const SCROLLBAR_MIN_SIZE = 15;
|
|
|
|
const SCROLLBAR_MARGIN = 4;
|
|
|
|
export const SCROLLBAR_WIDTH = 6;
|
|
|
|
export const SCROLLBAR_COLOR = "rgba(0,0,0,0.3)";
|
|
|
|
|
|
|
|
export function getScrollBars(
|
2020-01-09 19:22:04 +04:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-01-06 20:24:54 +04:00
|
|
|
canvasWidth: number,
|
|
|
|
canvasHeight: number,
|
|
|
|
scrollX: number,
|
2020-01-24 12:04:54 +02:00
|
|
|
scrollY: number,
|
2020-01-06 20:24:54 +04:00
|
|
|
) {
|
2020-01-26 20:15:08 +01:00
|
|
|
let [minX, minY, maxX, maxY] = getCommonBounds(elements);
|
2020-01-06 20:24:54 +04:00
|
|
|
|
|
|
|
minX += scrollX;
|
|
|
|
maxX += scrollX;
|
|
|
|
minY += scrollY;
|
|
|
|
maxY += scrollY;
|
2020-01-26 20:15:08 +01:00
|
|
|
|
2020-01-06 20:24:54 +04:00
|
|
|
const leftOverflow = Math.max(-minX, 0);
|
|
|
|
const rightOverflow = Math.max(-(canvasWidth - maxX), 0);
|
|
|
|
const topOverflow = Math.max(-minY, 0);
|
|
|
|
const bottomOverflow = Math.max(-(canvasHeight - maxY), 0);
|
|
|
|
|
|
|
|
// horizontal scrollbar
|
|
|
|
let horizontalScrollBar = null;
|
|
|
|
if (leftOverflow || rightOverflow) {
|
|
|
|
horizontalScrollBar = {
|
|
|
|
x: Math.min(
|
|
|
|
leftOverflow + SCROLLBAR_MARGIN,
|
2020-01-24 12:04:54 +02:00
|
|
|
canvasWidth - SCROLLBAR_MIN_SIZE - SCROLLBAR_MARGIN,
|
2020-01-06 20:24:54 +04:00
|
|
|
),
|
|
|
|
y: canvasHeight - SCROLLBAR_WIDTH - SCROLLBAR_MARGIN,
|
|
|
|
width: Math.max(
|
|
|
|
canvasWidth - rightOverflow - leftOverflow - SCROLLBAR_MARGIN * 2,
|
2020-01-24 12:04:54 +02:00
|
|
|
SCROLLBAR_MIN_SIZE,
|
2020-01-06 20:24:54 +04:00
|
|
|
),
|
2020-01-24 12:04:54 +02:00
|
|
|
height: SCROLLBAR_WIDTH,
|
2020-01-06 20:24:54 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// vertical scrollbar
|
|
|
|
let verticalScrollBar = null;
|
|
|
|
if (topOverflow || bottomOverflow) {
|
|
|
|
verticalScrollBar = {
|
|
|
|
x: canvasWidth - SCROLLBAR_WIDTH - SCROLLBAR_MARGIN,
|
|
|
|
y: Math.min(
|
|
|
|
topOverflow + SCROLLBAR_MARGIN,
|
2020-01-24 12:04:54 +02:00
|
|
|
canvasHeight - SCROLLBAR_MIN_SIZE - SCROLLBAR_MARGIN,
|
2020-01-06 20:24:54 +04:00
|
|
|
),
|
|
|
|
width: SCROLLBAR_WIDTH,
|
|
|
|
height: Math.max(
|
|
|
|
canvasHeight - bottomOverflow - topOverflow - SCROLLBAR_WIDTH * 2,
|
2020-01-24 12:04:54 +02:00
|
|
|
SCROLLBAR_MIN_SIZE,
|
|
|
|
),
|
2020-01-06 20:24:54 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
horizontal: horizontalScrollBar,
|
2020-01-24 12:04:54 +02:00
|
|
|
vertical: verticalScrollBar,
|
2020-01-06 20:24:54 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isOverScrollBars(
|
2020-01-09 19:22:04 +04:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-01-06 20:24:54 +04:00
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
canvasWidth: number,
|
|
|
|
canvasHeight: number,
|
|
|
|
scrollX: number,
|
2020-01-24 12:04:54 +02:00
|
|
|
scrollY: number,
|
2020-01-06 20:24:54 +04:00
|
|
|
) {
|
|
|
|
const scrollBars = getScrollBars(
|
|
|
|
elements,
|
|
|
|
canvasWidth,
|
|
|
|
canvasHeight,
|
|
|
|
scrollX,
|
2020-01-24 12:04:54 +02:00
|
|
|
scrollY,
|
2020-01-06 20:24:54 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
const [isOverHorizontalScrollBar, isOverVerticalScrollBar] = [
|
|
|
|
scrollBars.horizontal,
|
2020-01-24 12:04:54 +02:00
|
|
|
scrollBars.vertical,
|
2020-01-06 20:24:54 +04:00
|
|
|
].map(
|
|
|
|
scrollBar =>
|
|
|
|
scrollBar &&
|
|
|
|
scrollBar.x <= x &&
|
|
|
|
x <= scrollBar.x + scrollBar.width &&
|
|
|
|
scrollBar.y <= y &&
|
2020-01-24 12:04:54 +02:00
|
|
|
y <= scrollBar.y + scrollBar.height,
|
2020-01-06 20:24:54 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
isOverHorizontalScrollBar,
|
2020-01-24 12:04:54 +02:00
|
|
|
isOverVerticalScrollBar,
|
2020-01-06 20:24:54 +04:00
|
|
|
};
|
|
|
|
}
|