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,67 @@
import { describe, expect, it } from "vitest";
import { createEditable, createNoop } from "../../../src/visual-editing/editable.js";
describe("createEditable", () => {
it("returns entry-level annotation when spread", () => {
const edit = createEditable("posts", "my-post");
expect({ ...edit }).toEqual({
"data-emdash-ref": '{"collection":"posts","id":"my-post"}',
});
});
it("includes status and hasDraft in entry-level annotation", () => {
const edit = createEditable("posts", "my-post", {
status: "published",
hasDraft: true,
});
expect({ ...edit }).toEqual({
"data-emdash-ref":
'{"collection":"posts","id":"my-post","status":"published","hasDraft":true}',
});
});
it("includes status/hasDraft in field-level annotations", () => {
const edit = createEditable("posts", "my-post", {
status: "published",
hasDraft: true,
});
expect(edit.title).toEqual({
"data-emdash-ref":
'{"collection":"posts","id":"my-post","status":"published","hasDraft":true,"field":"title"}',
});
});
it("returns field-level annotation for property access", () => {
const edit = createEditable("posts", "my-post");
expect(edit.title).toEqual({
"data-emdash-ref": '{"collection":"posts","id":"my-post","field":"title"}',
});
});
it("handles nested fields via bracket notation", () => {
const edit = createEditable("posts", "my-post");
expect(edit["hero.src"]).toEqual({
"data-emdash-ref": '{"collection":"posts","id":"my-post","field":"hero.src"}',
});
});
it("serializes to JSON correctly", () => {
const edit = createEditable("posts", "my-post");
expect(JSON.stringify({ edit })).toBe(
'{"edit":{"data-emdash-ref":"{\\"collection\\":\\"posts\\",\\"id\\":\\"my-post\\"}"}}',
);
});
});
describe("createNoop", () => {
it("returns empty object", () => {
const edit = createNoop();
expect({ ...edit }).toEqual({});
});
it("property access returns undefined", () => {
const edit = createNoop();
expect((edit as Record<string, unknown>).title).toBeUndefined();
});
});

View File

@@ -0,0 +1,96 @@
import { describe, expect, it } from "vitest";
import { renderToolbar } from "../../../src/visual-editing/toolbar.js";
// Regex patterns for HTML validation
const EDIT_TOGGLE_CHECKED_REGEX = /id="emdash-edit-toggle"\s+checked/;
describe("renderToolbar", () => {
it("renders toolbar with edit mode off", () => {
const html = renderToolbar({ editMode: false, isPreview: false });
expect(html).toContain('id="emdash-toolbar"');
expect(html).toContain('data-edit-mode="false"');
expect(html).not.toMatch(EDIT_TOGGLE_CHECKED_REGEX);
});
it("renders toolbar with edit mode on", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain('data-edit-mode="true"');
expect(html).toContain("checked");
});
it("stores preview state as data attribute", () => {
const html = renderToolbar({ editMode: false, isPreview: true });
expect(html).toContain('data-preview="true"');
});
it("includes toggle switch", () => {
const html = renderToolbar({ editMode: false, isPreview: false });
expect(html).toContain('id="emdash-edit-toggle"');
expect(html).toContain("emdash-tb-toggle");
});
it("includes publish button (hidden by default)", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain('id="emdash-tb-publish"');
expect(html).toContain('style="display:none"');
});
it("includes save status element", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain('id="emdash-tb-save-status"');
});
it("includes inline editing script with save state tracking", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("<script>");
expect(html).toContain("setSaveState");
expect(html).toContain("unsaved");
expect(html).toContain("contentEditable");
});
it("includes text cursor for editable hover", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("[data-emdash-ref]:hover");
expect(html).toContain("cursor: text");
});
it("includes manifest fetching for field type lookup", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("fetchManifest");
expect(html).toContain("/_emdash/api/manifest");
});
it("unwraps the { data } envelope returned by /_emdash/api/manifest", () => {
// Regression for #103 / #445: the manifest endpoint wraps the payload in
// { data: manifest } (ApiResponse shape), but getFieldKind reads
// manifest.collections directly. Without the unwrap, getFieldKind returns
// null for every field kind, and every click on an edit annotation opens
// the admin in a new tab instead of inline-editing.
const html = renderToolbar({ editMode: true, isPreview: false });
// The unwrap happens inside the fetchManifest .then() callback. Verify
// the generated HTML contains the conditional unwrap rather than
// assigning the raw response.
expect(html).toMatch(/manifestCache\s*=\s*m\s*&&\s*m\.data\s*\?\s*m\.data\s*:\s*m/);
});
it("skips toolbar interception for portableText (inline editor)", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("portableText");
});
it("includes entry status badge styles", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("emdash-tb-badge--draft");
expect(html).toContain("emdash-tb-badge--published");
expect(html).toContain("emdash-tb-badge--pending");
});
it("includes save state badge styles", () => {
const html = renderToolbar({ editMode: true, isPreview: false });
expect(html).toContain("emdash-tb-badge--unsaved");
expect(html).toContain("emdash-tb-badge--saving");
expect(html).toContain("emdash-tb-badge--saved");
expect(html).toContain("emdash-tb-badge--error");
});
});