Line data Source code
1 : import type {
2 : Point2D,
3 : ReadOnlyRectEdges as RectEdges
4 : } from '@comfyorg/shared-frontend-utils/geometry'
5 :
6 : export interface MarqueeCard {
7 : id: string
8 : rect: RectEdges
9 : }
10 :
11 54 : export function normalizeMarqueeRect(start: Point2D, end: Point2D): RectEdges {
12 54 : return {
13 : left: Math.min(start.x, end.x),
14 : top: Math.min(start.y, end.y),
15 : right: Math.max(start.x, end.x),
16 : bottom: Math.max(start.y, end.y)
17 : }
18 : }
19 :
20 108 : function rectsIntersect(a: RectEdges, b: RectEdges): boolean {
21 108 : return !(
22 : a.right < b.left ||
23 : a.left > b.right ||
24 : a.bottom < b.top ||
25 : a.top > b.bottom
26 : )
27 : }
28 :
29 : /**
30 : * Resolve the asset ids a marquee covers, starting from `baseIds` (the selection
31 : * to preserve when a modifier makes the drag additive). With `subtract`, covered
32 : * ids are removed from `baseIds` instead of added. A fresh Set is returned;
33 : * `baseIds` is never mutated.
34 : */
35 54 : export function selectMarqueeIds(
36 : cards: readonly MarqueeCard[],
37 : marquee: RectEdges,
38 : baseIds: Iterable<string> = [],
39 : subtract = false
40 : ): Set<string> {
41 54 : const result = new Set(baseIds)
42 54 : for (const { id, rect } of cards) {
43 108 : if (!rectsIntersect(rect, marquee)) continue
44 108 : if (subtract) result.delete(id)
45 54 : else result.add(id)
46 : }
47 54 : return result
48 : }
|