@deepseek-ai/libreoffice-kit 0.0.2-rc4 → 0.0.2-rc5
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/NOTICE +238 -0
- package/README.md +3 -3
- package/README.zh.md +3 -3
- package/lib/browser/assets.json +2 -2
- package/lib/browser/index.js +65 -58
- package/lib/browser/types/connection.d.ts +3 -3
- package/lib/browser/types/index.d.ts +2 -2
- package/lib/browser/types/office-engine.d.ts +46 -0
- package/lib/browser/types/office-types.d.ts +107 -0
- package/lib/browser/types/office.d.ts +4 -0
- package/lib/browser/types/protocol.d.ts +24 -32
- package/lib/browser/types/types.d.ts +3 -1
- package/lib/browser/worker.js +276 -489
- package/lib/browser-assets.js +1 -1
- package/lib/cli.js +91 -58
- package/lib/font-source.js +89 -56
- package/lib/font-worker.js +710 -39
- package/lib/index.js +91 -58
- package/lib/types/default-fonts.d.ts +3 -0
- package/lib/types/engine.d.ts +1 -1
- package/lib/types/font-preferences-data.d.ts +16 -0
- package/lib/types/font-preferences.d.ts +29 -0
- package/lib/types/font-source-types.d.ts +2 -0
- package/lib/types/fonts.d.ts +11 -8
- package/lib/types/options.d.ts +1 -1
- package/lib/worker.js +707 -37
- package/package.json +3 -3
- package/sources/browser/packages/browser/package.json +2 -2
- package/sources/browser/packages/browser/src/connection.ts +7 -9
- package/sources/browser/packages/browser/src/index.ts +3 -3
- package/sources/browser/packages/browser/src/office-engine.ts +299 -0
- package/sources/browser/packages/browser/src/office-types.ts +77 -0
- package/sources/browser/packages/browser/src/office.ts +62 -0
- package/sources/browser/packages/browser/src/protocol.ts +15 -16
- package/sources/browser/packages/browser/src/types.ts +3 -1
- package/sources/browser/packages/browser/src/worker.ts +25 -30
- package/sources/browser/scripts/build-browser.mjs +9 -1
- package/lib/browser/types/editor-engine.d.ts +0 -50
- package/lib/browser/types/editor-types.d.ts +0 -141
- package/lib/browser/types/editor.d.ts +0 -9
- package/sources/browser/packages/browser/src/editor-engine.ts +0 -382
- package/sources/browser/packages/browser/src/editor-types.ts +0 -106
- package/sources/browser/packages/browser/src/editor.ts +0 -55
package/lib/browser/worker.js
CHANGED
|
@@ -2557,14 +2557,6 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
2557
2557
|
//#region ../entry/lib/sheet-geometry.js
|
|
2558
2558
|
const MAX_COLUMN = 16383;
|
|
2559
2559
|
const MAX_ROW = 1048575;
|
|
2560
|
-
function column(index) {
|
|
2561
|
-
let name = "";
|
|
2562
|
-
for (let value = index + 1; value > 0; value = Math.floor((value - 1) / 26)) name = String.fromCharCode(65 + (value - 1) % 26) + name;
|
|
2563
|
-
return name;
|
|
2564
|
-
}
|
|
2565
|
-
function rangeName(range) {
|
|
2566
|
-
return `${column(range.firstColumn)}${range.firstRow + 1}:${column(range.lastColumn)}${range.lastRow + 1}`;
|
|
2567
|
-
}
|
|
2568
2560
|
function parseCellRange(value) {
|
|
2569
2561
|
const match = /^\$?([A-Z]{1,3})\$?([1-9]\d{0,6})(?::\$?([A-Z]{1,3})\$?([1-9]\d{0,6}))?$/i.exec(value);
|
|
2570
2562
|
if (!match) throw new TypeError("range must be an A1 cell or rectangle without a sheet prefix.");
|
|
@@ -2581,92 +2573,10 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
2581
2573
|
lastRow
|
|
2582
2574
|
};
|
|
2583
2575
|
}
|
|
2584
|
-
function sheetPartInfo(value) {
|
|
2585
|
-
const info = JSON.parse(value);
|
|
2586
|
-
if (!info || typeof info.name !== "string" || ![0, 1].includes(info.visible) || ![0, 1].includes(info.rtllayout) || !Number.isInteger(info.lastcolumn) || !Number.isInteger(info.lastrow) || info.lastcolumn < 0 || info.lastrow < 0 || info.lastcolumn > MAX_COLUMN || info.lastrow > MAX_ROW) throw new Error("LibreOffice returned invalid worksheet data-area metadata.");
|
|
2587
|
-
return info;
|
|
2588
|
-
}
|
|
2589
|
-
/** Formatting-only cells and standalone drawings do not enlarge LibreOffice's data area. */
|
|
2590
|
-
function dataArea(info) {
|
|
2591
|
-
return {
|
|
2592
|
-
firstColumn: 0,
|
|
2593
|
-
firstRow: 0,
|
|
2594
|
-
lastColumn: info.lastcolumn,
|
|
2595
|
-
lastRow: info.lastrow
|
|
2596
|
-
};
|
|
2597
|
-
}
|
|
2598
|
-
function spans(encoded, flags, maximum) {
|
|
2599
|
-
if (typeof encoded !== "string" || encoded.length > 33554432) throw new Error("Invalid worksheet geometry span encoding.");
|
|
2600
|
-
const tokens = encoded.trim().split(/\s+/);
|
|
2601
|
-
let previous = -1;
|
|
2602
|
-
let flag = 0;
|
|
2603
|
-
const result = tokens.map((token, i) => {
|
|
2604
|
-
const fields = token.split(":");
|
|
2605
|
-
let value;
|
|
2606
|
-
let end;
|
|
2607
|
-
if (flags && i > 0) {
|
|
2608
|
-
value = flag = 1 - flag;
|
|
2609
|
-
end = Number(fields[0]);
|
|
2610
|
-
if (fields.length !== 1) throw new Error("Invalid worksheet flag span.");
|
|
2611
|
-
} else {
|
|
2612
|
-
value = Number(fields[0]);
|
|
2613
|
-
end = Number(fields[1]);
|
|
2614
|
-
flag = value;
|
|
2615
|
-
if (fields.length !== 2) throw new Error("Invalid worksheet size span.");
|
|
2616
|
-
}
|
|
2617
|
-
if (!Number.isSafeInteger(value) || value < 0 || (flags ? value > 1 : value > 65535) || !Number.isSafeInteger(end) || end <= previous || end > maximum) throw new Error("Invalid worksheet geometry span.");
|
|
2618
|
-
previous = end;
|
|
2619
|
-
return {
|
|
2620
|
-
end,
|
|
2621
|
-
value
|
|
2622
|
-
};
|
|
2623
|
-
});
|
|
2624
|
-
if (previous !== maximum) throw new Error("Worksheet geometry does not cover the complete dimension.");
|
|
2625
|
-
return result;
|
|
2626
|
-
}
|
|
2627
|
-
function dimension(data, first, last, maximum, scale) {
|
|
2628
|
-
const sizes = spans(data?.sizes, false, maximum);
|
|
2629
|
-
const hidden = spans(data?.hidden, true, maximum);
|
|
2630
|
-
const filtered = spans(data?.filtered, true, maximum);
|
|
2631
|
-
let position = 0, start = 0, index = 0, si = 0, hi = 0, fi = 0;
|
|
2632
|
-
while (index <= last) {
|
|
2633
|
-
const size = sizes[si], hide = hidden[hi], filter = filtered[fi];
|
|
2634
|
-
const end = Math.min(size.end, hide.end, filter.end, last, index < first ? first - 1 : last);
|
|
2635
|
-
const pixels = hide.value || filter.value || size.value === 0 ? 0 : Math.max(1, Math.floor(size.value * scale / 15));
|
|
2636
|
-
if (index === first) start = position;
|
|
2637
|
-
position += (end - index + 1) * pixels;
|
|
2638
|
-
index = end + 1;
|
|
2639
|
-
if (index > size.end) si++;
|
|
2640
|
-
if (index > hide.end) hi++;
|
|
2641
|
-
if (index > filter.end) fi++;
|
|
2642
|
-
}
|
|
2643
|
-
return [start / scale, (position - start) / scale];
|
|
2644
|
-
}
|
|
2645
|
-
/** Logical columns advance from A; LOK itself mirrors RTL sheet painting. No manual x inversion. */
|
|
2646
|
-
function sheetRangeRectangle(geometry, range, scale) {
|
|
2647
|
-
if (!Number.isFinite(scale) || scale <= 0 || scale > 16) throw new TypeError("Invalid worksheet raster scale.");
|
|
2648
|
-
const [x, width] = dimension(geometry.columns, range.firstColumn, range.lastColumn, MAX_COLUMN, scale);
|
|
2649
|
-
const [y, height] = dimension(geometry.rows, range.firstRow, range.lastRow, MAX_ROW, scale);
|
|
2650
|
-
if (width <= 0 || height <= 0) throw new Error("The selected worksheet range is entirely hidden or filtered.");
|
|
2651
|
-
if ([
|
|
2652
|
-
x,
|
|
2653
|
-
y,
|
|
2654
|
-
width,
|
|
2655
|
-
height,
|
|
2656
|
-
x + width,
|
|
2657
|
-
y + height
|
|
2658
|
-
].some((value) => !Number.isFinite(value) || value * 15 > 2147483647)) throw new Error("The selected worksheet range exceeds the supported coordinate range.");
|
|
2659
|
-
return {
|
|
2660
|
-
x,
|
|
2661
|
-
y,
|
|
2662
|
-
width,
|
|
2663
|
-
height
|
|
2664
|
-
};
|
|
2665
|
-
}
|
|
2666
2576
|
//#endregion
|
|
2667
2577
|
//#region ../entry/lib/engine-rendering.js
|
|
2668
2578
|
/** Validate coordinates before allocation or wasm32 integer conversion. */
|
|
2669
|
-
function tileDimensions
|
|
2579
|
+
function tileDimensions(request, pages) {
|
|
2670
2580
|
const page = pages[request.pageIndex];
|
|
2671
2581
|
if (!Number.isSafeInteger(request.pageIndex) || !page || ![
|
|
2672
2582
|
request.x,
|
|
@@ -2712,7 +2622,7 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
2712
2622
|
/** Paint into a temporary allocation; copy pixels before free or another engine call. */
|
|
2713
2623
|
function renderRegion(module, document, request, pages, failure, pdf = false) {
|
|
2714
2624
|
const call = (name, args) => numericCall(module, name, args);
|
|
2715
|
-
const { page, width, height } = tileDimensions
|
|
2625
|
+
const { page, width, height } = tileDimensions(request, pages);
|
|
2716
2626
|
const size = width * height * 4;
|
|
2717
2627
|
const pointer = call("malloc", [size]);
|
|
2718
2628
|
if (!pointer) throw failure();
|
|
@@ -2790,35 +2700,9 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
2790
2700
|
if (pages.length === 0) throw new Error("LibreOffice returned no document pages.");
|
|
2791
2701
|
return pages;
|
|
2792
2702
|
}
|
|
2793
|
-
/** Validate coordinates before allocation or wasm32 integer conversion. */
|
|
2794
|
-
function tileDimensions(request, pages) {
|
|
2795
|
-
const page = pages[request.pageIndex];
|
|
2796
|
-
if (!Number.isSafeInteger(request.pageIndex) || !page || ![
|
|
2797
|
-
request.x,
|
|
2798
|
-
request.y,
|
|
2799
|
-
request.width,
|
|
2800
|
-
request.height,
|
|
2801
|
-
request.scale
|
|
2802
|
-
].every(Number.isFinite) || request.x < 0 || request.y < 0 || request.width <= 0 || request.height <= 0 || request.scale <= 0 || request.x + request.width > page.width + .001 || request.y + request.height > page.height + .001) throw new Error("Tile rectangle lies outside its page.");
|
|
2803
|
-
const width = Math.ceil(request.width * request.scale - 1e-9);
|
|
2804
|
-
const height = Math.ceil(request.height * request.scale - 1e-9);
|
|
2805
|
-
if (width <= 0 || height <= 0 || !Number.isSafeInteger(width * height) || width * height > 16777216) throw new Error("Tile exceeds the 16 megapixel allocation limit.");
|
|
2806
|
-
const twips = [
|
|
2807
|
-
page.x + Math.round(request.x * 15),
|
|
2808
|
-
page.y + Math.round(request.y * 15),
|
|
2809
|
-
Math.round(request.width * 15),
|
|
2810
|
-
Math.round(request.height * 15)
|
|
2811
|
-
];
|
|
2812
|
-
if (twips.some((value) => !Number.isSafeInteger(value) || value < 0 || value > 2147483647) || twips[0] + twips[2] > 2147483647 || twips[1] + twips[3] > 2147483647) throw new Error("Tile coordinates exceed the wasm32 range.");
|
|
2813
|
-
return {
|
|
2814
|
-
page,
|
|
2815
|
-
width,
|
|
2816
|
-
height
|
|
2817
|
-
};
|
|
2818
|
-
}
|
|
2819
2703
|
//#endregion
|
|
2820
|
-
//#region lib/types/
|
|
2821
|
-
/** Worker-owned
|
|
2704
|
+
//#region lib/types/office-engine.js
|
|
2705
|
+
/** Worker-owned reading, bounded VCL event slices and immutable notifications. */
|
|
2822
2706
|
function rectangle(payload) {
|
|
2823
2707
|
if (payload.startsWith("{")) {
|
|
2824
2708
|
const value = JSON.parse(payload);
|
|
@@ -2844,46 +2728,48 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
2844
2728
|
if (!Number.isFinite(value) || Math.abs(value * 15) > 2147483647) throw new BrowserRenderError("render-failed", "Office input lies outside document coordinates.");
|
|
2845
2729
|
return Math.round(value * 15);
|
|
2846
2730
|
}
|
|
2847
|
-
|
|
2848
|
-
|
|
2731
|
+
const navigationKeys = {
|
|
2732
|
+
down: 1024,
|
|
2733
|
+
up: 1025,
|
|
2734
|
+
left: 1026,
|
|
2735
|
+
right: 1027,
|
|
2736
|
+
home: 1028,
|
|
2737
|
+
end: 1029,
|
|
2738
|
+
pageUp: 1030,
|
|
2739
|
+
pageDown: 1031
|
|
2740
|
+
};
|
|
2741
|
+
/** One read-only document, driven exclusively by its Worker's messages and timer. */
|
|
2742
|
+
var OfficeEngine = class {
|
|
2849
2743
|
module;
|
|
2850
2744
|
document;
|
|
2851
|
-
format;
|
|
2852
|
-
maxBytes;
|
|
2853
2745
|
emit;
|
|
2854
2746
|
failure;
|
|
2855
|
-
readOnly;
|
|
2856
2747
|
state;
|
|
2857
2748
|
timer;
|
|
2858
2749
|
callbacks = [];
|
|
2859
|
-
phase = "view";
|
|
2860
|
-
modified = false;
|
|
2861
2750
|
geometryDirty = false;
|
|
2862
2751
|
stopped = false;
|
|
2863
|
-
|
|
2752
|
+
layoutWidth = 0;
|
|
2753
|
+
viewport = "";
|
|
2864
2754
|
commandResult;
|
|
2865
|
-
constructor(module, document, format,
|
|
2755
|
+
constructor(module, document, format, emit, failure) {
|
|
2866
2756
|
this.module = module;
|
|
2867
2757
|
this.document = document;
|
|
2868
|
-
this.format = format;
|
|
2869
|
-
this.maxBytes = maxBytes;
|
|
2870
2758
|
this.emit = emit;
|
|
2871
2759
|
this.failure = failure;
|
|
2872
|
-
this.readOnly = readOnly;
|
|
2873
2760
|
this.state = {
|
|
2874
2761
|
documentType: format === "docx" || format === "doc" ? "text" : format === "xlsx" || format === "xls" ? "spreadsheet" : "presentation",
|
|
2875
|
-
revision: 0,
|
|
2876
2762
|
renderGeneration: 0,
|
|
2763
|
+
layoutGeneration: 0,
|
|
2764
|
+
layout: "paginated",
|
|
2877
2765
|
part: 0,
|
|
2878
2766
|
parts: [],
|
|
2879
2767
|
pages: [],
|
|
2880
2768
|
cursor: null,
|
|
2881
|
-
cursorVisible: false,
|
|
2882
2769
|
selection: [],
|
|
2883
2770
|
graphicSelection: null,
|
|
2884
2771
|
cellAddress: "",
|
|
2885
|
-
cellFormula: ""
|
|
2886
|
-
commands: {}
|
|
2772
|
+
cellFormula: ""
|
|
2887
2773
|
};
|
|
2888
2774
|
}
|
|
2889
2775
|
call(name, args = []) {
|
|
@@ -2902,32 +2788,27 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
2902
2788
|
if (!this.call(`dsh_lok_document_${name}`, [this.document, ...args])) throw new BrowserRenderError("render-failed", `LibreOffice could not complete ${name}.`);
|
|
2903
2789
|
}
|
|
2904
2790
|
requireOpen() {
|
|
2905
|
-
if (this.stopped) throw new BrowserRenderError("disposed", "Office
|
|
2791
|
+
if (this.stopped) throw new BrowserRenderError("disposed", "Office document is closed.");
|
|
2906
2792
|
}
|
|
2907
|
-
/** Collect callbacks without reentering LibreOffice
|
|
2793
|
+
/** Collect callbacks without reentering LibreOffice inside its callback. */
|
|
2908
2794
|
callback(type, payload) {
|
|
2909
2795
|
if (!this.stopped) this.callbacks.push({
|
|
2910
2796
|
type,
|
|
2911
|
-
payload
|
|
2912
|
-
phase: this.phase
|
|
2797
|
+
payload
|
|
2913
2798
|
});
|
|
2914
2799
|
}
|
|
2915
|
-
/**
|
|
2800
|
+
/** Enforce the native read-only view before accepting any input. */
|
|
2916
2801
|
async start() {
|
|
2917
2802
|
this.requireOpen();
|
|
2803
|
+
this.configure({ mode: "paginated" });
|
|
2918
2804
|
this.invoke("listen");
|
|
2919
2805
|
this.refreshGeometry();
|
|
2920
2806
|
this.pump();
|
|
2921
2807
|
this.schedule();
|
|
2922
2808
|
await this.command(".uno:ReportWhenIdle", { idleID: {
|
|
2923
2809
|
type: "string",
|
|
2924
|
-
value: "
|
|
2810
|
+
value: "office-open"
|
|
2925
2811
|
} });
|
|
2926
|
-
this.state = {
|
|
2927
|
-
...this.state,
|
|
2928
|
-
revision: 0
|
|
2929
|
-
};
|
|
2930
|
-
this.phase = "idle";
|
|
2931
2812
|
}
|
|
2932
2813
|
schedule() {
|
|
2933
2814
|
this.timer = setTimeout(() => {
|
|
@@ -2941,23 +2822,18 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
2941
2822
|
}
|
|
2942
2823
|
}, 16);
|
|
2943
2824
|
}
|
|
2944
|
-
/**
|
|
2825
|
+
/** Stop event processing before destroying the document. */
|
|
2945
2826
|
stop() {
|
|
2946
2827
|
this.stopped = true;
|
|
2947
2828
|
clearTimeout(this.timer);
|
|
2948
2829
|
this.callbacks.length = 0;
|
|
2949
|
-
this.commandResult?.reject(new BrowserRenderError("disposed", "Office
|
|
2830
|
+
this.commandResult?.reject(new BrowserRenderError("disposed", "Office document is closed."));
|
|
2950
2831
|
this.commandResult = void 0;
|
|
2951
|
-
if (this.renderView >= 0) {
|
|
2952
|
-
this.call("dsh_lok_document_destroy_view", [this.document, this.renderView]);
|
|
2953
|
-
this.renderView = -1;
|
|
2954
|
-
}
|
|
2955
2832
|
}
|
|
2833
|
+
/** Return the part whose cached coordinates changed, or -1 when part identities changed. */
|
|
2956
2834
|
refreshGeometry() {
|
|
2957
|
-
const phase = this.phase;
|
|
2958
|
-
this.phase = "view";
|
|
2959
2835
|
const count = this.state.documentType === "text" ? 1 : this.call("dsh_lok_document_parts", [this.document]);
|
|
2960
|
-
if (!Number.isSafeInteger(count) || count <= 0) throw new BrowserRenderError("invalid-document", "Office document has no
|
|
2836
|
+
if (!Number.isSafeInteger(count) || count <= 0) throw new BrowserRenderError("invalid-document", "Office document has no parts.");
|
|
2961
2837
|
const pointer = this.call("malloc", [8]);
|
|
2962
2838
|
if (!pointer) throw new BrowserRenderError("render-failed", "Office geometry allocation failed.");
|
|
2963
2839
|
try {
|
|
@@ -2975,44 +2851,47 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
2975
2851
|
width: index === part ? width : this.state.parts[index]?.width ?? width,
|
|
2976
2852
|
height: index === part ? height : this.state.parts[index]?.height ?? height
|
|
2977
2853
|
}));
|
|
2978
|
-
const pages = this.state.documentType
|
|
2854
|
+
const pages = this.state.documentType !== "text" ? [] : this.state.layout === "continuous" ? [{
|
|
2855
|
+
x: 0,
|
|
2856
|
+
y: 0,
|
|
2857
|
+
width,
|
|
2858
|
+
height
|
|
2859
|
+
}] : writerPages(this.text("dsh_lok_document_page_rectangles", [this.document])).map((page) => ({
|
|
2979
2860
|
x: page.x / 15,
|
|
2980
2861
|
y: page.y / 15,
|
|
2981
2862
|
width: page.width,
|
|
2982
2863
|
height: page.height
|
|
2983
|
-
}))
|
|
2864
|
+
}));
|
|
2865
|
+
const previous = this.state;
|
|
2866
|
+
const identitiesChanged = previous.parts.length > 0 && (previous.parts.length !== parts.length || parts.some((value, index) => value.name !== previous.parts[index].name));
|
|
2867
|
+
const measured = previous.parts[part];
|
|
2868
|
+
const geometryChanged = measured !== void 0 && (measured.width !== width || measured.height !== height || JSON.stringify(previous.pages) !== JSON.stringify(pages));
|
|
2984
2869
|
this.state = {
|
|
2985
|
-
...
|
|
2870
|
+
...previous,
|
|
2986
2871
|
part,
|
|
2987
2872
|
parts,
|
|
2988
2873
|
pages
|
|
2989
2874
|
};
|
|
2875
|
+
this.geometryDirty = false;
|
|
2876
|
+
return identitiesChanged ? -1 : geometryChanged ? part : void 0;
|
|
2990
2877
|
} finally {
|
|
2991
2878
|
this.call("free", [pointer]);
|
|
2992
|
-
this.phase = phase;
|
|
2993
2879
|
}
|
|
2994
|
-
this.geometryDirty = false;
|
|
2995
2880
|
}
|
|
2996
2881
|
pump() {
|
|
2997
2882
|
const deadline = performance.now() + 8;
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
if (result < 0) throw new BrowserRenderError("render-failed", "LibreOffice event processing failed.");
|
|
3003
|
-
if (!result || performance.now() >= deadline) break;
|
|
3004
|
-
}
|
|
3005
|
-
} finally {
|
|
3006
|
-
if (view >= 0 && !this.call("dsh_lok_document_set_view", [this.document, view])) throw new BrowserRenderError("render-failed", "The editor view could not be restored after event processing.");
|
|
2883
|
+
for (let pass = 0; pass < 8; pass++) {
|
|
2884
|
+
const result = this.call("dsh_lok_pump");
|
|
2885
|
+
if (result < 0) throw new BrowserRenderError("render-failed", "LibreOffice event processing failed.");
|
|
2886
|
+
if (!result || performance.now() >= deadline) break;
|
|
3007
2887
|
}
|
|
3008
2888
|
this.flush();
|
|
3009
2889
|
}
|
|
3010
2890
|
flush() {
|
|
3011
2891
|
if (this.callbacks.length === 0) return;
|
|
3012
2892
|
let changed = false;
|
|
3013
|
-
let edited = false;
|
|
3014
2893
|
const invalidations = [];
|
|
3015
|
-
for (const { type, payload
|
|
2894
|
+
for (const { type, payload } of this.callbacks.splice(0)) switch (type) {
|
|
3016
2895
|
case 0: {
|
|
3017
2896
|
const fields = payload.split(",");
|
|
3018
2897
|
const index = fields[0]?.trim() === "EMPTY" ? fields[1] : fields[4];
|
|
@@ -3021,15 +2900,10 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3021
2900
|
part,
|
|
3022
2901
|
rectangle: rectangle(payload)
|
|
3023
2902
|
});
|
|
3024
|
-
edited ||= ![
|
|
3025
|
-
"paint",
|
|
3026
|
-
"view",
|
|
3027
|
-
"save",
|
|
3028
|
-
"capture"
|
|
3029
|
-
].includes(phase);
|
|
3030
2903
|
break;
|
|
3031
2904
|
}
|
|
3032
2905
|
case 1:
|
|
2906
|
+
case 17:
|
|
3033
2907
|
this.state = {
|
|
3034
2908
|
...this.state,
|
|
3035
2909
|
cursor: rectangle(payload)
|
|
@@ -3043,13 +2917,6 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3043
2917
|
};
|
|
3044
2918
|
changed = true;
|
|
3045
2919
|
break;
|
|
3046
|
-
case 5:
|
|
3047
|
-
this.state = {
|
|
3048
|
-
...this.state,
|
|
3049
|
-
cursorVisible: payload === "true"
|
|
3050
|
-
};
|
|
3051
|
-
changed = true;
|
|
3052
|
-
break;
|
|
3053
2920
|
case 6:
|
|
3054
2921
|
this.state = {
|
|
3055
2922
|
...this.state,
|
|
@@ -3057,51 +2924,27 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3057
2924
|
};
|
|
3058
2925
|
changed = true;
|
|
3059
2926
|
break;
|
|
3060
|
-
case 8: {
|
|
3061
|
-
const separator = payload.indexOf("=");
|
|
3062
|
-
if (separator < 0) break;
|
|
3063
|
-
const command = payload.slice(0, separator), value = payload.slice(separator + 1);
|
|
3064
|
-
if (command === ".uno:ModifiedStatus") {
|
|
3065
|
-
this.modified = value === "true";
|
|
3066
|
-
edited ||= this.modified;
|
|
3067
|
-
}
|
|
3068
|
-
this.state = {
|
|
3069
|
-
...this.state,
|
|
3070
|
-
commands: {
|
|
3071
|
-
...this.state.commands,
|
|
3072
|
-
[command]: value
|
|
3073
|
-
}
|
|
3074
|
-
};
|
|
3075
|
-
changed = true;
|
|
3076
|
-
break;
|
|
3077
|
-
}
|
|
3078
2927
|
case 13:
|
|
3079
2928
|
this.geometryDirty = true;
|
|
3080
|
-
changed = true;
|
|
3081
2929
|
break;
|
|
3082
2930
|
case 14: {
|
|
3083
2931
|
const part = Number(payload);
|
|
3084
|
-
if (Number.isSafeInteger(part) && part >= 0 && part < this.state.parts.length)
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
2932
|
+
if (Number.isSafeInteger(part) && part >= 0 && part < this.state.parts.length) {
|
|
2933
|
+
this.state = {
|
|
2934
|
+
...this.state,
|
|
2935
|
+
part
|
|
2936
|
+
};
|
|
2937
|
+
this.viewport = "";
|
|
2938
|
+
this.geometryDirty = true;
|
|
2939
|
+
changed = true;
|
|
2940
|
+
}
|
|
3089
2941
|
break;
|
|
3090
2942
|
}
|
|
3091
2943
|
case 16: {
|
|
3092
2944
|
const result = JSON.parse(payload);
|
|
3093
|
-
if (typeof result
|
|
3094
|
-
this.commandResult.resolve();
|
|
2945
|
+
if (typeof result === "object" && result !== null && "commandName" in result && result.commandName === this.commandResult?.name) this.commandResult?.resolve();
|
|
3095
2946
|
break;
|
|
3096
2947
|
}
|
|
3097
|
-
case 17:
|
|
3098
|
-
this.state = {
|
|
3099
|
-
...this.state,
|
|
3100
|
-
cursor: rectangle(payload),
|
|
3101
|
-
cursorVisible: true
|
|
3102
|
-
};
|
|
3103
|
-
changed = true;
|
|
3104
|
-
break;
|
|
3105
2948
|
case 19:
|
|
3106
2949
|
this.state = {
|
|
3107
2950
|
...this.state,
|
|
@@ -3116,21 +2959,27 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3116
2959
|
};
|
|
3117
2960
|
changed = true;
|
|
3118
2961
|
}
|
|
3119
|
-
|
|
2962
|
+
const geometry = this.geometryDirty ? this.refreshGeometry() : void 0;
|
|
2963
|
+
if (geometry !== void 0) {
|
|
3120
2964
|
this.state = {
|
|
3121
2965
|
...this.state,
|
|
3122
|
-
|
|
2966
|
+
layoutGeneration: this.state.layoutGeneration + 1
|
|
3123
2967
|
};
|
|
3124
|
-
|
|
2968
|
+
if (!invalidations.some((value) => value.rectangle === null && (value.part === -1 || value.part === geometry))) {
|
|
2969
|
+
for (let index = invalidations.length - 1; index >= 0; index--) if (geometry === -1 || invalidations[index].part === geometry) invalidations.splice(index, 1);
|
|
2970
|
+
invalidations.push({
|
|
2971
|
+
part: geometry,
|
|
2972
|
+
rectangle: null
|
|
2973
|
+
});
|
|
2974
|
+
}
|
|
3125
2975
|
}
|
|
3126
|
-
if (invalidations.length > 0
|
|
2976
|
+
if (invalidations.length > 0) {
|
|
3127
2977
|
this.state = {
|
|
3128
2978
|
...this.state,
|
|
3129
2979
|
renderGeneration: this.state.renderGeneration + 1
|
|
3130
2980
|
};
|
|
3131
2981
|
changed = true;
|
|
3132
2982
|
}
|
|
3133
|
-
if (this.geometryDirty) this.refreshGeometry();
|
|
3134
2983
|
if (changed) this.emit({
|
|
3135
2984
|
type: "state",
|
|
3136
2985
|
state: this.state
|
|
@@ -3138,109 +2987,190 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3138
2987
|
for (const invalidation of invalidations) this.emit({
|
|
3139
2988
|
type: "invalidate",
|
|
3140
2989
|
...invalidation,
|
|
3141
|
-
|
|
2990
|
+
generation: this.state.renderGeneration
|
|
3142
2991
|
});
|
|
3143
2992
|
}
|
|
3144
|
-
/**
|
|
2993
|
+
/** The Worker permits only these typed reading operations, including at runtime. */
|
|
3145
2994
|
async operation(operation) {
|
|
3146
2995
|
this.requireOpen();
|
|
3147
|
-
if (
|
|
3148
|
-
|
|
3149
|
-
"
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
cursor: null,
|
|
3197
|
-
selection: [],
|
|
3198
|
-
graphicSelection: null
|
|
3199
|
-
};
|
|
3200
|
-
this.refreshGeometry();
|
|
3201
|
-
this.emit({
|
|
3202
|
-
type: "state",
|
|
3203
|
-
state: this.state
|
|
3204
|
-
});
|
|
3205
|
-
break;
|
|
3206
|
-
}
|
|
3207
|
-
case "viewport": {
|
|
3208
|
-
const { x, y, width, height } = operation.rectangle;
|
|
3209
|
-
if (!(operation.scale > 0 && operation.scale <= 16 && width > 0 && height > 0)) throw new BrowserRenderError("render-failed", "Office viewport is invalid.");
|
|
3210
|
-
this.invoke("viewport", [
|
|
3211
|
-
Math.max(1, Math.round(256 * operation.scale)),
|
|
3212
|
-
3840,
|
|
3213
|
-
twips(x),
|
|
3214
|
-
twips(y),
|
|
3215
|
-
twips(width),
|
|
3216
|
-
twips(height)
|
|
3217
|
-
]);
|
|
3218
|
-
break;
|
|
2996
|
+
if (!operation || typeof operation !== "object") throw new BrowserRenderError("render-failed", "Invalid Office reading operation.");
|
|
2997
|
+
switch (operation.type) {
|
|
2998
|
+
case "pointer": {
|
|
2999
|
+
const event = operation.event;
|
|
3000
|
+
if (!event || ![
|
|
3001
|
+
"down",
|
|
3002
|
+
"up",
|
|
3003
|
+
"move"
|
|
3004
|
+
].includes(event.action) || ![0, 1].includes(event.buttons) || ![
|
|
3005
|
+
0,
|
|
3006
|
+
4096,
|
|
3007
|
+
8192,
|
|
3008
|
+
12288
|
|
3009
|
+
].includes(event.modifiers)) throw new BrowserRenderError("render-failed", "Invalid Office selection pointer.");
|
|
3010
|
+
this.invoke("mouse", [
|
|
3011
|
+
event.action === "down" ? 0 : event.action === "up" ? 1 : 2,
|
|
3012
|
+
twips(event.x),
|
|
3013
|
+
twips(event.y),
|
|
3014
|
+
integer(event.clicks, 3),
|
|
3015
|
+
event.buttons,
|
|
3016
|
+
event.modifiers
|
|
3017
|
+
]);
|
|
3018
|
+
break;
|
|
3019
|
+
}
|
|
3020
|
+
case "navigate": {
|
|
3021
|
+
const event = operation.event;
|
|
3022
|
+
if (!event || !Object.hasOwn(navigationKeys, event.key) || event.extend !== void 0 && typeof event.extend !== "boolean" || event.word !== void 0 && typeof event.word !== "boolean") throw new BrowserRenderError("render-failed", "Invalid Office navigation key.");
|
|
3023
|
+
const key = navigationKeys[event.key] | (event.extend ? 4096 : 0) | (event.word ? 8192 : 0);
|
|
3024
|
+
this.invoke("key", [
|
|
3025
|
+
0,
|
|
3026
|
+
0,
|
|
3027
|
+
key
|
|
3028
|
+
]);
|
|
3029
|
+
this.invoke("key", [
|
|
3030
|
+
1,
|
|
3031
|
+
0,
|
|
3032
|
+
key
|
|
3033
|
+
]);
|
|
3034
|
+
break;
|
|
3035
|
+
}
|
|
3036
|
+
case "select-all":
|
|
3037
|
+
await this.command(".uno:SelectAll", {});
|
|
3038
|
+
break;
|
|
3039
|
+
case "cell":
|
|
3040
|
+
if (this.state.documentType !== "spreadsheet" || typeof operation.address !== "string") throw new BrowserRenderError("render-failed", "Cell navigation requires a spreadsheet address.");
|
|
3041
|
+
try {
|
|
3042
|
+
parseCellRange(operation.address);
|
|
3043
|
+
} catch {
|
|
3044
|
+
throw new BrowserRenderError("render-failed", "Cell navigation requires a bounded A1 cell or range.");
|
|
3219
3045
|
}
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3046
|
+
await this.command(".uno:GoToCell", { ToPoint: {
|
|
3047
|
+
type: "string",
|
|
3048
|
+
value: operation.address
|
|
3049
|
+
} });
|
|
3050
|
+
break;
|
|
3051
|
+
case "copy":
|
|
3052
|
+
this.pump();
|
|
3053
|
+
return { text: this.text("dsh_lok_document_selection", [this.document]) };
|
|
3054
|
+
case "part": {
|
|
3055
|
+
const part = integer(operation.part, this.state.parts.length - 1);
|
|
3056
|
+
if (part === this.state.part) break;
|
|
3057
|
+
this.invoke("part", [part]);
|
|
3058
|
+
this.viewport = "";
|
|
3059
|
+
this.state = {
|
|
3060
|
+
...this.state,
|
|
3061
|
+
part,
|
|
3062
|
+
cursor: null,
|
|
3063
|
+
selection: [],
|
|
3064
|
+
graphicSelection: null,
|
|
3065
|
+
cellAddress: "",
|
|
3066
|
+
cellFormula: ""
|
|
3067
|
+
};
|
|
3068
|
+
const geometry = this.refreshGeometry();
|
|
3069
|
+
if (geometry !== void 0) this.state = {
|
|
3070
|
+
...this.state,
|
|
3071
|
+
layoutGeneration: this.state.layoutGeneration + 1,
|
|
3072
|
+
renderGeneration: this.state.renderGeneration + 1
|
|
3073
|
+
};
|
|
3074
|
+
this.emit({
|
|
3075
|
+
type: "state",
|
|
3076
|
+
state: this.state
|
|
3077
|
+
});
|
|
3078
|
+
if (geometry !== void 0) this.emit({
|
|
3079
|
+
type: "invalidate",
|
|
3080
|
+
part: geometry,
|
|
3081
|
+
rectangle: null,
|
|
3082
|
+
generation: this.state.renderGeneration
|
|
3083
|
+
});
|
|
3084
|
+
break;
|
|
3085
|
+
}
|
|
3086
|
+
case "viewport": {
|
|
3087
|
+
const { x, y, width, height } = operation.rectangle;
|
|
3088
|
+
if (!(operation.scale > 0 && operation.scale <= 16 && width > 0 && height > 0)) throw new BrowserRenderError("render-failed", "Office viewport is invalid.");
|
|
3089
|
+
const args = [
|
|
3090
|
+
Math.max(1, Math.round(256 * operation.scale)),
|
|
3091
|
+
3840,
|
|
3092
|
+
twips(x),
|
|
3093
|
+
twips(y),
|
|
3094
|
+
twips(width),
|
|
3095
|
+
twips(height)
|
|
3096
|
+
];
|
|
3097
|
+
const key = args.join(",");
|
|
3098
|
+
if (key !== this.viewport) {
|
|
3099
|
+
this.invoke("viewport", args);
|
|
3100
|
+
this.viewport = key;
|
|
3233
3101
|
}
|
|
3102
|
+
break;
|
|
3234
3103
|
}
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3104
|
+
case "layout": {
|
|
3105
|
+
const request = operation.request;
|
|
3106
|
+
if (!request || !["paginated", "continuous"].includes(request.mode) || request.mode === "continuous" && this.state.documentType !== "text") throw new BrowserRenderError("render-failed", "Continuous layout is only available for Writer documents.");
|
|
3107
|
+
const width = request.mode === "continuous" ? twips(request.width) : 0;
|
|
3108
|
+
if (request.mode === "continuous" && width <= 0) throw new BrowserRenderError("render-failed", "Continuous layout requires a positive width.");
|
|
3109
|
+
if (request.anchor && (twips(request.anchor.x) < 0 || twips(request.anchor.y) < 0)) throw new BrowserRenderError("render-failed", "Reading anchors must lie within positive document coordinates.");
|
|
3110
|
+
if (request.mode === this.state.layout && width === this.layoutWidth) return { layout: { changed: false } };
|
|
3111
|
+
const result = this.configure(request);
|
|
3112
|
+
this.layoutWidth = width;
|
|
3113
|
+
this.viewport = "";
|
|
3114
|
+
this.state = {
|
|
3115
|
+
...this.state,
|
|
3116
|
+
layout: request.mode,
|
|
3117
|
+
layoutGeneration: this.state.layoutGeneration + 1,
|
|
3118
|
+
renderGeneration: this.state.renderGeneration + 1
|
|
3119
|
+
};
|
|
3120
|
+
this.refreshGeometry();
|
|
3121
|
+
await this.command(".uno:ReportWhenIdle", { idleID: {
|
|
3122
|
+
type: "string",
|
|
3123
|
+
value: "office-layout"
|
|
3124
|
+
} });
|
|
3125
|
+
this.refreshGeometry();
|
|
3126
|
+
this.emit({
|
|
3127
|
+
type: "state",
|
|
3128
|
+
state: this.state
|
|
3129
|
+
});
|
|
3130
|
+
this.emit({
|
|
3131
|
+
type: "invalidate",
|
|
3132
|
+
part: -1,
|
|
3133
|
+
rectangle: null,
|
|
3134
|
+
generation: this.state.renderGeneration
|
|
3135
|
+
});
|
|
3136
|
+
return { layout: result };
|
|
3137
|
+
}
|
|
3138
|
+
default: throw new BrowserRenderError("render-failed", "Unsupported Office reading operation.");
|
|
3239
3139
|
}
|
|
3140
|
+
this.pump();
|
|
3141
|
+
return {};
|
|
3240
3142
|
}
|
|
3241
|
-
|
|
3242
|
-
|
|
3143
|
+
configure(request) {
|
|
3144
|
+
const raw = this.text("dsh_lok_document_configure_view", [
|
|
3145
|
+
this.document,
|
|
3146
|
+
1,
|
|
3147
|
+
request.mode === "continuous" ? 1 : 0,
|
|
3148
|
+
request.mode === "continuous" ? twips(request.width) : 0,
|
|
3149
|
+
request.anchor ? twips(request.anchor.x) : -1,
|
|
3150
|
+
request.anchor ? twips(request.anchor.y) : -1
|
|
3151
|
+
]);
|
|
3152
|
+
if (!raw) throw new BrowserRenderError("render-failed", "LibreOffice could not configure its read-only view.");
|
|
3153
|
+
const result = JSON.parse(raw);
|
|
3154
|
+
if (typeof result.changed !== "boolean" || !(result.width > 0 && result.height > 0)) throw new BrowserRenderError("render-failed", "LibreOffice returned an invalid reading layout.");
|
|
3155
|
+
if (result.anchor === void 0) return { changed: result.changed };
|
|
3156
|
+
const { x, y, width, height } = result.anchor;
|
|
3157
|
+
if (![
|
|
3158
|
+
x,
|
|
3159
|
+
y,
|
|
3160
|
+
width,
|
|
3161
|
+
height
|
|
3162
|
+
].every((value) => Number.isFinite(value) && value >= 0)) throw new BrowserRenderError("render-failed", "LibreOffice returned an invalid reading anchor.");
|
|
3163
|
+
return {
|
|
3164
|
+
changed: result.changed,
|
|
3165
|
+
anchor: {
|
|
3166
|
+
x: x / 15,
|
|
3167
|
+
y: y / 15,
|
|
3168
|
+
width: width / 15,
|
|
3169
|
+
height: height / 15
|
|
3170
|
+
}
|
|
3171
|
+
};
|
|
3243
3172
|
}
|
|
3173
|
+
/** Internal whitelist commands only; no arbitrary command crosses the public API. */
|
|
3244
3174
|
async command(name, args) {
|
|
3245
3175
|
const result = Promise.withResolvers();
|
|
3246
3176
|
this.commandResult = {
|
|
@@ -3256,11 +3186,12 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3256
3186
|
this.commandResult = void 0;
|
|
3257
3187
|
}
|
|
3258
3188
|
}
|
|
3259
|
-
/**
|
|
3189
|
+
/** Direct tiles preserve the active view, navigation state and selection. */
|
|
3260
3190
|
render(request) {
|
|
3261
3191
|
this.requireOpen();
|
|
3192
|
+
this.flush();
|
|
3262
3193
|
const part = this.state.parts[integer(request.part, this.state.parts.length - 1)];
|
|
3263
|
-
this.
|
|
3194
|
+
if (request.part !== this.state.part) throw new BrowserRenderError("stale-part", "The Office tile belongs to an inactive worksheet or slide.");
|
|
3264
3195
|
try {
|
|
3265
3196
|
return renderRegion(this.module, this.document, {
|
|
3266
3197
|
...request,
|
|
@@ -3273,143 +3204,7 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3273
3204
|
}], () => new BrowserRenderError("render-failed", "Office tile rendering failed."));
|
|
3274
3205
|
} finally {
|
|
3275
3206
|
this.flush();
|
|
3276
|
-
this.phase = "idle";
|
|
3277
|
-
}
|
|
3278
|
-
}
|
|
3279
|
-
/** Capture one bounded batch with no input interleaving; cached tiles are checked at the same generation. */
|
|
3280
|
-
async capture(request) {
|
|
3281
|
-
this.requireOpen();
|
|
3282
|
-
integer(request.maxPixels);
|
|
3283
|
-
if (!request.maxPixels || request.maxPixels > 67108864 || request.tiles.length > 4096) throw new BrowserRenderError("render-failed", "Office capture exceeds its allocation limit.");
|
|
3284
|
-
await this.command(".uno:ReportWhenIdle", { idleID: {
|
|
3285
|
-
type: "string",
|
|
3286
|
-
value: "capture"
|
|
3287
|
-
} });
|
|
3288
|
-
this.pump();
|
|
3289
|
-
const generation = this.state.renderGeneration;
|
|
3290
|
-
if (request.generation !== void 0 && request.generation !== generation) throw new BrowserRenderError("snapshot-changed", "The document changed before capture.");
|
|
3291
|
-
const original = this.call("dsh_lok_document_get_view", [this.document]);
|
|
3292
|
-
if (original < 0) throw new BrowserRenderError("render-failed", "The editor view is unavailable.");
|
|
3293
|
-
this.phase = "capture";
|
|
3294
|
-
let parts = this.state.parts;
|
|
3295
|
-
const tiles = [];
|
|
3296
|
-
const regions = [];
|
|
3297
|
-
try {
|
|
3298
|
-
if (this.renderView < 0) this.renderView = this.call("dsh_lok_document_create_view", [this.document]);
|
|
3299
|
-
if (this.renderView < 0 || !this.call("dsh_lok_document_set_view", [this.document, this.renderView])) throw new BrowserRenderError("render-failed", "The capture view is unavailable.");
|
|
3300
|
-
const pointer = this.call("malloc", [8]);
|
|
3301
|
-
if (!pointer) throw new BrowserRenderError("render-failed", "Office capture geometry allocation failed.");
|
|
3302
|
-
try {
|
|
3303
|
-
parts = this.state.parts.map((part, index) => {
|
|
3304
|
-
this.invoke("size", [
|
|
3305
|
-
index,
|
|
3306
|
-
pointer,
|
|
3307
|
-
pointer + 4
|
|
3308
|
-
]);
|
|
3309
|
-
const width = this.module.HEAPU32[pointer / 4] / 15;
|
|
3310
|
-
const height = this.module.HEAPU32[pointer / 4 + 1] / 15;
|
|
3311
|
-
if (!(width > 0 && height > 0)) throw new BrowserRenderError("invalid-document", "Office returned empty capture dimensions.");
|
|
3312
|
-
return {
|
|
3313
|
-
...part,
|
|
3314
|
-
width,
|
|
3315
|
-
height
|
|
3316
|
-
};
|
|
3317
|
-
});
|
|
3318
|
-
} finally {
|
|
3319
|
-
this.call("free", [pointer]);
|
|
3320
|
-
}
|
|
3321
|
-
if (request.selection !== void 0 && this.state.documentType === "spreadsheet") {
|
|
3322
|
-
const selection = request.selection;
|
|
3323
|
-
for (let part = 0; part < parts.length; part++) {
|
|
3324
|
-
const info = sheetPartInfo(this.text("dsh_lok_document_part_info", [this.document, part]));
|
|
3325
|
-
if (selection.sheet !== void 0 ? selection.sheet !== info.name : !info.visible) continue;
|
|
3326
|
-
this.invoke("part", [part]);
|
|
3327
|
-
this.invoke("viewport", [
|
|
3328
|
-
Math.max(1, Math.round(256 * selection.scale)),
|
|
3329
|
-
3840,
|
|
3330
|
-
0,
|
|
3331
|
-
0,
|
|
3332
|
-
1,
|
|
3333
|
-
1
|
|
3334
|
-
]);
|
|
3335
|
-
const range = selection.range === void 0 ? dataArea(info) : parseCellRange(selection.range);
|
|
3336
|
-
const rectangle = sheetRangeRectangle(JSON.parse(this.text("dsh_lok_document_command_values", [this.document, ".uno:SheetGeometryData"])), range, selection.scale);
|
|
3337
|
-
this.text("dsh_lok_document_command_values", [this.document, `.uno:ViewRowColumnHeaders?x=${twips(rectangle.x)}&y=${twips(rectangle.y)}&width=${twips(rectangle.width)}&height=${twips(rectangle.height)}`]);
|
|
3338
|
-
regions.push({
|
|
3339
|
-
part,
|
|
3340
|
-
sheet: info.name,
|
|
3341
|
-
range: rangeName(range),
|
|
3342
|
-
rectangle
|
|
3343
|
-
});
|
|
3344
|
-
parts = parts.map((entry, index) => index === part ? {
|
|
3345
|
-
...entry,
|
|
3346
|
-
width: Math.max(entry.width, rectangle.x + rectangle.width),
|
|
3347
|
-
height: Math.max(entry.height, rectangle.y + rectangle.height)
|
|
3348
|
-
} : entry);
|
|
3349
|
-
}
|
|
3350
|
-
if (regions.length === 0) throw new BrowserRenderError("invalid-document", "No worksheet matches the export selection.");
|
|
3351
|
-
} else if (this.state.documentType === "text") regions.push(...this.state.pages.map((rectangle) => ({
|
|
3352
|
-
part: 0,
|
|
3353
|
-
rectangle
|
|
3354
|
-
})));
|
|
3355
|
-
else if (this.state.documentType === "presentation") regions.push(...parts.map((part, index) => ({
|
|
3356
|
-
part: index,
|
|
3357
|
-
rectangle: {
|
|
3358
|
-
x: 0,
|
|
3359
|
-
y: 0,
|
|
3360
|
-
width: part.width,
|
|
3361
|
-
height: part.height
|
|
3362
|
-
}
|
|
3363
|
-
})));
|
|
3364
|
-
let pixels = 0;
|
|
3365
|
-
for (const entry of request.tiles) {
|
|
3366
|
-
const page = {
|
|
3367
|
-
...parts[integer(entry.request.part, parts.length - 1)],
|
|
3368
|
-
x: 0,
|
|
3369
|
-
y: 0,
|
|
3370
|
-
part: this.state.documentType === "text" ? -1 : entry.request.part
|
|
3371
|
-
};
|
|
3372
|
-
const region = {
|
|
3373
|
-
...entry.request,
|
|
3374
|
-
pageIndex: 0
|
|
3375
|
-
};
|
|
3376
|
-
const size = tileDimensions(region, [page]);
|
|
3377
|
-
pixels += size.width * size.height;
|
|
3378
|
-
if (pixels > request.maxPixels) throw new BrowserRenderError("render-failed", "Office capture exceeds its pixel budget.");
|
|
3379
|
-
if (entry.cached !== void 0) {
|
|
3380
|
-
if (request.generation === void 0 || entry.cached.width !== size.width || entry.cached.height !== size.height || entry.cached.rgba.byteLength !== size.width * size.height * 4) throw new BrowserRenderError("render-failed", "Cached Office pixels do not match the capture.");
|
|
3381
|
-
tiles.push(entry.cached);
|
|
3382
|
-
} else {
|
|
3383
|
-
this.invoke("part", [entry.request.part]);
|
|
3384
|
-
tiles.push(renderRegion(this.module, this.document, region, [page], () => new BrowserRenderError("render-failed", "Office capture failed.")));
|
|
3385
|
-
}
|
|
3386
|
-
}
|
|
3387
|
-
} finally {
|
|
3388
|
-
try {
|
|
3389
|
-
if (!this.call("dsh_lok_document_set_view", [this.document, original])) throw new BrowserRenderError("render-failed", "The editor view could not be restored after capture.");
|
|
3390
|
-
this.pump();
|
|
3391
|
-
} catch (error) {
|
|
3392
|
-
this.stop();
|
|
3393
|
-
this.failure(error);
|
|
3394
|
-
throw error;
|
|
3395
|
-
} finally {
|
|
3396
|
-
this.phase = "idle";
|
|
3397
|
-
}
|
|
3398
3207
|
}
|
|
3399
|
-
await this.command(".uno:ReportWhenIdle", { idleID: {
|
|
3400
|
-
type: "string",
|
|
3401
|
-
value: "capture-complete"
|
|
3402
|
-
} });
|
|
3403
|
-
this.pump();
|
|
3404
|
-
if (this.state.renderGeneration !== generation) throw new BrowserRenderError("snapshot-changed", "The document changed during capture.");
|
|
3405
|
-
return {
|
|
3406
|
-
state: {
|
|
3407
|
-
...this.state,
|
|
3408
|
-
parts
|
|
3409
|
-
},
|
|
3410
|
-
tiles,
|
|
3411
|
-
regions
|
|
3412
|
-
};
|
|
3413
3208
|
}
|
|
3414
3209
|
};
|
|
3415
3210
|
//#endregion
|
|
@@ -3421,7 +3216,7 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3421
3216
|
let document = 0;
|
|
3422
3217
|
let pdf = 0;
|
|
3423
3218
|
let pages = [];
|
|
3424
|
-
let
|
|
3219
|
+
let reader;
|
|
3425
3220
|
let fatal = false;
|
|
3426
3221
|
let fontFailure;
|
|
3427
3222
|
let serial = Promise.resolve();
|
|
@@ -3443,7 +3238,7 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3443
3238
|
call("free", [pointer]);
|
|
3444
3239
|
}
|
|
3445
3240
|
}
|
|
3446
|
-
async function open(options, channel,
|
|
3241
|
+
async function open(options, channel, reading = false) {
|
|
3447
3242
|
const { assets } = options;
|
|
3448
3243
|
let metadata;
|
|
3449
3244
|
try {
|
|
@@ -3465,7 +3260,7 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3465
3260
|
module = await scope.createLibreOfficeModule({
|
|
3466
3261
|
noInitialRun: true,
|
|
3467
3262
|
dshOnCallback(type, payload) {
|
|
3468
|
-
|
|
3263
|
+
reader?.callback(type, payload);
|
|
3469
3264
|
},
|
|
3470
3265
|
mainScriptUrlOrBlob: assets.loaderUrl,
|
|
3471
3266
|
locateFile(name) {
|
|
@@ -3561,28 +3356,28 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3561
3356
|
], [
|
|
3562
3357
|
office,
|
|
3563
3358
|
`file://${input}`,
|
|
3564
|
-
|
|
3359
|
+
"ReadOnly=true,EnableMacrosExecution=false"
|
|
3565
3360
|
]);
|
|
3566
3361
|
if (!document || !call("dsh_lok_document_initialize_rendering", [document])) throw error("invalid-document");
|
|
3567
3362
|
const type = call("dsh_lok_document_type", [document]);
|
|
3568
3363
|
if (type !== (options.extension === "docx" || options.extension === "doc" ? 0 : options.extension === "xlsx" || options.extension === "xls" ? 1 : 2)) throw new BrowserRenderError("invalid-document", "The document type does not match its extension.");
|
|
3569
|
-
if (
|
|
3570
|
-
|
|
3571
|
-
type: "
|
|
3364
|
+
if (reading) {
|
|
3365
|
+
reader = new OfficeEngine(module, document, options.extension, (event) => send({
|
|
3366
|
+
type: "office-event",
|
|
3572
3367
|
event
|
|
3573
3368
|
}), (failure) => {
|
|
3574
3369
|
fatal ||= failure instanceof WebAssembly.RuntimeError;
|
|
3575
3370
|
send({
|
|
3576
|
-
type: "
|
|
3371
|
+
type: "office-event",
|
|
3577
3372
|
event: {
|
|
3578
3373
|
type: "error",
|
|
3579
3374
|
code: "render-failed",
|
|
3580
3375
|
message: failure instanceof Error ? failure.message : String(failure)
|
|
3581
3376
|
}
|
|
3582
3377
|
});
|
|
3583
|
-
}
|
|
3584
|
-
await
|
|
3585
|
-
pages =
|
|
3378
|
+
});
|
|
3379
|
+
await reader.start();
|
|
3380
|
+
pages = reader.state.parts.map((part, index) => ({
|
|
3586
3381
|
...part,
|
|
3587
3382
|
x: 0,
|
|
3588
3383
|
y: 0,
|
|
@@ -3598,8 +3393,8 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3598
3393
|
return renderRegion(module, pdf, request, pages, () => error("render-failed"), true);
|
|
3599
3394
|
}
|
|
3600
3395
|
function dispose() {
|
|
3601
|
-
|
|
3602
|
-
|
|
3396
|
+
reader?.stop();
|
|
3397
|
+
reader = void 0;
|
|
3603
3398
|
try {
|
|
3604
3399
|
if (module && !fatal) {
|
|
3605
3400
|
if (pdf) {
|
|
@@ -3631,13 +3426,13 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3631
3426
|
const message = event.data;
|
|
3632
3427
|
if (message.type === "dispose") {
|
|
3633
3428
|
lifetime.abort();
|
|
3634
|
-
|
|
3429
|
+
reader?.stop();
|
|
3635
3430
|
}
|
|
3636
3431
|
serial = serial.then(async () => {
|
|
3637
3432
|
try {
|
|
3638
3433
|
switch (message.type) {
|
|
3639
3434
|
case "open":
|
|
3640
|
-
await open(message.options, message.channel, message.
|
|
3435
|
+
await open(message.options, message.channel, message.office);
|
|
3641
3436
|
send({
|
|
3642
3437
|
type: "opened",
|
|
3643
3438
|
id: message.id,
|
|
@@ -3645,7 +3440,7 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3645
3440
|
width,
|
|
3646
3441
|
height
|
|
3647
3442
|
})),
|
|
3648
|
-
...
|
|
3443
|
+
...reader ? { office: reader.state } : {}
|
|
3649
3444
|
});
|
|
3650
3445
|
break;
|
|
3651
3446
|
case "tile": {
|
|
@@ -3657,9 +3452,9 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3657
3452
|
}, [tile.rgba.buffer]);
|
|
3658
3453
|
break;
|
|
3659
3454
|
}
|
|
3660
|
-
case "
|
|
3661
|
-
if (!
|
|
3662
|
-
const tile =
|
|
3455
|
+
case "office-tile": {
|
|
3456
|
+
if (!reader) throw new BrowserRenderError("disposed", "Office reader is closed.");
|
|
3457
|
+
const tile = reader.render(message.request);
|
|
3663
3458
|
send({
|
|
3664
3459
|
type: "tile",
|
|
3665
3460
|
id: message.id,
|
|
@@ -3667,24 +3462,14 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3667
3462
|
}, [tile.rgba.buffer]);
|
|
3668
3463
|
break;
|
|
3669
3464
|
}
|
|
3670
|
-
case "
|
|
3671
|
-
if (!
|
|
3672
|
-
const result = await
|
|
3465
|
+
case "office-operation": {
|
|
3466
|
+
if (!reader) throw new BrowserRenderError("disposed", "Office reader is closed.");
|
|
3467
|
+
const result = await reader.operation(message.operation);
|
|
3673
3468
|
send({
|
|
3674
|
-
type: "
|
|
3469
|
+
type: "office-result",
|
|
3675
3470
|
id: message.id,
|
|
3676
3471
|
...result
|
|
3677
|
-
}
|
|
3678
|
-
break;
|
|
3679
|
-
}
|
|
3680
|
-
case "capture": {
|
|
3681
|
-
if (!editor) throw new BrowserRenderError("disposed", "Office editor is closed.");
|
|
3682
|
-
const capture = await editor.capture(message.request);
|
|
3683
|
-
send({
|
|
3684
|
-
type: "captured",
|
|
3685
|
-
id: message.id,
|
|
3686
|
-
capture
|
|
3687
|
-
}, capture.tiles.map((tile) => tile.rgba.buffer));
|
|
3472
|
+
});
|
|
3688
3473
|
break;
|
|
3689
3474
|
}
|
|
3690
3475
|
case "dispose":
|
|
@@ -3694,6 +3479,8 @@ ${XMLNS_NAMESPACE}.`);
|
|
|
3694
3479
|
id: message.id
|
|
3695
3480
|
});
|
|
3696
3481
|
scope.close();
|
|
3482
|
+
break;
|
|
3483
|
+
default: throw new BrowserRenderError("render-failed", "Unsupported Office Worker request.");
|
|
3697
3484
|
}
|
|
3698
3485
|
} catch (failure) {
|
|
3699
3486
|
fatal ||= failure instanceof WebAssembly.RuntimeError;
|