Skip to content

Commit 5c79cd0

Browse files
committed
fix(studio): rewrite block dimensions to match host project on install
Registry blocks are authored at 1920x1080 but projects may use different dimensions (e.g. 1280x720). After installing a block, the server now reads the host project's data-width/data-height from index.html and rewrites the block's viewport meta and CSS dimensions to match, preventing overflow.
1 parent e316aa4 commit 5c79cd0

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

packages/cli/src/server/studioServer.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,38 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
398398
async installRegistryBlock(opts) {
399399
const { resolveItem } = await import("../registry/resolver.js");
400400
const { installItem } = await import("../registry/installer.js");
401+
const { readFileSync, writeFileSync, existsSync } = await import("node:fs");
402+
const { join } = await import("node:path");
401403
const item = await resolveItem(opts.blockName);
402404
const { written } = await installItem(item, { destDir: opts.project.dir });
405+
406+
const indexPath = join(opts.project.dir, "index.html");
407+
if (existsSync(indexPath)) {
408+
const indexHtml = readFileSync(indexPath, "utf-8");
409+
const hostW = indexHtml.match(/data-width="(\d+)"/)?.[1];
410+
const hostH = indexHtml.match(/data-height="(\d+)"/)?.[1];
411+
if (hostW && hostH) {
412+
for (const absPath of written) {
413+
if (!absPath.endsWith(".html")) continue;
414+
let content = readFileSync(absPath, "utf-8");
415+
content = content.replace(
416+
/(<meta\s+name="viewport"\s+content="width=)\d+(,\s*height=)\d+/i,
417+
`$1${hostW}$2${hostH}`,
418+
);
419+
content = content.replace(
420+
/(\bwidth:\s*)\d+(px;\s*\n?\s*height:\s*)\d+(px;)/g,
421+
(match, pre, mid, post) => {
422+
if (match.includes("1920") || match.includes("1080")) {
423+
return `${pre}${hostW}${mid}${hostH}${post}`;
424+
}
425+
return match;
426+
},
427+
);
428+
writeFileSync(absPath, content, "utf-8");
429+
}
430+
}
431+
}
432+
403433
const relativePaths = written.map((abs) => {
404434
const rel = abs.startsWith(opts.project.dir) ? abs.slice(opts.project.dir.length + 1) : abs;
405435
return rel;

0 commit comments

Comments
 (0)