Skip to content

Open source CapCut alternative.

TypeScript
6.0 KB
Raw Blame
"use client";

import { useTranslations } from "next-intl";
import { dealsMarks, lineLikeShape, shapePathD } from "@donkeycut/effects-kit";
import { SubTabs } from "@/cut/components/SubTabs";
import { clearElementDrag, setElementDragData, setObjectDragImage } from "@/cut/lib/assetDrag";
import { PICKED_RING, pickGridNav, useAssetPick } from "@/cut/lib/assetPick";
import { useEnvironment } from "@/cut/lib/environment";
import { useEditor } from "@/cut/lib/store";
import type { ShapeKind } from "@/cut/lib/types";
import { useLabels } from "@/i18n/labels";
import { SHAPE_KINDS } from "@/cut/components/ElementsPanel.tools";
import { useLocalPref } from "@/cut/lib/uiState";
import { cn } from "@/lib/utils";
import { CreateSticker, StickerTile, useProjectStickers } from "./Stickers";
import { ScrollArea } from "@/components/ui/scroll-area";

/**
 * The Elements tab: what you lay over the picture. A segmented toggle at the
 * top switches between the project's stickers and the shapes, each a grid of
 * tiles. A click picks a tile and nothing else; dragging one onto the
 * timeline is what places it (`assetPick.ts`).
 */

type Category = "stickers" | "shapes";

const CATEGORIES = [{ id: "stickers" }, { id: "shapes" }] as const;

export function ElementsPanel({ projectId }: { projectId: string }) {
  const t = useTranslations("media.elements");
  const readOnly = useEditor((s) => s.readOnly);
  const { chatgpt } = useEnvironment();
  // Inside ChatGPT the card shows no credits, so Stickers, a generation
  // surface, leaves the panel.
  const categories = (chatgpt ? CATEGORIES.filter((c) => c.id !== "stickers") : CATEGORIES).map(
    (c) => ({ id: c.id, label: t(c.id) })
  );
  const [viewPref, setView] = useLocalPref<Category>("cut-elements-view", "stickers", (v) =>
    CATEGORIES.some((c) => c.id === v)
  );
  const view = categories.some((c) => c.id === viewPref) ? viewPref : "shapes";
  const { stickers, handleOf } = useProjectStickers();

  return (
    <>
      {/* PanelHead's height, so the side panel's floating close button lands
          on the toggle's centerline; the right padding keeps clear of it. */}
      <div className="flex h-12 shrink-0 items-center pr-12 pl-3.5">
        {categories.length > 1 && <SubTabs tabs={categories} value={view} onChange={setView} />}
      </div>

      {/* The top pad is the selected tile's ring and its offset: the grid starts
          at the scroll edge, and a ring drawn outside a tile would be cut off
          there. */}
      <ScrollArea className="min-h-0 flex-1" contentClassName="flex flex-col gap-4 px-3.5 pt-1 pb-4">
        {view === "stickers" && (
          <>
            {!readOnly && <CreateSticker projectId={projectId} />}
            {stickers.length > 0 ? (
              <div className="grid shrink-0 grid-cols-2 gap-1.5" onKeyDown={pickGridNav}>
                {stickers.map((a) => (
                  <StickerTile
                    key={a.id}
                    asset={a}
                    projectId={projectId}
                    handle={handleOf(a.id)}
                    readOnly={readOnly}
                  />
                ))}
              </div>
            ) : (
              <p className="px-2 py-6 text-center text-xs leading-relaxed text-balance text-muted-foreground">
                {t("stickersEmpty")}
              </p>
            )}
          </>
        )}

        {view === "shapes" && (
          <div className="grid shrink-0 grid-cols-2 gap-2" onKeyDown={pickGridNav}>
            {SHAPE_KINDS.map((k) => (
              <ShapeTile key={k} shape={k} />
            ))}
          </div>
        )}
      </ScrollArea>
    </>
  );
}

/** A shape in the Shapes grid — a square swatch sized like the effect tiles
 * with its name under it. A click picks it, a drag onto the timeline places
 * it. */
function ShapeTile({ shape }: { shape: ShapeKind }) {
  const shapeLabel = useLabels("shape");
  const { picked, pick } = useAssetPick(`shape:${shape}`);
  return (
    <button
      type="button"
      data-pick-id={`shape:${shape}`}
      aria-pressed={picked}
      draggable
      onDragStart={(e) => {
        setElementDragData(e, { kind: "shape", shape });
        setObjectDragImage(e);
      }}
      onDragEnd={clearElementDrag}
      onClick={pick}
      className="flex flex-col items-center gap-1.5 text-[11px] font-medium text-muted-foreground outline-none transition-colors hover:text-foreground"
    >
      <span
        className={cn(
          "flex aspect-video w-full items-center justify-center rounded-lg border border-border",
          picked && PICKED_RING
        )}
      >
        <ShapeSwatch shape={shape} className="size-16" />
      </span>
      <span className="leading-none">{shapeLabel(shape)}</span>
    </button>
  );
}

/** A shape's silhouette, drawn the way the painter lays it out; lines and
 * arrows sit diagonally, pointing up and to the right. */
function ShapeSwatch({ shape, className }: { shape: ShapeKind; className?: string }) {
  return (
    <svg
      viewBox="0 0 24 24"
      fill="currentColor"
      aria-hidden
      data-drag-object
      className={cn("text-foreground/70", className)}
    >
      {shape === "rect" && <rect x="3" y="6" width="18" height="12" rx="2" />}
      {shape === "ellipse" && <ellipse cx="12" cy="12" rx="9.5" ry="7" />}
      {shape === "line" && <rect x="2" y="11" width="20" height="2" rx="1" transform="rotate(-45 12 12)" />}
      {shape === "arrow" && (
        <g transform="rotate(-45 12 12)">
          <rect x="2" y="11" width="14" height="2" rx="1" />
          <polygon points="22,12 14,7.5 14,16.5" />
        </g>
      )}
      {shape === "doodle" && (
        <g fill="none" stroke="currentColor" strokeLinecap="round">
          <path d="M5 16 C8 6, 12 18, 15 9 S20 8, 19 14" strokeWidth="3" />
          <path d="M6 5 L10 10 M10 5 L6 10" strokeWidth="1.5" />
          <circle cx="17" cy="18" r="2.5" strokeWidth="1.5" />
        </g>
      )}
      {!lineLikeShape(shape) && !dealsMarks(shape) && shape !== "rect" && shape !== "ellipse" && (
        <path d={shapePathD(shape, 18, 18)} transform="translate(3 3)" />
      )}
    </svg>
  );
}