feat: the editor records, shows and applies tracked changes
Two marks carry a revision with its author and date: an insertion is marked text, a deletion is text kept and struck through. Consecutive typing joins one change; accept/reject work at the cursor or on all. ODF splits the pair — text:change-start/-end around an insertion, a text:change where a deletion was, its text in the tracked-changes registry. DOCX uses w:ins/w:del with w:delText. Both round-trip, and accepting every change gives the document an unrecorded edit would.
This commit is contained in:
1 parent
da438a7655
commit
5ee4eec5d7
18 files changed
+646
-16
No files matched your search
@@ -31,6 +31,7 @@ The **Open .odt** button (`App.svelte`) parses an uploaded file with `importOdt(
|
||||
- **A page-anchored frame** (`text:anchor-type="page"`) is out of the flow: it rides a paragraph of its own that collapses to nothing, and the node view places it from its page's corner (`anchorPage`, `image.ts`) — that paragraph must stay `position: static` (`editor.css`, `!important` over the line-height rule's own `position: relative`), or it becomes the frame's containing block instead of the page and offsets it by a page margin. Behind the text by default (`style:run-through="background"`); a cover page's own graphic instead declares `"foreground"` (`inFront` attr) to sit in front and hide what the page hoists behind it (a redundant logo, a title paragraph the graphic already carries). In a cell it falls back to an ordinary floating frame.
|
||||
- **A header/footer zone is fitted to its schema** (`fitZoneSchema`): the zone editor has fewer marks and nodes than the body, and one it doesn't know makes `generateHTML` throw — which renders the whole zone blank. Unknown marks are dropped, unknown nodes leave their text.
|
||||
- **A hoisted text box keeps what its anchor paragraph gave it**: an as-char figure frame is centred by the paragraph it sits in, so `wrapAlign` carries that (or the frame's own `style:horizontal-pos`) onto the box. Its `fo:padding` is read too — the editor's own 0.15cm ring is only the default.
|
||||
- **Tracked changes**: ODF points at a registry, so `odfRevisions(body)` reads `<text:tracked-changes>` first and the body walk resolves each `text:change-start`/`-end` and `text:change` against it (the open insertions live in `Ctx` — a range may close in a later paragraph). A `text:change` is a deletion whose text is *only* in the registry: it is emitted there and then, marked. Word needs no registry — `w:ins`/`w:del` wrap their runs, whose text sits in `w:delText` rather than `w:t` for a deletion.
|
||||
- **Footnotes and endnotes** round-trip: `text:note` (ODF) and `word/footnotes.xml`/`endnotes.xml` (DOCX) become a `noteRef` anchor plus a `note` in the document-final `noteSection`, and the numbering configuration becomes `NoteSettings`. See `docs/architecture/notes.md`.
|
||||
- **Graceful degradation** (collected as `warnings`, surfaced once): comments dropped, hyperlinks flattened to text, nested tables flattened to paragraphs, non-`Pictures` drawings dropped, unreadable pictures skipped (each with a warning). Page size is normalized to A4. Floating/wrapped images are imported as left/right/top-bottom wrap (free x/y positions collapse to the nearest side; see `image.ts`).
|
||||
|
||||
|
||||
+21
-1
@@ -1267,6 +1267,9 @@ function convertInline(p: Element, ctx: Ctx, baseRun: RunProps, defaults: BlockD
|
||||
break;
|
||||
}
|
||||
case 'instrText': if (fieldMode === 'instr') fieldInstr += child.textContent ?? ''; break;
|
||||
// w:delText is a deleted run's text — the same content, kept because the
|
||||
// revision is only recorded, not applied.
|
||||
case 'delText':
|
||||
case 't':
|
||||
if ((fieldCrossRef || fieldSeq) && fieldMode === 'result') fieldResultText += child.textContent ?? '';
|
||||
else if (!skipResult()) pushText(child.textContent ?? '', marks);
|
||||
@@ -1339,9 +1342,26 @@ function convertInline(p: Element, ctx: Ctx, baseRun: RunProps, defaults: BlockD
|
||||
for (const r of fcAll(el, 'r')) handleRun(r); // body: keep the shown value
|
||||
break;
|
||||
}
|
||||
case 'ins': // accepted tracked-change insertion → keep its runs
|
||||
case 'ins':
|
||||
case 'del': {
|
||||
// A recorded revision. Its runs keep their formatting and take the mark; a
|
||||
// deletion's text sits in w:delText, which handleRun reads as ordinary text.
|
||||
const kind = el.localName === 'del' ? 'deletion' : 'insertion';
|
||||
const attrs = {
|
||||
id: el.getAttributeNS(W, 'id') ?? '',
|
||||
author: el.getAttributeNS(W, 'author') ?? '',
|
||||
date: el.getAttributeNS(W, 'date') ?? '',
|
||||
};
|
||||
const before = out.length;
|
||||
for (const r of fcAll(el, 'r')) handleRun(r);
|
||||
if (!hfFields) {
|
||||
for (let i = before; i < out.length; i++) {
|
||||
if (out[i].type !== 'text') continue;
|
||||
out[i].marks = [...(out[i].marks ?? []), { type: kind, attrs }];
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'smartTag':
|
||||
for (const r of fcAll(el, 'r')) handleRun(r);
|
||||
break;
|
||||
|
||||
+53
-4
@@ -138,6 +138,10 @@ type Ctx = {
|
||||
openBookmarks: Set<string>;
|
||||
// Ranged comments currently open, by their office:name — same shape as openBookmarks.
|
||||
openComments: Map<string, Record<string, unknown>>;
|
||||
// Recorded revisions: the <text:tracked-changes> registry by change id, and the
|
||||
// insertions whose <text:change-start> has been seen but not their end.
|
||||
revisions: Map<string, { kind: 'insertion' | 'deletion'; author: string; date: string; text: string }>;
|
||||
openInsertions: Map<string, Record<string, unknown>>;
|
||||
// Footnotes/endnotes in anchor order: the file stores each note's text at its anchor,
|
||||
// the editor keeps them in one section at the document end (notes.ts).
|
||||
notes: { id: string; kind: NoteKind; label: string | null; text: string; content: Node[]; styleName: string | null }[];
|
||||
@@ -616,7 +620,7 @@ export function importOdt(bytes: Uint8Array, convertedImages: ConvertedImages =
|
||||
const contentWidthCm = geo
|
||||
? pageDimsCm(geo.format, geo.orientation).w - geo.margins.left - geo.margins.right
|
||||
: pageDimsCm('A4', 'portrait').w - 2 * 2.12;
|
||||
const ctx: Ctx = { resolver, styleNames, usedStyles: new Set(), charStyleNames, usedCharStyles: new Set(), warnings, files, imageCache: new Map(), convertedImages, pendingBlocks: [], contentWidthCm, pageRtl: geo?.rtl ?? false, masterPages: [], masterBlocks: new Map(), openBookmarks: new Set(), openComments: new Map(), notes: [] };
|
||||
const ctx: Ctx = { resolver, styleNames, usedStyles: new Set(), charStyleNames, usedCharStyles: new Set(), warnings, files, imageCache: new Map(), convertedImages, pendingBlocks: [], contentWidthCm, pageRtl: geo?.rtl ?? false, masterPages: [], masterBlocks: new Map(), openBookmarks: new Set(), openComments: new Map(), revisions: odfRevisions(body), openInsertions: new Map(), notes: [] };
|
||||
let blocks = convertBlocks(Array.from(body.children), ctx, 'body');
|
||||
if (blocks.length === 0) blocks.push({ type: 'paragraph' });
|
||||
pairAlignedFrames(blocks, Math.floor(cmToPx(contentWidthCm)));
|
||||
@@ -1611,6 +1615,27 @@ function convertDateTimeField(e: Element, ctx: Ctx): Node | null {
|
||||
return { type: 'dateTimeField', attrs: { kind, format, fixed, value: fieldValue(kind, raw) } };
|
||||
}
|
||||
|
||||
// The <text:tracked-changes> registry: one entry per change id. A deletion carries the
|
||||
// text it removed (the body has only a point marker), an insertion only who and when.
|
||||
function odfRevisions(body: Element): Map<string, { kind: 'insertion' | 'deletion'; author: string; date: string; text: string }> {
|
||||
const out = new Map<string, { kind: 'insertion' | 'deletion'; author: string; date: string; text: string }>();
|
||||
const registry = body.getElementsByTagNameNS(NS.text, 'tracked-changes')[0];
|
||||
for (const region of Array.from(registry?.children ?? [])) {
|
||||
const id = region.getAttributeNS(NS.text, 'id') ?? region.getAttribute('xml:id');
|
||||
const kindEl = Array.from(region.children).find((c) => c.localName === 'insertion' || c.localName === 'deletion');
|
||||
if (!id || !kindEl) continue;
|
||||
const info = kindEl.getElementsByTagNameNS(NS.office, 'change-info')[0];
|
||||
const paras = Array.from(kindEl.children).filter((c) => c.namespaceURI === NS.text && c.localName === 'p');
|
||||
out.set(id, {
|
||||
kind: kindEl.localName === 'deletion' ? 'deletion' : 'insertion',
|
||||
author: info?.getElementsByTagNameNS(NS.dc, 'creator')[0]?.textContent?.trim() ?? '',
|
||||
date: info?.getElementsByTagNameNS(NS.dc, 'date')[0]?.textContent?.trim() ?? '',
|
||||
text: paras.map((n) => n.textContent ?? '').join('\n'),
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// <text:sequence> → a caption's running number. The counter's name is the category
|
||||
// (LibreOffice's "Illustration"/"Table"); the element text is the file's cached value,
|
||||
// which the editor's own numbering overwrites on the first edit.
|
||||
@@ -1710,6 +1735,8 @@ function convertInline(root: Element, ctx: Ctx, baseProps: PropMap, defaults: Bl
|
||||
if (bookmark) marks.push({ type: 'bookmark', attrs: { name: bookmark } });
|
||||
const comment = hfFields ? undefined : ctx.openComments.values().next().value;
|
||||
if (comment) marks.push({ type: 'comment', attrs: comment });
|
||||
const insertion = hfFields ? undefined : ctx.openInsertions.values().next().value;
|
||||
if (insertion) marks.push({ type: 'insertion', attrs: insertion });
|
||||
const node: Node = { type: 'text', text: clean };
|
||||
if (marks.length) node.marks = marks;
|
||||
out.push(node);
|
||||
@@ -1792,12 +1819,34 @@ function convertInline(root: Element, ctx: Ctx, baseProps: PropMap, defaults: Bl
|
||||
if (e.textContent) pushText(e.textContent, props, linkHref);
|
||||
continue;
|
||||
}
|
||||
// A point bookmark has no range to mark; a soft page break is the
|
||||
// producer's own pagination, which the editor recomputes.
|
||||
case 'bookmark':
|
||||
case 'soft-page-break':
|
||||
case 'change':
|
||||
case 'change-start':
|
||||
case 'change-end':
|
||||
continue;
|
||||
case 'change-start':
|
||||
case 'change-end': {
|
||||
// A recorded insertion brackets its runs; the registry holds who and when.
|
||||
const id = e.getAttributeNS(NS.text, 'change-id');
|
||||
const rev = id ? ctx.revisions.get(id) : undefined;
|
||||
if (!hfFields && id && rev?.kind === 'insertion') {
|
||||
if (e.localName === 'change-start') ctx.openInsertions.set(id, { id, author: rev.author, date: rev.date });
|
||||
else ctx.openInsertions.delete(id);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
case 'change': {
|
||||
// A deletion is a point marker: its text lives only in the registry, and the
|
||||
// editor keeps it in the document marked as removed.
|
||||
const id = e.getAttributeNS(NS.text, 'change-id');
|
||||
const rev = id ? ctx.revisions.get(id) : undefined;
|
||||
if (!hfFields && id && rev?.kind === 'deletion' && rev.text) {
|
||||
const marks = marksFor(props, ctx.resolver, defaults);
|
||||
marks.push({ type: 'deletion', attrs: { id, author: rev.author, date: rev.date } });
|
||||
out.push({ type: 'text', text: rev.text, marks });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
default:
|
||||
// In headers/footers, page fields stay live fields (pageField.ts). The atom
|
||||
// carries the run's marks so its digits render in the run's font/size.
|
||||
|
||||
Reference in new issue
Block a user