|
| 1 | +import type { Layout, RegisteredGroup } from "../../components/group/types"; |
| 2 | +import { |
| 3 | + CURSOR_FLAG_HORIZONTAL_MAX, |
| 4 | + CURSOR_FLAG_HORIZONTAL_MIN, |
| 5 | + CURSOR_FLAG_VERTICAL_MAX, |
| 6 | + CURSOR_FLAG_VERTICAL_MIN |
| 7 | +} from "../../constants"; |
| 8 | +import type { Point } from "../../types"; |
| 9 | +import { updateCursorStyle } from "../cursor/updateCursorStyle"; |
| 10 | +import type { HitRegion } from "../dom/calculateHitRegions"; |
| 11 | +import { update, type MountedGroupMap } from "../mutableState"; |
| 12 | +import { adjustLayoutByDelta } from "./adjustLayoutByDelta"; |
| 13 | +import { layoutsEqual } from "./layoutsEqual"; |
| 14 | + |
| 15 | +export function updateActiveHitRegions({ |
| 16 | + event, |
| 17 | + hitRegions, |
| 18 | + initialLayoutMap, |
| 19 | + mountedGroups, |
| 20 | + pointerDownAtPoint |
| 21 | +}: { |
| 22 | + event: { |
| 23 | + clientX: number; |
| 24 | + clientY: number; |
| 25 | + }; |
| 26 | + hitRegions: HitRegion[]; |
| 27 | + initialLayoutMap: Map<RegisteredGroup, Layout>; |
| 28 | + mountedGroups: MountedGroupMap; |
| 29 | + pointerDownAtPoint?: Point; |
| 30 | +}) { |
| 31 | + let cursorFlags = 0; |
| 32 | + const nextMountedGroups = new Map(mountedGroups); |
| 33 | + |
| 34 | + // Note that HitRegions are frozen once a drag has started |
| 35 | + // Modify the Group layouts for all matching HitRegions though |
| 36 | + hitRegions.forEach((current) => { |
| 37 | + const { disableCursor, element, orientation, panels } = current.group; |
| 38 | + |
| 39 | + let deltaAsPercentage = 0; |
| 40 | + if (pointerDownAtPoint) { |
| 41 | + if (orientation === "horizontal") { |
| 42 | + deltaAsPercentage = |
| 43 | + ((event.clientX - pointerDownAtPoint.x) / element.offsetWidth) * 100; |
| 44 | + } else { |
| 45 | + deltaAsPercentage = |
| 46 | + ((event.clientY - pointerDownAtPoint.y) / element.offsetHeight) * 100; |
| 47 | + } |
| 48 | + } else { |
| 49 | + if (orientation === "horizontal") { |
| 50 | + deltaAsPercentage = event.clientX < 0 ? -100 : 100; |
| 51 | + } else { |
| 52 | + deltaAsPercentage = event.clientY < 0 ? -100 : 100; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + const initialLayout = initialLayoutMap.get(current.group); |
| 57 | + |
| 58 | + const { |
| 59 | + derivedPanelConstraints, |
| 60 | + layout: prevLayout, |
| 61 | + separatorToPanels |
| 62 | + } = mountedGroups.get(current.group) ?? {}; |
| 63 | + if ( |
| 64 | + derivedPanelConstraints && |
| 65 | + initialLayout && |
| 66 | + prevLayout && |
| 67 | + separatorToPanels |
| 68 | + ) { |
| 69 | + const nextLayout = adjustLayoutByDelta({ |
| 70 | + delta: deltaAsPercentage, |
| 71 | + initialLayout, |
| 72 | + panelConstraints: derivedPanelConstraints, |
| 73 | + pivotIndices: current.panels.map((panel) => panels.indexOf(panel)), |
| 74 | + prevLayout, |
| 75 | + trigger: "mouse-or-touch" |
| 76 | + }); |
| 77 | + |
| 78 | + if (layoutsEqual(nextLayout, prevLayout)) { |
| 79 | + if (deltaAsPercentage !== 0 && !disableCursor) { |
| 80 | + // An unchanged means the cursor has exceeded the allowed bounds |
| 81 | + switch (orientation) { |
| 82 | + case "horizontal": { |
| 83 | + cursorFlags |= |
| 84 | + deltaAsPercentage < 0 |
| 85 | + ? CURSOR_FLAG_HORIZONTAL_MIN |
| 86 | + : CURSOR_FLAG_HORIZONTAL_MAX; |
| 87 | + break; |
| 88 | + } |
| 89 | + case "vertical": { |
| 90 | + cursorFlags |= |
| 91 | + deltaAsPercentage < 0 |
| 92 | + ? CURSOR_FLAG_VERTICAL_MIN |
| 93 | + : CURSOR_FLAG_VERTICAL_MAX; |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } else { |
| 99 | + nextMountedGroups.set(current.group, { |
| 100 | + derivedPanelConstraints: derivedPanelConstraints, |
| 101 | + layout: nextLayout, |
| 102 | + separatorToPanels |
| 103 | + }); |
| 104 | + |
| 105 | + // Save the most recent layout for this group of panels in-memory |
| 106 | + // so that layouts will be remembered between different sets of conditionally rendered panels |
| 107 | + const panelIdsKey = current.group.panels.map(({ id }) => id).join(","); |
| 108 | + current.group.inMemoryLayouts[panelIdsKey] = nextLayout; |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + update({ |
| 114 | + cursorFlags, |
| 115 | + mountedGroups: nextMountedGroups |
| 116 | + }); |
| 117 | + |
| 118 | + updateCursorStyle(); |
| 119 | +} |
0 commit comments