Skip to content

🎥 Make videos programmatically with React

Vibe Code: Expand a folder when creating inside it and only offer moves the codemods support

JonnyBurger8edbe54d47dc
diff --git a/packages/template-vibe-code/src/editor/components/SidebarPanel.tsx b/packages/template-vibe-code/src/editor/components/SidebarPanel.tsx
index 62818464ad..6b0c8819af 100644
--- a/packages/template-vibe-code/src/editor/components/SidebarPanel.tsx
+++ b/packages/template-vibe-code/src/editor/components/SidebarPanel.tsx
@@ -102,6 +102,11 @@ const getItemParentName = (item: RegistrationTreeItem) =>
? item.composition.folderPath
: item.folder.parentName;
+// The codemods can only move elements that are written directly inside another
+// element or fragment, and only relative to such elements.
+const isMovable = (item: RegistrationTreeItem) =>
+ item.type === "composition" ? item.composition.movable : item.folder.movable;
+
/** Whether `folderPath` is the folder `candidate` or one of its descendants. */
const isInsideFolder = (
folderPath: string | null,
@@ -155,10 +160,15 @@ const MoveToMenu: React.FC<{
const parentName = getItemParentName(item);
const destinations = folders.filter(
(folder) =>
+ folder.movable &&
getFolderPath(folder) !== parentName &&
!isInsideFolder(getFolderPath(folder), item),
);
+ if (!isMovable(item)) {
+ return null;
+ }
+
return (
<ContextMenuSub>
<ContextMenuSubTrigger>Move to</ContextMenuSubTrigger>
@@ -209,9 +219,11 @@ const getRowDragProps = (item: RegistrationTreeItem, tree: TreeContext) => {
return ratio < 0.5 ? "before" : "after";
};
- // An item cannot be dropped on itself or inside its own folder subtree.
+ // An item cannot be dropped on itself, inside its own folder subtree, or
+ // relative to an element the codemods cannot address.
const canDrop = () =>
tree.dragging !== null &&
+ isMovable(item) &&
getItemKey(tree.dragging) !== key &&
!isInsideFolder(
item.type === "folder"
@@ -223,7 +235,7 @@ const getRowDragProps = (item: RegistrationTreeItem, tree: TreeContext) => {
return {
dropPosition,
props: {
- draggable: true,
+ draggable: isMovable(item),
onDragStart: (event: React.DragEvent) => {
event.stopPropagation();
event.dataTransfer.effectAllowed = "move";
@@ -578,7 +590,20 @@ const RegistrationTree: React.FC<{
creating,
setDragging,
setDropTarget,
- setCreating,
+ // The input for a new registration is rendered among the folder's
+ // children, so the folder has to be expanded to show it.
+ setCreating: (next) => {
+ const parentName = next?.parentName ?? null;
+ if (parentName !== null && collapsed.has(parentName)) {
+ setCollapsed((previous) => {
+ const expanded = new Set(previous);
+ expanded.delete(parentName);
+ return expanded;
+ });
+ }
+
+ setCreating(next);
+ },
toggleCollapsed: (folderPath) =>
setCollapsed((previous) => {
const next = new Set(previous);
diff --git a/packages/template-vibe-code/src/editor/model/compositions.ts b/packages/template-vibe-code/src/editor/model/compositions.ts
index ed5739a389..27a2e1376b 100644
--- a/packages/template-vibe-code/src/editor/model/compositions.ts
+++ b/packages/template-vibe-code/src/editor/model/compositions.ts
@@ -15,6 +15,12 @@ export type CompositionInfo = {
line: number | null;
/** The enclosing folders as a slash-separated path, `null` at the root. */
folderPath: string | null;
+ /**
+ * Whether the element is written directly inside another element or
+ * fragment. `moveComposition()` and `moveFolder()` can only move and target
+ * such elements, not ones returned from a function or a `.map()` callback.
+ */
+ movable: boolean;
width: number | null;
height: number | null;
fps: number | null;
@@ -26,6 +32,8 @@ export type FolderInfo = FolderReference & {
filePath: string;
node: NodeReference;
line: number | null;
+ /** See `CompositionInfo.movable`. */
+ movable: boolean;
};
export type RegistrationTreeItem =
@@ -185,6 +193,8 @@ export const getRegistrations = (
const children = parent?.children ?? registrations.tree;
const folderPath = parent ? getFolderPath(parent.folder) : null;
const reference = { filePath: node.filePath, nodePath: node.nodePath };
+ // A JSX child's path ends in [..., "children", index, "openingElement"].
+ const movable = node.nodePath.at(-3) === "children";
if (tagName === "Folder") {
const { props } = getNodeProps({ project, node, keys: ["name"] });
const name = readStatic(props, "name", isString);
@@ -198,6 +208,7 @@ export const getRegistrations = (
filePath: node.filePath,
node: reference,
line: node.location?.line ?? null,
+ movable,
};
const item = { type: "folder" as const, folder, children: [] };
foldersByPath.set(JSON.stringify(node.nodePath), item);
@@ -230,6 +241,7 @@ export const getRegistrations = (
node: reference,
line: node.location?.line ?? null,
folderPath,
+ movable,
width: readStatic(props, "width", isNumber),
height: readStatic(props, "height", isNumber),
fps: readStatic(props, "fps", isNumber),