summaryrefslogtreecommitdiff
path: root/apps/web/src/actions/use-editor-actions.ts
diff options
context:
space:
mode:
Diffstat (limited to 'apps/web/src/actions/use-editor-actions.ts')
-rw-r--r--apps/web/src/actions/use-editor-actions.ts46
1 files changed, 17 insertions, 29 deletions
diff --git a/apps/web/src/actions/use-editor-actions.ts b/apps/web/src/actions/use-editor-actions.ts
index 5e90a6a1..73300c6e 100644
--- a/apps/web/src/actions/use-editor-actions.ts
+++ b/apps/web/src/actions/use-editor-actions.ts
@@ -1,6 +1,6 @@
"use client";
-import { useEffect, useRef } from "react";
+import { useEffect, useState } from "react";
import { useTimelineStore } from "@/timeline/timeline-store";
import { useActionHandler } from "@/actions/use-action-handler";
import { useEditor } from "@/editor/use-editor";
@@ -25,6 +25,7 @@ import {
clearActiveScope,
type ScopeEntry,
} from "@/selection/scope";
+import { useCommittedRef } from "@/hooks/use-committed-ref";
export function useEditorActions() {
const editor = useEditor();
@@ -36,47 +37,34 @@ export function useEditorActions() {
const toggleSnapping = useTimelineStore((s) => s.toggleSnapping);
const rippleEditingEnabled = useTimelineStore((s) => s.rippleEditingEnabled);
const toggleRippleEditing = useTimelineStore((s) => s.toggleRippleEditing);
- const hasTimelineSelectionRef = useRef(false);
- const clearTimelineSelectionRef = useRef(() => {});
- const clearTimelineActiveSelectionRef = useRef(() => {});
- const timelineScopeRef = useRef<ScopeEntry | null>(null);
const hasTimelineSelection =
selectedElements.length > 0 ||
selectedKeyframes.length > 0 ||
selectedMaskPointSelection !== null;
-
- hasTimelineSelectionRef.current = hasTimelineSelection;
- clearTimelineSelectionRef.current = () => {
+ const hasTimelineSelectionRef = useCommittedRef(hasTimelineSelection);
+ const clearTimelineSelectionRef = useCommittedRef(() => {
editor.selection.clearSelection();
- };
- clearTimelineActiveSelectionRef.current = () => {
+ });
+ const clearTimelineActiveSelectionRef = useCommittedRef(() => {
editor.selection.clearMostSpecificSelection();
- };
-
- if (!timelineScopeRef.current) {
- timelineScopeRef.current = {
- hasSelection: () => hasTimelineSelectionRef.current,
- clear: () => {
- clearTimelineSelectionRef.current();
- },
- clearActive: () => {
- clearTimelineActiveSelectionRef.current();
- },
- };
- }
+ });
+ const [timelineScope] = useState<ScopeEntry>(() => ({
+ hasSelection: () => hasTimelineSelectionRef.current,
+ clear: () => {
+ clearTimelineSelectionRef.current();
+ },
+ clearActive: () => {
+ clearTimelineActiveSelectionRef.current();
+ },
+ }));
useEffect(() => {
if (!hasTimelineSelection) {
return;
}
- const timelineScope = timelineScopeRef.current;
- if (!timelineScope) {
- return;
- }
-
return activateScope({ entry: timelineScope });
- }, [hasTimelineSelection]);
+ }, [hasTimelineSelection, timelineScope]);
useActionHandler(
"toggle-play",