summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/selection/scope.ts
blob: 35e869457590dc6728fcaad5c20223feaec36983 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
export type ScopeEntry = {
	hasSelection: () => boolean;
	clear: () => void;
};

let activeScope: ScopeEntry | null = null;

export function activateScope({ entry }: { entry: ScopeEntry }): () => void {
	if (activeScope && activeScope !== entry) {
		activeScope.clear();
	}

	activeScope = entry;

	return () => {
		if (activeScope === entry) {
			activeScope = null;
		}
	};
}

export function clearActiveScope(): boolean {
	if (!activeScope?.hasSelection()) {
		return false;
	}

	activeScope.clear();
	return true;
}