documents.js 1.102.2 → 2.1.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.
- package/README.md +42 -33
- package/dist/codecs/registry.cjs +7 -0
- package/dist/codecs/registry.js +7 -0
- package/dist/convert/capability.cjs +5 -0
- package/dist/convert/capability.js +5 -0
- package/dist/convert/codec.cjs +20 -0
- package/dist/convert/codec.d.cts +5 -1
- package/dist/convert/codec.d.ts +5 -1
- package/dist/convert/codec.js +19 -3
- package/dist/convert/composition.cjs +47 -11
- package/dist/convert/composition.d.cts +9 -5
- package/dist/convert/composition.d.ts +9 -5
- package/dist/convert/composition.js +47 -11
- package/dist/convert/convert.cjs +38 -3
- package/dist/convert/convert.d.cts +18 -1
- package/dist/convert/convert.d.ts +18 -1
- package/dist/convert/convert.js +31 -4
- package/dist/convert/from-package.cjs +179 -2
- package/dist/convert/from-package.d.cts +3 -2
- package/dist/convert/from-package.d.ts +3 -2
- package/dist/convert/from-package.js +179 -3
- package/dist/convert/local.cjs +2 -0
- package/dist/convert/local.js +2 -0
- package/dist/convert/port.cjs +1 -0
- package/dist/convert/port.d.cts +3 -0
- package/dist/convert/port.d.ts +3 -0
- package/dist/convert/port.js +1 -0
- package/dist/csv/read.cjs +85 -0
- package/dist/csv/read.d.cts +10 -0
- package/dist/csv/read.d.ts +10 -0
- package/dist/csv/read.js +84 -0
- package/dist/csv/records.cjs +85 -0
- package/dist/csv/records.d.cts +10 -0
- package/dist/csv/records.d.ts +10 -0
- package/dist/csv/records.js +80 -0
- package/dist/csv/text.cjs +22 -0
- package/dist/csv/text.d.cts +8 -0
- package/dist/csv/text.d.ts +8 -0
- package/dist/csv/text.js +19 -0
- package/dist/csv/write.cjs +75 -0
- package/dist/csv/write.d.cts +22 -0
- package/dist/csv/write.d.ts +22 -0
- package/dist/csv/write.js +71 -0
- package/dist/index.cjs +28 -1
- package/dist/index.d.cts +11 -7
- package/dist/index.d.ts +11 -7
- package/dist/index.js +10 -6
- package/dist/layout/drawing.cjs +71 -9
- package/dist/layout/drawing.d.cts +8 -3
- package/dist/layout/drawing.d.ts +8 -3
- package/dist/layout/drawing.js +71 -10
- package/dist/layout/engine.cjs +55 -43
- package/dist/layout/engine.d.cts +2 -1
- package/dist/layout/engine.d.ts +2 -1
- package/dist/layout/engine.js +57 -45
- package/dist/layout/reconstruct.cjs +260 -105
- package/dist/layout/reconstruct.js +260 -105
- package/dist/layout/shared.cjs +47 -2
- package/dist/layout/shared.d.cts +15 -4
- package/dist/layout/shared.d.ts +15 -4
- package/dist/layout/shared.js +44 -4
- package/dist/layout/sheets.cjs +18 -13
- package/dist/layout/sheets.d.cts +4 -2
- package/dist/layout/sheets.d.ts +4 -2
- package/dist/layout/sheets.js +20 -16
- package/dist/layout/slides.cjs +35 -31
- package/dist/layout/slides.d.cts +3 -2
- package/dist/layout/slides.d.ts +3 -2
- package/dist/layout/slides.js +37 -33
- package/dist/layout/text-layout.cjs +2 -1
- package/dist/layout/text-layout.d.cts +14 -3
- package/dist/layout/text-layout.d.ts +14 -3
- package/dist/layout/text-layout.js +2 -1
- package/dist/metadata/write.cjs +1 -0
- package/dist/metadata/write.js +1 -0
- package/dist/model/bytes.cjs +2 -0
- package/dist/model/bytes.d.cts +2 -1
- package/dist/model/bytes.d.ts +2 -1
- package/dist/model/bytes.js +2 -1
- package/dist/odb/csv.cjs +3 -6
- package/dist/odb/csv.js +3 -6
- package/package.json +5 -5
|
@@ -1,16 +1,193 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_model_geometry = require("../model/geometry.cjs");
|
|
3
|
+
const require_layout_shared = require("../layout/shared.cjs");
|
|
4
|
+
require("../layout/sheets.cjs");
|
|
5
|
+
const require_layout_drawing = require("../layout/drawing.cjs");
|
|
2
6
|
const require_codecs_registry = require("../codecs/registry.cjs");
|
|
7
|
+
let document_schema_js = require("document-schema.js");
|
|
3
8
|
let pdf_codec = require("pdf-codec");
|
|
4
9
|
//#region src/convert/from-package.ts
|
|
5
10
|
function buildDocumentBytes(pkg, target) {
|
|
6
11
|
if (target === "pdf") {
|
|
7
|
-
if (pkg.
|
|
8
|
-
return (0, pdf_codec.writePdf)(pkg
|
|
12
|
+
if (pkg.pages === void 0) throw new Error("this DocumentPackage has no pages -- only a package dumped from a <format>-to-pdf or pdf-to-<format> conversion carries them; a bridge conversion's own dump (e.g. odt-to-docx) never does, so 'pdf' is not a reachable target from it");
|
|
13
|
+
return (0, pdf_codec.writePdf)(layoutDocumentFromPackage(pkg));
|
|
9
14
|
}
|
|
10
15
|
if (target === "odf") throw new Error("'odf' (a standalone formula document) cannot be built from a DocumentPackage -- there is no ContentDocument-to-odf builder");
|
|
11
16
|
const content = require_codecs_registry.DOCUMENT_FORMAT_CODECS[target].content;
|
|
12
17
|
if (!content?.write) throw new Error(`DocumentFormat '${target}' has no content.write codec in DOCUMENT_FORMAT_CODECS, and is not 'pdf'/'odf' -- this is an internal invariant violation, not a caller error`);
|
|
13
18
|
return require_codecs_registry.requireArrayBufferBytes(content.write(pkg.content));
|
|
14
19
|
}
|
|
20
|
+
function pageOfFrame(state, frame) {
|
|
21
|
+
return state.pages[frame.pageIndex];
|
|
22
|
+
}
|
|
23
|
+
function emitRun(state, run) {
|
|
24
|
+
const frame = run.frames?.[0];
|
|
25
|
+
if (frame === void 0) return;
|
|
26
|
+
const page = pageOfFrame(state, frame);
|
|
27
|
+
if (page === void 0) return;
|
|
28
|
+
const font = require_layout_shared.runFont(run);
|
|
29
|
+
const sizePt = run.sizePt ?? 18;
|
|
30
|
+
const textItem = {
|
|
31
|
+
kind: "text",
|
|
32
|
+
text: run.text,
|
|
33
|
+
xPt: frame.xPt,
|
|
34
|
+
yPt: frame.yPt,
|
|
35
|
+
font,
|
|
36
|
+
sizePt,
|
|
37
|
+
color: run.color ?? document_schema_js.COLOR_BLACK,
|
|
38
|
+
underline: run.underline
|
|
39
|
+
};
|
|
40
|
+
page.items.push(textItem);
|
|
41
|
+
if (run.hyperlink !== void 0) {
|
|
42
|
+
const link = {
|
|
43
|
+
kind: "link",
|
|
44
|
+
uri: run.hyperlink,
|
|
45
|
+
xPt: frame.xPt,
|
|
46
|
+
yPt: frame.yPt,
|
|
47
|
+
widthPt: frame.widthPt,
|
|
48
|
+
heightPt: frame.heightPt
|
|
49
|
+
};
|
|
50
|
+
page.items.push(link);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function emitParagraph(state, paragraph) {
|
|
54
|
+
for (const run of paragraph.runs) emitRun(state, run);
|
|
55
|
+
}
|
|
56
|
+
function emitImageBlock(state, block, frames) {
|
|
57
|
+
for (const frame of frames ?? []) {
|
|
58
|
+
const page = pageOfFrame(state, frame);
|
|
59
|
+
if (page === void 0) continue;
|
|
60
|
+
const image = {
|
|
61
|
+
kind: "image",
|
|
62
|
+
imageId: require_layout_shared.registerImage(block, state.images),
|
|
63
|
+
xPt: frame.xPt,
|
|
64
|
+
yPt: frame.yPt,
|
|
65
|
+
widthPt: frame.widthPt,
|
|
66
|
+
heightPt: frame.heightPt
|
|
67
|
+
};
|
|
68
|
+
page.items.push(image);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function emitTableCell(state, cell) {
|
|
72
|
+
for (const frame of cell.frames ?? []) {
|
|
73
|
+
const page = pageOfFrame(state, frame);
|
|
74
|
+
if (page === void 0) continue;
|
|
75
|
+
if (cell.background !== void 0) page.items.push({
|
|
76
|
+
kind: "rect",
|
|
77
|
+
xPt: frame.xPt,
|
|
78
|
+
yPt: frame.yPt,
|
|
79
|
+
widthPt: frame.widthPt,
|
|
80
|
+
heightPt: frame.heightPt,
|
|
81
|
+
fill: cell.background
|
|
82
|
+
});
|
|
83
|
+
if (cell.borders !== void 0) {
|
|
84
|
+
const frameYDown = require_model_geometry.flipY({
|
|
85
|
+
xPt: frame.xPt,
|
|
86
|
+
yPt: frame.yPt,
|
|
87
|
+
widthPt: frame.widthPt,
|
|
88
|
+
heightPt: frame.heightPt
|
|
89
|
+
}, page.heightPt);
|
|
90
|
+
require_layout_shared.pushCellBorderLines(cell.borders, frameYDown, page.heightPt, cell.sourcePath, page.items);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
for (const block of cell.blocks) if (block.kind === "paragraph") emitParagraph(state, block);
|
|
94
|
+
else if (block.kind === "image") emitImageBlock(state, block, block.frames);
|
|
95
|
+
else if (block.kind === "table") emitTable(state, block);
|
|
96
|
+
}
|
|
97
|
+
function emitTable(state, table) {
|
|
98
|
+
for (const row of table.rows) for (const cell of row.cells) emitTableCell(state, cell);
|
|
99
|
+
}
|
|
100
|
+
function emitVector(state, vector) {
|
|
101
|
+
for (const frame of vector.frames ?? []) {
|
|
102
|
+
const page = pageOfFrame(state, frame);
|
|
103
|
+
if (page === void 0) continue;
|
|
104
|
+
const items = page.items;
|
|
105
|
+
require_layout_drawing.convertVector(vector, page.heightPt, items);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function emitSheetCell(state, cell) {
|
|
109
|
+
if ((cell.runs ?? []).some((run) => (run.frames?.length ?? 0) > 0)) for (const run of cell.runs ?? []) emitRun(state, run);
|
|
110
|
+
else for (const frame of cell.frames ?? []) {
|
|
111
|
+
const page = pageOfFrame(state, frame);
|
|
112
|
+
if (page === void 0) continue;
|
|
113
|
+
page.items.push({
|
|
114
|
+
kind: "text",
|
|
115
|
+
text: cell.displayText,
|
|
116
|
+
xPt: frame.xPt,
|
|
117
|
+
yPt: frame.yPt,
|
|
118
|
+
font: document_schema_js.DEFAULT_LAYOUT_FONT,
|
|
119
|
+
sizePt: 10,
|
|
120
|
+
color: document_schema_js.COLOR_BLACK
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
for (const frame of cell.frames ?? []) {
|
|
124
|
+
const page = pageOfFrame(state, frame);
|
|
125
|
+
if (page === void 0) continue;
|
|
126
|
+
if (cell.background !== void 0) page.items.push({
|
|
127
|
+
kind: "rect",
|
|
128
|
+
xPt: frame.xPt,
|
|
129
|
+
yPt: frame.yPt,
|
|
130
|
+
widthPt: frame.widthPt,
|
|
131
|
+
heightPt: frame.heightPt,
|
|
132
|
+
fill: cell.background
|
|
133
|
+
});
|
|
134
|
+
if (cell.borders !== void 0) {
|
|
135
|
+
const frameYDown = require_model_geometry.flipY({
|
|
136
|
+
xPt: frame.xPt,
|
|
137
|
+
yPt: frame.yPt,
|
|
138
|
+
widthPt: frame.widthPt,
|
|
139
|
+
heightPt: frame.heightPt
|
|
140
|
+
}, page.heightPt);
|
|
141
|
+
require_layout_shared.pushCellBorderLines(cell.borders, frameYDown, page.heightPt, cell.sourcePath, page.items);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function emitSheet(state, sheet) {
|
|
146
|
+
for (const cell of sheet.cells) emitSheetCell(state, cell);
|
|
147
|
+
for (const image of sheet.images) for (const frame of image.frames ?? []) {
|
|
148
|
+
const page = pageOfFrame(state, frame);
|
|
149
|
+
if (page === void 0) continue;
|
|
150
|
+
const layoutImage = {
|
|
151
|
+
kind: "image",
|
|
152
|
+
imageId: require_layout_shared.registerImage(image, state.images),
|
|
153
|
+
xPt: frame.xPt,
|
|
154
|
+
yPt: frame.yPt,
|
|
155
|
+
widthPt: frame.widthPt,
|
|
156
|
+
heightPt: frame.heightPt
|
|
157
|
+
};
|
|
158
|
+
page.items.push(layoutImage);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function emitBlocks(state, blocks) {
|
|
162
|
+
for (const block of blocks) if (block.kind === "paragraph") emitParagraph(state, block);
|
|
163
|
+
else if (block.kind === "image") emitImageBlock(state, block, block.frames);
|
|
164
|
+
else if (block.kind === "table") emitTable(state, block);
|
|
165
|
+
}
|
|
166
|
+
function layoutDocumentFromPackage(pkg) {
|
|
167
|
+
const pages = (pkg.pages ?? []).map((page) => ({
|
|
168
|
+
widthPt: page.widthPt,
|
|
169
|
+
heightPt: page.heightPt,
|
|
170
|
+
items: []
|
|
171
|
+
}));
|
|
172
|
+
const state = {
|
|
173
|
+
pages,
|
|
174
|
+
images: {}
|
|
175
|
+
};
|
|
176
|
+
const content = pkg.content;
|
|
177
|
+
if (content.kind === "wordprocessing") for (const section of content.sections) emitBlocks(state, section.blocks);
|
|
178
|
+
else if (content.kind === "presentation") for (const slide of content.slides) for (const shape of slide.shapes) emitBlocks(state, shape.blocks);
|
|
179
|
+
else if (content.kind === "spreadsheet") for (const sheet of content.sheets) emitSheet(state, sheet);
|
|
180
|
+
else if (content.kind === "drawing") for (const drawPage of content.pages) {
|
|
181
|
+
for (const vector of drawPage.vectors) emitVector(state, vector);
|
|
182
|
+
for (const shape of drawPage.shapes) emitBlocks(state, shape.blocks);
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
formatVersion: document_schema_js.LAYOUT_FORMAT_VERSION,
|
|
186
|
+
metadata: content.metadata,
|
|
187
|
+
pages,
|
|
188
|
+
images: state.images
|
|
189
|
+
};
|
|
190
|
+
}
|
|
15
191
|
//#endregion
|
|
16
192
|
exports.buildDocumentBytes = buildDocumentBytes;
|
|
193
|
+
exports.layoutDocumentFromPackage = layoutDocumentFromPackage;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DocumentFormat } from "./port.cjs";
|
|
2
|
-
import { DocumentPackage } from "document-schema.js";
|
|
2
|
+
import { DocumentPackage, LayoutDocument } from "document-schema.js";
|
|
3
3
|
//#region src/convert/from-package.d.ts
|
|
4
4
|
declare function buildDocumentBytes(pkg: DocumentPackage, target: DocumentFormat): Uint8Array<ArrayBuffer>;
|
|
5
|
+
declare function layoutDocumentFromPackage(pkg: DocumentPackage): LayoutDocument;
|
|
5
6
|
//#endregion
|
|
6
|
-
export { buildDocumentBytes };
|
|
7
|
+
export { buildDocumentBytes, layoutDocumentFromPackage };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DocumentFormat } from "./port.js";
|
|
2
|
-
import { DocumentPackage } from "document-schema.js";
|
|
2
|
+
import { DocumentPackage, LayoutDocument } from "document-schema.js";
|
|
3
3
|
//#region src/convert/from-package.d.ts
|
|
4
4
|
declare function buildDocumentBytes(pkg: DocumentPackage, target: DocumentFormat): Uint8Array<ArrayBuffer>;
|
|
5
|
+
declare function layoutDocumentFromPackage(pkg: DocumentPackage): LayoutDocument;
|
|
5
6
|
//#endregion
|
|
6
|
-
export { buildDocumentBytes };
|
|
7
|
+
export { buildDocumentBytes, layoutDocumentFromPackage };
|
|
@@ -1,15 +1,191 @@
|
|
|
1
|
+
import { flipY } from "../model/geometry.js";
|
|
2
|
+
import { pushCellBorderLines, registerImage, runFont } from "../layout/shared.js";
|
|
3
|
+
import "../layout/sheets.js";
|
|
4
|
+
import { convertVector } from "../layout/drawing.js";
|
|
1
5
|
import { DOCUMENT_FORMAT_CODECS, requireArrayBufferBytes } from "../codecs/registry.js";
|
|
6
|
+
import { COLOR_BLACK, DEFAULT_LAYOUT_FONT, LAYOUT_FORMAT_VERSION } from "document-schema.js";
|
|
2
7
|
import { writePdf } from "pdf-codec";
|
|
3
8
|
//#region src/convert/from-package.ts
|
|
4
9
|
function buildDocumentBytes(pkg, target) {
|
|
5
10
|
if (target === "pdf") {
|
|
6
|
-
if (pkg.
|
|
7
|
-
return writePdf(pkg
|
|
11
|
+
if (pkg.pages === void 0) throw new Error("this DocumentPackage has no pages -- only a package dumped from a <format>-to-pdf or pdf-to-<format> conversion carries them; a bridge conversion's own dump (e.g. odt-to-docx) never does, so 'pdf' is not a reachable target from it");
|
|
12
|
+
return writePdf(layoutDocumentFromPackage(pkg));
|
|
8
13
|
}
|
|
9
14
|
if (target === "odf") throw new Error("'odf' (a standalone formula document) cannot be built from a DocumentPackage -- there is no ContentDocument-to-odf builder");
|
|
10
15
|
const content = DOCUMENT_FORMAT_CODECS[target].content;
|
|
11
16
|
if (!content?.write) throw new Error(`DocumentFormat '${target}' has no content.write codec in DOCUMENT_FORMAT_CODECS, and is not 'pdf'/'odf' -- this is an internal invariant violation, not a caller error`);
|
|
12
17
|
return requireArrayBufferBytes(content.write(pkg.content));
|
|
13
18
|
}
|
|
19
|
+
function pageOfFrame(state, frame) {
|
|
20
|
+
return state.pages[frame.pageIndex];
|
|
21
|
+
}
|
|
22
|
+
function emitRun(state, run) {
|
|
23
|
+
const frame = run.frames?.[0];
|
|
24
|
+
if (frame === void 0) return;
|
|
25
|
+
const page = pageOfFrame(state, frame);
|
|
26
|
+
if (page === void 0) return;
|
|
27
|
+
const font = runFont(run);
|
|
28
|
+
const sizePt = run.sizePt ?? 18;
|
|
29
|
+
const textItem = {
|
|
30
|
+
kind: "text",
|
|
31
|
+
text: run.text,
|
|
32
|
+
xPt: frame.xPt,
|
|
33
|
+
yPt: frame.yPt,
|
|
34
|
+
font,
|
|
35
|
+
sizePt,
|
|
36
|
+
color: run.color ?? COLOR_BLACK,
|
|
37
|
+
underline: run.underline
|
|
38
|
+
};
|
|
39
|
+
page.items.push(textItem);
|
|
40
|
+
if (run.hyperlink !== void 0) {
|
|
41
|
+
const link = {
|
|
42
|
+
kind: "link",
|
|
43
|
+
uri: run.hyperlink,
|
|
44
|
+
xPt: frame.xPt,
|
|
45
|
+
yPt: frame.yPt,
|
|
46
|
+
widthPt: frame.widthPt,
|
|
47
|
+
heightPt: frame.heightPt
|
|
48
|
+
};
|
|
49
|
+
page.items.push(link);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function emitParagraph(state, paragraph) {
|
|
53
|
+
for (const run of paragraph.runs) emitRun(state, run);
|
|
54
|
+
}
|
|
55
|
+
function emitImageBlock(state, block, frames) {
|
|
56
|
+
for (const frame of frames ?? []) {
|
|
57
|
+
const page = pageOfFrame(state, frame);
|
|
58
|
+
if (page === void 0) continue;
|
|
59
|
+
const image = {
|
|
60
|
+
kind: "image",
|
|
61
|
+
imageId: registerImage(block, state.images),
|
|
62
|
+
xPt: frame.xPt,
|
|
63
|
+
yPt: frame.yPt,
|
|
64
|
+
widthPt: frame.widthPt,
|
|
65
|
+
heightPt: frame.heightPt
|
|
66
|
+
};
|
|
67
|
+
page.items.push(image);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function emitTableCell(state, cell) {
|
|
71
|
+
for (const frame of cell.frames ?? []) {
|
|
72
|
+
const page = pageOfFrame(state, frame);
|
|
73
|
+
if (page === void 0) continue;
|
|
74
|
+
if (cell.background !== void 0) page.items.push({
|
|
75
|
+
kind: "rect",
|
|
76
|
+
xPt: frame.xPt,
|
|
77
|
+
yPt: frame.yPt,
|
|
78
|
+
widthPt: frame.widthPt,
|
|
79
|
+
heightPt: frame.heightPt,
|
|
80
|
+
fill: cell.background
|
|
81
|
+
});
|
|
82
|
+
if (cell.borders !== void 0) {
|
|
83
|
+
const frameYDown = flipY({
|
|
84
|
+
xPt: frame.xPt,
|
|
85
|
+
yPt: frame.yPt,
|
|
86
|
+
widthPt: frame.widthPt,
|
|
87
|
+
heightPt: frame.heightPt
|
|
88
|
+
}, page.heightPt);
|
|
89
|
+
pushCellBorderLines(cell.borders, frameYDown, page.heightPt, cell.sourcePath, page.items);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
for (const block of cell.blocks) if (block.kind === "paragraph") emitParagraph(state, block);
|
|
93
|
+
else if (block.kind === "image") emitImageBlock(state, block, block.frames);
|
|
94
|
+
else if (block.kind === "table") emitTable(state, block);
|
|
95
|
+
}
|
|
96
|
+
function emitTable(state, table) {
|
|
97
|
+
for (const row of table.rows) for (const cell of row.cells) emitTableCell(state, cell);
|
|
98
|
+
}
|
|
99
|
+
function emitVector(state, vector) {
|
|
100
|
+
for (const frame of vector.frames ?? []) {
|
|
101
|
+
const page = pageOfFrame(state, frame);
|
|
102
|
+
if (page === void 0) continue;
|
|
103
|
+
const items = page.items;
|
|
104
|
+
convertVector(vector, page.heightPt, items);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function emitSheetCell(state, cell) {
|
|
108
|
+
if ((cell.runs ?? []).some((run) => (run.frames?.length ?? 0) > 0)) for (const run of cell.runs ?? []) emitRun(state, run);
|
|
109
|
+
else for (const frame of cell.frames ?? []) {
|
|
110
|
+
const page = pageOfFrame(state, frame);
|
|
111
|
+
if (page === void 0) continue;
|
|
112
|
+
page.items.push({
|
|
113
|
+
kind: "text",
|
|
114
|
+
text: cell.displayText,
|
|
115
|
+
xPt: frame.xPt,
|
|
116
|
+
yPt: frame.yPt,
|
|
117
|
+
font: DEFAULT_LAYOUT_FONT,
|
|
118
|
+
sizePt: 10,
|
|
119
|
+
color: COLOR_BLACK
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
for (const frame of cell.frames ?? []) {
|
|
123
|
+
const page = pageOfFrame(state, frame);
|
|
124
|
+
if (page === void 0) continue;
|
|
125
|
+
if (cell.background !== void 0) page.items.push({
|
|
126
|
+
kind: "rect",
|
|
127
|
+
xPt: frame.xPt,
|
|
128
|
+
yPt: frame.yPt,
|
|
129
|
+
widthPt: frame.widthPt,
|
|
130
|
+
heightPt: frame.heightPt,
|
|
131
|
+
fill: cell.background
|
|
132
|
+
});
|
|
133
|
+
if (cell.borders !== void 0) {
|
|
134
|
+
const frameYDown = flipY({
|
|
135
|
+
xPt: frame.xPt,
|
|
136
|
+
yPt: frame.yPt,
|
|
137
|
+
widthPt: frame.widthPt,
|
|
138
|
+
heightPt: frame.heightPt
|
|
139
|
+
}, page.heightPt);
|
|
140
|
+
pushCellBorderLines(cell.borders, frameYDown, page.heightPt, cell.sourcePath, page.items);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function emitSheet(state, sheet) {
|
|
145
|
+
for (const cell of sheet.cells) emitSheetCell(state, cell);
|
|
146
|
+
for (const image of sheet.images) for (const frame of image.frames ?? []) {
|
|
147
|
+
const page = pageOfFrame(state, frame);
|
|
148
|
+
if (page === void 0) continue;
|
|
149
|
+
const layoutImage = {
|
|
150
|
+
kind: "image",
|
|
151
|
+
imageId: registerImage(image, state.images),
|
|
152
|
+
xPt: frame.xPt,
|
|
153
|
+
yPt: frame.yPt,
|
|
154
|
+
widthPt: frame.widthPt,
|
|
155
|
+
heightPt: frame.heightPt
|
|
156
|
+
};
|
|
157
|
+
page.items.push(layoutImage);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function emitBlocks(state, blocks) {
|
|
161
|
+
for (const block of blocks) if (block.kind === "paragraph") emitParagraph(state, block);
|
|
162
|
+
else if (block.kind === "image") emitImageBlock(state, block, block.frames);
|
|
163
|
+
else if (block.kind === "table") emitTable(state, block);
|
|
164
|
+
}
|
|
165
|
+
function layoutDocumentFromPackage(pkg) {
|
|
166
|
+
const pages = (pkg.pages ?? []).map((page) => ({
|
|
167
|
+
widthPt: page.widthPt,
|
|
168
|
+
heightPt: page.heightPt,
|
|
169
|
+
items: []
|
|
170
|
+
}));
|
|
171
|
+
const state = {
|
|
172
|
+
pages,
|
|
173
|
+
images: {}
|
|
174
|
+
};
|
|
175
|
+
const content = pkg.content;
|
|
176
|
+
if (content.kind === "wordprocessing") for (const section of content.sections) emitBlocks(state, section.blocks);
|
|
177
|
+
else if (content.kind === "presentation") for (const slide of content.slides) for (const shape of slide.shapes) emitBlocks(state, shape.blocks);
|
|
178
|
+
else if (content.kind === "spreadsheet") for (const sheet of content.sheets) emitSheet(state, sheet);
|
|
179
|
+
else if (content.kind === "drawing") for (const drawPage of content.pages) {
|
|
180
|
+
for (const vector of drawPage.vectors) emitVector(state, vector);
|
|
181
|
+
for (const shape of drawPage.shapes) emitBlocks(state, shape.blocks);
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
formatVersion: LAYOUT_FORMAT_VERSION,
|
|
185
|
+
metadata: content.metadata,
|
|
186
|
+
pages,
|
|
187
|
+
images: state.images
|
|
188
|
+
};
|
|
189
|
+
}
|
|
14
190
|
//#endregion
|
|
15
|
-
export { buildDocumentBytes };
|
|
191
|
+
export { buildDocumentBytes, layoutDocumentFromPackage };
|
package/dist/convert/local.cjs
CHANGED
|
@@ -83,6 +83,8 @@ function createLocalDocumentConverter() {
|
|
|
83
83
|
onSubstitution: (s, c) => diagnostics.push(substitutionDiagnostic(s, c)),
|
|
84
84
|
sink: (d) => diagnostics.push(fromPdfDiagnostic(d)),
|
|
85
85
|
images: options.images,
|
|
86
|
+
delimiter: options.delimiter,
|
|
87
|
+
sheet: options.sheet,
|
|
86
88
|
clock: options.clock,
|
|
87
89
|
onDocument
|
|
88
90
|
});
|
package/dist/convert/local.js
CHANGED
|
@@ -82,6 +82,8 @@ function createLocalDocumentConverter() {
|
|
|
82
82
|
onSubstitution: (s, c) => diagnostics.push(substitutionDiagnostic(s, c)),
|
|
83
83
|
sink: (d) => diagnostics.push(fromPdfDiagnostic(d)),
|
|
84
84
|
images: options.images,
|
|
85
|
+
delimiter: options.delimiter,
|
|
86
|
+
sheet: options.sheet,
|
|
85
87
|
clock: options.clock,
|
|
86
88
|
onDocument
|
|
87
89
|
});
|
package/dist/convert/port.cjs
CHANGED
package/dist/convert/port.d.cts
CHANGED
|
@@ -12,6 +12,7 @@ declare const DocumentFormatSchema: z.ZodEnum<{
|
|
|
12
12
|
ods: "ods";
|
|
13
13
|
odg: "odg";
|
|
14
14
|
odf: "odf";
|
|
15
|
+
csv: "csv";
|
|
15
16
|
markdown: "markdown";
|
|
16
17
|
pdf: "pdf";
|
|
17
18
|
}>;
|
|
@@ -41,6 +42,8 @@ interface ConversionOptions {
|
|
|
41
42
|
readonly fonts?: readonly ProvidedFont[];
|
|
42
43
|
readonly onFontSubstitution?: (substitution: FontSubstitution) => void;
|
|
43
44
|
readonly images?: MarkdownImageResolver;
|
|
45
|
+
readonly delimiter?: string;
|
|
46
|
+
readonly sheet?: string;
|
|
44
47
|
readonly clock?: ClockPort;
|
|
45
48
|
}
|
|
46
49
|
interface DocumentConverter {
|
package/dist/convert/port.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ declare const DocumentFormatSchema: z.ZodEnum<{
|
|
|
12
12
|
ods: "ods";
|
|
13
13
|
odg: "odg";
|
|
14
14
|
odf: "odf";
|
|
15
|
+
csv: "csv";
|
|
15
16
|
markdown: "markdown";
|
|
16
17
|
pdf: "pdf";
|
|
17
18
|
}>;
|
|
@@ -41,6 +42,8 @@ interface ConversionOptions {
|
|
|
41
42
|
readonly fonts?: readonly ProvidedFont[];
|
|
42
43
|
readonly onFontSubstitution?: (substitution: FontSubstitution) => void;
|
|
43
44
|
readonly images?: MarkdownImageResolver;
|
|
45
|
+
readonly delimiter?: string;
|
|
46
|
+
readonly sheet?: string;
|
|
44
47
|
readonly clock?: ClockPort;
|
|
45
48
|
}
|
|
46
49
|
interface DocumentConverter {
|
package/dist/convert/port.js
CHANGED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_layout_cell_typing = require("../layout/cell-typing.cjs");
|
|
3
|
+
const require_csv_records = require("./records.cjs");
|
|
4
|
+
let document_schema_js = require("document-schema.js");
|
|
5
|
+
//#region src/csv/read.ts
|
|
6
|
+
const HEADER_ROW_INDEX = 0;
|
|
7
|
+
const SHEET_NAME = "Sheet1";
|
|
8
|
+
const COLUMN_WIDTH_PT = 64;
|
|
9
|
+
const ROW_HEIGHT_PT = 15;
|
|
10
|
+
const MARGIN_PT = 56.69291338582677;
|
|
11
|
+
const DEFAULT_PRINT_SETTINGS = {
|
|
12
|
+
pageSize: document_schema_js.PAGE_SIZE_A4,
|
|
13
|
+
margins: {
|
|
14
|
+
topPt: MARGIN_PT,
|
|
15
|
+
rightPt: MARGIN_PT,
|
|
16
|
+
bottomPt: MARGIN_PT,
|
|
17
|
+
leftPt: MARGIN_PT
|
|
18
|
+
},
|
|
19
|
+
gridlines: true,
|
|
20
|
+
headers: true,
|
|
21
|
+
pageOrder: "downThenOver"
|
|
22
|
+
};
|
|
23
|
+
function dataCell(rowIndex, columnIndex, field, onCellTypeInference) {
|
|
24
|
+
const inference = field === "" ? void 0 : require_layout_cell_typing.inferCellValue(field);
|
|
25
|
+
if (inference !== void 0) onCellTypeInference?.({
|
|
26
|
+
sheetIndex: 0,
|
|
27
|
+
row: rowIndex,
|
|
28
|
+
column: columnIndex,
|
|
29
|
+
displayText: field,
|
|
30
|
+
...inference
|
|
31
|
+
});
|
|
32
|
+
return {
|
|
33
|
+
row: rowIndex,
|
|
34
|
+
column: columnIndex,
|
|
35
|
+
value: field === "" ? { kind: "empty" } : inference?.outcome === "retyped" ? inference.value : {
|
|
36
|
+
kind: "string",
|
|
37
|
+
value: field
|
|
38
|
+
},
|
|
39
|
+
displayText: field
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function readCsvContent(text, options) {
|
|
43
|
+
const records = require_csv_records.parseCsvRecords(text, options?.delimiter ?? ",");
|
|
44
|
+
const onCellTypeInference = options?.onCellTypeInference;
|
|
45
|
+
const columnCount = records.reduce((max, record) => Math.max(max, record.length), 0);
|
|
46
|
+
const cells = [];
|
|
47
|
+
records.forEach((record, rowIndex) => {
|
|
48
|
+
for (let columnIndex = 0; columnIndex < columnCount; columnIndex++) {
|
|
49
|
+
const field = record[columnIndex] ?? "";
|
|
50
|
+
if (rowIndex === HEADER_ROW_INDEX) cells.push({
|
|
51
|
+
row: rowIndex,
|
|
52
|
+
column: columnIndex,
|
|
53
|
+
value: {
|
|
54
|
+
kind: "string",
|
|
55
|
+
value: field
|
|
56
|
+
},
|
|
57
|
+
displayText: field
|
|
58
|
+
});
|
|
59
|
+
else cells.push(dataCell(rowIndex, columnIndex, field, onCellTypeInference));
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
const columns = Array.from({ length: columnCount }, (_unused, index) => ({
|
|
63
|
+
index,
|
|
64
|
+
widthPt: COLUMN_WIDTH_PT
|
|
65
|
+
}));
|
|
66
|
+
const rows = Array.from({ length: records.length }, (_unused, index) => ({
|
|
67
|
+
index,
|
|
68
|
+
heightPt: ROW_HEIGHT_PT
|
|
69
|
+
}));
|
|
70
|
+
return {
|
|
71
|
+
kind: "spreadsheet",
|
|
72
|
+
formatVersion: document_schema_js.CONTENT_FORMAT_VERSION,
|
|
73
|
+
metadata: {},
|
|
74
|
+
sheets: [{
|
|
75
|
+
name: SHEET_NAME,
|
|
76
|
+
cells,
|
|
77
|
+
columns,
|
|
78
|
+
rows,
|
|
79
|
+
images: [],
|
|
80
|
+
printSettings: DEFAULT_PRINT_SETTINGS
|
|
81
|
+
}]
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
exports.readCsvContent = readCsvContent;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CellTypeInferenceSink } from "../layout/cell-typing.cjs";
|
|
2
|
+
import { ContentDocument } from "document-schema.js";
|
|
3
|
+
//#region src/csv/read.d.ts
|
|
4
|
+
interface ReadCsvContentOptions {
|
|
5
|
+
readonly delimiter?: string;
|
|
6
|
+
readonly onCellTypeInference?: CellTypeInferenceSink;
|
|
7
|
+
}
|
|
8
|
+
declare function readCsvContent(text: string, options?: ReadCsvContentOptions): ContentDocument;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { ReadCsvContentOptions, readCsvContent };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CellTypeInferenceSink } from "../layout/cell-typing.js";
|
|
2
|
+
import { ContentDocument } from "document-schema.js";
|
|
3
|
+
//#region src/csv/read.d.ts
|
|
4
|
+
interface ReadCsvContentOptions {
|
|
5
|
+
readonly delimiter?: string;
|
|
6
|
+
readonly onCellTypeInference?: CellTypeInferenceSink;
|
|
7
|
+
}
|
|
8
|
+
declare function readCsvContent(text: string, options?: ReadCsvContentOptions): ContentDocument;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { ReadCsvContentOptions, readCsvContent };
|
package/dist/csv/read.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { inferCellValue } from "../layout/cell-typing.js";
|
|
2
|
+
import { parseCsvRecords } from "./records.js";
|
|
3
|
+
import { CONTENT_FORMAT_VERSION, PAGE_SIZE_A4 } from "document-schema.js";
|
|
4
|
+
//#region src/csv/read.ts
|
|
5
|
+
const HEADER_ROW_INDEX = 0;
|
|
6
|
+
const SHEET_NAME = "Sheet1";
|
|
7
|
+
const COLUMN_WIDTH_PT = 64;
|
|
8
|
+
const ROW_HEIGHT_PT = 15;
|
|
9
|
+
const MARGIN_PT = 56.69291338582677;
|
|
10
|
+
const DEFAULT_PRINT_SETTINGS = {
|
|
11
|
+
pageSize: PAGE_SIZE_A4,
|
|
12
|
+
margins: {
|
|
13
|
+
topPt: MARGIN_PT,
|
|
14
|
+
rightPt: MARGIN_PT,
|
|
15
|
+
bottomPt: MARGIN_PT,
|
|
16
|
+
leftPt: MARGIN_PT
|
|
17
|
+
},
|
|
18
|
+
gridlines: true,
|
|
19
|
+
headers: true,
|
|
20
|
+
pageOrder: "downThenOver"
|
|
21
|
+
};
|
|
22
|
+
function dataCell(rowIndex, columnIndex, field, onCellTypeInference) {
|
|
23
|
+
const inference = field === "" ? void 0 : inferCellValue(field);
|
|
24
|
+
if (inference !== void 0) onCellTypeInference?.({
|
|
25
|
+
sheetIndex: 0,
|
|
26
|
+
row: rowIndex,
|
|
27
|
+
column: columnIndex,
|
|
28
|
+
displayText: field,
|
|
29
|
+
...inference
|
|
30
|
+
});
|
|
31
|
+
return {
|
|
32
|
+
row: rowIndex,
|
|
33
|
+
column: columnIndex,
|
|
34
|
+
value: field === "" ? { kind: "empty" } : inference?.outcome === "retyped" ? inference.value : {
|
|
35
|
+
kind: "string",
|
|
36
|
+
value: field
|
|
37
|
+
},
|
|
38
|
+
displayText: field
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function readCsvContent(text, options) {
|
|
42
|
+
const records = parseCsvRecords(text, options?.delimiter ?? ",");
|
|
43
|
+
const onCellTypeInference = options?.onCellTypeInference;
|
|
44
|
+
const columnCount = records.reduce((max, record) => Math.max(max, record.length), 0);
|
|
45
|
+
const cells = [];
|
|
46
|
+
records.forEach((record, rowIndex) => {
|
|
47
|
+
for (let columnIndex = 0; columnIndex < columnCount; columnIndex++) {
|
|
48
|
+
const field = record[columnIndex] ?? "";
|
|
49
|
+
if (rowIndex === HEADER_ROW_INDEX) cells.push({
|
|
50
|
+
row: rowIndex,
|
|
51
|
+
column: columnIndex,
|
|
52
|
+
value: {
|
|
53
|
+
kind: "string",
|
|
54
|
+
value: field
|
|
55
|
+
},
|
|
56
|
+
displayText: field
|
|
57
|
+
});
|
|
58
|
+
else cells.push(dataCell(rowIndex, columnIndex, field, onCellTypeInference));
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
const columns = Array.from({ length: columnCount }, (_unused, index) => ({
|
|
62
|
+
index,
|
|
63
|
+
widthPt: COLUMN_WIDTH_PT
|
|
64
|
+
}));
|
|
65
|
+
const rows = Array.from({ length: records.length }, (_unused, index) => ({
|
|
66
|
+
index,
|
|
67
|
+
heightPt: ROW_HEIGHT_PT
|
|
68
|
+
}));
|
|
69
|
+
return {
|
|
70
|
+
kind: "spreadsheet",
|
|
71
|
+
formatVersion: CONTENT_FORMAT_VERSION,
|
|
72
|
+
metadata: {},
|
|
73
|
+
sheets: [{
|
|
74
|
+
name: SHEET_NAME,
|
|
75
|
+
cells,
|
|
76
|
+
columns,
|
|
77
|
+
rows,
|
|
78
|
+
images: [],
|
|
79
|
+
printSettings: DEFAULT_PRINT_SETTINGS
|
|
80
|
+
}]
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
export { readCsvContent };
|