@file-viewer/renderer-spreadsheet 3.3.1 → 3.3.4

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.
Files changed (52) hide show
  1. package/README.en.md +14 -0
  2. package/README.md +14 -0
  3. package/README.zh-CN.md +14 -0
  4. package/dist/spreadsheet/autoRowHeight.d.ts +16 -0
  5. package/dist/spreadsheet/autoRowHeight.js +208 -0
  6. package/dist/spreadsheet/chartRenderer.js +0 -5
  7. package/dist/spreadsheet/chartSampling.d.ts +0 -4
  8. package/dist/spreadsheet/chartSampling.js +0 -6
  9. package/dist/spreadsheet/comments.d.ts +40 -0
  10. package/dist/spreadsheet/comments.js +388 -0
  11. package/dist/spreadsheet/drawingAutoHeight.d.ts +7 -0
  12. package/dist/spreadsheet/drawingAutoHeight.js +51 -0
  13. package/dist/spreadsheet/rangeNavigation.d.ts +0 -2
  14. package/dist/spreadsheet/rangeNavigation.js +0 -4
  15. package/dist/spreadsheet/sourceRegion.d.ts +37 -0
  16. package/dist/spreadsheet/sourceRegion.js +54 -0
  17. package/dist/spreadsheet/state.d.ts +7 -2
  18. package/dist/spreadsheet/state.js +6 -2
  19. package/dist/spreadsheet/view.d.ts +9 -4
  20. package/dist/spreadsheet/view.js +98 -39
  21. package/dist/spreadsheet/viewport.d.ts +0 -1
  22. package/dist/spreadsheet/viewport.js +0 -2
  23. package/dist/spreadsheet/worker/sheetjs/SheetJsModel.d.ts +61 -2
  24. package/dist/spreadsheet/worker/sheetjs/SheetJsModel.js +134 -43
  25. package/dist/spreadsheet/worker/sheetjs/chartParser.d.ts +3 -2
  26. package/dist/spreadsheet/worker/sheetjs/chartParser.js +13 -24
  27. package/dist/spreadsheet/worker/sheetjs/color.js +8 -10
  28. package/dist/spreadsheet/worker/sheetjs/commentParser.d.ts +7 -0
  29. package/dist/spreadsheet/worker/sheetjs/commentParser.js +232 -0
  30. package/dist/spreadsheet/worker/sheetjs/drawingRowAnchor.d.ts +4 -0
  31. package/dist/spreadsheet/worker/sheetjs/drawingRowAnchor.js +90 -0
  32. package/dist/spreadsheet/worker/sheetjs/formulaValues.js +0 -9
  33. package/dist/spreadsheet/worker/sheetjs/parser.d.ts +40 -4
  34. package/dist/spreadsheet/worker/sheetjs/parser.js +453 -78
  35. package/dist/spreadsheet/worker/sheetjs/richDataParser.js +5 -4
  36. package/dist/spreadsheet/worker/sheetjs/richText.js +0 -1
  37. package/dist/spreadsheet/worker/sheetjs/rowSizing.d.ts +10 -0
  38. package/dist/spreadsheet/worker/sheetjs/rowSizing.js +156 -0
  39. package/dist/spreadsheet/worker/sheetjs/sheetLayout.d.ts +2 -4
  40. package/dist/spreadsheet/worker/sheetjs/sheetLayout.js +0 -8
  41. package/dist/spreadsheet/worker/sheetjs/sourceAnchor.d.ts +10 -0
  42. package/dist/spreadsheet/worker/sheetjs/sourceAnchor.js +286 -0
  43. package/dist/spreadsheet/worker/sheetjs/textEncoding.d.ts +1 -1
  44. package/dist/spreadsheet/worker/sheetjs/textEncoding.js +1 -8
  45. package/dist/spreadsheet/worker/sheetjs/workbookFont.d.ts +4 -0
  46. package/dist/spreadsheet/worker/sheetjs/workbookFont.js +15 -0
  47. package/dist/spreadsheet/worker/sheetjs/xmlBudget.d.ts +11 -0
  48. package/dist/spreadsheet/worker/sheetjs/xmlBudget.js +39 -0
  49. package/dist/spreadsheet/worker/type.d.ts +145 -3
  50. package/dist/spreadsheet.js +150 -63
  51. package/dist/worker/sheet.worker.js +49 -65
  52. package/package.json +23 -3
@@ -8,7 +8,10 @@ const METADATA_RELATIONSHIP_SUFFIX = '/sheetmetadata';
8
8
  const RICH_VALUE_RELATIONSHIP_SUFFIX = '/rdrichvalue';
9
9
  const RICH_VALUE_STRUCTURE_RELATIONSHIP_SUFFIX = '/rdrichvaluestructure';
10
10
  const RICH_VALUE_RELATION_LIST_SUFFIX = '/richvaluerel';
11
- const WPS_CELL_IMAGE_RELATIONSHIP = 'http://www.wps.cn/officeDocument/2017/relationships/cellimage';
11
+ const WPS_CELL_IMAGE_RELATIONSHIPS = new Set([
12
+ 'http://www.wps.cn/officeDocument/2017/relationships/cellimage',
13
+ 'http://www.wps.cn/officeDocument/2020/cellImage'
14
+ ].map(type => type.toLowerCase()));
12
15
  const RICH_VALUE_METADATA_NAME = 'XLRICHVALUE';
13
16
  const LOCAL_IMAGE_STRUCTURE_TYPE = '_localImage';
14
17
  const LOCAL_IMAGE_KEY_SUFFIX = 'LocalImageIdentifier';
@@ -135,8 +138,6 @@ const parseWorksheetCellMetadata = (xml) => {
135
138
  if (!ref || !metadataText || !Number.isInteger(metadataValue) || metadataValue < 0) {
136
139
  continue;
137
140
  }
138
- // Cell metadata indexes are one-based in OOXML. Accept zero as a
139
- // producer-tolerance fallback without shifting it below zero.
140
141
  result.push({ ref, metadataIndex: metadataValue > 0 ? metadataValue - 1 : 0 });
141
142
  }
142
143
  return result;
@@ -269,7 +270,7 @@ export const parseSpreadsheetCellImages = async (data) => {
269
270
  metadataRichValueIndexes = parseMetadataRichValueIndexes(metadataDocument);
270
271
  richValueImages = await loadRichValueImages(zip, parseRichValueStructures(richValueStructureDocument), parseRichValues(richValueDocument), parseRichValueRelationshipIds(richValueRelationListDocument), richValueRelationships, contentTypes);
271
272
  }
272
- const wpsCellImageRelationship = workbookRelationships.find((relationship) => (relationship.type.toLowerCase() === WPS_CELL_IMAGE_RELATIONSHIP.toLowerCase()));
273
+ const wpsCellImageRelationship = workbookRelationships.find((relationship) => (WPS_CELL_IMAGE_RELATIONSHIPS.has(relationship.type.toLowerCase())));
273
274
  const wpsCellImages = await loadWpsCellImages(zip, wpsCellImageRelationship, contentTypes);
274
275
  if (!richValueImages.size && !wpsCellImages.size) {
275
276
  return result;
@@ -49,7 +49,6 @@ const parseColor = (element) => {
49
49
  };
50
50
  const parseFontSize = (element) => {
51
51
  const value = Number(element === null || element === void 0 ? void 0 : element.getAttribute('val'));
52
- // 富文本的 <sz> 同样使用 pt,段字号与普通单元格都以 CSS px 传给视图。
53
52
  return Number.isFinite(value) && value > 0 ? value * 96 / 72 : undefined;
54
53
  };
55
54
  const normalizeSymbolText = (text, fontFamily) => {
@@ -0,0 +1,10 @@
1
+ import type JSZip from 'jszip';
2
+ export interface StoredRowSizing {
3
+ fixedRows: number[];
4
+ autoHeightDisabled?: boolean;
5
+ }
6
+ export declare const createRowSizingScanner: () => {
7
+ push: (chunk: string) => void;
8
+ finish: () => StoredRowSizing;
9
+ };
10
+ export declare const readStoredRowSizing: (zip: JSZip, part: string) => Promise<StoredRowSizing>;
@@ -0,0 +1,156 @@
1
+ const decodeAttribute = (value) => value.replace(/&(#x[\da-f]+|#\d+|amp|lt|gt|quot|apos);/gi, (_, token) => {
2
+ if (token[0] !== '#')
3
+ return ({ amp: '&', lt: '<', gt: '>', quot: '"', apos: "'" }[token] || '');
4
+ const code = token[1].toLowerCase() === 'x'
5
+ ? parseInt(token.slice(2), 16)
6
+ : parseInt(token.slice(1), 10);
7
+ return code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : '';
8
+ });
9
+ export const createRowSizingScanner = () => {
10
+ let buffer = '', skipping = '', lastRow = 0;
11
+ const fixed = new Set();
12
+ const push = (chunk) => {
13
+ var _a;
14
+ buffer += chunk;
15
+ for (;;) {
16
+ if (skipping) {
17
+ const end = buffer.indexOf(skipping);
18
+ if (end < 0) {
19
+ buffer = buffer.slice(-skipping.length);
20
+ return;
21
+ }
22
+ buffer = buffer.slice(end + skipping.length);
23
+ skipping = '';
24
+ continue;
25
+ }
26
+ const start = buffer.indexOf('<');
27
+ if (start < 0) {
28
+ buffer = '';
29
+ return;
30
+ }
31
+ buffer = buffer.slice(start);
32
+ if (buffer.startsWith('<!--')) {
33
+ buffer = buffer.slice(4);
34
+ skipping = '-->';
35
+ continue;
36
+ }
37
+ if (buffer.startsWith('<![CDATA[')) {
38
+ buffer = buffer.slice(9);
39
+ skipping = ']]>';
40
+ continue;
41
+ }
42
+ if (/^<!DOCTYPE/i.test(buffer))
43
+ throw new Error('Row sizing does not expand XML document types');
44
+ let end = -1, quote = '';
45
+ for (let i = 1; i < buffer.length; i++) {
46
+ const c = buffer[i];
47
+ if (quote) {
48
+ if (c === quote)
49
+ quote = '';
50
+ continue;
51
+ }
52
+ if (c === '"' || c === "'")
53
+ quote = c;
54
+ else if (c === '>') {
55
+ end = i;
56
+ break;
57
+ }
58
+ }
59
+ if (end < 0) {
60
+ if (buffer.length > 65536)
61
+ throw new Error('Row sizing XML tag exceeds the preview limit');
62
+ return;
63
+ }
64
+ const tag = buffer.slice(0, end + 1);
65
+ buffer = buffer.slice(end + 1);
66
+ const row = /^<(?:[\w.-]+:)?row\b/.exec(tag);
67
+ if (!row)
68
+ continue;
69
+ const attrs = {};
70
+ let tail = tag.slice(row[0].length);
71
+ while (tail.trim() && !/^\s*\/?>/.test(tail)) {
72
+ const attr = /^\s+([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/.exec(tail);
73
+ if (!attr)
74
+ throw new Error('Invalid row sizing attribute');
75
+ attrs[attr[1]] = decodeAttribute((_a = attr[2]) !== null && _a !== void 0 ? _a : attr[3]);
76
+ tail = tail.slice(attr[0].length);
77
+ }
78
+ const index = attrs.r === undefined ? lastRow + 1 : Number(attrs.r);
79
+ if (!Number.isSafeInteger(index) || index < 1 || index > 1048576)
80
+ throw new Error('Invalid source row index');
81
+ lastRow = index;
82
+ const hasHeight = Object.prototype.hasOwnProperty.call(attrs, 'ht');
83
+ const validHeight = hasHeight &&
84
+ attrs.ht.trim() !== '' &&
85
+ Number.isFinite(Number(attrs.ht)) &&
86
+ Number(attrs.ht) >= 0;
87
+ const custom = attrs.customHeight !== undefined &&
88
+ !/^(?:0|false)$/i.test(attrs.customHeight);
89
+ if ((custom && !validHeight) || (hasHeight && !validHeight))
90
+ fixed.add(index - 1);
91
+ if (fixed.size > 100000)
92
+ throw new Error('Row sizing hints exceed the preview limit');
93
+ }
94
+ };
95
+ return {
96
+ push,
97
+ finish: () => {
98
+ if (skipping || buffer.trim())
99
+ throw new Error('Incomplete row sizing XML');
100
+ return { fixedRows: [...fixed] };
101
+ },
102
+ };
103
+ };
104
+ export const readStoredRowSizing = async (zip, part) => {
105
+ const file = zip.file(part);
106
+ if (!file)
107
+ return { fixedRows: [], autoHeightDisabled: true };
108
+ try {
109
+ return await new Promise((resolve, reject) => {
110
+ const scanner = createRowSizingScanner(), decoder = new TextDecoder();
111
+ const stream = file.internalStream('uint8array');
112
+ let finished = false, bytes = 0;
113
+ const fail = (error) => {
114
+ if (finished)
115
+ return;
116
+ finished = true;
117
+ stream.pause();
118
+ reject(error);
119
+ };
120
+ stream.on('data', (chunk) => {
121
+ if (finished)
122
+ return;
123
+ bytes += chunk.byteLength;
124
+ if (bytes > 128 * 1024 * 1024) {
125
+ fail(new Error('Row sizing XML exceeds the preview limit'));
126
+ return;
127
+ }
128
+ try {
129
+ scanner.push(decoder.decode(chunk, { stream: true }));
130
+ }
131
+ catch (error) {
132
+ fail(error);
133
+ }
134
+ });
135
+ stream.on('error', fail);
136
+ stream.on('end', () => {
137
+ if (finished)
138
+ return;
139
+ try {
140
+ scanner.push(decoder.decode());
141
+ const result = scanner.finish();
142
+ finished = true;
143
+ resolve(result);
144
+ }
145
+ catch (error) {
146
+ fail(error);
147
+ }
148
+ });
149
+ stream.resume();
150
+ });
151
+ }
152
+ catch (error) {
153
+ console.warn('[file-viewer] Automatic row sizing disabled: source row metadata could not be verified.', error);
154
+ return { fixedRows: [], autoHeightDisabled: true };
155
+ }
156
+ };
@@ -1,13 +1,11 @@
1
1
  import type JSZip from 'jszip';
2
2
  import type { WorkBook } from 'styled-exceljs';
3
+ import type { StoredRowSizing } from './rowSizing.js';
3
4
  export interface StoredSheetLayout {
4
5
  columnWidth?: number;
5
6
  rowHeightPt?: number;
7
+ rowSizing?: StoredRowSizing;
6
8
  maximumDigitWidth?: number;
7
9
  }
8
- /** OOXML column units refer to the workbook's Normal style, which need not
9
- * use fonts[0]. Preserve the established fallback for unmeasured fonts. */
10
10
  export declare const readWorkbookDigitWidth: (workbook?: WorkBook | null) => number;
11
- /** Read only the worksheet header; never materialize a large sheet's cells a
12
- * second time merely to recover sheetFormatPr. */
13
11
  export declare const readStoredSheetLayout: (zip: JSZip, part: string) => Promise<StoredSheetLayout>;
@@ -1,6 +1,4 @@
1
1
  import { DOMParser } from '@xmldom/xmldom';
2
- /** OOXML column units refer to the workbook's Normal style, which need not
3
- * use fonts[0]. Preserve the established fallback for unmeasured fonts. */
4
2
  export const readWorkbookDigitWidth = (workbook) => {
5
3
  var _a, _b, _c, _d, _e, _f, _g, _h;
6
4
  const styles = workbook === null || workbook === void 0 ? void 0 : workbook.Styles;
@@ -8,17 +6,12 @@ export const readWorkbookDigitWidth = (workbook) => {
8
6
  const fontId = (normal === null || normal === void 0 ? void 0 : normal.xfId) === undefined ? 0 : (_f = (_e = (_d = styles === null || styles === void 0 ? void 0 : styles.CellStyleXf) === null || _d === void 0 ? void 0 : _d[normal.xfId]) === null || _e === void 0 ? void 0 : _e.fontId) !== null && _f !== void 0 ? _f : 0;
9
7
  const font = (_g = styles === null || styles === void 0 ? void 0 : styles.Fonts) === null || _g === void 0 ? void 0 : _g[fontId];
10
8
  const size = Number(font === null || font === void 0 ? void 0 : font.sz);
11
- // SimSun/NSimSun digits advance half an em. Convert font points to CSS
12
- // pixels before rounding; scaling an already rounded 11pt grid widens
13
- // 10.5pt and 12pt templates. Other CJK families can have different metrics.
14
9
  if (/^(?:宋体|新宋体|simsun|nsimsun)$/i.test(((_h = font === null || font === void 0 ? void 0 : font.name) === null || _h === void 0 ? void 0 : _h.trim()) || '')
15
10
  && Number.isFinite(size) && size >= 1 && size <= 409) {
16
11
  return Math.max(1, Math.ceil(size * 96 / 72 / 2));
17
12
  }
18
13
  return 7;
19
14
  };
20
- /** Read only the worksheet header; never materialize a large sheet's cells a
21
- * second time merely to recover sheetFormatPr. */
22
15
  export const readStoredSheetLayout = async (zip, part) => {
23
16
  var _a, _b, _c, _d;
24
17
  const file = zip.file(part);
@@ -28,7 +21,6 @@ export const readStoredSheetLayout = async (zip, part) => {
28
21
  const header = await new Promise((resolve, reject) => {
29
22
  let prefix = '';
30
23
  let done = false;
31
- // JSZip exposes this streaming API at runtime but omits it from JSZipObject's declaration.
32
24
  const stream = file.internalStream('string');
33
25
  const finish = () => {
34
26
  if (done)
@@ -0,0 +1,10 @@
1
+ import type { DrawingImage } from 'styled-exceljs';
2
+ import type { SheetSourceAnchor, SheetImageSourceSummary } from '../type.js';
3
+ export declare const validSourceReference: (anchor: any, reference: any, kind: "image" | "chart") => boolean;
4
+ export declare const readSourceAnchor: (element: any, reference?: any, kind?: "image" | "chart") => SheetSourceAnchor;
5
+ export declare const unknownSourceAnchor: (reason: string) => SheetSourceAnchor;
6
+ export declare const readImageSourceProvenance: (raw: unknown, images: DrawingImage[], drawingPresent?: boolean) => {
7
+ anchors: Map<DrawingImage, SheetSourceAnchor>;
8
+ summary: SheetImageSourceSummary;
9
+ };
10
+ export declare const readImageSourceAnchors: (raw: unknown, images: DrawingImage[]) => Map<DrawingImage, SheetSourceAnchor>;
@@ -0,0 +1,286 @@
1
+ import { DOMParser } from '@xmldom/xmldom';
2
+ const local = (node) => { var _a; return (node === null || node === void 0 ? void 0 : node.localName) || ((_a = node === null || node === void 0 ? void 0 : node.nodeName) === null || _a === void 0 ? void 0 : _a.split(':').pop()) || ''; };
3
+ const child = (node, name) => {
4
+ for (let current = node === null || node === void 0 ? void 0 : node.firstChild; current; current = current.nextSibling)
5
+ if (current.nodeType === 1 && local(current) === name)
6
+ return current;
7
+ return undefined;
8
+ };
9
+ const XDR = 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing';
10
+ const MAIN = 'http://schemas.openxmlformats.org/drawingml/2006/main';
11
+ const CHART = 'http://schemas.openxmlformats.org/drawingml/2006/chart';
12
+ const REL = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
13
+ const isElement = (node, name, namespace) => (node === null || node === void 0 ? void 0 : node.nodeType) === 1 && local(node) === name && node.namespaceURI === namespace;
14
+ const soleChild = (parent, node, name, namespace) => {
15
+ let count = 0;
16
+ for (let current = parent === null || parent === void 0 ? void 0 : parent.firstChild; current; current = current.nextSibling)
17
+ if (current.nodeType === 1 && local(current) === name)
18
+ count++;
19
+ return count === 1 && (node === null || node === void 0 ? void 0 : node.parentNode) === parent && isElement(node, name, namespace);
20
+ };
21
+ const plainAttribute = (element, name) => {
22
+ var _a;
23
+ let count = 0, value;
24
+ for (let index = 0; index < (((_a = element === null || element === void 0 ? void 0 : element.attributes) === null || _a === void 0 ? void 0 : _a.length) || 0); index++) {
25
+ const attribute = element.attributes.item(index);
26
+ if (local(attribute) !== name)
27
+ continue;
28
+ count++;
29
+ if (!attribute.namespaceURI)
30
+ value = attribute.value;
31
+ }
32
+ return { value, valid: count === 0 || (count === 1 && value !== undefined) };
33
+ };
34
+ const validMarkerSource = (anchor, name) => {
35
+ const point = child(anchor, name);
36
+ if (!point)
37
+ return true;
38
+ if (!soleChild(anchor, point, name, XDR))
39
+ return false;
40
+ return ['row', 'col', 'rowOff', 'colOff'].every(key => {
41
+ const field = child(point, key);
42
+ if (!soleChild(point, field, key, XDR))
43
+ return false;
44
+ for (let current = field.firstChild; current; current = current.nextSibling)
45
+ if (current.nodeType === 1)
46
+ return false;
47
+ return true;
48
+ });
49
+ };
50
+ export const validSourceReference = (anchor, reference, kind) => {
51
+ var _a;
52
+ const path = kind === 'image'
53
+ ? [['blip', MAIN], ['blipFill', XDR], ['pic', XDR]]
54
+ : [['chart', CHART], ['graphicData', MAIN], ['graphic', MAIN], ['graphicFrame', XDR]];
55
+ let current = reference;
56
+ for (const [name, namespace] of path) {
57
+ if (!soleChild(current === null || current === void 0 ? void 0 : current.parentNode, current, name, namespace))
58
+ return false;
59
+ if (name === 'graphicData') {
60
+ const uri = plainAttribute(current, 'uri');
61
+ if (!uri.valid || uri.value !== CHART)
62
+ return false;
63
+ }
64
+ current = current.parentNode;
65
+ }
66
+ if (current !== anchor)
67
+ return false;
68
+ const name = kind === 'image' ? 'embed' : 'id';
69
+ let matches = 0;
70
+ for (let index = 0; index < (((_a = reference === null || reference === void 0 ? void 0 : reference.attributes) === null || _a === void 0 ? void 0 : _a.length) || 0); index++) {
71
+ const attribute = reference.attributes.item(index);
72
+ if (local(attribute) !== name)
73
+ continue;
74
+ if (attribute.namespaceURI !== REL || !attribute.value)
75
+ return false;
76
+ matches++;
77
+ }
78
+ return matches === 1;
79
+ };
80
+ const marker = (node) => {
81
+ if (!node)
82
+ return undefined;
83
+ const raw = Object.fromEntries(['row', 'col', 'rowOff', 'colOff'].map(key => { var _a, _b; return [key, (_b = (_a = child(node, key)) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : null]; }));
84
+ const value = Object.fromEntries(Object.entries(raw).map(([key, text]) => [key, text === null || !text.trim() ? NaN : Number(text)]));
85
+ return { raw, value };
86
+ };
87
+ const validMarker = (point) => !!point && Number.isSafeInteger(point.row) && Number.isSafeInteger(point.col)
88
+ && point.row >= 0 && point.row < 1048576 && point.col >= 0 && point.col < 16384
89
+ && Number.isFinite(point.rowOff) && Number.isFinite(point.colOff);
90
+ export const readSourceAnchor = (element, reference, kind) => {
91
+ var _a, _b, _c;
92
+ const type = local(element), edit = plainAttribute(element, 'editAs'), editAs = edit.value;
93
+ const from = marker(child(element, 'from')), to = marker(child(element, 'to'));
94
+ const source = { status: 'valid', type, editAs, fromPresent: !!from, toPresent: !!to,
95
+ from: from === null || from === void 0 ? void 0 : from.value, to: to === null || to === void 0 ? void 0 : to.value, fromRaw: from === null || from === void 0 ? void 0 : from.raw, toRaw: to === null || to === void 0 ? void 0 : to.raw };
96
+ const ordered = validMarker(from === null || from === void 0 ? void 0 : from.value) && validMarker(to === null || to === void 0 ? void 0 : to.value)
97
+ && to.value.row >= from.value.row && to.value.col >= from.value.col;
98
+ const root = element === null || element === void 0 ? void 0 : element.parentNode;
99
+ if (!isElement(root, 'wsDr', XDR) || root !== ((_a = element === null || element === void 0 ? void 0 : element.ownerDocument) === null || _a === void 0 ? void 0 : _a.documentElement) || element.namespaceURI !== XDR)
100
+ source.reason = 'unknown-anchor-context';
101
+ else if (!validMarkerSource(element, 'from') || !validMarkerSource(element, 'to'))
102
+ source.reason = 'unknown-marker-source';
103
+ else if (!edit.valid)
104
+ source.reason = 'unknown-edit-as-attribute';
105
+ else if (kind && !validSourceReference(element, reference, kind))
106
+ source.reason = 'unknown-reference-source';
107
+ else if (!['oneCellAnchor', 'twoCellAnchor', 'absoluteAnchor'].includes(type))
108
+ source.reason = 'unknown-anchor-mode';
109
+ else if (editAs !== undefined && !['absolute', 'oneCell', 'twoCell'].includes(editAs))
110
+ source.reason = 'unknown-edit-as';
111
+ else if (type !== 'absoluteAnchor' && (!validMarker(from === null || from === void 0 ? void 0 : from.value) || (type === 'twoCellAnchor' && !ordered) || (to && !ordered)))
112
+ source.reason = 'invalid-anchor-endpoints';
113
+ const truncated = [];
114
+ for (const key of ['type', 'editAs'])
115
+ if ((((_b = source[key]) === null || _b === void 0 ? void 0 : _b.length) || 0) > 128) {
116
+ source[key] = source[key].slice(0, 128);
117
+ truncated.push(key);
118
+ }
119
+ for (const side of ['fromRaw', 'toRaw'])
120
+ for (const key of ['row', 'col', 'rowOff', 'colOff']) {
121
+ const value = (_c = source[side]) === null || _c === void 0 ? void 0 : _c[key];
122
+ if (value && value.length > 64) {
123
+ source[side][key] = value.slice(0, 64);
124
+ truncated.push(side + '.' + key);
125
+ }
126
+ }
127
+ if (truncated.length) {
128
+ source.truncatedFields = truncated;
129
+ source.reason = 'source-field-budget';
130
+ }
131
+ if (source.reason)
132
+ source.status = 'unknown';
133
+ return source;
134
+ };
135
+ export const unknownSourceAnchor = (reason) => ({ status: 'unknown', reason });
136
+ export const readImageSourceProvenance = (raw, images, drawingPresent = true) => {
137
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
138
+ const result = new Map();
139
+ const summary = { scope: 'raw-drawing-image-references', status: 'complete',
140
+ observedReferences: 0, representedReferences: 0, unrepresentedReferences: 0,
141
+ countsComplete: true, reasons: [], reasonsTruncated: false };
142
+ const incomplete = (reason, countsComplete = true) => {
143
+ summary.status = 'partial';
144
+ summary.countsComplete && (summary.countsComplete = countsComplete);
145
+ if (!summary.reasons.includes(reason)) {
146
+ if (summary.reasons.length < 8)
147
+ summary.reasons.push(reason);
148
+ else
149
+ summary.reasonsTruncated = true;
150
+ }
151
+ };
152
+ let matchingComplete = false;
153
+ const finish = () => {
154
+ summary.unrepresentedReferences = matchingComplete
155
+ ? summary.observedReferences - summary.representedReferences : 0;
156
+ if (summary.unrepresentedReferences)
157
+ incomplete('unrepresented-raw-image-source');
158
+ return { anchors: result, summary };
159
+ };
160
+ if (!drawingPresent && raw == null && images.length === 0) {
161
+ summary.status = 'absent';
162
+ return finish();
163
+ }
164
+ const fail = (reason) => {
165
+ incomplete(reason, false);
166
+ for (let index = 0; index < Math.min(images.length, 4096); index++)
167
+ result.set(images[index], unknownSourceAnchor(reason));
168
+ return finish();
169
+ };
170
+ if (images.length > 4096)
171
+ return fail('image-source-budget');
172
+ if (typeof raw !== 'string')
173
+ return fail('missing-raw-source');
174
+ if (raw.length > 8 * 1024 * 1024)
175
+ return fail('raw-source-budget');
176
+ let sourceBytes = 0;
177
+ for (let index = 0; index < raw.length; index++) {
178
+ const code = raw.charCodeAt(index);
179
+ if (code < 0x80)
180
+ sourceBytes++;
181
+ else if (code < 0x800)
182
+ sourceBytes += 2;
183
+ else if (code >= 0xd800 && code <= 0xdbff && index + 1 < raw.length && raw.charCodeAt(index + 1) >= 0xdc00 && raw.charCodeAt(index + 1) <= 0xdfff) {
184
+ sourceBytes += 4;
185
+ index++;
186
+ }
187
+ else
188
+ sourceBytes += 3;
189
+ if (sourceBytes > 8 * 1024 * 1024)
190
+ return fail('raw-source-byte-budget');
191
+ }
192
+ if (/<!DOCTYPE|<!ENTITY/i.test(raw))
193
+ return fail('unsupported-xml-declaration');
194
+ let markup = 0;
195
+ for (let offset = raw.indexOf('<'); offset !== -1; offset = raw.indexOf('<', offset + 1))
196
+ if (++markup > 65536)
197
+ return fail('raw-markup-budget');
198
+ let document, malformed = false;
199
+ try {
200
+ document = new DOMParser({ onError: () => { malformed = true; } }).parseFromString(raw, 'application/xml');
201
+ }
202
+ catch {
203
+ return fail('unreadable-raw-source');
204
+ }
205
+ if (malformed || !(document === null || document === void 0 ? void 0 : document.documentElement))
206
+ return fail('unreadable-raw-source');
207
+ if (!isElement(document.documentElement, 'wsDr', XDR))
208
+ return fail('unknown-drawing-root');
209
+ const markerKey = (point) => point ? ['row', 'col', 'rowOff', 'colOff'].map(key => typeof point[key] + ':' + String(point[key])).join(',') : 'absent';
210
+ const keyFor = (id, type, from, to) => JSON.stringify([id, type, markerKey(from), markerKey(to)]);
211
+ const candidates = new Map();
212
+ let anchors = 0, nodes = 0, entries = 0;
213
+ for (let anchor = document.documentElement.firstChild; anchor; anchor = anchor.nextSibling) {
214
+ if (anchor.nodeType !== 1)
215
+ continue;
216
+ if (++anchors > 8192)
217
+ return fail('source-anchor-budget');
218
+ const source = readSourceAnchor(anchor);
219
+ if (source.status !== 'valid')
220
+ incomplete('unknown-anchor-source', false);
221
+ let current = anchor.firstChild;
222
+ while (current) {
223
+ if (++nodes > 65536)
224
+ return fail('source-node-budget');
225
+ if (current.nodeType === 1 && ['pic', 'blipFill'].includes(local(current))) {
226
+ if (!isElement(current, local(current), XDR) || (local(current) === 'blipFill' && !child(current, 'blip'))
227
+ || (local(current) === 'pic' && !child(current, 'blipFill')))
228
+ incomplete('unclassified-image-container', false);
229
+ }
230
+ if (current.nodeType === 1 && local(current) === 'blip') {
231
+ if (entries >= 8192)
232
+ return fail('source-reference-budget');
233
+ entries++;
234
+ summary.observedReferences++;
235
+ const trusted = validSourceReference(anchor, current, 'image');
236
+ if (!trusted)
237
+ incomplete('unknown-image-reference', false);
238
+ const id = current.getAttributeNS(REL, 'embed');
239
+ if (!id)
240
+ incomplete('missing-image-relationship', false);
241
+ if (id) {
242
+ if (id.length > 256)
243
+ return fail('source-relationship-field-budget');
244
+ const key = keyFor(id, source.type, source.from, source.to), prior = candidates.get(key);
245
+ candidates.set(key, { source: trusted ? source : { ...source, status: 'unknown', reason: 'unknown-reference-source' }, count: ((prior === null || prior === void 0 ? void 0 : prior.count) || 0) + 1 });
246
+ }
247
+ }
248
+ if (current.firstChild)
249
+ current = current.firstChild;
250
+ else {
251
+ while (current && current !== anchor && !current.nextSibling)
252
+ current = current.parentNode;
253
+ current = !current || current === anchor ? null : current.nextSibling;
254
+ }
255
+ }
256
+ }
257
+ const vendorCounts = new Map();
258
+ for (const image of images) {
259
+ const key = keyFor(image.id, (_a = image.anchor) === null || _a === void 0 ? void 0 : _a.type, (_b = image.anchor) === null || _b === void 0 ? void 0 : _b.from, (_c = image.anchor) === null || _c === void 0 ? void 0 : _c.to);
260
+ vendorCounts.set(key, (vendorCounts.get(key) || 0) + 1);
261
+ }
262
+ for (let index = 0; index < images.length; index++) {
263
+ const image = images[index], key = keyFor(image.id, (_d = image.anchor) === null || _d === void 0 ? void 0 : _d.type, (_e = image.anchor) === null || _e === void 0 ? void 0 : _e.from, (_f = image.anchor) === null || _f === void 0 ? void 0 : _f.to), match = candidates.get(keyFor(image.id, (_g = image.anchor) === null || _g === void 0 ? void 0 : _g.type, (_h = image.anchor) === null || _h === void 0 ? void 0 : _h.from, (_j = image.anchor) === null || _j === void 0 ? void 0 : _j.to));
264
+ const relationship = image.rel;
265
+ if (((_k = relationship === null || relationship === void 0 ? void 0 : relationship.TargetMode) === null || _k === void 0 ? void 0 : _k.toLowerCase()) === 'external') {
266
+ incomplete('external-image-relationship');
267
+ result.set(image, unknownSourceAnchor('external-relationship'));
268
+ continue;
269
+ }
270
+ if (relationship && !relationship.Target)
271
+ incomplete('unresolved-image-relationship');
272
+ if ((match === null || match === void 0 ? void 0 : match.count) === 1 && vendorCounts.get(key) === 1)
273
+ summary.representedReferences++;
274
+ else
275
+ incomplete(match ? 'ambiguous-image-source' : 'unmatched-vendor-image');
276
+ result.set(image, (match === null || match === void 0 ? void 0 : match.count) === 1
277
+ ? vendorCounts.get(key) === 1 ? match.source : unknownSourceAnchor('ambiguous-vendor-match')
278
+ : unknownSourceAnchor(match ? 'ambiguous-source-match' : 'unmatched-source-anchor'));
279
+ }
280
+ for (const candidate of candidates.values())
281
+ if (candidate.count > 1)
282
+ incomplete('ambiguous-image-source');
283
+ matchingComplete = true;
284
+ return finish();
285
+ };
286
+ export const readImageSourceAnchors = (raw, images) => readImageSourceProvenance(raw, images).anchors;
@@ -1,4 +1,4 @@
1
- import { type ResolvedFileViewerTextEncoding } from '@file-viewer/core';
1
+ import { type ResolvedFileViewerTextEncoding } from '@file-viewer/core/headless';
2
2
  export type SpreadsheetTextEncoding = 'auto' | 'utf-8' | 'gbk' | 'gb18030';
3
3
  export interface SpreadsheetTextSource {
4
4
  fileType?: string;
@@ -1,4 +1,4 @@
1
- import { decodeFileViewerTextBuffer, isValidFileViewerUtf8, resolveFileViewerTextEncoding, } from '@file-viewer/core';
1
+ import { decodeFileViewerTextBuffer, isValidFileViewerUtf8, resolveFileViewerTextEncoding, } from '@file-viewer/core/headless';
2
2
  const TEXT_SPREADSHEET_EXTENSIONS = new Set(['csv', 'tsv']);
3
3
  const TEXT_SPREADSHEET_MIME_TYPES = new Set([
4
4
  'text/csv',
@@ -32,12 +32,6 @@ const LEGACY_SPREADSHEET_TYPES = new Set([
32
32
  ]);
33
33
  const OLE_SIGNATURE = [0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1];
34
34
  const TEXT_PROBE_BYTES = 65536;
35
- /**
36
- * Legacy Excel exports can contain delimited text or HTML rather than BIFF.
37
- * Passing those bytes directly to the workbook reader selects its single-byte
38
- * fallback and corrupts UTF-8/GBK values. Sniff only legacy spreadsheet inputs;
39
- * preserve real binary signatures and let the shared decoder select encoding.
40
- */
41
35
  const isTextBackedLegacySpreadsheet = (data, source) => {
42
36
  const type = normalizeFileType(source.fileType) || getFilenameExtension(source.filename);
43
37
  if (!LEGACY_SPREADSHEET_TYPES.has(type) || !data.byteLength)
@@ -47,7 +41,6 @@ const isTextBackedLegacySpreadsheet = (data, source) => {
47
41
  return false;
48
42
  if (bytes[0] === 0x50 && bytes[1] === 0x4b)
49
43
  return false;
50
- // Raw BIFF2/3/4/5/8 BOF records need not be wrapped in an OLE container.
51
44
  if (bytes[0] === 0x09 && [0x00, 0x02, 0x04, 0x08].includes(bytes[1]))
52
45
  return false;
53
46
  const encoding = resolveFileViewerTextEncoding(bytes).encoding;
@@ -0,0 +1,4 @@
1
+ import type { CellStyle, WorkBook } from 'styled-exceljs';
2
+ type Font = NonNullable<CellStyle['font']>;
3
+ export declare const readWorkbookDefaultFont: (workbook?: WorkBook | null) => Font | undefined;
4
+ export {};
@@ -0,0 +1,15 @@
1
+ export const readWorkbookDefaultFont = (workbook) => {
2
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
3
+ const styles = workbook === null || workbook === void 0 ? void 0 : workbook.Styles;
4
+ if (!styles)
5
+ return undefined;
6
+ const format = (_a = styles.CellXf) === null || _a === void 0 ? void 0 : _a[0];
7
+ const normal = (_c = (_b = styles.CellStyles) === null || _b === void 0 ? void 0 : _b.find(style => style.builtinId === 0)) !== null && _c !== void 0 ? _c : (_d = styles.CellStyles) === null || _d === void 0 ? void 0 : _d.find(style => { var _a; return ((_a = style.name) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'normal'; });
8
+ const parentId = (_f = (_e = format === null || format === void 0 ? void 0 : format.xfId) !== null && _e !== void 0 ? _e : normal === null || normal === void 0 ? void 0 : normal.xfId) !== null && _f !== void 0 ? _f : 0;
9
+ const parent = (_g = styles.CellStyleXf) === null || _g === void 0 ? void 0 : _g[parentId];
10
+ const fontId = (_j = (_h = format === null || format === void 0 ? void 0 : format.fontId) !== null && _h !== void 0 ? _h : parent === null || parent === void 0 ? void 0 : parent.fontId) !== null && _j !== void 0 ? _j : 0;
11
+ if (!Number.isSafeInteger(fontId) || fontId < 0)
12
+ return undefined;
13
+ const font = (_k = styles.Fonts) === null || _k === void 0 ? void 0 : _k[fontId];
14
+ return font ? { ...font } : undefined;
15
+ };
@@ -0,0 +1,11 @@
1
+ import type JSZip from 'jszip';
2
+ declare module 'jszip' {
3
+ interface JSZipObject {
4
+ internalStream(type: 'uint8array'): JSZip.JSZipStreamHelper<Uint8Array>;
5
+ }
6
+ }
7
+ export interface XmlReadBudget {
8
+ maxPartBytes: number;
9
+ remainingBytes: number;
10
+ }
11
+ export declare const readBoundedZipText: (file: JSZip.JSZipObject, budget: XmlReadBudget) => Promise<string>;