docxkeun 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Building blocks for DocxEditor's table and copy operations: a new table's XML, cleaning a copied
3
+ * block so it can live next to its original, and marking new content as a tracked insertion.
4
+ */
5
+ import { type XmlElement } from '../xml/index.js';
6
+ import { type RevisionAuthor } from './revisions.js';
7
+ /** Options for a new table. */
8
+ export interface TableOptions {
9
+ /** The first row is a header: bold, and repeated at the top of each page the table runs onto. */
10
+ header?: boolean;
11
+ /** Relative column widths (e.g. [1, 3, 1]); default: equal. The table spans the text width. */
12
+ widths?: number[];
13
+ /** Grid borders (default true). Ignored when `style` is given. */
14
+ borders?: boolean;
15
+ /** The table's position between the margins (default left). */
16
+ align?: 'left' | 'center' | 'right';
17
+ /** A table style id; default: Word's "Table Grid" when the document has it. */
18
+ style?: string;
19
+ }
20
+ /**
21
+ * A new table (w:tbl). `widths` are the columns' widths in twips; `paragraph` makes a cell's
22
+ * paragraph for one line of text (lines come from "\n" in the cell text).
23
+ */
24
+ export declare function buildTable(rows: string[][], widths: number[], o: {
25
+ header: boolean;
26
+ style: string | undefined;
27
+ borders: boolean;
28
+ align: TableOptions['align'];
29
+ paragraph: (text: string, header: boolean) => XmlElement;
30
+ }): XmlElement;
31
+ /** Column widths in twips: `weights` (default equal) scaled to fill `total`. */
32
+ export declare function columnWidths(columns: number, total: number, weights?: number[]): number[];
33
+ /** What a copy left out because it can only exist once in a document. */
34
+ export interface CopyDropped {
35
+ /** Comment anchors (the comments stay on the original). */
36
+ comments: number;
37
+ /** Footnote and endnote references. */
38
+ notes: number;
39
+ /** Bookmarks (their names must be unique; links keep pointing at the original). */
40
+ bookmarks: number;
41
+ }
42
+ /** Fresh ids for the copy's elements that must be unique in the document. */
43
+ export interface CopyIds {
44
+ revision: () => string;
45
+ drawing: () => string;
46
+ sdt: () => string;
47
+ }
48
+ /**
49
+ * Make a cloned block fit to sit next to its original: drop what may exist only once (bookmarks,
50
+ * comment anchors, note references, editable-range markers, a paragraph's section break), give
51
+ * drawings, content controls and tracked changes new ids, and drop Word's paragraph ids.
52
+ */
53
+ export declare function cleanCopy(node: XmlElement, ids: CopyIds, dropped: CopyDropped): void;
54
+ /** Mark a table row inserted or deleted (w:trPr/w:ins or w:del). */
55
+ export declare function markRow(row: XmlElement, kind: 'ins' | 'del', id: string, who: RevisionAuthor): void;
56
+ /**
57
+ * Mark new content (a paragraph, a table, a row) as a tracked insertion by `who`: its runs, its
58
+ * paragraph marks and its table rows. `markParagraph` marks one paragraph mark.
59
+ */
60
+ export declare function markInserted(node: XmlElement, nextId: () => string, who: RevisionAuthor, markParagraph: (p: XmlElement) => void): void;
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Building blocks for DocxEditor's table and copy operations: a new table's XML, cleaning a copied
3
+ * block so it can live next to its original, and marking new content as a tracked insertion.
4
+ */
5
+ import { el, findAll, firstChild, insertOrdered, removeChildren } from '../xml/index.js';
6
+ import { insertedRuns } from './revisions.js';
7
+ const BORDER_SIDES = ['w:top', 'w:left', 'w:bottom', 'w:right', 'w:insideH', 'w:insideV'];
8
+ const TRPR_ORDER = ['w:cnfStyle', 'w:divId', 'w:gridBefore', 'w:gridAfter', 'w:wBefore', 'w:wAfter', 'w:cantSplit', 'w:trHeight', 'w:tblHeader', 'w:tblCellSpacing', 'w:jc', 'w:hidden', 'w:ins', 'w:del', 'w:trPrChange'];
9
+ /**
10
+ * A new table (w:tbl). `widths` are the columns' widths in twips; `paragraph` makes a cell's
11
+ * paragraph for one line of text (lines come from "\n" in the cell text).
12
+ */
13
+ export function buildTable(rows, widths, o) {
14
+ const total = widths.reduce((a, b) => a + b, 0);
15
+ const tblPr = el('w:tblPr', {}, [
16
+ ...(o.style ? [el('w:tblStyle', { 'w:val': o.style })] : []),
17
+ el('w:tblW', { 'w:w': String(total), 'w:type': 'dxa' }),
18
+ ...(o.align && o.align !== 'left' ? [el('w:jc', { 'w:val': o.align })] : []),
19
+ ...(o.borders && !o.style ? [el('w:tblBorders', {}, BORDER_SIDES.map((n) => el(n, { 'w:val': 'single', 'w:sz': '4', 'w:space': '0', 'w:color': 'auto' })))] : []),
20
+ el('w:tblLayout', { 'w:type': 'fixed' }),
21
+ el('w:tblLook', { 'w:val': '04A0', 'w:firstRow': o.header ? '1' : '0', 'w:lastRow': '0', 'w:firstColumn': '1', 'w:lastColumn': '0', 'w:noHBand': '0', 'w:noVBand': '1' }),
22
+ ]);
23
+ const grid = el('w:tblGrid', {}, widths.map((w) => el('w:gridCol', { 'w:w': String(w) })));
24
+ const trs = rows.map((cells, r) => {
25
+ const isHeader = o.header && r === 0;
26
+ return el('w:tr', {}, [
27
+ ...(isHeader ? [el('w:trPr', {}, [el('w:tblHeader')])] : []),
28
+ ...widths.map((w, c) => el('w:tc', {}, [el('w:tcPr', {}, [el('w:tcW', { 'w:w': String(w), 'w:type': 'dxa' })]), ...(cells[c] ?? '').split('\n').map((line) => o.paragraph(line, isHeader))])),
29
+ ]);
30
+ });
31
+ return el('w:tbl', {}, [tblPr, grid, ...trs]);
32
+ }
33
+ /** Column widths in twips: `weights` (default equal) scaled to fill `total`. */
34
+ export function columnWidths(columns, total, weights) {
35
+ const w = weights ?? Array.from({ length: columns }, () => 1);
36
+ const sum = w.reduce((a, b) => a + b, 0);
37
+ const out = w.map((x) => Math.max(1, Math.floor((x / sum) * total)));
38
+ // Rounding: the last column takes what is left so the table is exactly `total` wide.
39
+ out[out.length - 1] += total - out.reduce((a, b) => a + b, 0);
40
+ return out;
41
+ }
42
+ const DROP_MARKERS = new Set(['w:bookmarkStart', 'w:bookmarkEnd', 'w:commentRangeStart', 'w:commentRangeEnd', 'w:permStart', 'w:permEnd']);
43
+ const REVISIONS_WITH_ID = new Set([
44
+ 'w:ins', 'w:del', 'w:moveFrom', 'w:moveTo', 'w:moveFromRangeStart', 'w:moveFromRangeEnd', 'w:moveToRangeStart', 'w:moveToRangeEnd',
45
+ 'w:rPrChange', 'w:pPrChange', 'w:sectPrChange', 'w:tblPrChange', 'w:tblGridChange', 'w:trPrChange', 'w:tcPrChange', 'w:tblPrExChange',
46
+ 'w:numberingChange', 'w:cellIns', 'w:cellDel', 'w:cellMerge',
47
+ ]);
48
+ /**
49
+ * Make a cloned block fit to sit next to its original: drop what may exist only once (bookmarks,
50
+ * comment anchors, note references, editable-range markers, a paragraph's section break), give
51
+ * drawings, content controls and tracked changes new ids, and drop Word's paragraph ids.
52
+ */
53
+ export function cleanCopy(node, ids, dropped) {
54
+ delete node.attrs['w14:paraId'];
55
+ delete node.attrs['w14:textId'];
56
+ if (node.name === 'wp:docPr')
57
+ node.attrs.id = ids.drawing();
58
+ if (REVISIONS_WITH_ID.has(node.name) && node.attrs['w:id'] !== undefined)
59
+ node.attrs['w:id'] = ids.revision();
60
+ if (node.name === 'w:sdtPr') {
61
+ const id = firstChild(node, 'w:id');
62
+ if (id)
63
+ id.attrs['w:val'] = ids.sdt();
64
+ }
65
+ const kept = [];
66
+ for (const c of node.children) {
67
+ if (c.type === 'element') {
68
+ if (DROP_MARKERS.has(c.name)) {
69
+ if (c.name === 'w:bookmarkStart')
70
+ dropped.bookmarks++;
71
+ continue;
72
+ }
73
+ if (node.name === 'w:pPr' && c.name === 'w:sectPr')
74
+ continue;
75
+ if (c.name === 'w:r') {
76
+ if (firstChild(c, 'w:commentReference')) {
77
+ dropped.comments++;
78
+ continue;
79
+ }
80
+ if (firstChild(c, 'w:footnoteReference') || firstChild(c, 'w:endnoteReference')) {
81
+ dropped.notes++;
82
+ continue;
83
+ }
84
+ }
85
+ cleanCopy(c, ids, dropped);
86
+ }
87
+ kept.push(c);
88
+ }
89
+ node.children = kept;
90
+ }
91
+ // ---- tracked insertion ----------------------------------------------------------------------------
92
+ const RUN_CONTAINERS = new Set(['w:hyperlink', 'w:smartTag', 'w:fldSimple', 'w:sdt', 'w:sdtContent', 'w:customXml']);
93
+ /** Wrap the runs of a paragraph (and of its links and fields) in w:ins, a group at a time. */
94
+ function wrapRuns(container, nextId, who) {
95
+ const out = [];
96
+ let group = [];
97
+ const flush = () => {
98
+ if (group.length)
99
+ out.push(insertedRuns(group, nextId(), who));
100
+ group = [];
101
+ };
102
+ for (const c of container.children) {
103
+ if (c.type === 'element' && c.name === 'w:r') {
104
+ group.push(c);
105
+ continue;
106
+ }
107
+ flush();
108
+ if (c.type === 'element' && RUN_CONTAINERS.has(c.name))
109
+ wrapRuns(c, nextId, who);
110
+ out.push(c);
111
+ }
112
+ flush();
113
+ container.children = out;
114
+ }
115
+ /** Mark a table row inserted or deleted (w:trPr/w:ins or w:del). */
116
+ export function markRow(row, kind, id, who) {
117
+ let trPr = firstChild(row, 'w:trPr');
118
+ if (!trPr) {
119
+ trPr = el('w:trPr');
120
+ const exceptions = firstChild(row, 'w:tblPrEx');
121
+ row.children.splice(exceptions ? row.children.indexOf(exceptions) + 1 : 0, 0, trPr);
122
+ }
123
+ removeChildren(trPr, 'w:ins');
124
+ removeChildren(trPr, 'w:del');
125
+ insertOrdered(trPr, el(kind === 'ins' ? 'w:ins' : 'w:del', { 'w:id': id, 'w:author': who.author, 'w:date': who.date }), TRPR_ORDER);
126
+ }
127
+ /**
128
+ * Mark new content (a paragraph, a table, a row) as a tracked insertion by `who`: its runs, its
129
+ * paragraph marks and its table rows. `markParagraph` marks one paragraph mark.
130
+ */
131
+ export function markInserted(node, nextId, who, markParagraph) {
132
+ for (const p of node.name === 'w:p' ? [node] : findAll(node, 'w:p')) {
133
+ wrapRuns(p, nextId, who);
134
+ markParagraph(p);
135
+ }
136
+ for (const row of node.name === 'w:tr' ? [node] : findAll(node, 'w:tr'))
137
+ markRow(row, 'ins', nextId(), who);
138
+ }
@@ -10,6 +10,8 @@ export interface ParagraphChange {
10
10
  cuts: number[];
11
11
  /** The paragraph's text before (changed paragraphs). */
12
12
  before?: string;
13
+ /** Index (0-based) of the paragraph in the old text (changed paragraphs). */
14
+ from?: number;
13
15
  /** Only the formatting changed (bold, style, alignment, list). */
14
16
  formatting?: boolean;
15
17
  }
@@ -35,9 +37,23 @@ export declare function diffText(before: string, after: string): {
35
37
  };
36
38
  /** Paragraph-level changes between two versions of a document's paragraph texts. */
37
39
  export declare function diffParagraphs(before: string[], after: string[]): ParagraphDiff;
40
+ /** How the AI's work shows on the paragraphs it is changing (see `ACTIVITY_STYLES`). */
41
+ export type ActivityStyle = 'highlight' | 'words' | 'underline' | 'edge' | 'spotlight' | 'quiet' | 'sparkles';
42
+ /** The styles, with a label and a line to show in a picker, in the order to show them. */
43
+ export declare const ACTIVITY_STYLES: ReadonlyArray<{
44
+ style: ActivityStyle;
45
+ label: string;
46
+ description: string;
47
+ }>;
38
48
  export interface ActivityOptions {
39
49
  /** The name on the caret and in the change marks' tooltips (default "Claude"). */
40
50
  name?: string;
51
+ /**
52
+ * How the AI's work shows (default `'highlight'`), and so which mark stays after a change:
53
+ * highlight → a still tint on the changed paragraph, words → the changed words glow,
54
+ * underline → the changed words stay underlined, the others → the ✦ margin bar.
55
+ */
56
+ style?: ActivityStyle;
41
57
  /**
42
58
  * Colour of the caret, shimmer, glow and change marks: one CSS colour, or two for a gradient
43
59
  * (default `['#7c5cff', '#d97757']`). The caret's label text turns dark on light colours.
@@ -47,7 +63,7 @@ export interface ActivityOptions {
47
63
  enabled?: boolean;
48
64
  /** Motion: shimmer sweep, ink, ghosts, scan line, the gliding caret (default true). Off: static highlights. */
49
65
  animate?: boolean;
50
- /** Change bars and the change map that stay after a change (default true). */
66
+ /** The marks that stay after a change, and the change map on the right edge (default true). */
51
67
  marks?: boolean;
52
68
  }
53
69
  /** The AI's presence on the pages: see the module comment. Get it from `viewer.activity`. */
@@ -55,6 +71,7 @@ export declare class AiActivity {
55
71
  private readonly viewer;
56
72
  private aiName;
57
73
  private aiColor;
74
+ private aiStyle;
58
75
  private readonly layer;
59
76
  private readonly map;
60
77
  private readonly caret;
@@ -79,6 +96,12 @@ export declare class AiActivity {
79
96
  */
80
97
  get color(): string | [string, string] | undefined;
81
98
  set color(value: string | [string, string] | undefined);
99
+ /**
100
+ * How the AI's work shows, and so which mark stays after a change (see `ActivityOptions.style`).
101
+ * Changing it redraws the work in progress and the marks. Throws a TypeError for an unknown style.
102
+ */
103
+ get style(): ActivityStyle;
104
+ set style(style: ActivityStyle);
82
105
  /** Show the AI's activity at all. Turning it off clears everything on screen. */
83
106
  get enabled(): boolean;
84
107
  set enabled(on: boolean);
@@ -113,6 +136,8 @@ export declare class AiActivity {
113
136
  /** The AI finished (without a change to show). */
114
137
  idle(): void;
115
138
  private drawWorking;
139
+ /** The working effect of the current style over each paragraph's lines. */
140
+ private drawStyle;
116
141
  private workingText;
117
142
  private moveCaret;
118
143
  /** The caret near the top of the view (reading the whole document). */
@@ -128,11 +153,27 @@ export declare class AiActivity {
128
153
  reveal(before: ParagraphSnapshot | string[], options?: {
129
154
  scroll?: boolean;
130
155
  }): Promise<ParagraphDiff>;
131
- /** Remove the change bars and the change map. */
156
+ /**
157
+ * Remove every mark and the change map. Marks build up over a run of changes (each follows its
158
+ * paragraph as others are added or removed) until this is called, e.g. when a new request starts.
159
+ */
132
160
  clearMarks(): void;
161
+ /**
162
+ * The marks after a change: earlier marks follow their paragraphs (and go with deleted ones), a
163
+ * paragraph changed again is marked against its text before the first change, and new ones join.
164
+ */
165
+ private mergeMarks;
166
+ /** What a mark's tooltip says. */
167
+ private markTitle;
168
+ /** Draw the marks; those of the paragraphs in `fresh` (just changed) appear with a short animation. */
133
169
  private drawMarks;
134
170
  /** Ticks on the right edge for every change, scaled to the scroll height; click to jump. */
135
171
  private drawMap;
172
+ /**
173
+ * The viewer rebuilt its page rows (zoom, the comment column's width): redraw at the new positions.
174
+ * @internal
175
+ */
176
+ rowsBuilt(): void;
136
177
  private redraw;
137
178
  destroy(): void;
138
179
  }