mirror of
https://github.com/bitbaum/writingkit.git
synced 2026-12-04 05:37:40 +01:00
* fix(pile): chip classes no longer collide with whenkit; v0.4.1 .wk-chip/.wk-chips are whenkit's; where both stylesheets load each restyled the other's chips. Renamed to .wk-pile-chip(s). Version bump so publish.yml ships it on merge. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmsN9mz8QfGDCrNhY9p * fix(styles): words use the text colour, not the hairline colour .wk-meta, .wk-diff del and .wk-chart-problem used --ck-muted (dots and hairlines; ~2:1 in OrangeCat). They use --ck-fg-3 (4.5:1). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmsN9mz8QfGDCrNhY9p * test(browser): wait for Bold's placeholder selection before typing Bold selects its placeholder a frame later. Filling before that frame let the late selection turn the fill into "replace the word bold", failing the improve/speak/preview checks in about 1 run in 3 (on main too: 3/8). Now 0/10. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmsN9mz8QfGDCrNhY9p * ci: re-run verify Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmsN9mz8QfGDCrNhY9p --------- Co-authored-by: Claude <noreply@anthropic.com>
127 lines
7.6 KiB
JavaScript
127 lines
7.6 KiB
JavaScript
"use client";
|
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
import { useMemo, useRef, useState } from "react";
|
|
import { DictationField } from "@bitbaum/chatkit/react";
|
|
import { COMPOSE_LIMITS, splitPile } from "../compose.js";
|
|
export const DEFAULT_PILE_LABELS = {
|
|
title: "Write with AI",
|
|
help: "Throw in everything: thoughts, a pasted chat, links, documents, pictures. Speak if that is easier. The article is written for you; change what you like after.",
|
|
field: "Your material",
|
|
placeholder: "Type, paste or speak. Links are read.",
|
|
add: "Add pictures or files",
|
|
links: "Links to read",
|
|
remove: "Remove",
|
|
write: "Write the article",
|
|
writing: "Writing your article…",
|
|
cancel: "Cancel",
|
|
needSomething: "Add some thoughts, links, documents or pictures first.",
|
|
failed: "Something went wrong while writing. Your material is still here: try again.",
|
|
unreadable: "could not be read here. Paste its text into the box instead, or attach it as a PDF.",
|
|
uploadFailed: "could not be added. Try it again, or use a JPEG or PNG under 10 MB.",
|
|
tooMany: "Only the first links are read; paste the text of the rest.",
|
|
};
|
|
const TEXT_TYPES = /^(text\/|application\/(json|xml|markdown))/;
|
|
export function PileComposer({ transport, onComposed, voice = {}, labels: custom, }) {
|
|
const labels = { ...DEFAULT_PILE_LABELS, ...custom };
|
|
const [text, setText] = useState("");
|
|
const [attachments, setAttachments] = useState([]);
|
|
const [busy, setBusy] = useState(false);
|
|
const [problem, setProblem] = useState(null);
|
|
const controller = useRef(null);
|
|
const nextId = useRef(1);
|
|
const fileInput = useRef(null);
|
|
const { urls, skippedUrls } = useMemo(() => splitPile(text), [text]);
|
|
const accepts = [transport.uploadImage ? "image/*" : "", ".txt,.md,.csv,.json", transport.extractText ? ".pdf,application/pdf" : ""]
|
|
.filter(Boolean)
|
|
.join(",");
|
|
async function addFiles(files) {
|
|
setProblem(null);
|
|
for (const file of files) {
|
|
const id = nextId.current++;
|
|
try {
|
|
if (file.type.startsWith("image/") && transport.uploadImage) {
|
|
const stored = await transport.uploadImage(file);
|
|
if (!stored)
|
|
throw new Error();
|
|
setAttachments((a) => [...a, { id, kind: "image", name: file.name, ...stored }]);
|
|
}
|
|
else if (TEXT_TYPES.test(file.type) || /\.(txt|md|csv|json)$/i.test(file.name)) {
|
|
setAttachments((a) => [...a, { id, kind: "document", name: file.name, text: "" }]);
|
|
const content = await file.text();
|
|
setAttachments((a) => a.map((x) => (x.id === id ? { ...x, text: content } : x)));
|
|
}
|
|
else if (transport.extractText) {
|
|
const content = await transport.extractText(file);
|
|
if (!content?.trim())
|
|
throw new Error();
|
|
setAttachments((a) => [...a, { id, kind: "document", name: file.name, text: content }]);
|
|
}
|
|
else
|
|
throw new Error();
|
|
}
|
|
catch (error) {
|
|
setAttachments((a) => a.filter((x) => x.id !== id));
|
|
const isImage = file.type.startsWith("image/") && transport.uploadImage;
|
|
// The app's own reason (size, format) is the actionable one; ours is the fallback.
|
|
const reason = error instanceof Error && error.message ? error.message : null;
|
|
setProblem(isImage
|
|
? reason
|
|
? `${file.name}: ${reason}`
|
|
: `${file.name} ${labels.uploadFailed}`
|
|
: `${file.name} ${labels.unreadable}`);
|
|
}
|
|
}
|
|
}
|
|
function onPaste(event) {
|
|
const files = [...event.clipboardData.files];
|
|
if (files.length) {
|
|
event.preventDefault();
|
|
void addFiles(files);
|
|
}
|
|
}
|
|
async function write() {
|
|
const items = [
|
|
...(text.trim() ? [{ kind: "text", text }] : []),
|
|
...attachments.map((a) => a.kind === "image"
|
|
? { kind: "image", src: a.src, description: a.description }
|
|
: { kind: "document", name: a.name, text: a.text }),
|
|
];
|
|
if (!items.length) {
|
|
setProblem(labels.needSomething);
|
|
return;
|
|
}
|
|
const c = new AbortController();
|
|
controller.current = c;
|
|
setBusy(true);
|
|
setProblem(null);
|
|
try {
|
|
const result = await transport.compose(items, { signal: c.signal });
|
|
if (c.signal.aborted)
|
|
return;
|
|
if ("problem" in result)
|
|
setProblem(result.problem);
|
|
else
|
|
onComposed(result);
|
|
}
|
|
catch {
|
|
if (!c.signal.aborted)
|
|
setProblem(labels.failed);
|
|
}
|
|
finally {
|
|
if (controller.current === c) {
|
|
controller.current = null;
|
|
setBusy(false);
|
|
}
|
|
}
|
|
}
|
|
function cancel() {
|
|
controller.current?.abort();
|
|
controller.current = null;
|
|
setBusy(false);
|
|
}
|
|
return (_jsx("div", { className: "wk-composer wk-pile", onPaste: onPaste, children: _jsxs("div", { className: "wk-panel", children: [_jsx("h2", { className: "wk-panel-title", children: labels.title }), _jsx("p", { className: "wk-meta", children: labels.help }), _jsx(DictationField, { label: labels.field, className: "wk-body", value: text, onValueChange: setText, rows: 10, maxLength: COMPOSE_LIMITS.pile, placeholder: labels.placeholder, voice: voice, disabled: busy }), urls.length > 0 && (_jsxs("div", { className: "wk-pile-group", children: [_jsx("p", { className: "wk-meta", children: labels.links }), _jsx("ul", { className: "wk-pile-chips", children: urls.map((u) => (_jsx("li", { className: "wk-pile-chip", children: _jsx("span", { className: "wk-pile-chip-name", children: u.replace(/^https?:\/\/(www\.)?/, "") }) }, u))) }), skippedUrls.length > 0 && _jsx("p", { className: "wk-meta", children: labels.tooMany })] })), attachments.length > 0 && (_jsx("ul", { className: "wk-pile-chips", children: attachments.map((a) => (_jsxs("li", { className: "wk-pile-chip", children: [a.kind === "image" && _jsx("img", { src: a.src, alt: "", className: "wk-pile-chip-thumb" }), _jsx("span", { className: "wk-pile-chip-name", children: a.name }), _jsx("button", { type: "button", className: "wk-tool", "aria-label": `${labels.remove} ${a.name}`, disabled: busy, onClick: () => setAttachments((x) => x.filter((y) => y.id !== a.id)), children: "\u00D7" })] }, a.id))) })), _jsx("input", { ref: fileInput, type: "file", multiple: true, accept: accepts, className: "wk-sr", tabIndex: -1, "aria-hidden": "true", onChange: (e) => {
|
|
void addFiles([...(e.currentTarget.files ?? [])]);
|
|
e.currentTarget.value = "";
|
|
} }), _jsx("button", { type: "button", className: "wk-btn", disabled: busy, onClick: () => fileInput.current?.click(), children: labels.add }), problem && (_jsx("p", { className: "wk-problems", role: "alert", children: problem })), _jsx("div", { className: "wk-actions-row", children: busy ? (_jsxs(_Fragment, { children: [_jsx("p", { className: "wk-meta", role: "status", children: labels.writing }), _jsx("button", { type: "button", className: "wk-btn", onClick: cancel, children: labels.cancel })] })) : (_jsx("button", { type: "button", className: "wk-btn wk-btn-primary wk-btn-wide", onClick: () => void write(), children: labels.write })) })] }) }));
|
|
}
|