Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(poster): prevent template layers from obscuring titles

tianma-ifeea5e794a978
diff --git a/apps/web/src/components/PosterEditorPane.tsx b/apps/web/src/components/PosterEditorPane.tsx
index 130d39d0..f834b2ed 100644
--- a/apps/web/src/components/PosterEditorPane.tsx
+++ b/apps/web/src/components/PosterEditorPane.tsx
@@ -70,7 +70,8 @@ import {
renderPosterBlob,
} from "@/lib/poster-canvas";
import {
- buildPosterDesign,
+ applyPosterDesign,
+ DESIGN_TEMPLATES,
PLATFORMS,
posterCopy,
posterPrimitiveFromObject,
@@ -1142,29 +1143,8 @@ export default function PosterEditorPane({
const editing = canvas.current?.getActiveObject();
if (editing instanceof Textbox && editing.isEditing) editing.exitEditing();
const value = current.current.document;
- const built = await buildPosterDesign({
- id,
- ...posterCopy(value),
- width,
- height,
- zh,
- });
- const extras = value.elements.filter(
- (element) =>
- !element.id.startsWith("design_") &&
- !["heading", "subtitle", "accent"].includes(element.id),
- );
- const scaled = resizePosterDocument(
- { ...value, elements: extras },
- width,
- height,
- ).elements;
- return {
- ...built,
- sourceMemoId: value.sourceMemoId,
- platformId,
- elements: [...built.elements, ...scaled],
- };
+ const built = await applyPosterDesign(value, id, zh, width, height);
+ return { ...built, platformId };
};
const applyTemplate = (id: string) =>
run(async () => {
@@ -1176,7 +1156,7 @@ export default function PosterEditorPane({
const resize = (width: number, height: number, platformId?: string) =>
run(async () => {
const value = current.current.document;
- const next = value.templateId
+ const next = value.templateId && DESIGN_TEMPLATES.some(template => template.id === value.templateId)
? await designAt(value.templateId, width, height, platformId)
: { ...resizePosterDocument(value, width, height), platformId };
syncSelection([]);
diff --git a/apps/web/src/lib/poster-client-render.ts b/apps/web/src/lib/poster-client-render.ts
index ae3a10ff..a4bace32 100644
--- a/apps/web/src/lib/poster-client-render.ts
+++ b/apps/web/src/lib/poster-client-render.ts
@@ -1,14 +1,9 @@
-import { parsePosterDocument, resizePosterDocument, type PosterDocument } from '@edgeever/shared';
+import { parsePosterDocument, type PosterDocument } from '@edgeever/shared';
import { api, ApiRequestError } from './api';
export async function applyClientPosterTemplate(document: PosterDocument, templateId: string, zh: boolean) {
- const { buildPosterDesign, posterCopy, templatesFor } = await import('./poster-design/templates');
- if (!templatesFor(undefined).some(template => template.id === templateId)) throw new Error('Unknown poster template');
- if (document.elements.some(element => element.locked)) throw new Error('Locked poster elements');
- const built = await buildPosterDesign({ id: templateId, ...posterCopy(document), width: document.width, height: document.height, zh });
- const extras = document.elements.filter(element => !element.id.startsWith('design_') && !['heading', 'subtitle', 'accent'].includes(element.id));
- return { ...built, sourceMemoId: document.sourceMemoId, platformId: document.platformId,
- elements: [...built.elements, ...resizePosterDocument({ ...document, elements: extras }, document.width, document.height).elements] };
+ const { applyPosterDesign } = await import('./poster-design/templates');
+ return applyPosterDesign(document, templateId, zh);
}
// One task at a time per workspace window. Database claims serialize all windows
diff --git a/apps/web/src/lib/poster-design/templates.ts b/apps/web/src/lib/poster-design/templates.ts
index 89bd1e84..6e30d398 100644
--- a/apps/web/src/lib/poster-design/templates.ts
+++ b/apps/web/src/lib/poster-design/templates.ts
@@ -22,6 +22,7 @@ import { TEMPLATES, sourceOf, templatesFor } from "./vendor/qiaomu/templates";
import { BadgeBox } from "./vendor/qiaomu/badge";
import { resolvePair, usePairing } from "./vendor/qiaomu/typeset";
import { POSTER_FONTS, loadDesignFonts } from "./fonts";
+import { composePosterTemplate, posterTemplateHeaderHeight } from "../poster-template-content";
export { PLATFORMS, platformFor } from "./vendor/qiaomu/platforms";
export const DESIGN_TEMPLATES = TEMPLATES;
export { templatesFor };
@@ -255,3 +256,11 @@ export async function buildPosterDesign(input: {
usePairing(undefined);
}
}
+
+export async function applyPosterDesign(document: PosterDocument, templateId: string, zh: boolean, width = document.width, height = document.height) {
+ if (!TEMPLATES.some(template => template.id === templateId)) throw new Error("Unknown poster template");
+ if (document.elements.some(element => element.locked)) throw new Error("Locked poster elements");
+ const headerHeight = posterTemplateHeaderHeight(document, height);
+ const cover = await buildPosterDesign({ id: templateId, ...posterCopy(document), width, height: Math.max(320, headerHeight), zh });
+ return composePosterTemplate(document, cover, width, height, headerHeight);
+}
diff --git a/apps/web/src/lib/poster-template-content.test.mjs b/apps/web/src/lib/poster-template-content.test.mjs
new file mode 100644
index 00000000..acfac662
--- /dev/null
+++ b/apps/web/src/lib/poster-template-content.test.mjs
@@ -0,0 +1,46 @@
+import { expect, test } from "bun:test";
+import { PosterDocumentSchema, createPosterDocument, parsePosterDocument, serializePosterDocument } from "@edgeever/shared";
+import { compilePosterMemo } from "../../../api/src/mcp-poster-tools";
+import { composePosterTemplate, posterTemplateHeaderHeight } from "./poster-template-content";
+
+const sections = Array.from({ length: 6 }, (_, i) => ({ heading: `业务 ${i + 1}`, body: `保留业务说明 ${i + 1}` }));
+const source = () => compilePosterMemo({ notebookId: "inbox", title: "产业版图", heading: "产业版图", subtitle: "连接内容与服务", badge: "产业观察", footer: "数字生态", sections, template: "bold" }).document;
+const cover = (height, background = "#d9e1d3") => PosterDocumentSchema.parse({ ...createPosterDocument("产业版图", "连接内容与服务", "minimal", 1080, height), background,
+ templateId: "sage", copy: { title: "产业版图", subtitle: "连接内容与服务", badge: "产业观察" },
+ elements: [{ id: "design_0", name: "title", role: "title", type: "text", text: "产业版图", sourceText: "产业版图", x: 80, y: 100, width: 900, height: 100, fontSize: 60, fontFamily: "sans-serif", fontWeight: "bold", textAlign: "left", fill: "#26302a", rotation: 0, opacity: 1 }] });
+
+test("template changes remove generated cover layers and keep body below the new title", () => {
+ const original = source();
+ original.elements.push({ id: "user-shape", name: "User shape", type: "ellipse", x: 10, y: 10, width: 20, height: 20, fill: "#ff0000", rotation: 0, opacity: 1 });
+ const headerHeight = posterTemplateHeaderHeight(original, original.height);
+ const next = composePosterTemplate(original, cover(headerHeight), original.width, original.height, headerHeight);
+ expect(next.elements.some(element => ["content-header", "heading", "subtitle", "badge", "accent"].includes(element.id))).toBe(false);
+ expect(next.elements.find(element => element.id === "user-shape")).toEqual(original.elements.find(element => element.id === "user-shape"));
+ const title = next.elements.find(element => element.role === "title");
+ for (const card of next.elements.filter(element => /^section-\d+-card$/.test(element.id))) expect(card.y).toBeGreaterThan(title.y + title.height);
+ for (let i = 1; i <= 6; i++) expect(next.elements.find(element => element.id === `section-${i}-body`).text).toBe(`保留业务说明 ${i}`);
+ expect(next.elements.find(element => element.id === "footer").text).toBe("数字生态");
+ expect(parsePosterDocument(serializePosterDocument(next))).toEqual(next);
+ const again = composePosterTemplate(next, cover(headerHeight, "#ffffff"), next.width, next.height, headerHeight);
+ expect(again.elements).toHaveLength(next.elements.length);
+ expect(again.elements.find(element => element.id === "section-1-card").y).toBe(next.elements.find(element => element.id === "section-1-card").y);
+});
+
+test("headline-only posters preserve custom objects without introducing a body region", () => {
+ const original = createPosterDocument("产业版图");
+ const custom = { id: "custom", name: "Custom", type: "rect", x: 50, y: 50, width: 100, height: 100, fill: "#16a06e", rotation: 0, opacity: 1 };
+ original.elements.push(custom);
+ expect(posterTemplateHeaderHeight(original, original.height)).toBe(original.height);
+ const next = composePosterTemplate(original, cover(original.height), original.width, original.height, original.height);
+ expect(next.elements.map(element => element.id)).toEqual(["design_0", "custom"]);
+});
+
+test("short canvases scale the template header and clear stale previews", () => {
+ const original = source();
+ original.previewResourceId = "stale";
+ const next = composePosterTemplate(original, cover(320), 1080, 600, 216);
+ expect(next.height).toBe(600);
+ expect(next.previewResourceId).toBeUndefined();
+ expect(next.elements.find(element => element.id === "design_0").y).toBe(67.5);
+ expect(next.elements.find(element => element.id === "section-1-card").y).toBeGreaterThan(216);
+});
diff --git a/apps/web/src/lib/poster-template-content.ts b/apps/web/src/lib/poster-template-content.ts
new file mode 100644
index 00000000..8f411645
--- /dev/null
+++ b/apps/web/src/lib/poster-template-content.ts
@@ -0,0 +1,40 @@
+import { PosterDocumentSchema, resizePosterDocument, type PosterDocument } from "@edgeever/shared";
+import { ensureReadable, readableOn } from "./poster-design/vendor/qiaomu/quality";
+
+const sectionId = /^section-\d+-(?:card|accent|number|heading|body)$/;
+export const hasPosterSections = (document: PosterDocument) => document.elements.some(element => /^section-\d+-card$/.test(element.id));
+export const posterTemplateHeaderHeight = (document: PosterDocument, height: number) => hasPosterSections(document) ? Math.round(height * 0.36) : height;
+
+// Template artwork owns the cover region. Semantic sections own the body region.
+// Only known generated IDs are replaced; user-created objects remain independent.
+export function composePosterTemplate(source: PosterDocument, cover: PosterDocument, width: number, height: number, headerHeight: number) {
+ // Hairline rules must remain at least one document pixel after resizing.
+ const scaled = resizePosterDocument({ ...source, elements: source.elements.map(element => ({ ...element, width: Math.max(element.width, source.width / width), height: Math.max(element.height, source.height / height) })) }, width, height);
+ const structured = hasPosterSections(source);
+ const extras = scaled.elements.filter(element => !element.id.startsWith("design_") && !["heading", "subtitle", "accent"].includes(element.id)
+ && !(structured && ["content-header", "badge", "footer", "footer-rule"].includes(element.id)) && !(structured && sectionId.test(element.id)));
+ if (!structured) return PosterDocumentSchema.parse({ ...cover, sourceMemoId: source.sourceMemoId, platformId: source.platformId, elements: [...cover.elements, ...extras] });
+
+ const headerScale = headerHeight / cover.height;
+ const header = cover.elements.map(element => ({ ...element, y: element.y * headerScale, height: Math.max(1, element.height * headerScale),
+ ...(element.type === "text" ? { fontSize: Math.max(4, element.fontSize * headerScale) } : {}) }));
+ const sections = scaled.elements.filter(element => sectionId.test(element.id));
+ const cards = sections.filter(element => element.id.endsWith("-card"));
+ const oldTop = Math.min(...cards.map(element => element.y));
+ const oldBottom = Math.max(...cards.map(element => element.y + element.height));
+ const unit = Math.min(width, height);
+ const top = headerHeight + unit * 0.025;
+ const bottom = scaled.elements.some(element => element.id === "footer") ? height * 0.88 : height - unit * 0.065;
+ const bodyScale = (bottom - top) / (oldBottom - oldTop);
+ const backdrop = cover.elements.filter(element => element.type === "rect" && /^#[\da-f]{6}$/i.test(element.fill))
+ .sort((a, b) => b.width * b.height - a.width * a.height)[0];
+ const cardFill = backdrop && backdrop.type === "rect" ? backdrop.fill : cover.background;
+ const ink = readableOn(cardFill);
+ const badge = cover.elements.find(element => element.type === "text" && element.role === "badge");
+ const accent = ensureReadable(badge?.type === "text" ? badge.fill : ink, cardFill);
+ const body = sections.map(element => ({ ...element, y: top + (element.y - oldTop) * bodyScale, height: Math.max(1, element.height * bodyScale),
+ ...(element.type === "text" ? { fontSize: Math.max(4, element.fontSize * Math.min(1, bodyScale)), fill: element.id.endsWith("-number") ? accent : ink } : { fill: element.id.endsWith("-card") ? cardFill : accent }) }));
+ const footer = scaled.elements.filter(element => ["footer", "footer-rule"].includes(element.id)).map(element => ({ ...element, fill: readableOn(cover.background) }));
+ return PosterDocumentSchema.parse({ ...cover, width, height, sourceMemoId: source.sourceMemoId, platformId: source.platformId,
+ elements: [...header, ...body, ...footer, ...extras] });
+}