Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Add Music library context actions

Bittif1aa7e5ab449
diff --git a/src/components/workspace/MusicView.test.tsx b/src/components/workspace/MusicView.test.tsx
index 06425d1..0272281 100644
--- a/src/components/workspace/MusicView.test.tsx
+++ b/src/components/workspace/MusicView.test.tsx
@@ -8,14 +8,15 @@ import { createProjectConfig, type ProjectConfig } from "../../lib/project";
import { newMusicFolder, newSong, newSongVariation } from "../../lib/music";
import { readMediaFileUrl } from "../../lib/persistence";
import { loadGeneratorTemplateSettings, musicTemplates, saveGeneratorTemplateSettings } from "../../lib/settings";
+import type { WorkItem } from "../../lib/workQueue";
vi.mock("../../lib/persistence", async (original) => ({ ...await original<typeof import("../../lib/persistence")>(), isTauri: () => true, readMediaFileUrl: vi.fn(async () => "blob:song") }));
let latest: ProjectConfig;
const generate = vi.fn();
const createConfig = () => createProjectConfig({ name: "Music", prompt: "", aspectRatio: "16:9", resolution: "416p", targetDurationSeconds: 30 });
-function Harness({ initial = createConfig() }: { initial?: ProjectConfig }) {
+function Harness({ initial = createConfig(), workItems = [] }: { initial?: ProjectConfig; workItems?: WorkItem[] }) {
const [config, setConfig] = useState(initial); latest = config;
- return <MusicView config={config} folderPath="C:/Music" onChange={setConfig} onGenerate={generate} onCancel={() => {}} workItems={[]} />;
+ return <MusicView config={config} folderPath="C:/Music" onChange={setConfig} onGenerate={generate} onCancel={() => {}} workItems={workItems} />;
}
beforeEach(() => {
vi.clearAllMocks(); localStorage.clear();
@@ -60,6 +61,66 @@ it("creates songs in the browsed folder and moves them between folders by drag a
expect(latest.musicSongs![1].folderId).toBeNull();
});
+it("creates songs and folders from the library and song context menus in the current folder", () => {
+ render(<Harness />);
+ fireEvent.contextMenu(screen.getByLabelText("Music library"));
+ fireEvent.click(screen.getByRole("menuitem", { name: "New folder" }));
+ fireEvent.change(screen.getByLabelText("Folder name"), { target: { value: "Jazz" } });
+ fireEvent.click(screen.getByRole("button", { name: "Create folder" }));
+ fireEvent.keyDown(screen.getByLabelText("Music library"), { key: "F10", shiftKey: true });
+ fireEvent.click(screen.getByRole("menuitem", { name: "New song" }));
+ expect(latest.musicSongs![0].folderId).toBe(latest.musicFolders![0].id);
+ fireEvent.click(screen.getByRole("button", { name: "List view" }));
+ fireEvent.contextMenu(screen.getByRole("listitem"));
+ expect(screen.getByRole("menuitem", { name: "New folder" })).toBeInTheDocument();
+ fireEvent.click(screen.getByRole("menuitem", { name: "New song" }));
+ expect(latest.musicSongs).toHaveLength(2);
+ expect(latest.musicSongs![1].folderId).toBe(latest.musicFolders![0].id);
+});
+
+it("removes the context-clicked folder and moves its contents to the parent", () => {
+ const parent = newMusicFolder("Jazz", null), folder = newMusicFolder("Piano", parent.id), child = newMusicFolder("Solo", folder.id);
+ const song = newSong("yue2", 32, folder.id), nested = newSong("yue2", 32, child.id);
+ render(<Harness initial={{ ...createConfig(), musicFolders: [parent, folder, child], musicSongs: [song, nested] }} />);
+ const navigation = within(screen.getByRole("navigation", { name: "Music folders" }));
+ fireEvent.click(navigation.getByRole("button", { name: "Jazz / Piano" }));
+ fireEvent.keyDown(navigation.getByRole("button", { name: "Jazz / Piano" }), { key: "ContextMenu" });
+ fireEvent.click(screen.getByRole("menuitem", { name: "Remove folder" }));
+ expect(latest.musicFolders).toEqual([parent, { ...child, parentId: parent.id }]);
+ expect(latest.musicSongs).toEqual([{ ...song, folderId: parent.id }, nested]);
+ expect(navigation.getByRole("button", { name: "Jazz" })).toHaveAttribute("aria-current", "page");
+ expect(navigation.getByRole("button", { name: "Jazz / Solo" })).toBeInTheDocument();
+ fireEvent.contextMenu(navigation.getByRole("button", { name: "Your music" }));
+ expect(screen.queryByRole("menuitem", { name: "Remove folder" })).not.toBeInTheDocument();
+});
+
+it("removes the context-clicked playing song, stops playback and preserves timeline media", async () => {
+ const first = { ...newSong(), title: "First", assetId: "audio" }, second = { ...newSong(), title: "Second" };
+ const asset = { id: "audio", kind: "audio" as const, mimeType: "audio/wav", name: "First", relativePath: "media/first.wav", durationMs: 30000, createdAt: first.createdAt };
+ const config = { ...createConfig(), musicSongs: [first, second], assets: [asset] };
+ const { container } = render(<Harness initial={config} />);
+ fireEvent.click(screen.getByRole("button", { name: "Play First" }));
+ await waitFor(() => expect(container.querySelector("audio")).toHaveAttribute("src", "blob:song"));
+ fireEvent.click(screen.getByRole("button", { name: "Select Second" }));
+ fireEvent.contextMenu(screen.getByRole("button", { name: "Select First" }));
+ fireEvent.click(screen.getByRole("menuitem", { name: "Remove song" }));
+ expect(latest.musicSongs).toEqual([second]);
+ expect(latest.assets).toEqual(config.assets);
+ expect(latest.timeline).toEqual(config.timeline);
+ expect(screen.getByLabelText("Song title")).toHaveValue("Second");
+ await waitFor(() => expect(container.querySelector("audio")).not.toHaveAttribute("src"));
+ expect(screen.getByRole("button", { name: "Next song" })).toBeDisabled();
+});
+
+it("disables removal for a song with active generation", () => {
+ const song = newSong();
+ render(<Harness initial={{ ...createConfig(), musicSongs: [song] }} workItems={[{ sceneId: song.id, status: "queued", detail: "Waiting", progress: 0 } as WorkItem]} />);
+ fireEvent.contextMenu(screen.getByRole("listitem"));
+ expect(screen.getByRole("menuitem", { name: "Remove song" })).toBeDisabled();
+ fireEvent.click(screen.getByRole("menuitem", { name: "Remove song" }));
+ expect(latest.musicSongs).toEqual([song]);
+});
+
it("automatically plays the next generated song in the starting folder even after browsing elsewhere", async () => {
const jazz = newMusicFolder("Jazz", null), other = newMusicFolder("Other", null);
const songs = [
diff --git a/src/components/workspace/MusicView.tsx b/src/components/workspace/MusicView.tsx
index 803355c..13d4721 100644
--- a/src/components/workspace/MusicView.tsx
+++ b/src/components/workspace/MusicView.tsx
@@ -2,10 +2,10 @@ import { useEffect, useRef, useState } from "react";
import type { ProjectAsset, ProjectConfig } from "../../lib/project";
import { isTauri, readMediaFileUrl } from "../../lib/persistence";
import { loadGeneratorTemplateSettings, subscribeGeneratorTemplates, templateSupportsMusic, templateUsable, generatorPathFields, type GeneratorTemplate } from "../../lib/settings";
-import { musicFolderLabel, musicSettingsForModel, moveSong, newMusicFolder, newSong, songReference, updateSong, type MusicSettings, type MusicSong } from "../../lib/music";
+import { musicFolderLabel, musicSettingsForModel, moveSong, newMusicFolder, newSong, removeMusicFolder, songReference, updateSong, type MusicSettings, type MusicSong } from "../../lib/music";
import { isWorkActive, type WorkItem } from "../../lib/workQueue";
-import { ComboBox, PropRow, PropSection, Slider, Splitter, useContextMenu, usePaneSize } from "../ui";
-import { Add16, Audio16, FolderAdd16, FolderOpen16, GoToEnd16, GoToStart16, GridView16, ListView16, Pause16, Play16, References16, Speaker14, Sparkle16, Stop14 } from "../ui/icons";
+import { ComboBox, PropRow, PropSection, Slider, Splitter, useContextMenu, usePaneSize, type MenuEntry } from "../ui";
+import { Add16, Audio16, Delete16, FolderAdd16, FolderOpen16, GoToEnd16, GoToStart16, GridView16, ListView16, Pause16, Play16, References16, Speaker14, Sparkle16, Stop14 } from "../ui/icons";
import { generatorOptions } from "../generatorOptions";
import { SliderNumberInput, GENERATION_STEPS_RANGE } from "./SliderNumberInput";
import type { ConfigUpdate } from "./TimelineView";
@@ -58,6 +58,20 @@ export function MusicView({ config, folderPath, onChange, onGenerate, onCancel,
onChange((current) => ({ ...current, musicSongs: [...(current.musicSongs ?? []), song] }));
setSelectedId(song.id); setError(null);
};
+ const beginFolder = () => { setCreatingFolder(true); setFolderName(""); };
+ const newItems = (): MenuEntry[] => [
+ { label: "New song", icon: <Add16 />, onSelect: add },
+ { label: "New folder", icon: <FolderAdd16 />, onSelect: beginFolder },
+ ];
+ const folderContext = (event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>, id: string | null) => {
+ const folder = folders.find((item) => item.id === id);
+ menu.open(event, [...newItems(), ...(folder ? [{ separator: true } as const,
+ { label: "Remove folder", icon: <Delete16 />, danger: true, onSelect: () => {
+ onChange((current) => removeMusicFolder(current, folder.id));
+ if (currentFolderId === folder.id) setFolderId(folder.parentId ?? null);
+ setNotice(`Removed ${folder.name}. Its contents were moved to the parent folder.`);
+ } }] : [])], { "aria-label": folder ? `${folder.name} actions` : "Music actions" });
+ };
const createFolder = (event: React.FormEvent) => {
event.preventDefault();
try {
@@ -103,6 +117,7 @@ export function MusicView({ config, folderPath, onChange, onGenerate, onCancel,
// Capture the folder's order when playback starts. Browsing other folders
// does not change what plays next; drafts and unavailable assets are skipped.
const playable = playlist.length ? playlist.flatMap((id) => {
+ if (!config.musicSongs?.some((song) => song.assetId === id)) return [];
const asset = config.assets.find((item) => item.id === id && item.kind === "audio" && item.relativePath);
return asset ? [{ asset }] : [];
}) : visiblePlayable;
@@ -142,14 +157,22 @@ export function MusicView({ config, folderPath, onChange, onGenerate, onCancel,
};
const context = (event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>, song: MusicSong) => {
const asset = config.assets.find((item) => item.id === song.assetId);
- menu.open(event, [{ label: "Create Reference", icon: <References16 />, disabled: !asset?.relativePath || (asset.durationMs ?? 0) < 2000,
+ menu.open(event, [...newItems(), { separator: true }, { label: "Create Reference", icon: <References16 />, disabled: !asset?.relativePath || (asset.durationMs ?? 0) < 2000,
onSelect: () => {
try {
const reference = songReference(song, asset!);
onChange((current) => ({ ...current, references: [...current.references, reference] }));
setNotice(`Added ${song.title} to References.`);
} catch (reason) { setError(String(reason)); }
- } }], { "aria-label": `${song.title} actions` });
+ } }, { separator: true }, { label: "Remove song", icon: <Delete16 />, danger: true,
+ disabled: workItems.some((item) => item.sceneId === song.id && isWorkActive(item)),
+ onSelect: () => {
+ onChange((current) => ({ ...current, musicSongs: current.musicSongs?.filter((item) => item.id !== song.id) }));
+ if (selectedId === song.id) setSelectedId(undefined);
+ if (song.assetId && playerId === song.assetId) { playWhenReady.current = false; audio.current?.pause(); setPlayerId(null); }
+ setPlaylist((current) => current.filter((id) => id !== song.assetId));
+ setNotice(`Removed ${song.title} from Music.`);
+ } }], { "aria-label": `${song.title} actions` });
};
return <div className="music-view" style={inspector.style}>
@@ -159,18 +182,22 @@ export function MusicView({ config, folderPath, onChange, onGenerate, onCancel,
options={generatorOptions(templates, (item) => ({ disabled: !templateReady(item), description: !templateReady(item) ? "Download in Settings" : undefined }))}
onChange={chooseGenerator} />
<button type="button" className="secondary-button" onClick={add}><Add16 />New song</button>
- <button type="button" className="secondary-button" onClick={() => { setCreatingFolder(true); setFolderName(""); }}><FolderAdd16 />New folder</button>
+ <button type="button" className="secondary-button" onClick={beginFolder}><FolderAdd16 />New folder</button>
<div className="music-toolbar__views" role="group" aria-label="Song view">
{(["grid", "list"] as const).map((mode) => <button type="button" className="icon-button" key={mode} aria-label={`${mode === "grid" ? "Grid" : "List"} view`} aria-pressed={view === mode}
onClick={() => { setView(mode); localStorage.setItem("slopus.music.view", mode); }}>{mode === "grid" ? <GridView16 /> : <ListView16 />}</button>)}
</div>
</div>
<div className="music-body">
- <div className="music-library">
+ <div className="music-library" tabIndex={0} aria-label="Music library"
+ onContextMenu={(event) => { if (!(event.target as HTMLElement).closest("input, textarea")) folderContext(event, currentFolderId); }}
+ onKeyDown={(event) => { if (event.target === event.currentTarget && (event.key === "ContextMenu" || event.shiftKey && event.key === "F10")) folderContext(event, currentFolderId); }}>
<div className="music-library__heading"><h2>{currentFolder?.name ?? "Your music"}</h2><span>{visibleSongs.length} songs</span></div>
<nav className="music-folders" aria-label="Music folders">
{[{ id: null, name: "Your music" }, ...folders.map((folder) => ({ id: folder.id, name: musicFolderLabel(folders, folder.id) }))].map((folder) =>
<button key={folder.id ?? "root"} type="button" className="secondary-button" aria-current={currentFolderId === folder.id ? "page" : undefined}
+ onContextMenu={(event) => folderContext(event, folder.id)}
+ onKeyDown={(event) => { if (event.key === "ContextMenu" || event.shiftKey && event.key === "F10") folderContext(event, folder.id); }}
data-drop-target={dropFolder === (folder.id ?? "") ? "true" : undefined} onClick={() => { setFolderId(folder.id); setSelectedId(undefined); setNotice(null); }}
onDragOver={(event) => { if (event.dataTransfer.types.includes(SONG_DRAG_TYPE)) { event.preventDefault(); event.dataTransfer.dropEffect = "move"; setDropFolder(folder.id ?? ""); } }}
onDragLeave={() => setDropFolder(undefined)} onDrop={(event) => dropSong(event, folder.id)} data-tooltip={folder.name}><FolderOpen16 /><span>{folder.name}</span></button>)}
diff --git a/src/lib/music.ts b/src/lib/music.ts
index 183fc6b..10a647a 100644
--- a/src/lib/music.ts
+++ b/src/lib/music.ts
@@ -58,6 +58,16 @@ export function moveSong(config: ProjectConfig, songId: string, folderId: string
return updateSong(config, songId, { folderId });
}
+export function removeMusicFolder(config: ProjectConfig, id: string): ProjectConfig {
+ const folder = config.musicFolders?.find((item) => item.id === id);
+ if (!folder) return config;
+ const parentId = folder.parentId ?? null;
+ return { ...config,
+ musicFolders: config.musicFolders!.filter((item) => item.id !== id).map((item) => item.parentId === id ? { ...item, parentId } : item),
+ musicSongs: config.musicSongs?.map((song) => song.folderId === id ? { ...song, folderId: parentId } : song),
+ };
+}
+
export function musicFolderLabel(folders: readonly MusicFolder[], id: string): string {
const names: string[] = [], visited = new Set<string>();
let folder = folders.find((item) => item.id === id);