mirror of
https://github.com/OpenCut-app/OpenCut.git
synced 2026-07-13 21:52:53 +02:00
Merge branch 'refactor-selection-commands'
This commit is contained in:
@@ -10,6 +10,7 @@ import { AudioManager } from "./managers/audio-manager";
|
|||||||
import { SelectionManager } from "./managers/selection-manager";
|
import { SelectionManager } from "./managers/selection-manager";
|
||||||
import { registerDefaultEffects } from "@/lib/effects";
|
import { registerDefaultEffects } from "@/lib/effects";
|
||||||
import { registerDefaultMasks } from "@/lib/masks";
|
import { registerDefaultMasks } from "@/lib/masks";
|
||||||
|
import { isMainTrack } from "@/lib/timeline/placement";
|
||||||
|
|
||||||
export class EditorCore {
|
export class EditorCore {
|
||||||
private static instance: EditorCore | null = null;
|
private static instance: EditorCore | null = null;
|
||||||
@@ -37,6 +38,15 @@ export class EditorCore {
|
|||||||
this.save = new SaveManager(this);
|
this.save = new SaveManager(this);
|
||||||
this.audio = new AudioManager(this);
|
this.audio = new AudioManager(this);
|
||||||
this.selection = new SelectionManager(this);
|
this.selection = new SelectionManager(this);
|
||||||
|
this.command.registerReactor(() => {
|
||||||
|
const tracks = this.timeline.getTracks();
|
||||||
|
const prunedTracks = tracks.filter(
|
||||||
|
(track) => track.elements.length > 0 || isMainTrack(track),
|
||||||
|
);
|
||||||
|
if (prunedTracks.length !== tracks.length) {
|
||||||
|
this.timeline.updateTracks(prunedTracks);
|
||||||
|
}
|
||||||
|
});
|
||||||
this.save.start();
|
this.save.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ interface CommandHistoryEntry {
|
|||||||
export class CommandManager {
|
export class CommandManager {
|
||||||
private history: CommandHistoryEntry[] = [];
|
private history: CommandHistoryEntry[] = [];
|
||||||
private redoStack: CommandHistoryEntry[] = [];
|
private redoStack: CommandHistoryEntry[] = [];
|
||||||
|
private reactors: Array<() => void> = [];
|
||||||
|
|
||||||
constructor(private editor: EditorCore) {}
|
constructor(private editor: EditorCore) {}
|
||||||
|
|
||||||
@@ -18,6 +19,7 @@ export class CommandManager {
|
|||||||
const previousSelection = this.getSelectionSnapshot();
|
const previousSelection = this.getSelectionSnapshot();
|
||||||
const result = command.execute();
|
const result = command.execute();
|
||||||
const selectionOverride = this.applySelectionOverride(result);
|
const selectionOverride = this.applySelectionOverride(result);
|
||||||
|
this.runReactors();
|
||||||
this.history.push({
|
this.history.push({
|
||||||
command,
|
command,
|
||||||
previousSelection,
|
previousSelection,
|
||||||
@@ -35,6 +37,10 @@ export class CommandManager {
|
|||||||
this.redoStack = [];
|
this.redoStack = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerReactor(reactor: () => void): void {
|
||||||
|
this.reactors.push(reactor);
|
||||||
|
}
|
||||||
|
|
||||||
undo(): void {
|
undo(): void {
|
||||||
if (this.history.length === 0) return;
|
if (this.history.length === 0) return;
|
||||||
const entry = this.history.pop();
|
const entry = this.history.pop();
|
||||||
@@ -64,6 +70,7 @@ export class CommandManager {
|
|||||||
const previousSelection = this.getSelectionSnapshot();
|
const previousSelection = this.getSelectionSnapshot();
|
||||||
const result = entry.command.redo();
|
const result = entry.command.redo();
|
||||||
const selectionOverride = this.applySelectionOverride(result);
|
const selectionOverride = this.applySelectionOverride(result);
|
||||||
|
this.runReactors();
|
||||||
|
|
||||||
this.history.push({
|
this.history.push({
|
||||||
command: entry.command,
|
command: entry.command,
|
||||||
@@ -100,4 +107,10 @@ export class CommandManager {
|
|||||||
this.editor.selection.setSelectedElements({ elements: selectionOverride });
|
this.editor.selection.setSelectedElements({ elements: selectionOverride });
|
||||||
return selectionOverride;
|
return selectionOverride;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private runReactors(): void {
|
||||||
|
for (const reactor of this.reactors) {
|
||||||
|
reactor();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { Command, type CommandResult } from "@/lib/commands/base-command";
|
|||||||
import type { TimelineTrack } from "@/lib/timeline";
|
import type { TimelineTrack } from "@/lib/timeline";
|
||||||
import { EditorCore } from "@/core";
|
import { EditorCore } from "@/core";
|
||||||
import { rippleShiftElements } from "@/lib/timeline";
|
import { rippleShiftElements } from "@/lib/timeline";
|
||||||
import { isMainTrack } from "@/lib/timeline/placement";
|
|
||||||
|
|
||||||
export class DeleteElementsCommand extends Command {
|
export class DeleteElementsCommand extends Command {
|
||||||
private savedState: TimelineTrack[] | null = null;
|
private savedState: TimelineTrack[] | null = null;
|
||||||
@@ -25,8 +24,7 @@ export class DeleteElementsCommand extends Command {
|
|||||||
const editor = EditorCore.getInstance();
|
const editor = EditorCore.getInstance();
|
||||||
this.savedState = editor.timeline.getTracks();
|
this.savedState = editor.timeline.getTracks();
|
||||||
|
|
||||||
const updatedTracks = this.savedState
|
const updatedTracks = this.savedState.map((track) => {
|
||||||
.map((track) => {
|
|
||||||
const elementsToDeleteOnTrack = this.elements.filter(
|
const elementsToDeleteOnTrack = this.elements.filter(
|
||||||
(target) => target.trackId === track.id,
|
(target) => target.trackId === track.id,
|
||||||
);
|
);
|
||||||
@@ -65,8 +63,7 @@ export class DeleteElementsCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return { ...track, elements } as typeof track;
|
return { ...track, elements } as typeof track;
|
||||||
})
|
});
|
||||||
.filter((track) => track.elements.length > 0 || isMainTrack(track));
|
|
||||||
|
|
||||||
editor.timeline.updateTracks(updatedTracks);
|
editor.timeline.updateTracks(updatedTracks);
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import type {
|
|||||||
} from "@/lib/timeline";
|
} from "@/lib/timeline";
|
||||||
import {
|
import {
|
||||||
buildEmptyTrack,
|
buildEmptyTrack,
|
||||||
isMainTrack,
|
|
||||||
validateElementTrackCompatibility,
|
validateElementTrackCompatibility,
|
||||||
enforceMainTrackStart,
|
enforceMainTrackStart,
|
||||||
} from "@/lib/timeline/placement";
|
} from "@/lib/timeline/placement";
|
||||||
@@ -100,7 +99,7 @@ export class MoveElementCommand extends Command {
|
|||||||
|
|
||||||
const isSameTrack = this.sourceTrackId === this.targetTrackId;
|
const isSameTrack = this.sourceTrackId === this.targetTrackId;
|
||||||
|
|
||||||
let updatedTracks = tracksToUpdate.map((track): TimelineTrack => {
|
const updatedTracks = tracksToUpdate.map((track): TimelineTrack => {
|
||||||
if (isSameTrack && track.id === this.sourceTrackId) {
|
if (isSameTrack && track.id === this.sourceTrackId) {
|
||||||
return {
|
return {
|
||||||
...track,
|
...track,
|
||||||
@@ -134,21 +133,6 @@ export class MoveElementCommand extends Command {
|
|||||||
return track;
|
return track;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!isSameTrack) {
|
|
||||||
const sourceTrackAfterMove = updatedTracks.find(
|
|
||||||
(track) => track.id === this.sourceTrackId,
|
|
||||||
);
|
|
||||||
if (
|
|
||||||
sourceTrackAfterMove &&
|
|
||||||
sourceTrackAfterMove.elements.length === 0 &&
|
|
||||||
!isMainTrack(sourceTrackAfterMove)
|
|
||||||
) {
|
|
||||||
updatedTracks = updatedTracks.filter(
|
|
||||||
(track) => track.id !== this.sourceTrackId,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
editor.timeline.updateTracks(updatedTracks);
|
editor.timeline.updateTracks(updatedTracks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user