( opts: AddBlockOptions, )
| 55 | } |
| 56 | |
| 57 | export async function addBlockToProject( |
| 58 | opts: AddBlockOptions, |
| 59 | ): Promise<{ block: RegistryItem; compositionPath: string } | null> { |
| 60 | const { |
| 61 | projectId, |
| 62 | blockName, |
| 63 | activeCompPath, |
| 64 | placement, |
| 65 | visualPosition, |
| 66 | timelineElements, |
| 67 | readProjectFile, |
| 68 | writeProjectFile, |
| 69 | recordEdit, |
| 70 | refreshFileTree, |
| 71 | reloadPreview, |
| 72 | showToast, |
| 73 | } = opts; |
| 74 | |
| 75 | try { |
| 76 | const res = await fetch(`/api/projects/${projectId}/registry/install`, { |
| 77 | method: "POST", |
| 78 | headers: { "Content-Type": "application/json" }, |
| 79 | body: JSON.stringify({ blockName }), |
| 80 | }); |
| 81 | |
| 82 | if (!res.ok) { |
| 83 | const err = await res.json().catch(() => ({ error: "Install failed" })); |
| 84 | showToast((err as { error?: string }).error || "Failed to install block"); |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | const { written, block } = (await res.json()) as { |
| 89 | written: string[]; |
| 90 | block: RegistryItem; |
| 91 | }; |
| 92 | |
| 93 | const compositionFile = written.find((f) => f.endsWith(".html")) ?? written[0]; |
| 94 | if (!compositionFile) { |
| 95 | showToast("Installed but no composition file was written"); |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | if (block.type === "hyperframes:component") { |
| 100 | const compContent = await readProjectFile(compositionFile); |
| 101 | const transparentContent = compContent.replace( |
| 102 | /background:\s*(?:#(?:0a0a0a|000000|000|0a0805)|rgba?\([^)]*\))\s*;/g, |
| 103 | "background: transparent;", |
| 104 | ); |
| 105 | if (transparentContent !== compContent) { |
| 106 | await writeProjectFile(compositionFile, transparentContent); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | { |
| 111 | const targetPath = activeCompPath || "index.html"; |
| 112 | const originalContent = await readProjectFile(targetPath); |
| 113 | const existingIds = collectHtmlIds(originalContent); |
| 114 | const compId = buildUniqueCompositionId(block.name, existingIds); |
no test coverage detected