@open-file-viewer/core 0.1.8 → 0.1.10
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 +35 -0
- package/dist/index.cjs +55 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +55 -20
- package/dist/index.js.map +1 -1
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -36,6 +36,24 @@ Copy `libredwg-web.wasm` to a public directory and point `cadPlugin` to it:
|
|
|
36
36
|
cadPlugin({ libreDwg: { wasmBaseUrl: "/vendor/libredwg-web" } });
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
+
Native browser video formats such as MP4, WebM and MOV do not need extra dependencies. HLS uses `hls.js`, which is bundled with the core package. FLV and MPEG-TS/M2TS playback is optional: install `mpegts.js` in your application only if you need those formats. If it is not installed, `videoPlugin()` shows the built-in download fallback for FLV/M2TS files.
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm install mpegts.js
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`mpegts.js` currently depends on a git-based `webworkify-webpack` fork. pnpm 11 users with `blockExoticSubdeps` enabled can keep `@open-file-viewer/core` installed normally because `mpegts.js` is no longer a required dependency. If your app really needs FLV/M2TS playback, either allow that dependency in your app or override it to the npm release:
|
|
46
|
+
|
|
47
|
+
```json
|
|
48
|
+
{
|
|
49
|
+
"pnpm": {
|
|
50
|
+
"overrides": {
|
|
51
|
+
"webworkify-webpack": "2.1.5"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
39
57
|
## Quick Start
|
|
40
58
|
|
|
41
59
|
```ts
|
|
@@ -88,6 +106,23 @@ viewer.resize();
|
|
|
88
106
|
viewer.destroy();
|
|
89
107
|
```
|
|
90
108
|
|
|
109
|
+
### PDF loading compatibility
|
|
110
|
+
|
|
111
|
+
If PDF preview falls back in Umi Max, utoo pack or similar build environments and the console shows
|
|
112
|
+
`Cannot set properties of undefined (setting 'onPull')` from pdf.js, enable `useFetchData`. It fetches
|
|
113
|
+
the PDF bytes on the main thread and then passes `data` to pdf.js, avoiding the worker network stream
|
|
114
|
+
path that can break in those bundlers:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
pdfPlugin({
|
|
118
|
+
workerSrc: pdfWorkerSrc,
|
|
119
|
+
useFetchData: true
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
This keeps compatibility at the cost of holding one extra copy of the PDF in memory, so use it only
|
|
124
|
+
for affected environments.
|
|
125
|
+
|
|
91
126
|
## CAD Customization
|
|
92
127
|
|
|
93
128
|
`cadPlugin()` has two CAD preview layers:
|
package/dist/index.cjs
CHANGED
|
@@ -2569,6 +2569,11 @@ function objectFit(fit) {
|
|
|
2569
2569
|
}
|
|
2570
2570
|
|
|
2571
2571
|
// src/plugins/video.ts
|
|
2572
|
+
var mpegtsPackageName = "mpegts.js";
|
|
2573
|
+
var loadMpegts = () => import(
|
|
2574
|
+
/* @vite-ignore */
|
|
2575
|
+
mpegtsPackageName
|
|
2576
|
+
);
|
|
2572
2577
|
var videoExtensions = /* @__PURE__ */ new Set([
|
|
2573
2578
|
"mp4",
|
|
2574
2579
|
"mpg",
|
|
@@ -2683,7 +2688,8 @@ function videoPlugin() {
|
|
|
2683
2688
|
showTranscodeFallback();
|
|
2684
2689
|
}
|
|
2685
2690
|
} else if (isFlv || isMpegTs) {
|
|
2686
|
-
const
|
|
2691
|
+
const mpegtsModule = await loadMpegts();
|
|
2692
|
+
const mpegts = resolveMpegtsApi(mpegtsModule);
|
|
2687
2693
|
if (mpegts.isSupported()) {
|
|
2688
2694
|
mpegtsPlayer = mpegts.createPlayer({
|
|
2689
2695
|
type: isFlv ? "flv" : "mpegts",
|
|
@@ -2735,6 +2741,13 @@ function videoPlugin() {
|
|
|
2735
2741
|
}
|
|
2736
2742
|
};
|
|
2737
2743
|
}
|
|
2744
|
+
function resolveMpegtsApi(module2) {
|
|
2745
|
+
const api = module2.default || module2;
|
|
2746
|
+
if (!api.Events || !api.isSupported || !api.createPlayer) {
|
|
2747
|
+
throw new Error("mpegts.js is not available.");
|
|
2748
|
+
}
|
|
2749
|
+
return api;
|
|
2750
|
+
}
|
|
2738
2751
|
function createVideoTransformController(video, ctx) {
|
|
2739
2752
|
let scale = 1;
|
|
2740
2753
|
let rotation = 0;
|
|
@@ -4563,14 +4576,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4563
4576
|
scroller.className = "ofv-pdf ofv-pdf-pages";
|
|
4564
4577
|
viewer.append(summary, scroller);
|
|
4565
4578
|
options.viewport.append(viewer);
|
|
4566
|
-
const
|
|
4567
|
-
url: options.fileUrl,
|
|
4568
|
-
cMapUrl: options.cMapUrl ?? `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdf.version}/cmaps/`,
|
|
4569
|
-
cMapPacked: options.cMapPacked ?? true,
|
|
4570
|
-
standardFontDataUrl: options.standardFontDataUrl ?? `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdf.version}/standard_fonts/`,
|
|
4571
|
-
useSystemFonts: options.useSystemFonts ?? true
|
|
4572
|
-
});
|
|
4573
|
-
const doc = await documentTask.promise.catch((error) => {
|
|
4579
|
+
const showDocumentFallback = (error) => {
|
|
4574
4580
|
viewer.remove();
|
|
4575
4581
|
options.viewport.classList.add("ofv-center");
|
|
4576
4582
|
const fileLike = {
|
|
@@ -4587,8 +4593,29 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4587
4593
|
action: options.encryptedAction || "\u4E0B\u8F7D PDF"
|
|
4588
4594
|
}) : createPdfFallback(options.fileName, options.fileUrl, normalizePdfError(error), options.fallbackTitle);
|
|
4589
4595
|
options.viewport.append(fallback);
|
|
4590
|
-
|
|
4591
|
-
|
|
4596
|
+
};
|
|
4597
|
+
let documentTask;
|
|
4598
|
+
let doc;
|
|
4599
|
+
try {
|
|
4600
|
+
const pdfData = options.useFetchData ? await loadPdfData(options.fileUrl) : void 0;
|
|
4601
|
+
documentTask = pdf.getDocument({
|
|
4602
|
+
...pdfData ? { data: pdfData } : { url: options.fileUrl },
|
|
4603
|
+
cMapUrl: options.cMapUrl ?? `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdf.version}/cmaps/`,
|
|
4604
|
+
cMapPacked: options.cMapPacked ?? true,
|
|
4605
|
+
standardFontDataUrl: options.standardFontDataUrl ?? `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdf.version}/standard_fonts/`,
|
|
4606
|
+
useSystemFonts: options.useSystemFonts ?? true,
|
|
4607
|
+
disableStream: options.disableStream,
|
|
4608
|
+
disableAutoFetch: options.disableAutoFetch,
|
|
4609
|
+
disableRange: options.disableRange,
|
|
4610
|
+
rangeChunkSize: options.rangeChunkSize
|
|
4611
|
+
});
|
|
4612
|
+
doc = await documentTask.promise.catch((error) => {
|
|
4613
|
+
showDocumentFallback(error);
|
|
4614
|
+
return void 0;
|
|
4615
|
+
});
|
|
4616
|
+
} catch (error) {
|
|
4617
|
+
showDocumentFallback(error);
|
|
4618
|
+
}
|
|
4592
4619
|
if (!doc) {
|
|
4593
4620
|
return {
|
|
4594
4621
|
canCommand() {
|
|
@@ -4601,17 +4628,18 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4601
4628
|
},
|
|
4602
4629
|
destroy() {
|
|
4603
4630
|
options.viewport.classList.remove("ofv-center");
|
|
4604
|
-
documentTask
|
|
4631
|
+
documentTask?.destroy?.();
|
|
4605
4632
|
if (options.revokeUrlOnDestroy) {
|
|
4606
4633
|
revokeObjectUrl(options.fileUrl, Boolean(options.isExternal));
|
|
4607
4634
|
}
|
|
4608
4635
|
}
|
|
4609
4636
|
};
|
|
4610
4637
|
}
|
|
4638
|
+
const pdfDocument = doc;
|
|
4611
4639
|
const pagesMeta = [];
|
|
4612
|
-
for (let pageNumber = 1; pageNumber <=
|
|
4640
|
+
for (let pageNumber = 1; pageNumber <= pdfDocument.numPages; pageNumber += 1) {
|
|
4613
4641
|
try {
|
|
4614
|
-
const page = await
|
|
4642
|
+
const page = await pdfDocument.getPage(pageNumber);
|
|
4615
4643
|
const baseViewport = page.getViewport({ scale: 1 });
|
|
4616
4644
|
pagesMeta.push({
|
|
4617
4645
|
width: baseViewport.width,
|
|
@@ -4627,7 +4655,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4627
4655
|
let zoomFactor = 1;
|
|
4628
4656
|
let rotation = 0;
|
|
4629
4657
|
const updateSummary = () => {
|
|
4630
|
-
renderPdfSummary(summary,
|
|
4658
|
+
renderPdfSummary(summary, pdfDocument.numPages, pagesMeta, options.fit, zoomFactor);
|
|
4631
4659
|
options.toolbar?.setZoom(zoomFactor);
|
|
4632
4660
|
};
|
|
4633
4661
|
const clearPage = (pageIdx) => {
|
|
@@ -4650,7 +4678,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4650
4678
|
if (!state || state.rendered) return;
|
|
4651
4679
|
state.rendered = true;
|
|
4652
4680
|
try {
|
|
4653
|
-
const page = await
|
|
4681
|
+
const page = await pdfDocument.getPage(pageIdx + 1);
|
|
4654
4682
|
const meta = pagesMeta[pageIdx];
|
|
4655
4683
|
const scale = options.fit === "actual" ? zoomFactor : Math.max(0.05, Math.min(5, getPdfAvailableWidth(size.width) / rotatedPdfWidth(meta, rotation) * zoomFactor));
|
|
4656
4684
|
const viewport = page.getViewport({ scale, rotation });
|
|
@@ -4737,7 +4765,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4737
4765
|
if (!state.rendered) {
|
|
4738
4766
|
void renderPage(pageIdx, size);
|
|
4739
4767
|
}
|
|
4740
|
-
} else if (state.rendered &&
|
|
4768
|
+
} else if (state.rendered && pdfDocument.numPages > 8) {
|
|
4741
4769
|
clearPage(pageIdx);
|
|
4742
4770
|
}
|
|
4743
4771
|
});
|
|
@@ -4748,7 +4776,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4748
4776
|
}
|
|
4749
4777
|
);
|
|
4750
4778
|
}
|
|
4751
|
-
for (let i = 0; i <
|
|
4779
|
+
for (let i = 0; i < pdfDocument.numPages; i++) {
|
|
4752
4780
|
const meta = pagesMeta[i];
|
|
4753
4781
|
const rotatedWidth = rotatedPdfWidth(meta, rotation);
|
|
4754
4782
|
const rotatedHeight = rotatedPdfHeight(meta, rotation);
|
|
@@ -4776,7 +4804,7 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4776
4804
|
}
|
|
4777
4805
|
if (observer) {
|
|
4778
4806
|
window.setTimeout(() => {
|
|
4779
|
-
const eagerPages =
|
|
4807
|
+
const eagerPages = pdfDocument.numPages > 8 ? 2 : pdfDocument.numPages;
|
|
4780
4808
|
for (let i = 0; i < eagerPages; i++) {
|
|
4781
4809
|
void renderPage(i, size);
|
|
4782
4810
|
}
|
|
@@ -4833,10 +4861,10 @@ async function renderPdfDocumentPreview(options) {
|
|
|
4833
4861
|
}
|
|
4834
4862
|
});
|
|
4835
4863
|
pageStates.length = 0;
|
|
4864
|
+
void pdfDocument.destroy?.();
|
|
4836
4865
|
if (options.revokeUrlOnDestroy) {
|
|
4837
4866
|
revokeObjectUrl(options.fileUrl, Boolean(options.isExternal));
|
|
4838
4867
|
}
|
|
4839
|
-
void doc.destroy();
|
|
4840
4868
|
}
|
|
4841
4869
|
};
|
|
4842
4870
|
}
|
|
@@ -4899,6 +4927,13 @@ function normalizePdfOptions(options) {
|
|
|
4899
4927
|
}
|
|
4900
4928
|
return options;
|
|
4901
4929
|
}
|
|
4930
|
+
async function loadPdfData(url) {
|
|
4931
|
+
const response = await fetch(url);
|
|
4932
|
+
if (!response.ok) {
|
|
4933
|
+
throw new Error(`Failed to load PDF data: ${response.status} ${response.statusText}`);
|
|
4934
|
+
}
|
|
4935
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
4936
|
+
}
|
|
4902
4937
|
function isCanvasVisuallyBlank(canvas, context) {
|
|
4903
4938
|
if (canvas.width === 0 || canvas.height === 0 || typeof context.getImageData !== "function") {
|
|
4904
4939
|
return false;
|