Files
OpenCut/apps/web/src/core/managers/selection-manager.ts
T

73 lines
1.7 KiB
TypeScript
Raw Normal View History

2026-01-31 00:20:04 +01:00
import type { EditorCore } from "@/core";
2026-02-27 16:33:57 +01:00
import type { SelectedKeyframeRef } from "@/types/animation";
2026-01-31 00:20:04 +01:00
type ElementRef = { trackId: string; elementId: string };
export class SelectionManager {
private selectedElements: ElementRef[] = [];
2026-02-27 16:33:57 +01:00
private selectedKeyframes: SelectedKeyframeRef[] = [];
private keyframeSelectionAnchor: SelectedKeyframeRef | null = null;
2026-01-31 00:20:04 +01:00
private listeners = new Set<() => void>();
constructor(editor: EditorCore) {
void editor;
}
getSelectedElements(): ElementRef[] {
return this.selectedElements;
}
2026-02-27 16:33:57 +01:00
getSelectedKeyframes(): SelectedKeyframeRef[] {
return this.selectedKeyframes;
}
getKeyframeSelectionAnchor(): SelectedKeyframeRef | null {
return this.keyframeSelectionAnchor;
}
2026-01-31 00:20:04 +01:00
setSelectedElements({ elements }: { elements: ElementRef[] }): void {
this.selectedElements = elements;
2026-02-27 16:33:57 +01:00
this.selectedKeyframes = [];
this.keyframeSelectionAnchor = null;
this.notify();
}
setSelectedKeyframes({
keyframes,
anchorKeyframe,
}: {
keyframes: SelectedKeyframeRef[];
anchorKeyframe?: SelectedKeyframeRef | null;
}): void {
this.selectedKeyframes = keyframes;
if (anchorKeyframe !== undefined) {
this.keyframeSelectionAnchor = anchorKeyframe;
} else if (keyframes.length === 0) {
this.keyframeSelectionAnchor = null;
}
2026-01-31 00:20:04 +01:00
this.notify();
}
clearSelection(): void {
this.selectedElements = [];
2026-02-27 16:33:57 +01:00
this.selectedKeyframes = [];
this.keyframeSelectionAnchor = null;
this.notify();
}
clearKeyframeSelection(): void {
this.selectedKeyframes = [];
this.keyframeSelectionAnchor = null;
2026-01-31 00:20:04 +01:00
this.notify();
}
subscribe(listener: () => void): () => void {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
private notify(): void {
this.listeners.forEach((fn) => fn());
}
}