@open-file-viewer/core 0.1.14 → 0.1.15
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 +17 -0
- package/dist/index.cjs +158 -105
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -2
- package/dist/index.d.ts +24 -2
- package/dist/index.js +158 -105
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -4,6 +4,7 @@ type PreviewSource = File | Blob | string | ArrayBuffer;
|
|
|
4
4
|
type PreviewFit = "contain" | "cover" | "width" | "height" | "actual" | "scale-down";
|
|
5
5
|
type PreviewFallback = "inline" | "download" | "custom";
|
|
6
6
|
type PreviewTheme = "light" | "dark" | "auto";
|
|
7
|
+
type PreviewLocale = "zh-CN" | "en-US";
|
|
7
8
|
type PreviewToolbarBuiltInAction = "previous" | "next" | "queue" | "zoom-out" | "zoom-in" | "zoom-reset" | "rotate-right" | "download" | "fullscreen" | "print" | "search";
|
|
8
9
|
type PreviewToolbarActionId = PreviewToolbarBuiltInAction | (string & {});
|
|
9
10
|
interface PreviewFile {
|
|
@@ -79,9 +80,12 @@ interface PreviewOptions {
|
|
|
79
80
|
mimeType?: string;
|
|
80
81
|
width?: number | string;
|
|
81
82
|
height?: number | string;
|
|
83
|
+
zoom?: number;
|
|
82
84
|
fit?: PreviewFit;
|
|
83
85
|
plugins?: PreviewPlugin[];
|
|
84
86
|
fallback?: PreviewFallback;
|
|
87
|
+
locale?: PreviewLocale;
|
|
88
|
+
messages?: Partial<PreviewMessages>;
|
|
85
89
|
renderFallback?: (ctx: PreviewContext) => Promise<PreviewInstance> | PreviewInstance;
|
|
86
90
|
toolbar?: boolean | PreviewToolbarOptions;
|
|
87
91
|
theme?: PreviewTheme;
|
|
@@ -90,12 +94,30 @@ interface PreviewOptions {
|
|
|
90
94
|
onError?: (error: Error, file?: PreviewFile) => void;
|
|
91
95
|
onUnsupported?: (file: PreviewFile) => void;
|
|
92
96
|
}
|
|
97
|
+
interface PreviewMessages {
|
|
98
|
+
loading: string;
|
|
99
|
+
unsupportedTitle: string;
|
|
100
|
+
downloadTitle: string;
|
|
101
|
+
downloadFile: string;
|
|
102
|
+
file: string;
|
|
103
|
+
unnamedFile: string;
|
|
104
|
+
format: string;
|
|
105
|
+
unknown: string;
|
|
106
|
+
mime: string;
|
|
107
|
+
undeclared: string;
|
|
108
|
+
size: string;
|
|
109
|
+
source: string;
|
|
110
|
+
remoteUrl: string;
|
|
111
|
+
localFile: string;
|
|
112
|
+
}
|
|
93
113
|
interface PreviewContext {
|
|
94
114
|
host: HTMLElement;
|
|
95
115
|
viewport: HTMLElement;
|
|
96
116
|
file: PreviewFile;
|
|
97
117
|
size: PreviewSize;
|
|
98
|
-
options: Required<Pick<PreviewOptions, "fit" | "fallback">> &
|
|
118
|
+
options: Omit<PreviewOptions, "messages"> & Required<Pick<PreviewOptions, "fit" | "fallback" | "zoom">> & {
|
|
119
|
+
messages: PreviewMessages;
|
|
120
|
+
};
|
|
99
121
|
toolbar?: PreviewToolbarRenderContext;
|
|
100
122
|
setLoading: (loading: boolean) => void;
|
|
101
123
|
setError: (error: Error | string) => void;
|
|
@@ -230,4 +252,4 @@ declare function assetPlugin(): PreviewPlugin;
|
|
|
230
252
|
|
|
231
253
|
declare function fallbackPlugin(): PreviewPlugin;
|
|
232
254
|
|
|
233
|
-
export { type CadBinaryPreviewContext, type CadPluginOptions, type FileViewer, type LibreDwgPreviewOptions, type OfficeConversionContext, type OfficeConversionResult, type OfficePluginOptions, type PreviewCommand, type PreviewContext, type PreviewFallback, type PreviewFile, type PreviewFit, type PreviewInstance, type PreviewItem, type PreviewOptions, type PreviewPlugin, type PreviewSize, type PreviewSource, type PreviewTheme, type PreviewToolbarActionId, type PreviewToolbarBuiltInAction, type PreviewToolbarCustomAction, type PreviewToolbarOptions, type PreviewToolbarRenderContext, archivePlugin, assetPlugin, audioPlugin, cadPlugin, createViewer, drawingPlugin, emailPlugin, epubPlugin, fallbackPlugin, gisPlugin, imagePlugin, model3dPlugin, ofdPlugin, officePlugin, pdfPlugin, textPlugin, videoPlugin, xpsPlugin };
|
|
255
|
+
export { type CadBinaryPreviewContext, type CadPluginOptions, type FileViewer, type LibreDwgPreviewOptions, type OfficeConversionContext, type OfficeConversionResult, type OfficePluginOptions, type PreviewCommand, type PreviewContext, type PreviewFallback, type PreviewFile, type PreviewFit, type PreviewInstance, type PreviewItem, type PreviewLocale, type PreviewMessages, type PreviewOptions, type PreviewPlugin, type PreviewSize, type PreviewSource, type PreviewTheme, type PreviewToolbarActionId, type PreviewToolbarBuiltInAction, type PreviewToolbarCustomAction, type PreviewToolbarOptions, type PreviewToolbarRenderContext, archivePlugin, assetPlugin, audioPlugin, cadPlugin, createViewer, drawingPlugin, emailPlugin, epubPlugin, fallbackPlugin, gisPlugin, imagePlugin, model3dPlugin, ofdPlugin, officePlugin, pdfPlugin, textPlugin, videoPlugin, xpsPlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ type PreviewSource = File | Blob | string | ArrayBuffer;
|
|
|
4
4
|
type PreviewFit = "contain" | "cover" | "width" | "height" | "actual" | "scale-down";
|
|
5
5
|
type PreviewFallback = "inline" | "download" | "custom";
|
|
6
6
|
type PreviewTheme = "light" | "dark" | "auto";
|
|
7
|
+
type PreviewLocale = "zh-CN" | "en-US";
|
|
7
8
|
type PreviewToolbarBuiltInAction = "previous" | "next" | "queue" | "zoom-out" | "zoom-in" | "zoom-reset" | "rotate-right" | "download" | "fullscreen" | "print" | "search";
|
|
8
9
|
type PreviewToolbarActionId = PreviewToolbarBuiltInAction | (string & {});
|
|
9
10
|
interface PreviewFile {
|
|
@@ -79,9 +80,12 @@ interface PreviewOptions {
|
|
|
79
80
|
mimeType?: string;
|
|
80
81
|
width?: number | string;
|
|
81
82
|
height?: number | string;
|
|
83
|
+
zoom?: number;
|
|
82
84
|
fit?: PreviewFit;
|
|
83
85
|
plugins?: PreviewPlugin[];
|
|
84
86
|
fallback?: PreviewFallback;
|
|
87
|
+
locale?: PreviewLocale;
|
|
88
|
+
messages?: Partial<PreviewMessages>;
|
|
85
89
|
renderFallback?: (ctx: PreviewContext) => Promise<PreviewInstance> | PreviewInstance;
|
|
86
90
|
toolbar?: boolean | PreviewToolbarOptions;
|
|
87
91
|
theme?: PreviewTheme;
|
|
@@ -90,12 +94,30 @@ interface PreviewOptions {
|
|
|
90
94
|
onError?: (error: Error, file?: PreviewFile) => void;
|
|
91
95
|
onUnsupported?: (file: PreviewFile) => void;
|
|
92
96
|
}
|
|
97
|
+
interface PreviewMessages {
|
|
98
|
+
loading: string;
|
|
99
|
+
unsupportedTitle: string;
|
|
100
|
+
downloadTitle: string;
|
|
101
|
+
downloadFile: string;
|
|
102
|
+
file: string;
|
|
103
|
+
unnamedFile: string;
|
|
104
|
+
format: string;
|
|
105
|
+
unknown: string;
|
|
106
|
+
mime: string;
|
|
107
|
+
undeclared: string;
|
|
108
|
+
size: string;
|
|
109
|
+
source: string;
|
|
110
|
+
remoteUrl: string;
|
|
111
|
+
localFile: string;
|
|
112
|
+
}
|
|
93
113
|
interface PreviewContext {
|
|
94
114
|
host: HTMLElement;
|
|
95
115
|
viewport: HTMLElement;
|
|
96
116
|
file: PreviewFile;
|
|
97
117
|
size: PreviewSize;
|
|
98
|
-
options: Required<Pick<PreviewOptions, "fit" | "fallback">> &
|
|
118
|
+
options: Omit<PreviewOptions, "messages"> & Required<Pick<PreviewOptions, "fit" | "fallback" | "zoom">> & {
|
|
119
|
+
messages: PreviewMessages;
|
|
120
|
+
};
|
|
99
121
|
toolbar?: PreviewToolbarRenderContext;
|
|
100
122
|
setLoading: (loading: boolean) => void;
|
|
101
123
|
setError: (error: Error | string) => void;
|
|
@@ -230,4 +252,4 @@ declare function assetPlugin(): PreviewPlugin;
|
|
|
230
252
|
|
|
231
253
|
declare function fallbackPlugin(): PreviewPlugin;
|
|
232
254
|
|
|
233
|
-
export { type CadBinaryPreviewContext, type CadPluginOptions, type FileViewer, type LibreDwgPreviewOptions, type OfficeConversionContext, type OfficeConversionResult, type OfficePluginOptions, type PreviewCommand, type PreviewContext, type PreviewFallback, type PreviewFile, type PreviewFit, type PreviewInstance, type PreviewItem, type PreviewOptions, type PreviewPlugin, type PreviewSize, type PreviewSource, type PreviewTheme, type PreviewToolbarActionId, type PreviewToolbarBuiltInAction, type PreviewToolbarCustomAction, type PreviewToolbarOptions, type PreviewToolbarRenderContext, archivePlugin, assetPlugin, audioPlugin, cadPlugin, createViewer, drawingPlugin, emailPlugin, epubPlugin, fallbackPlugin, gisPlugin, imagePlugin, model3dPlugin, ofdPlugin, officePlugin, pdfPlugin, textPlugin, videoPlugin, xpsPlugin };
|
|
255
|
+
export { type CadBinaryPreviewContext, type CadPluginOptions, type FileViewer, type LibreDwgPreviewOptions, type OfficeConversionContext, type OfficeConversionResult, type OfficePluginOptions, type PreviewCommand, type PreviewContext, type PreviewFallback, type PreviewFile, type PreviewFit, type PreviewInstance, type PreviewItem, type PreviewLocale, type PreviewMessages, type PreviewOptions, type PreviewPlugin, type PreviewSize, type PreviewSource, type PreviewTheme, type PreviewToolbarActionId, type PreviewToolbarBuiltInAction, type PreviewToolbarCustomAction, type PreviewToolbarOptions, type PreviewToolbarRenderContext, archivePlugin, assetPlugin, audioPlugin, cadPlugin, createViewer, drawingPlugin, emailPlugin, epubPlugin, fallbackPlugin, gisPlugin, imagePlugin, model3dPlugin, ofdPlugin, officePlugin, pdfPlugin, textPlugin, videoPlugin, xpsPlugin };
|
package/dist/index.js
CHANGED
|
@@ -553,6 +553,48 @@ function revokeObjectUrl(url, isExternal) {
|
|
|
553
553
|
}
|
|
554
554
|
}
|
|
555
555
|
|
|
556
|
+
// src/messages.ts
|
|
557
|
+
var defaultMessages = {
|
|
558
|
+
"zh-CN": {
|
|
559
|
+
loading: "\u6B63\u5728\u52A0\u8F7D\u9884\u89C8...",
|
|
560
|
+
unsupportedTitle: "\u5F53\u524D\u6587\u4EF6\u6682\u4E0D\u652F\u6301\u5728\u7EBF\u9884\u89C8",
|
|
561
|
+
downloadTitle: "\u5F53\u524D\u6587\u4EF6\u53EF\u4E0B\u8F7D\u540E\u67E5\u770B",
|
|
562
|
+
downloadFile: "\u4E0B\u8F7D\u6587\u4EF6",
|
|
563
|
+
file: "\u6587\u4EF6",
|
|
564
|
+
unnamedFile: "\u672A\u547D\u540D\u6587\u4EF6",
|
|
565
|
+
format: "\u683C\u5F0F",
|
|
566
|
+
unknown: "\u672A\u77E5",
|
|
567
|
+
mime: "MIME",
|
|
568
|
+
undeclared: "\u672A\u58F0\u660E",
|
|
569
|
+
size: "\u5927\u5C0F",
|
|
570
|
+
source: "\u6765\u6E90",
|
|
571
|
+
remoteUrl: "\u8FDC\u7A0B URL",
|
|
572
|
+
localFile: "\u672C\u5730/\u5185\u5B58\u6587\u4EF6"
|
|
573
|
+
},
|
|
574
|
+
"en-US": {
|
|
575
|
+
loading: "Loading preview...",
|
|
576
|
+
unsupportedTitle: "Preview is not available for this file",
|
|
577
|
+
downloadTitle: "This file can be downloaded and opened locally",
|
|
578
|
+
downloadFile: "Download file",
|
|
579
|
+
file: "File",
|
|
580
|
+
unnamedFile: "Untitled file",
|
|
581
|
+
format: "Format",
|
|
582
|
+
unknown: "Unknown",
|
|
583
|
+
mime: "MIME",
|
|
584
|
+
undeclared: "Not declared",
|
|
585
|
+
size: "Size",
|
|
586
|
+
source: "Source",
|
|
587
|
+
remoteUrl: "Remote URL",
|
|
588
|
+
localFile: "Local or in-memory file"
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
function resolveMessages(options) {
|
|
592
|
+
return {
|
|
593
|
+
...defaultMessages[options.locale || "zh-CN"],
|
|
594
|
+
...options.messages
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
|
|
556
598
|
// src/plugins/fallback.ts
|
|
557
599
|
function fallbackPlugin() {
|
|
558
600
|
return {
|
|
@@ -570,12 +612,12 @@ function fallbackPlugin() {
|
|
|
570
612
|
const panel = document.createElement("div");
|
|
571
613
|
panel.className = "ofv-fallback";
|
|
572
614
|
const title = document.createElement("strong");
|
|
573
|
-
title.textContent = ctx.options.fallback === "download" ?
|
|
574
|
-
const meta = createFallbackMeta(ctx.file);
|
|
615
|
+
title.textContent = ctx.options.fallback === "download" ? ctx.options.messages.downloadTitle : ctx.options.messages.unsupportedTitle;
|
|
616
|
+
const meta = createFallbackMeta(ctx.file, ctx.options.messages);
|
|
575
617
|
const download = document.createElement("a");
|
|
576
618
|
download.href = url;
|
|
577
619
|
download.download = ctx.file.name;
|
|
578
|
-
download.textContent =
|
|
620
|
+
download.textContent = ctx.options.messages.downloadFile;
|
|
579
621
|
panel.append(title, meta, download);
|
|
580
622
|
ctx.viewport.classList.add("ofv-center");
|
|
581
623
|
ctx.viewport.append(panel);
|
|
@@ -591,14 +633,14 @@ function fallbackPlugin() {
|
|
|
591
633
|
}
|
|
592
634
|
};
|
|
593
635
|
}
|
|
594
|
-
function createFallbackMeta(file) {
|
|
636
|
+
function createFallbackMeta(file, messages) {
|
|
595
637
|
const meta = document.createElement("dl");
|
|
596
638
|
meta.className = "ofv-fallback-meta";
|
|
597
|
-
appendFallbackMeta(meta,
|
|
598
|
-
appendFallbackMeta(meta,
|
|
599
|
-
appendFallbackMeta(meta,
|
|
600
|
-
appendFallbackMeta(meta,
|
|
601
|
-
appendFallbackMeta(meta,
|
|
639
|
+
appendFallbackMeta(meta, messages.file, file.name || messages.unnamedFile);
|
|
640
|
+
appendFallbackMeta(meta, messages.format, file.extension ? `.${file.extension}` : messages.unknown);
|
|
641
|
+
appendFallbackMeta(meta, messages.mime, file.mimeType || messages.undeclared);
|
|
642
|
+
appendFallbackMeta(meta, messages.size, file.size === void 0 ? messages.unknown : formatFallbackBytes(file.size));
|
|
643
|
+
appendFallbackMeta(meta, messages.source, file.url ? messages.remoteUrl : messages.localFile);
|
|
602
644
|
return meta;
|
|
603
645
|
}
|
|
604
646
|
function appendFallbackMeta(parent, label, value) {
|
|
@@ -656,15 +698,17 @@ function createViewer(options) {
|
|
|
656
698
|
host.append(status, viewport);
|
|
657
699
|
container.replaceChildren(host);
|
|
658
700
|
const normalizedOptions = {
|
|
701
|
+
...options,
|
|
659
702
|
fit: options.fit || "contain",
|
|
660
703
|
fallback: options.fallback || "inline",
|
|
661
|
-
|
|
704
|
+
zoom: normalizeInitialZoom(options.zoom),
|
|
705
|
+
messages: resolveMessages(options)
|
|
662
706
|
};
|
|
663
707
|
let destroyed = false;
|
|
664
708
|
let renderToken = 0;
|
|
665
709
|
const setLoading = (loading) => {
|
|
666
710
|
status.hidden = !loading;
|
|
667
|
-
status.textContent = loading ?
|
|
711
|
+
status.textContent = loading ? normalizedOptions.messages.loading : "";
|
|
668
712
|
};
|
|
669
713
|
const setError = (error) => {
|
|
670
714
|
status.hidden = false;
|
|
@@ -820,6 +864,9 @@ function clampIndex(index, length) {
|
|
|
820
864
|
}
|
|
821
865
|
return Math.min(Math.max(index, 0), length - 1);
|
|
822
866
|
}
|
|
867
|
+
function normalizeInitialZoom(zoom) {
|
|
868
|
+
return typeof zoom === "number" && Number.isFinite(zoom) && zoom > 0 ? zoom : 1;
|
|
869
|
+
}
|
|
823
870
|
function applyTheme(container, theme) {
|
|
824
871
|
const media = window.matchMedia?.("(prefers-color-scheme: dark)");
|
|
825
872
|
const classes = ["ofv-theme-light", "ofv-theme-dark"];
|
|
@@ -1696,6 +1743,98 @@ async function findPlugin(plugins, file) {
|
|
|
1696
1743
|
return fallbackPlugin();
|
|
1697
1744
|
}
|
|
1698
1745
|
|
|
1746
|
+
// src/plugins/utils.ts
|
|
1747
|
+
async function readArrayBuffer(file) {
|
|
1748
|
+
if (file.source instanceof ArrayBuffer) {
|
|
1749
|
+
return file.source;
|
|
1750
|
+
}
|
|
1751
|
+
if (file.blob) {
|
|
1752
|
+
return file.blob.arrayBuffer();
|
|
1753
|
+
}
|
|
1754
|
+
if (typeof file.source === "string") {
|
|
1755
|
+
const response = await fetch(file.source);
|
|
1756
|
+
if (!response.ok) {
|
|
1757
|
+
throw new Error(`Failed to fetch file: ${response.status}`);
|
|
1758
|
+
}
|
|
1759
|
+
return response.arrayBuffer();
|
|
1760
|
+
}
|
|
1761
|
+
throw new Error("Unsupported file source.");
|
|
1762
|
+
}
|
|
1763
|
+
async function readTextFile(file) {
|
|
1764
|
+
const decode = (buffer) => decodeTextBuffer(buffer);
|
|
1765
|
+
if (typeof file.source === "string") {
|
|
1766
|
+
const response = await fetch(file.source);
|
|
1767
|
+
if (!response.ok) {
|
|
1768
|
+
throw new Error(`Failed to fetch text file: ${response.status}`);
|
|
1769
|
+
}
|
|
1770
|
+
return decode(await response.arrayBuffer());
|
|
1771
|
+
}
|
|
1772
|
+
if (file.blob) {
|
|
1773
|
+
return decode(await file.blob.arrayBuffer());
|
|
1774
|
+
}
|
|
1775
|
+
if (file.source instanceof ArrayBuffer) {
|
|
1776
|
+
return decode(file.source);
|
|
1777
|
+
}
|
|
1778
|
+
return String(file.source);
|
|
1779
|
+
}
|
|
1780
|
+
function createPanel(className = "") {
|
|
1781
|
+
const panel = document.createElement("div");
|
|
1782
|
+
panel.className = `ofv-panel ${className}`.trim();
|
|
1783
|
+
return panel;
|
|
1784
|
+
}
|
|
1785
|
+
function getInitialZoom(ctx, min = 0.1, max = 8) {
|
|
1786
|
+
return Math.min(max, Math.max(min, ctx.options.zoom));
|
|
1787
|
+
}
|
|
1788
|
+
function createSection(title) {
|
|
1789
|
+
const section = document.createElement("section");
|
|
1790
|
+
section.className = "ofv-section";
|
|
1791
|
+
const heading = document.createElement("h3");
|
|
1792
|
+
heading.textContent = title;
|
|
1793
|
+
section.append(heading);
|
|
1794
|
+
return section;
|
|
1795
|
+
}
|
|
1796
|
+
function appendMeta(parent, label, value) {
|
|
1797
|
+
const row = document.createElement("div");
|
|
1798
|
+
row.className = "ofv-meta-row";
|
|
1799
|
+
const key = document.createElement("span");
|
|
1800
|
+
key.textContent = label;
|
|
1801
|
+
const content = document.createElement("strong");
|
|
1802
|
+
content.textContent = String(value);
|
|
1803
|
+
row.append(key, content);
|
|
1804
|
+
parent.append(row);
|
|
1805
|
+
}
|
|
1806
|
+
function decodeTextBuffer(buffer) {
|
|
1807
|
+
return decodeTextBytes(new Uint8Array(buffer));
|
|
1808
|
+
}
|
|
1809
|
+
function decodeTextBytes(bytes) {
|
|
1810
|
+
if (bytes.length >= 3 && bytes[0] === 239 && bytes[1] === 187 && bytes[2] === 191) {
|
|
1811
|
+
return new TextDecoder("utf-8").decode(bytes.subarray(3));
|
|
1812
|
+
}
|
|
1813
|
+
if (bytes.length >= 2) {
|
|
1814
|
+
if (bytes[0] === 255 && bytes[1] === 254) {
|
|
1815
|
+
return new TextDecoder("utf-16le").decode(bytes.subarray(2));
|
|
1816
|
+
}
|
|
1817
|
+
if (bytes[0] === 254 && bytes[1] === 255) {
|
|
1818
|
+
return new TextDecoder("utf-16be").decode(bytes.subarray(2));
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
try {
|
|
1822
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
1823
|
+
} catch {
|
|
1824
|
+
return decodeWithFallback(bytes, "gb18030") || decodeWithFallback(bytes, "gbk") || new TextDecoder("utf-8").decode(bytes);
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
function decodeWithFallback(bytes, encoding) {
|
|
1828
|
+
try {
|
|
1829
|
+
return new TextDecoder(encoding).decode(bytes);
|
|
1830
|
+
} catch {
|
|
1831
|
+
return void 0;
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
function resolveFormat(file, mimeMap) {
|
|
1835
|
+
return file.extension || mimeMap[file.mimeType] || "";
|
|
1836
|
+
}
|
|
1837
|
+
|
|
1699
1838
|
// src/plugins/image.ts
|
|
1700
1839
|
var imageExtensions = /* @__PURE__ */ new Set([
|
|
1701
1840
|
"jpg",
|
|
@@ -1809,7 +1948,7 @@ function imagePlugin() {
|
|
|
1809
1948
|
canvasSource.setAttribute("role", "img");
|
|
1810
1949
|
canvasSource.setAttribute("aria-label", ctx.file.name);
|
|
1811
1950
|
}
|
|
1812
|
-
let scale =
|
|
1951
|
+
let scale = getInitialZoom(ctx);
|
|
1813
1952
|
let rotation = 0;
|
|
1814
1953
|
let offsetX = 0;
|
|
1815
1954
|
let offsetY = 0;
|
|
@@ -3637,95 +3776,6 @@ function readAiffExtended(bytes, offset) {
|
|
|
3637
3776
|
return mantissa * Math.pow(2, exponent - 16383 - 63);
|
|
3638
3777
|
}
|
|
3639
3778
|
|
|
3640
|
-
// src/plugins/utils.ts
|
|
3641
|
-
async function readArrayBuffer(file) {
|
|
3642
|
-
if (file.source instanceof ArrayBuffer) {
|
|
3643
|
-
return file.source;
|
|
3644
|
-
}
|
|
3645
|
-
if (file.blob) {
|
|
3646
|
-
return file.blob.arrayBuffer();
|
|
3647
|
-
}
|
|
3648
|
-
if (typeof file.source === "string") {
|
|
3649
|
-
const response = await fetch(file.source);
|
|
3650
|
-
if (!response.ok) {
|
|
3651
|
-
throw new Error(`Failed to fetch file: ${response.status}`);
|
|
3652
|
-
}
|
|
3653
|
-
return response.arrayBuffer();
|
|
3654
|
-
}
|
|
3655
|
-
throw new Error("Unsupported file source.");
|
|
3656
|
-
}
|
|
3657
|
-
async function readTextFile(file) {
|
|
3658
|
-
const decode = (buffer) => decodeTextBuffer(buffer);
|
|
3659
|
-
if (typeof file.source === "string") {
|
|
3660
|
-
const response = await fetch(file.source);
|
|
3661
|
-
if (!response.ok) {
|
|
3662
|
-
throw new Error(`Failed to fetch text file: ${response.status}`);
|
|
3663
|
-
}
|
|
3664
|
-
return decode(await response.arrayBuffer());
|
|
3665
|
-
}
|
|
3666
|
-
if (file.blob) {
|
|
3667
|
-
return decode(await file.blob.arrayBuffer());
|
|
3668
|
-
}
|
|
3669
|
-
if (file.source instanceof ArrayBuffer) {
|
|
3670
|
-
return decode(file.source);
|
|
3671
|
-
}
|
|
3672
|
-
return String(file.source);
|
|
3673
|
-
}
|
|
3674
|
-
function createPanel(className = "") {
|
|
3675
|
-
const panel = document.createElement("div");
|
|
3676
|
-
panel.className = `ofv-panel ${className}`.trim();
|
|
3677
|
-
return panel;
|
|
3678
|
-
}
|
|
3679
|
-
function createSection(title) {
|
|
3680
|
-
const section = document.createElement("section");
|
|
3681
|
-
section.className = "ofv-section";
|
|
3682
|
-
const heading = document.createElement("h3");
|
|
3683
|
-
heading.textContent = title;
|
|
3684
|
-
section.append(heading);
|
|
3685
|
-
return section;
|
|
3686
|
-
}
|
|
3687
|
-
function appendMeta(parent, label, value) {
|
|
3688
|
-
const row = document.createElement("div");
|
|
3689
|
-
row.className = "ofv-meta-row";
|
|
3690
|
-
const key = document.createElement("span");
|
|
3691
|
-
key.textContent = label;
|
|
3692
|
-
const content = document.createElement("strong");
|
|
3693
|
-
content.textContent = String(value);
|
|
3694
|
-
row.append(key, content);
|
|
3695
|
-
parent.append(row);
|
|
3696
|
-
}
|
|
3697
|
-
function decodeTextBuffer(buffer) {
|
|
3698
|
-
return decodeTextBytes(new Uint8Array(buffer));
|
|
3699
|
-
}
|
|
3700
|
-
function decodeTextBytes(bytes) {
|
|
3701
|
-
if (bytes.length >= 3 && bytes[0] === 239 && bytes[1] === 187 && bytes[2] === 191) {
|
|
3702
|
-
return new TextDecoder("utf-8").decode(bytes.subarray(3));
|
|
3703
|
-
}
|
|
3704
|
-
if (bytes.length >= 2) {
|
|
3705
|
-
if (bytes[0] === 255 && bytes[1] === 254) {
|
|
3706
|
-
return new TextDecoder("utf-16le").decode(bytes.subarray(2));
|
|
3707
|
-
}
|
|
3708
|
-
if (bytes[0] === 254 && bytes[1] === 255) {
|
|
3709
|
-
return new TextDecoder("utf-16be").decode(bytes.subarray(2));
|
|
3710
|
-
}
|
|
3711
|
-
}
|
|
3712
|
-
try {
|
|
3713
|
-
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
3714
|
-
} catch {
|
|
3715
|
-
return decodeWithFallback(bytes, "gb18030") || decodeWithFallback(bytes, "gbk") || new TextDecoder("utf-8").decode(bytes);
|
|
3716
|
-
}
|
|
3717
|
-
}
|
|
3718
|
-
function decodeWithFallback(bytes, encoding) {
|
|
3719
|
-
try {
|
|
3720
|
-
return new TextDecoder(encoding).decode(bytes);
|
|
3721
|
-
} catch {
|
|
3722
|
-
return void 0;
|
|
3723
|
-
}
|
|
3724
|
-
}
|
|
3725
|
-
function resolveFormat(file, mimeMap) {
|
|
3726
|
-
return file.extension || mimeMap[file.mimeType] || "";
|
|
3727
|
-
}
|
|
3728
|
-
|
|
3729
3779
|
// src/plugins/text.ts
|
|
3730
3780
|
var langMap = {
|
|
3731
3781
|
js: "javascript",
|
|
@@ -4194,7 +4244,7 @@ function textPlugin() {
|
|
|
4194
4244
|
};
|
|
4195
4245
|
}
|
|
4196
4246
|
function createTextZoomController(target, cssVariable, ctx) {
|
|
4197
|
-
let zoom =
|
|
4247
|
+
let zoom = getInitialZoom(ctx, 0.5, 3);
|
|
4198
4248
|
const apply = () => {
|
|
4199
4249
|
const normalized = Math.round(zoom * 100) / 100;
|
|
4200
4250
|
target.style.setProperty(cssVariable, String(normalized));
|
|
@@ -4504,6 +4554,7 @@ function pdfPlugin(options = {}) {
|
|
|
4504
4554
|
viewport: ctx.viewport,
|
|
4505
4555
|
size: ctx.size,
|
|
4506
4556
|
fit: ctx.options.fit,
|
|
4557
|
+
zoom: ctx.options.zoom,
|
|
4507
4558
|
toolbar: ctx.toolbar,
|
|
4508
4559
|
encryptedTitle: "PDF \u5DF2\u52A0\u5BC6\uFF0C\u65E0\u6CD5\u5728\u7EBF\u9884\u89C8",
|
|
4509
4560
|
encryptedMessage: "\u8BF7\u4E0B\u8F7D\u540E\u4F7F\u7528\u5BC6\u7801\u6253\u5F00\uFF0C\u6216\u4E0A\u4F20\u89E3\u5BC6\u540E\u7684 PDF \u6587\u4EF6\u3002",
|
|
@@ -4609,7 +4660,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4609
4660
|
const pageStates = [];
|
|
4610
4661
|
let observer = null;
|
|
4611
4662
|
let currentSize = options.size;
|
|
4612
|
-
let zoomFactor = 1;
|
|
4663
|
+
let zoomFactor = getInitialZoom({ options: { zoom: options.zoom ?? 1 } }, 0.25, 4);
|
|
4613
4664
|
let rotation = 0;
|
|
4614
4665
|
const updateSummary = () => {
|
|
4615
4666
|
renderPdfSummary(summary, pdfDocument.numPages, pagesMeta, options.fit, zoomFactor);
|
|
@@ -5934,6 +5985,7 @@ async function renderConvertedOfficePreview(panel, ctx, options, conversionConte
|
|
|
5934
5985
|
viewport: panel,
|
|
5935
5986
|
size: ctx.size,
|
|
5936
5987
|
fit: ctx.options.fit,
|
|
5988
|
+
zoom: ctx.options.zoom,
|
|
5937
5989
|
toolbar: ctx.toolbar,
|
|
5938
5990
|
title: "Office \u9AD8\u4FDD\u771F\u8F6C\u6362\u9884\u89C8",
|
|
5939
5991
|
fallbackTitle: "Office \u8F6C\u6362\u540E\u7684 PDF \u65E0\u6CD5\u9884\u89C8",
|
|
@@ -5997,7 +6049,7 @@ function createOfficeZoomController(panel, ctx) {
|
|
|
5997
6049
|
if (!canZoom) {
|
|
5998
6050
|
return void 0;
|
|
5999
6051
|
}
|
|
6000
|
-
let zoom =
|
|
6052
|
+
let zoom = getInitialZoom(ctx, 0.5, 3);
|
|
6001
6053
|
const apply = () => {
|
|
6002
6054
|
panel.style.setProperty("--ofv-office-zoom", String(zoom));
|
|
6003
6055
|
panel.dispatchEvent(new CustomEvent("ofv-office-zoom"));
|
|
@@ -18817,7 +18869,7 @@ async function createPostScriptPreview(bytes, url, fileName, size, fit, toolbar)
|
|
|
18817
18869
|
preview.append(summary);
|
|
18818
18870
|
return { element: preview };
|
|
18819
18871
|
}
|
|
18820
|
-
async function createPdfCompatibleAiPreview(bytes, url, fileName, size, fit, toolbar, pdfOffset = 0) {
|
|
18872
|
+
async function createPdfCompatibleAiPreview(bytes, url, fileName, size, fit, toolbar, pdfOffset = 0, zoom = 1) {
|
|
18821
18873
|
const wrapper = document.createElement("div");
|
|
18822
18874
|
wrapper.className = "ofv-ai-pdf-preview";
|
|
18823
18875
|
let pdfUrl = url;
|
|
@@ -18832,6 +18884,7 @@ async function createPdfCompatibleAiPreview(bytes, url, fileName, size, fit, too
|
|
|
18832
18884
|
viewport: wrapper,
|
|
18833
18885
|
size,
|
|
18834
18886
|
fit,
|
|
18887
|
+
zoom,
|
|
18835
18888
|
toolbar,
|
|
18836
18889
|
fallbackTitle: "AI PDF \u517C\u5BB9\u9884\u89C8\u5931\u8D25",
|
|
18837
18890
|
revokeUrlOnDestroy: false
|