Files
CatoandClaude Opus 5.5 0f3f481a3b feat: charts in articles and suggestions the writer accepts or rejects (0.3.0) (#14)
* feat: charts in articles and suggestions the writer accepts or rejects (0.3.0)

- ```chart blocks (JSON or CSV spec): parseChartBlock, splitChartBlocks,
  table fallbacks for RSS/share cards (chartsToTables), chartValuesNotInText
  so a proposed chart draws only the writer's own numbers
- @bitbaum/writingkit/react/chart: ArticleChart / ArticleChartBlock drawn by
  @bitbaum/chartkit (new optional peer)
- Suggestion, applySuggestion (refuses stale text), wordDiff, SuggestionCard
- suggestMedia: a chart beside the paragraph with figures, else an illustration
- ArticleComposer: renderChart in Preview; draft-from-brief is a suggestion

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvT2nQyPBK4EfWf3Rn

* feat(chart): read bip-kit's series-line format too

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvT2nQyPBK4EfWf3Rn

* fix(suggestions): show a rewrite whole; keep Publish the one primary action

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvT2nQyPBK4EfWf3Rn

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-15 02:26:30 +01:00

96 lines
3.3 KiB
JavaScript

/**
* Writing help proposes; the writer decides. A suggestion carries the text it
* was made from, so accepting it after the writer kept typing cannot overwrite
* their newer words: `applySuggestion` answers `null` and the host says so.
*/
/**
* The fields with the suggestion applied, or `null` when the text it was made
* from is no longer there (the writer changed it in the meantime).
*/
export function applySuggestion(fields, suggestion, chosen = suggestion.after) {
const current = fields[suggestion.field];
if (suggestion.start === undefined || suggestion.end === undefined) {
if (current !== suggestion.before)
return null;
return { ...fields, [suggestion.field]: chosen };
}
const { start, end } = suggestion;
if (start < 0 || end < start || end > current.length)
return null;
if (current.slice(start, end) !== suggestion.before)
return null;
return {
...fields,
[suggestion.field]: current.slice(0, start) + chosen + current.slice(end),
};
}
/** Above this many words on a side, the diff is shown as removed + added. */
export const DIFF_WORD_LIMIT = 1_500;
/**
* A word-level diff for showing a suggestion: what goes, what comes. Plain
* LCS on words with whitespace kept, so joining every part except `removed`
* gives `after` exactly, and every part except `added` gives `before`.
*/
export function wordDiff(before, after) {
const a = before.split(/(\s+)/).filter(Boolean);
const b = after.split(/(\s+)/).filter(Boolean);
if (a.length > DIFF_WORD_LIMIT || b.length > DIFF_WORD_LIMIT) {
return [
...(before ? [{ kind: "removed", text: before }] : []),
...(after ? [{ kind: "added", text: after }] : []),
];
}
const rows = a.length + 1;
const cols = b.length + 1;
const table = new Uint32Array(rows * cols);
for (let i = a.length - 1; i >= 0; i--)
for (let j = b.length - 1; j >= 0; j--)
table[i * cols + j] =
a[i] === b[j]
? table[(i + 1) * cols + j + 1] + 1
: Math.max(table[(i + 1) * cols + j], table[i * cols + j + 1]);
const parts = [];
const push = (kind, text) => {
const last = parts[parts.length - 1];
if (last && last.kind === kind)
last.text += text;
else
parts.push({ kind, text });
};
let i = 0;
let j = 0;
while (i < a.length && j < b.length) {
if (a[i] === b[j]) {
push("same", a[i]);
i++;
j++;
}
else if (table[(i + 1) * cols + j] >= table[i * cols + j + 1]) {
push("removed", a[i++]);
}
else {
push("added", b[j++]);
}
}
while (i < a.length)
push("removed", a[i++]);
while (j < b.length)
push("added", b[j++]);
return parts;
}
/**
* Whether a diff is really a rewrite: more than half of the old text goes.
* Then strike-through noise helps nobody; show the new text whole.
*/
export function isRewrite(parts) {
let kept = 0;
let removed = 0;
for (const p of parts) {
if (p.kind === "same")
kept += p.text.trim().length;
else if (p.kind === "removed")
removed += p.text.trim().length;
}
return removed > kept;
}