AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
feat(poster): replace images and placeholders on double-click
tianma-ifb151f73ab570
diff --git a/apps/web/src/components/PosterEditorPane.tsx b/apps/web/src/components/PosterEditorPane.tsxindex f834b2ed..34cc24b2 100644--- a/apps/web/src/components/PosterEditorPane.tsx+++ b/apps/web/src/components/PosterEditorPane.tsx@@ -74,10 +74,12 @@ import {DESIGN_TEMPLATES,PLATFORMS,posterCopy,+ posterImageSlot,posterPrimitiveFromObject,platformFor,} from "@/lib/poster-design/templates";import { canRestorePosterDraft, classifyPosterUpdate, posterMemoContent, savePosterMemo } from "@/lib/poster-save";+import { posterImageReplacementTarget, replacePosterImage } from "@/lib/poster-image-replacement";import { copyImageBlobToClipboard } from "@/lib/clipboard";import { compressImageForUpload } from "@/lib/image-compression";import {@@ -211,6 +213,8 @@ export default function PosterEditorPane({const fileInput = useRef<HTMLInputElement>(null),canvas = useRef<Canvas | null>(null);const objectsRef = useRef<FabricObject[]>([]);+ const replacementTarget = useRef<string | null>(null);+ const requestImageUploadRef = useRef<(id?: string) => void>(() => {});const selectionSync = useRef(false);const session = useRef<MemoEditSession | null>(null),currentMemo = useRef(memo);@@ -705,6 +709,14 @@ export default function PosterEditorPane({setSelectedIds(ids);if (ids.length) setRightTab("properties");};+ instance.on("mouse:dblclick", (event) => {+ const state = options.current;+ if (state.readOnly || state.busy || state.tool !== "select" || state.spaceHeld) return;+ const value = current.current.document;+ const targetId = value.elements[objectsRef.current.indexOf(event.target!)]?.id;+ const target = posterImageReplacementTarget(value, targetId, event.scenePoint, posterImageSlot(value));+ if (target) requestImageUploadRef.current(target.id);+ });instance.on("selection:created", pick);instance.on("selection:updated", pick);instance.on("selection:cleared", pick);@@ -1170,7 +1182,13 @@ export default function PosterEditorPane({const platform = PLATFORMS.find((entry) => entry.id === id);if (platform) void resize(platform.width, platform.height, id);};- const upload = (file: File) =>+ const requestImageUpload = (id?: string) => {+ if (!editable) return;+ replacementTarget.current = id ?? null;+ fileInput.current?.click();+ };+ requestImageUploadRef.current = requestImageUpload;+ const upload = (file: File, replaceId: string | null = null) =>run(async () => {if (file.size > 10 * 1024 * 1024 ||@@ -1178,7 +1196,7 @@ export default function PosterEditorPane({)throw new Error(t("poster.imageLimit"));if (!navigator.onLine) throw new Error(t("poster.offline"));- if (current.current.document.elements.length >= 500)+ if (!replaceId && current.current.document.elements.length >= 500)throw new Error(t("poster.invalid"));const bitmap = await createImageBitmap(file);const width = bitmap.width,@@ -1196,6 +1214,17 @@ export default function PosterEditorPane({(value.height * 0.5) / height,1,);+ if (replaceId) {+ const target = posterImageReplacementTarget(value, replaceId, undefined, posterImageSlot(value));+ if (!target) {+ await repository.deleteResource(resource.id).catch(() => undefined);+ throw new Error(t("poster.invalid"));+ }+ const replacement = replacePosterImage(target, resource.id, width, height);+ commit({ ...value, previewResourceId: undefined, elements: value.elements.map(element => element.id === replaceId ? replacement : element) });+ syncSelection([replaceId]);+ return;+ }add({id: crypto.randomUUID(),name: file.name.slice(0, 120),@@ -1631,7 +1660,7 @@ export default function PosterEditorPane({{toolButton(t("poster.image"),<ImagePlus className="h-4 w-4" />,- () => fileInput.current?.click(),+ () => requestImageUpload(),!editable,false,true,@@ -1729,7 +1758,9 @@ export default function PosterEditorPane({onChange={(event) => {const file = event.target.files?.[0];event.target.value = "";- if (file && editable) void upload(file);+ const targetId = replacementTarget.current;+ replacementTarget.current = null;+ if (file && editable) void upload(file, targetId);}}/><div className="flex min-h-0 flex-1 overflow-hidden">@@ -1761,7 +1792,7 @@ export default function PosterEditorPane({<PosterAssetsPanelrepository={repository}disabled={!editable}- onUpload={() => fileInput.current?.click()}+ onUpload={() => requestImageUpload()}onResource={(resource) => {void readResource(resource.id).then((blob) =>diff --git a/apps/web/src/lib/poster-design/templates.ts b/apps/web/src/lib/poster-design/templates.tsindex 6e30d398..4c31dbd5 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 { matchesImageSlot, type ImageSlot } from "../poster-image-replacement";import { composePosterTemplate, posterTemplateHeaderHeight } from "../poster-template-content";export { PLATFORMS, platformFor } from "./vendor/qiaomu/platforms";export const DESIGN_TEMPLATES = TEMPLATES;@@ -224,6 +225,11 @@ export async function buildPosterDesign(input: {const elements = built.objects.map(posterPrimitiveFromObject).filter((value): value is PosterElement => Boolean(value));+ const slot = template.slot?.(input);+ if (slot) {+ const region = { x: slot.x, y: slot.y, width: slot.w, height: slot.h };+ for (const element of elements) if (matchesImageSlot(element, region)) element.imagePlaceholder = true;+ }const background =built.background.kind === "solid"? built.background.color@@ -264,3 +270,15 @@ export async function applyPosterDesign(document: PosterDocument, templateId: stconst cover = await buildPosterDesign({ id: templateId, ...posterCopy(document), width, height: Math.max(320, headerHeight), zh });return composePosterTemplate(document, cover, width, height, headerHeight);}++// Legacy saved templates did not persist a placeholder marker. Derive only the+// template's declared image slot, with the same header transform as its artwork.+export function posterImageSlot(document: PosterDocument): ImageSlot | undefined {+ const template = TEMPLATES.find(entry => entry.id === document.templateId);+ if (!template?.slot) return;+ const header = posterTemplateHeaderHeight(document, document.height);+ const buildHeight = Math.max(320, header);+ const slot = template.slot({ ...posterCopy(document), width: document.width, height: buildHeight, zh: true });+ if (!slot) return;+ return { x: slot.x, y: slot.y * header / buildHeight, width: slot.w, height: slot.h * header / buildHeight };+}diff --git a/apps/web/src/lib/poster-image-replacement.test.mjs b/apps/web/src/lib/poster-image-replacement.test.mjsnew file mode 100644index 00000000..c694dc69--- /dev/null+++ b/apps/web/src/lib/poster-image-replacement.test.mjs@@ -0,0 +1,35 @@+import { expect, test } from "bun:test";+import { createPosterDocument, parsePosterDocument, serializePosterDocument } from "@edgeever/shared";+import { posterImageReplacementTarget, replacePosterImage } from "./poster-image-replacement";++const placeholder = { id: "design_1", name: "Photo", type: "rect", x: 500, y: 60, width: 320, height: 320, fill: "#16a06e", rotation: 0, opacity: 1, imagePlaceholder: true };+const doc = () => ({ ...createPosterDocument("Title"), elements: [placeholder] });+test("only images and declared placeholders offer replacement, including legacy slots", () => {+ const document = doc();+ expect(posterImageReplacementTarget(document, placeholder.id).id).toBe(placeholder.id);+ expect(posterImageReplacementTarget({ ...document, elements: [{ ...placeholder, imagePlaceholder: undefined }] }, placeholder.id)).toBeUndefined();+ const slot = { x: 500, y: 60, width: 320, height: 320 };+ expect(posterImageReplacementTarget({ ...document, elements: [{ ...placeholder, imagePlaceholder: undefined }] }, placeholder.id, undefined, slot).id).toBe(placeholder.id);+ for (const patch of [{ locked: true }, { visible: false }]) expect(posterImageReplacementTarget({ ...document, elements: [{ ...placeholder, ...patch }] }, placeholder.id)).toBeUndefined();+ const heading = createPosterDocument("Title").elements.find(element => element.type === "text");+ expect(posterImageReplacementTarget({ ...document, elements: [placeholder, heading] }, heading.id, { x: 600, y: 100 })).toBeUndefined();+ const glow = { ...placeholder, id: "design_2", type: "ellipse", imagePlaceholder: undefined };+ expect(posterImageReplacementTarget({ ...document, elements: [placeholder, glow] }, glow.id, { x: 600, y: 100 }).id).toBe(placeholder.id);+ expect(posterImageReplacementTarget({ ...document, elements: [placeholder, glow] }, glow.id, { x: 10, y: 10 })).toBeUndefined();+});++test("replacements retain geometry and layer identity, use centered cover crops and round-trip", () => {+ const replacement = replacePosterImage(placeholder, "photo", 1600, 900);+ expect(replacement).toMatchObject({ id: placeholder.id, type: "image", x: 500, y: 60, width: 320, height: 320, crop: { x: 0.21875, y: 0, width: 0.5625, height: 1 } });+ const glow = { ...placeholder, id: "design_2", type: "ellipse", imagePlaceholder: undefined };+ expect(posterImageReplacementTarget({ ...doc(), elements: [replacement, glow] }, glow.id, { x: 600, y: 100 }).id).toBe(replacement.id);+ expect(replacement.imagePlaceholder).toBeUndefined();+ expect(replacement.fill).toBeUndefined();+ const document = { ...doc(), elements: [replacement] };+ expect(parsePosterDocument(serializePosterDocument(document))).toEqual(document);+ expect(posterImageReplacementTarget(document, replacement.id).id).toBe(placeholder.id);+ const cropped = { ...replacement, rotation: 20, crop: { x: 0.1, y: 0.1, width: 0.8, height: 0.8 } };+ expect(replacePosterImage(cropped, "second", 900, 1600)).toMatchObject({ crop: cropped.crop, rotation: 20 });+ expect(() => replacePosterImage({ ...placeholder, locked: true }, "photo", 1600, 900)).toThrow();+ expect(parsePosterDocument(serializePosterDocument(doc())).elements[0].imagePlaceholder).toBe(true);+});diff --git a/apps/web/src/lib/poster-image-replacement.ts b/apps/web/src/lib/poster-image-replacement.tsnew file mode 100644index 00000000..d9e42cce--- /dev/null+++ b/apps/web/src/lib/poster-image-replacement.ts@@ -0,0 +1,25 @@+import { PosterElementSchema, type PosterDocument, type PosterElement } from "@edgeever/shared";+export type ImageSlot = { x: number; y: number; width: number; height: number };+export function matchesImageSlot(element: PosterElement, slot?: ImageSlot) {+ return element.type === "rect" && Boolean(slot) && element.id.startsWith("design_") && Math.abs(element.rotation) < 0.01+ && ["x", "y", "width", "height"].every(key => Math.abs(element[key as keyof ImageSlot] - slot![key as keyof ImageSlot]) < 2);+}+export function posterImageReplacementTarget(document: PosterDocument, targetId?: string, point?: { x: number; y: number }, slot?: ImageSlot) {+ const target = document.elements.find(element => element.id === targetId);+ if (!target || target.locked || target.visible === false || target.type === "text") return;+ if (target.type === "image" || target.imagePlaceholder || matchesImageSlot(target, slot)) return target;+ // A decorative glow can be on top of a photo placeholder. Hit the declared+ // image region underneath it, never an arbitrary rectangle or text block.+ if (!point || !target.id.startsWith("design_")) return;+ return [...document.elements].reverse().find(element => !element.locked && element.visible !== false && (element.imagePlaceholder || (element.type === "image" && element.id.startsWith("design_")) || matchesImageSlot(element, slot))+ && Math.abs(element.rotation) < 0.01 && point.x >= element.x && point.x <= element.x + element.width && point.y >= element.y && point.y <= element.y + element.height);+}+export function replacePosterImage(element: PosterElement, resourceId: string, imageWidth: number, imageHeight: number): PosterElement {+ if ((element.type !== "image" && element.type !== "rect") || element.locked || element.visible === false) throw new Error("Poster image is missing or locked");+ const ratio = imageWidth / imageHeight;+ const frame = element.width / element.height;+ const cropWidth = Math.min(1, frame / ratio);+ const cropHeight = Math.min(1, ratio / frame);+ return PosterElementSchema.parse({ ...element, type: "image", resourceId, imagePlaceholder: undefined, gradient: undefined,+ crop: element.type === "image" && element.crop ? element.crop : { x: (1 - cropWidth) / 2, y: (1 - cropHeight) / 2, width: cropWidth, height: cropHeight } });+}diff --git a/packages/shared/src/poster.ts b/packages/shared/src/poster.tsindex 44e20069..0ba164b3 100644--- a/packages/shared/src/poster.ts+++ b/packages/shared/src/poster.ts@@ -59,6 +59,7 @@ const geometry = {name: z.string().max(120),role: z.enum(["title", "subtitle", "badge", "decor"]).optional(),visible: z.boolean().optional(),+ imagePlaceholder: z.boolean().optional(),flipX: z.boolean().optional(),flipY: z.boolean().optional(),stroke: strokeColor.optional(),