Emdash source with visual editor image upload fix

Fixes:
1. media.ts: wrap placeholder generation in try-catch
2. toolbar.ts: check r.ok, display error message in popover
This commit is contained in:
2026-05-03 10:44:54 +07:00
parent 78f81bebb6
commit 2d1be52177
2352 changed files with 662964 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
import { describe, it, expect } from "vitest";
import { portableTextToProsemirror } from "../../../src/content/converters/portable-text-to-prosemirror.js";
import { prosemirrorToPortableText } from "../../../src/content/converters/prosemirror-to-portable-text.js";
import type { PortableTextImageBlock } from "../../../src/content/converters/types.js";
describe("Image dimension round-trip", () => {
const imageBlock: PortableTextImageBlock = {
_type: "image",
_key: "abc123",
asset: { _ref: "media-123", url: "https://example.com/photo.jpg" },
alt: "A photo",
caption: "My caption",
width: 1920,
height: 1080,
displayWidth: 400,
displayHeight: 225,
};
it("preserves displayWidth and displayHeight through PT → PM → PT", () => {
// PT → PM
const pm = portableTextToProsemirror([imageBlock]);
const imageNode = pm.content[0];
expect(imageNode.type).toBe("image");
expect(imageNode.attrs?.displayWidth).toBe(400);
expect(imageNode.attrs?.displayHeight).toBe(225);
expect(imageNode.attrs?.width).toBe(1920);
expect(imageNode.attrs?.height).toBe(1080);
// PM → PT
const pt = prosemirrorToPortableText(pm);
const restored = pt[0] as PortableTextImageBlock;
expect(restored._type).toBe("image");
expect(restored.displayWidth).toBe(400);
expect(restored.displayHeight).toBe(225);
expect(restored.width).toBe(1920);
expect(restored.height).toBe(1080);
});
it("handles images without display dimensions", () => {
const noDisplayDims: PortableTextImageBlock = {
_type: "image",
_key: "def456",
asset: { _ref: "media-456", url: "https://example.com/other.jpg" },
width: 800,
height: 600,
};
const pm = portableTextToProsemirror([noDisplayDims]);
const pt = prosemirrorToPortableText(pm);
const restored = pt[0] as PortableTextImageBlock;
expect(restored.displayWidth).toBeUndefined();
expect(restored.displayHeight).toBeUndefined();
expect(restored.width).toBe(800);
expect(restored.height).toBe(600);
});
});

View File

@@ -0,0 +1,92 @@
import { describe, it, expect } from "vitest";
import { portableTextToProsemirror } from "../../../src/content/converters/portable-text-to-prosemirror.js";
import type { PortableTextBlock } from "../../../src/content/converters/types.js";
describe("Image blocks without asset wrapper", () => {
it("does not crash when an image block has url at the top level instead of inside asset", () => {
// This is the format that can originate from migrations or third-party imports
// (e.g. Ghost → Portable Text). Without the fix, accessing block.asset.url
// throws TypeError: Cannot read properties of undefined (reading 'url').
const blocks: PortableTextBlock[] = [
{
_type: "block",
_key: "b1",
style: "normal",
children: [{ _type: "span", _key: "s1", text: "Before image", marks: [] }],
markDefs: [],
},
{
_type: "image",
_key: "img1",
url: "https://example.com/photo.jpg",
alt: "A photo without asset wrapper",
} as unknown as PortableTextBlock,
{
_type: "block",
_key: "b2",
style: "normal",
children: [{ _type: "span", _key: "s2", text: "After image", marks: [] }],
markDefs: [],
},
];
const result = portableTextToProsemirror(blocks);
expect(result.type).toBe("doc");
expect(result.content).toHaveLength(3);
});
it("extracts src and alt from top-level url when asset is missing", () => {
const blocks: PortableTextBlock[] = [
{
_type: "image",
_key: "img1",
url: "https://example.com/photo.jpg",
alt: "A test image",
} as unknown as PortableTextBlock,
];
const result = portableTextToProsemirror(blocks);
const imageNode = result.content[0];
expect(imageNode.type).toBe("image");
expect(imageNode.attrs?.src).toBe("https://example.com/photo.jpg");
expect(imageNode.attrs?.alt).toBe("A test image");
});
it("handles image block with neither asset nor url gracefully", () => {
const blocks: PortableTextBlock[] = [
{
_type: "image",
_key: "img1",
} as unknown as PortableTextBlock,
];
const result = portableTextToProsemirror(blocks);
const imageNode = result.content[0];
expect(imageNode.type).toBe("image");
expect(imageNode.attrs?.src).toBe("");
expect(imageNode.attrs?.alt).toBe("");
});
it("still converts well-formed image blocks with asset wrapper correctly", () => {
const blocks: PortableTextBlock[] = [
{
_type: "image",
_key: "img1",
asset: { _ref: "media-123", url: "https://example.com/photo.jpg" },
alt: "A proper image",
},
];
const result = portableTextToProsemirror(blocks);
const imageNode = result.content[0];
expect(imageNode.type).toBe("image");
expect(imageNode.attrs?.src).toBe("https://example.com/photo.jpg");
expect(imageNode.attrs?.alt).toBe("A proper image");
expect(imageNode.attrs?.mediaId).toBe("media-123");
});
});