Import a Word document as a blog post, and add table blocks
- Table content blocks (migration 044): a grid of text, a row per line, cells split by |, the first row the header; cells take the same formatting as a paragraph; wide tables scroll on a phone - Blog posts -> Import from Word: the server reads the .docx (zip and XML read without a library, sizes capped) and creates an unpublished post through the CRUD engine: title, date, headings, paragraphs, lists, tables, bold, italic and links; the editor opens with a note of what came over and what didn't (images) - docs/importing.md, and an nginx block for the upload Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
adc88e5714
commit
7f12824c93
16 changed files with 656 additions and 6 deletions
|
|
@ -15,7 +15,7 @@
|
|||
import { useCallback, useEffect, useState } from "react";
|
||||
import { Link, Navigate, useNavigate, useParams, useSearchParams } from "react-router-dom";
|
||||
|
||||
import { get, ApiError } from "../../lib/api.ts";
|
||||
import { get, upload, ApiError } from "../../lib/api.ts";
|
||||
import { isUnauthorized, useAuth } from "../../lib/auth.tsx";
|
||||
import { ADMIN_ENTITIES } from "../../lib/adminSchema.ts";
|
||||
import { atLeast } from "../../lib/roles.ts";
|
||||
|
|
@ -93,10 +93,11 @@ export default function EntityList() {
|
|||
<div>
|
||||
<div className="flex flex-wrap items-center gap-4">
|
||||
<h1 className="text-3xl font-bold text-link">{manifest.label}</h1>
|
||||
{canWrite && manifest.importer && <ImportButton manifest={manifest} />}
|
||||
{canWrite && (
|
||||
<Link
|
||||
to={`/admin/${manifest.key}/new`}
|
||||
className="ml-auto rounded-full bg-action px-4 py-2 text-sm font-semibold text-white transition-colors hover:bg-action-hover"
|
||||
className={`${manifest.importer ? "" : "ml-auto "}rounded-full bg-action px-4 py-2 text-sm font-semibold text-white transition-colors hover:bg-action-hover`}
|
||||
>
|
||||
New {manifest.singular}
|
||||
</Link>
|
||||
|
|
@ -213,3 +214,54 @@ export default function EntityList() {
|
|||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/* A manifest's `importer`: pick a file, the server turns it into a
|
||||
new record, and its editor opens with a note of what came over
|
||||
(EntityEdit shows it). Blog posts take Word documents. */
|
||||
function ImportButton({ manifest }: { manifest: any }) {
|
||||
const navigate = useNavigate();
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
async function send(file: File) {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
const form = new FormData();
|
||||
form.set("file", file);
|
||||
try {
|
||||
const result = await upload(manifest.importer.endpoint, form);
|
||||
navigate(`/admin/${manifest.key}/${result.id}`, { state: { imported: result.summary } });
|
||||
} catch (err) {
|
||||
setError(err instanceof ApiError ? err.message : "Couldn't reach the server.");
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="ml-auto flex items-center gap-3">
|
||||
{error && (
|
||||
<span role="alert" className="text-sm text-danger">
|
||||
{error}
|
||||
</span>
|
||||
)}
|
||||
<label
|
||||
className={`cursor-pointer rounded-full border border-link px-4 py-2 text-sm font-semibold text-link transition-colors hover:bg-tint ${
|
||||
busy ? "pointer-events-none opacity-60" : ""
|
||||
}`}
|
||||
>
|
||||
{busy ? "Importing…" : manifest.importer.label}
|
||||
<input
|
||||
type="file"
|
||||
accept={manifest.importer.accept}
|
||||
className="sr-only"
|
||||
disabled={busy}
|
||||
onChange={(e) => {
|
||||
const file = e.target.files?.[0];
|
||||
e.target.value = "";
|
||||
if (file) send(file);
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue