Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript
2.5 KB
Raw Blame
import { v4 as uuidv4 } from "uuid";
import type { StoreApi } from "zustand";
import type { Action } from "@openreel/core";
import type { ProjectState } from "../project-store";

type Get = StoreApi<ProjectState>["getState"];
type Set = StoreApi<ProjectState>["setState"];

type MarkerSlice = Pick<
  ProjectState,
  "addMarker" | "removeMarker" | "updateMarker" | "getMarker" | "getMarkers" | "setClipMarkers"
>;

export function createMarkerSlice(set: Set, get: Get): MarkerSlice {
  return {
    addMarker: async (time, label = "Marker", color = "#3b82f6", extra) => {
      const { project, actionExecutor } = get();
      const action: Action = {
        type: "marker/add",
        id: uuidv4(),
        timestamp: Date.now(),
        params: { time, label, color, ...extra },
      };
      const result = await actionExecutor.execute(action, project);
      if (result.success) {
        set({ project: { ...project, modifiedAt: Date.now() } });
      }
      return result;
    },

    removeMarker: async (markerId) => {
      const { project, actionExecutor } = get();
      const action: Action = {
        type: "marker/remove",
        id: uuidv4(),
        timestamp: Date.now(),
        params: { markerId },
      };
      const result = await actionExecutor.execute(action, project);
      if (result.success) {
        set({ project: { ...project, modifiedAt: Date.now() } });
      }
      return result;
    },

    updateMarker: async (markerId, updates) => {
      const { project, actionExecutor } = get();
      const action: Action = {
        type: "marker/update",
        id: uuidv4(),
        timestamp: Date.now(),
        params: { markerId, updates },
      };
      const result = await actionExecutor.execute(action, project);
      if (result.success) {
        set({ project: { ...project, modifiedAt: Date.now() } });
      }
      return result;
    },

    setClipMarkers: async (target, markers) => {
      const { project, actionExecutor } = get();
      const action: Action = {
        type: "clipmarker/set",
        id: uuidv4(),
        timestamp: Date.now(),
        params: { ...target, markers },
      };
      const result = await actionExecutor.execute(action, project);
      if (result.success) {
        set({ project: { ...project, modifiedAt: Date.now() } });
      }
      return result;
    },

    getMarker: (markerId) =>
      get().project.timeline.markers.find((m) => m.id === markerId),

    getMarkers: () => get().project.timeline.markers,
  };
}