docxkeun 0.2.0 → 0.4.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.
@@ -1,7 +1,10 @@
1
- export { DocxEditor, OBJECT_CHAR, paragraphText, paragraphsOf, applyFormat, runContent, lockModeOf, LockedRegionError, LOCK_TAG, type EditAction, type EditGuard, type TextFormat, type CommentInput, type Match, type LockMode, type LockedRegion, type SetListOptions, type EditorOptions, type ParagraphFormat, documentTracksChanges } from './editor.js';
1
+ export { DocxEditor, OBJECT_CHAR, paragraphText, paragraphsOf, applyFormat, runContent, lockModeOf, LockedRegionError, LOCK_TAG, type EditAction, type EditGuard, type TextFormat, type TextPiece, type CommentInput, type Match, type LockMode, type LockedRegion, type SetListOptions, type EditorOptions, type ParagraphFormat, documentTracksChanges } from './editor.js';
2
2
  export { listRevisions, acceptRevisions, rejectRevisions, type Revision, type RevisionKind, type RevisionFilter } from './revisions.js';
3
3
  export { LIST_FORMATS, type ListFormat, type ListItem } from './lists.js';
4
4
  export { EditError, TreeIndex, type DocPosition, type DocRange } from './positions.js';
5
5
  export { ensurePart, randomHexId } from './parts.js';
6
+ export { ChartDataError, type NewChart, type NewChartType, type LegendPosition, type ChartChanges, type ChartTable, type ChartSeriesData } from '../charts/write.js';
7
+ export type { DocObject, ChartInfo, ChartUpdate, ShapeKind, ObjectPlacement, NewShape } from './objects.js';
6
8
  export { selectionInfoFor, describeSelection, type SelectionInfo, type SelectedParagraph, type TextSpot } from './selection.js';
7
9
  export type { HeaderFooterInfo, HeaderFooterKind, HeaderFooterOptions, HeaderFooterType } from './headers.js';
10
+ export type { TableOptions, CopyDropped } from './tables.js';
package/dist/api/index.js CHANGED
@@ -3,4 +3,5 @@ export { listRevisions, acceptRevisions, rejectRevisions } from './revisions.js'
3
3
  export { LIST_FORMATS } from './lists.js';
4
4
  export { EditError, TreeIndex } from './positions.js';
5
5
  export { ensurePart, randomHexId } from './parts.js';
6
+ export { ChartDataError } from '../charts/write.js';
6
7
  export { selectionInfoFor, describeSelection } from './selection.js';
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Objects in a story (pictures, charts, SmartArt, shapes, text boxes, groups): listing them in
3
+ * document order, the drawing XML for new ones, resizing and placing existing ones, and the parts
4
+ * behind a chart (its workbook, and copies of a chart part for copied content).
5
+ */
6
+ import type { ChartTable, NewChart, NewChartType } from '../charts/write.js';
7
+ import type { DocParagraph } from '../model/content.js';
8
+ import type { DocxDocument } from '../model/document.js';
9
+ import { type OpcPackage } from '../opc/index.js';
10
+ import { type XmlElement } from '../xml/index.js';
11
+ import type { TextFormat } from './editor.js';
12
+ import { TreeIndex } from './positions.js';
13
+ export interface DocObject {
14
+ /** Object number, from 1, in the order of `DocxEditor.paragraphs()` and then along each paragraph. */
15
+ n: number;
16
+ kind: 'picture' | 'chart' | 'diagram' | 'shape' | 'textBox' | 'group';
17
+ /** The paragraph holding the object. */
18
+ paragraph: DocParagraph;
19
+ /** The w:r holding the object. */
20
+ run: XmlElement;
21
+ /** The w:drawing (inside mc:Choice when wrapped), the w:pict of an old VML object, or the w:object of an embedded OLE object. */
22
+ drawing: XmlElement;
23
+ /** An old VML object or an embedded OLE object (w:object), drawn in VML: these can only be deleted. */
24
+ vml: boolean;
25
+ /** Size in points. */
26
+ width: number;
27
+ height: number;
28
+ floating: boolean;
29
+ alt?: string;
30
+ /** The chart part, for a chart. */
31
+ chartPart?: string;
32
+ }
33
+ export type ShapeKind = 'textBox' | 'rect' | 'roundRect' | 'ellipse' | 'triangle' | 'diamond' | 'rightArrow' | 'leftArrow' | 'upArrow' | 'downArrow' | 'chevron' | 'pentagon' | 'star5' | 'wedgeRectCallout' | 'wedgeRoundRectCallout' | 'cloudCallout' | 'line';
34
+ /**
35
+ * Inline in the text, or floating at (x, y) points from the paragraph (default), margin or page,
36
+ * with text wrapped square around it (default), above and below it, or not at all (in front of or
37
+ * behind the text). An object that already floats keeps what is left out (its offsets, reference
38
+ * and wrap); a new floating object, or one that was inline, needs x and y.
39
+ */
40
+ export type ObjectPlacement = {
41
+ inline: true;
42
+ } | {
43
+ x?: number;
44
+ y?: number;
45
+ from?: 'margin' | 'page' | 'paragraph';
46
+ wrap?: 'square' | 'topAndBottom' | 'inFront' | 'behind';
47
+ };
48
+ export type FloatingPlacement = Exclude<ObjectPlacement, {
49
+ inline: true;
50
+ }>;
51
+ /** A new shape or text box (sizes in points; colours RRGGBB or 'none'). */
52
+ export interface NewShape {
53
+ shape: ShapeKind;
54
+ /** Text inside: each line becomes a paragraph. */
55
+ text?: string;
56
+ /** Default: text box 180 × 72, line 144 × 0, other shapes 108 × 72. */
57
+ width?: number;
58
+ height?: number;
59
+ /** Default: white for a text box, the theme's accent 1 for a shape. */
60
+ fill?: string;
61
+ /** Default: thin black for a text box, a darker accent 1 for a shape. */
62
+ outline?: string;
63
+ /** Default: floating at the top left of the paragraph, square wrap. */
64
+ place?: ObjectPlacement;
65
+ alt?: string;
66
+ }
67
+ /** A chart's data, and where it comes from: an embedded workbook, a linked outside file, or neither. */
68
+ export interface ChartInfo extends ChartTable {
69
+ source: 'embedded' | 'linked' | 'none';
70
+ }
71
+ export interface ChartUpdate {
72
+ source: 'embedded' | 'linked' | 'none';
73
+ /** The embedded workbook was rewritten with the chart's new data. */
74
+ workbookRewritten: boolean;
75
+ /**
76
+ * The old workbook held more than the chart's table (other sheets or cells), which is gone now;
77
+ * also true when a workbook that can't be read (an old .xls object, broken bytes) was replaced.
78
+ */
79
+ workbookHadMore: boolean;
80
+ }
81
+ /** The objects of some paragraphs, numbered from 1 (a group counts once, not per shape in it). */
82
+ export declare function listObjects(paragraphs: DocParagraph[]): DocObject[];
83
+ /** A w:drawing with an inline frame of `widthPt` × `heightPt` points around `graphicData`. */
84
+ export declare function inlineFrame(id: number, name: string, widthPt: number, heightPt: number, graphicData: XmlElement, alt?: string): XmlElement;
85
+ /**
86
+ * Check a placement's position, reference and wrap style (EditError when wrong). x and y may be left
87
+ * out only for an object that already floats (`floating`), which keeps its own.
88
+ */
89
+ export declare function checkPlacement(place: ObjectPlacement, floating?: boolean): void;
90
+ /** The z-order for a new floating object: above every other one in `roots` (Word starts at 251659264). */
91
+ export declare function nextRelativeHeight(roots: XmlElement[]): number;
92
+ /**
93
+ * A w:drawing with a floating frame (wp:anchor) of `widthPt` × `heightPt` points around
94
+ * `graphicData`, at `place` (from the paragraph by default, square wrap by default) and at z-order
95
+ * `relativeHeight`.
96
+ */
97
+ export declare function anchorFrame(id: number, name: string, widthPt: number, heightPt: number, place: FloatingPlacement, graphicData: XmlElement, alt: string | undefined, relativeHeight: number): XmlElement;
98
+ /** The graphic of a chart whose part the relationship `relId` points at. */
99
+ export declare function chartGraphic(relId: string): XmlElement;
100
+ /** The frame (wp:inline or wp:anchor) of an object's w:drawing. */
101
+ export declare function frameOf(drawing: XmlElement): XmlElement;
102
+ /**
103
+ * Word's old VML copy of an object (the mc:Fallback beside its mc:Choice) is stale once the
104
+ * object changes, so it goes. `node` is the run's child holding the object.
105
+ */
106
+ export declare function dropFallback(node: XmlElement): void;
107
+ /** An object's new size in points: as given, or with one side given, the other keeps the frame's proportions. */
108
+ export declare function resized(frame: XmlElement, size: {
109
+ width?: number | undefined;
110
+ height?: number | undefined;
111
+ }): {
112
+ width: number;
113
+ height: number;
114
+ };
115
+ /** Size a frame to `widthPt` × `heightPt`, with the picture, shape or group in it (a group's children scale with it). */
116
+ export declare function resizeFrame(frame: XmlElement, widthPt: number, heightPt: number): void;
117
+ /**
118
+ * Place an object's w:drawing inline, or floating at `place`: an inline frame becomes a wp:anchor
119
+ * (at z-order `relativeHeight()`, from the paragraph and square wrap by default) around the same
120
+ * docPr, extent and graphic, and back; a floating one moves (see `moveAnchor`).
121
+ */
122
+ export declare function placeFrame(drawing: XmlElement, place: ObjectPlacement, relativeHeight: () => number): void;
123
+ /**
124
+ * A copy of an object: of `node`, the run's child holding it (a w:drawing, or the
125
+ * mc:AlternateContent around one), without Word's old VML copy; and the w:drawing in the copy that
126
+ * stands where `drawing` stands in the original.
127
+ */
128
+ export declare function cloneObject(idx: TreeIndex, node: XmlElement, drawing: XmlElement): {
129
+ copy: XmlElement;
130
+ drawing: XmlElement;
131
+ };
132
+ /** Take an object's run child out of its run; a run (and a tracked change around it) left empty goes too. */
133
+ export declare function takeOut(idx: TreeIndex, node: XmlElement): void;
134
+ /** Word's names for new shapes (the wp:docPr name, before the drawing id). */
135
+ export declare const SHAPE_NAMES: Record<ShapeKind, string>;
136
+ /** Whether an object's w:drawing holds a picture (pic:pic), not a chart, diagram, canvas or a graphic docxkeun doesn't know. */
137
+ export declare function isPicture(drawing: XmlElement): boolean;
138
+ /** Whether a node sits inside a text box (DrawingML or VML: both keep their text in w:txbxContent). */
139
+ export declare function inTextBox(idx: TreeIndex, node: XmlElement): boolean;
140
+ /**
141
+ * Check a new shape's options (EditError when wrong) and fill in its size and placement: a text box
142
+ * is 180 × 72 pt, a line 144 × 0 pt, other shapes 108 × 72 pt; by default it floats at the top left
143
+ * of its paragraph.
144
+ */
145
+ export declare function checkShape(shape: NewShape): {
146
+ width: number;
147
+ height: number;
148
+ place: ObjectPlacement;
149
+ };
150
+ /**
151
+ * The graphic of a new shape (wps:wsp) of `widthPt` × `heightPt` points, holding `shape.text` (each
152
+ * line a paragraph, each run made by `newRun`). A text box is white with a thin black outline and
153
+ * top-aligned text; other shapes take the theme's accent 1 with a darker outline (wps:style) and
154
+ * white, centred text; `fill` and `outline` override either.
155
+ */
156
+ export declare function shapeGraphic(shape: NewShape, widthPt: number, heightPt: number, newRun: (text: string, format?: TextFormat) => XmlElement): XmlElement;
157
+ /** The w:sectPr of the section holding `node`: the next paragraph section break, or the body's own. */
158
+ export declare function sectionAt(body: XmlElement, idx: TreeIndex, node: XmlElement): XmlElement | undefined;
159
+ /** A new chart's size in points: as given (one side keeps Word's 432 × 252 proportions), else Word's default shrunk to `maxWidth`. */
160
+ export declare function chartSize(size: {
161
+ width?: number | undefined;
162
+ height?: number | undefined;
163
+ }, maxWidth: number): {
164
+ width: number;
165
+ height: number;
166
+ };
167
+ /** `{base}{n}{ext}` with the lowest n from 1 that no part has. */
168
+ export declare function freePartName(pkg: OpcPackage, base: string, ext: string): string;
169
+ /** Store a new embedded workbook and relate it to a chart part; returns the relationship id. */
170
+ export declare function addWorkbook(pkg: OpcPackage, chartPart: string, bytes: Uint8Array): Promise<string>;
171
+ /** The chart part and its c:chartSpace behind a chart object. */
172
+ export declare function chartOf(doc: DocxDocument, obj: DocObject): {
173
+ part: string;
174
+ chartSpace: XmlElement;
175
+ };
176
+ /** The new-chart type of a chart's first plot (undefined for kinds new charts can't have), and its stacking. */
177
+ export declare function chartKind(chartSpace: XmlElement): {
178
+ type: NewChartType | undefined;
179
+ stacked: NewChart['stacked'];
180
+ };
181
+ /**
182
+ * Where a chart's data lives (its c:externalData relationship): an outside file ('linked'), a part
183
+ * in the package ('embedded'; `workbook` is set when that part is an .xlsx package that can be
184
+ * rewritten, not an old OLE object), or nowhere.
185
+ */
186
+ export declare function chartData(doc: DocxDocument, chartPart: string, chartSpace: XmlElement): {
187
+ source: ChartInfo['source'];
188
+ relId: string | undefined;
189
+ workbook: string | undefined;
190
+ };
191
+ /**
192
+ * Give a chart a new embedded .xlsx workbook in place of the data source it had (an old OLE object,
193
+ * a missing part, or none), pointing its c:externalData at it.
194
+ */
195
+ export declare function replaceWorkbook(doc: DocxDocument, chartPart: string, chartSpace: XmlElement, oldRelId: string | undefined, bytes: Uint8Array): Promise<void>;
196
+ /**
197
+ * Copy a part and the parts its relationships point to inside the package (a chart's workbook,
198
+ * styles and colours), so the copy shares nothing it could change; pictures are shared. References
199
+ * to relationships (r:id, r:embed, …) in the copied XML are renumbered to the copy's. Returns the
200
+ * copy's name.
201
+ */
202
+ export declare function copyPart(pkg: OpcPackage, name: string, copies?: Map<string, string>): Promise<string>;