@flyfish-group/file-viewer-react 1.0.24 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +39 -0
- package/README.md +6 -17
- package/dist/index.d.ts +2 -54
- package/dist/index.js +1 -112
- package/package.json +6 -5
package/README.en.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @flyfish-group/file-viewer-react
|
|
2
|
+
|
|
3
|
+
React file preview component. This package is the historical alias of `@file-viewer/react` and now provides the same native React component experience: it mounts the shared core directly and exposes consistent props, events, and ref APIs.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @flyfish-group/file-viewer-react
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import FileViewer from '@flyfish-group/file-viewer-react'
|
|
11
|
+
|
|
12
|
+
export function Preview() {
|
|
13
|
+
return (
|
|
14
|
+
<div style={{ height: '100vh' }}>
|
|
15
|
+
<FileViewer
|
|
16
|
+
url="/files/demo.docx"
|
|
17
|
+
options={{
|
|
18
|
+
theme: 'light',
|
|
19
|
+
toolbar: { position: 'bottom-right' },
|
|
20
|
+
archive: { cache: true }
|
|
21
|
+
}}
|
|
22
|
+
onViewerEvent={(event) => {
|
|
23
|
+
console.log(event.type, event.event, event.payload)
|
|
24
|
+
}}
|
|
25
|
+
/>
|
|
26
|
+
</div>
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`file` takes precedence over `url`. When passing a `Blob` or `ArrayBuffer`, also provide `name`, for example `contract.pdf`, so the viewer can detect the format:
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
<FileViewer file={blob} name="contract.pdf" />
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`options` cover theme, watermarking, search, unified zoom, download, print, HTML export, beforeOperation guards, lifecycle hooks, and format-specific settings. Lifecycle, operation availability, search state, and document location updates are emitted through `onViewerEvent`.
|
|
38
|
+
|
|
39
|
+
New projects should prefer the standard package name `@file-viewer/react`. Official documentation: https://doc.flyfish-viewer.app/
|
package/README.md
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
# @flyfish-group/file-viewer-react
|
|
2
2
|
|
|
3
|
-
React
|
|
3
|
+
React 文件预览组件。这个包是 `@file-viewer/react` 的历史包名同步版本,当前提供原生 React 组件体验: 组件内部直接挂载共享预览底座,并暴露一致的 props、事件和 ref API。
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
|
-
npm install @flyfish-group/file-viewer-react
|
|
6
|
+
npm install @flyfish-group/file-viewer-react
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
pnpm 10 默认会拦截依赖包的 `postinstall`。如果安装后提示 `Ignored build scripts: @flyfish-group/file-viewer-web`,请执行 `pnpm approve-builds` 允许该包,或运行 `pnpm exec file-viewer-copy-assets ./public/file-viewer`。复制脚本会先清空目标目录再复制,避免 `index.html` 和 `assets/*` hash 不同版本导致动态 import 404。
|
|
10
|
-
|
|
11
9
|
```tsx
|
|
12
10
|
import FileViewer from '@flyfish-group/file-viewer-react'
|
|
13
11
|
|
|
@@ -23,10 +21,7 @@ export function Preview() {
|
|
|
23
21
|
theme: 'light',
|
|
24
22
|
toolbar: { position: 'bottom-right' },
|
|
25
23
|
watermark: { text: '内部预览', opacity: 0.14 },
|
|
26
|
-
archive: {
|
|
27
|
-
workerUrl: '/file-viewer/vendor/libarchive/worker-bundle.js',
|
|
28
|
-
cache: true
|
|
29
|
-
}
|
|
24
|
+
archive: { cache: true }
|
|
30
25
|
}}
|
|
31
26
|
/>
|
|
32
27
|
</div>
|
|
@@ -34,18 +29,12 @@ export function Preview() {
|
|
|
34
29
|
}
|
|
35
30
|
```
|
|
36
31
|
|
|
37
|
-
`file` 优先级高于 `url`。当传入 `Blob` 或 `ArrayBuffer` 时,请同时传 `name`,例如 `contract.pdf
|
|
32
|
+
`file` 优先级高于 `url`。当传入 `Blob` 或 `ArrayBuffer` 时,请同时传 `name`,例如 `contract.pdf`,用于识别格式:
|
|
38
33
|
|
|
39
34
|
```tsx
|
|
40
35
|
<FileViewer file={blob} name="contract.pdf" />
|
|
41
36
|
```
|
|
42
37
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
```tsx
|
|
46
|
-
<FileViewer viewerUrl="/vendor/file-viewer/index.html" url={url} />
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
React 组件内部复用 `mountViewerFrame` 的地址协议,会默认追加 `__flyfish_viewer_version` 来绕开旧 iframe 入口页缓存;静态服务已经保证 HTML 不缓存时,可传 `cacheKey={false}` 关闭。
|
|
38
|
+
`options` 支持主题、水印、搜索、统一缩放、下载、打印、导出 HTML、beforeOperation 前置校验、生命周期 hooks 和格式专项参数。生命周期、操作能力变化、搜索状态和当前位置会通过 `onViewerEvent` 回传给宿主。
|
|
50
39
|
|
|
51
|
-
|
|
40
|
+
新项目建议优先使用标准包名 `@file-viewer/react`。官方文档: https://doc.flyfish-viewer.app/
|
package/dist/index.d.ts
CHANGED
|
@@ -1,54 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export type { FileRef, ViewerFrameEventHandler, ViewerFrameOptions, ViewerRuntimeOptions, ViewerToolbarOptions, ViewerToolbarPosition, ViewerThemeMode } from '@flyfish-group/file-viewer-web';
|
|
4
|
-
export interface FileViewerHandle {
|
|
5
|
-
iframe: HTMLIFrameElement | null;
|
|
6
|
-
postFile(): boolean;
|
|
7
|
-
reload(): void;
|
|
8
|
-
}
|
|
9
|
-
export interface FileViewerProps extends Omit<IframeHTMLAttributes<HTMLIFrameElement>, 'children' | 'src'> {
|
|
10
|
-
/**
|
|
11
|
-
* 私有化部署后的 Vue 基线预览器页面地址。
|
|
12
|
-
*
|
|
13
|
-
* 不传时默认使用 `@flyfish-group/file-viewer-web` 安装后复制到宿主项目的 `/file-viewer/index.html`。
|
|
14
|
-
*/
|
|
15
|
-
viewerUrl?: string;
|
|
16
|
-
/**
|
|
17
|
-
* 远端文件地址。会透传给 iframe 里的 Vue 基线预览器。
|
|
18
|
-
*/
|
|
19
|
-
url?: string;
|
|
20
|
-
/**
|
|
21
|
-
* 本地二进制输入。优先级高于 url,会通过 postMessage 推送给 iframe。
|
|
22
|
-
*/
|
|
23
|
-
file?: FileRef;
|
|
24
|
-
/**
|
|
25
|
-
* 当 file 是 Blob 或 ArrayBuffer 时用于识别扩展名。
|
|
26
|
-
*/
|
|
27
|
-
name?: string;
|
|
28
|
-
/**
|
|
29
|
-
* 允许推送二进制的宿主 origin。默认取当前页面 origin。
|
|
30
|
-
*/
|
|
31
|
-
from?: string;
|
|
32
|
-
/**
|
|
33
|
-
* postMessage 的目标 origin。默认从 viewerUrl 推导。
|
|
34
|
-
*/
|
|
35
|
-
targetOrigin?: string;
|
|
36
|
-
/**
|
|
37
|
-
* 预留给 Vue 基线页面的查询参数。
|
|
38
|
-
*/
|
|
39
|
-
params?: ViewerFrameOptions['params'];
|
|
40
|
-
/**
|
|
41
|
-
* iframe 入口页的缓存标识。默认使用 web 包版本;传 false 可关闭。
|
|
42
|
-
*/
|
|
43
|
-
cacheKey?: ViewerFrameOptions['cacheKey'];
|
|
44
|
-
/**
|
|
45
|
-
* 透传给 Vue 基线预览器的运行时选项,例如水印、工具栏和压缩包缓存限制。
|
|
46
|
-
*/
|
|
47
|
-
options?: ViewerFrameOptions['options'];
|
|
48
|
-
/**
|
|
49
|
-
* iframe 模式下接收基线预览器抛出的生命周期和操作事件。
|
|
50
|
-
*/
|
|
51
|
-
onViewerEvent?: ViewerFrameEventHandler;
|
|
52
|
-
}
|
|
53
|
-
export declare const FileViewer: import("react").ForwardRefExoticComponent<FileViewerProps & import("react").RefAttributes<FileViewerHandle>>;
|
|
54
|
-
export default FileViewer;
|
|
1
|
+
export { FileViewer, default, } from '@file-viewer/react';
|
|
2
|
+
export type { FileRef, FileViewerHandle, FileViewerProps, ViewerAiOptions, ViewerArchiveOptions, ViewerCadOptions, ViewerController, ViewerControllerAccessor, ViewerControllerHandle, ViewerDocxOptions, ViewerEvent, ViewerEventHandler, ViewerEventType, ViewerMountOptions, ViewerOptions, ViewerPdfOptions, ViewerSpreadsheetOptions, ViewerSearchOptions, ViewerThemeMode, ViewerToolbarOptions, ViewerToolbarPosition, ViewerTypstOptions, ViewerWatermarkOptions, } from '@file-viewer/react';
|
package/dist/index.js
CHANGED
|
@@ -1,112 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
|
3
|
-
import { buildViewerSrc, postFileToViewer } from '@flyfish-group/file-viewer-web';
|
|
4
|
-
const defaultStyle = {
|
|
5
|
-
width: '100%',
|
|
6
|
-
height: '100%',
|
|
7
|
-
border: 0,
|
|
8
|
-
display: 'block'
|
|
9
|
-
};
|
|
10
|
-
export const FileViewer = forwardRef((props, forwardedRef) => {
|
|
11
|
-
const { viewerUrl, url, file, name, from, targetOrigin, params, cacheKey, options, onViewerEvent, onLoad, style, title = 'Flyfish Viewer 文件预览', ...iframeProps } = props;
|
|
12
|
-
const iframeRef = useRef(null);
|
|
13
|
-
const [frameReady, setFrameReady] = useState(false);
|
|
14
|
-
const frameOptions = useMemo(() => ({
|
|
15
|
-
viewerUrl,
|
|
16
|
-
url,
|
|
17
|
-
file,
|
|
18
|
-
name,
|
|
19
|
-
from,
|
|
20
|
-
targetOrigin,
|
|
21
|
-
params,
|
|
22
|
-
cacheKey,
|
|
23
|
-
options
|
|
24
|
-
}), [viewerUrl, url, file, name, from, targetOrigin, params, cacheKey, options]);
|
|
25
|
-
const src = useMemo(() => buildViewerSrc(frameOptions), [frameOptions]);
|
|
26
|
-
const retryTimerRef = useRef(undefined);
|
|
27
|
-
const retryCountRef = useRef(0);
|
|
28
|
-
const lifecycleAcknowledgedRef = useRef(false);
|
|
29
|
-
const postFile = useCallback(() => {
|
|
30
|
-
return postFileToViewer(iframeRef.current, frameOptions);
|
|
31
|
-
}, [frameOptions]);
|
|
32
|
-
const clearFilePostRetry = useCallback(() => {
|
|
33
|
-
if (retryTimerRef.current) {
|
|
34
|
-
window.clearTimeout(retryTimerRef.current);
|
|
35
|
-
retryTimerRef.current = undefined;
|
|
36
|
-
}
|
|
37
|
-
}, []);
|
|
38
|
-
const scheduleFilePost = useCallback(() => {
|
|
39
|
-
clearFilePostRetry();
|
|
40
|
-
retryCountRef.current = 0;
|
|
41
|
-
lifecycleAcknowledgedRef.current = false;
|
|
42
|
-
if (!frameOptions.file) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const post = () => {
|
|
46
|
-
if (lifecycleAcknowledgedRef.current) {
|
|
47
|
-
clearFilePostRetry();
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
postFile();
|
|
51
|
-
retryCountRef.current += 1;
|
|
52
|
-
if (retryCountRef.current < 8) {
|
|
53
|
-
retryTimerRef.current = window.setTimeout(post, 120);
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
retryTimerRef.current = undefined;
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
post();
|
|
60
|
-
}, [clearFilePostRetry, frameOptions.file, postFile]);
|
|
61
|
-
const reload = useCallback(() => {
|
|
62
|
-
if (iframeRef.current) {
|
|
63
|
-
iframeRef.current.src = src;
|
|
64
|
-
}
|
|
65
|
-
}, [src]);
|
|
66
|
-
useImperativeHandle(forwardedRef, () => ({
|
|
67
|
-
get iframe() {
|
|
68
|
-
return iframeRef.current;
|
|
69
|
-
},
|
|
70
|
-
postFile,
|
|
71
|
-
reload
|
|
72
|
-
}), [postFile, reload]);
|
|
73
|
-
useEffect(() => {
|
|
74
|
-
clearFilePostRetry();
|
|
75
|
-
lifecycleAcknowledgedRef.current = false;
|
|
76
|
-
setFrameReady(false);
|
|
77
|
-
}, [clearFilePostRetry, src]);
|
|
78
|
-
useEffect(() => {
|
|
79
|
-
if (frameReady) {
|
|
80
|
-
scheduleFilePost();
|
|
81
|
-
}
|
|
82
|
-
}, [frameReady, scheduleFilePost]);
|
|
83
|
-
useEffect(() => {
|
|
84
|
-
const handleMessage = (event) => {
|
|
85
|
-
var _a, _b, _c;
|
|
86
|
-
if (event.source !== ((_a = iframeRef.current) === null || _a === void 0 ? void 0 : _a.contentWindow)) {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
if (((_b = event.data) === null || _b === void 0 ? void 0 : _b.type) !== 'flyfish-viewer:lifecycle' &&
|
|
90
|
-
((_c = event.data) === null || _c === void 0 ? void 0 : _c.type) !== 'flyfish-viewer:operation') {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
if (event.data.type === 'flyfish-viewer:lifecycle') {
|
|
94
|
-
lifecycleAcknowledgedRef.current = true;
|
|
95
|
-
clearFilePostRetry();
|
|
96
|
-
}
|
|
97
|
-
onViewerEvent === null || onViewerEvent === void 0 ? void 0 : onViewerEvent(event.data, event);
|
|
98
|
-
};
|
|
99
|
-
window.addEventListener('message', handleMessage);
|
|
100
|
-
return () => window.removeEventListener('message', handleMessage);
|
|
101
|
-
}, [clearFilePostRetry, onViewerEvent]);
|
|
102
|
-
useEffect(() => {
|
|
103
|
-
return () => clearFilePostRetry();
|
|
104
|
-
}, [clearFilePostRetry]);
|
|
105
|
-
const handleLoad = useCallback((event) => {
|
|
106
|
-
setFrameReady(true);
|
|
107
|
-
onLoad === null || onLoad === void 0 ? void 0 : onLoad(event);
|
|
108
|
-
}, [onLoad]);
|
|
109
|
-
return (_jsx("iframe", { ...iframeProps, ref: iframeRef, src: src, title: title, style: { ...defaultStyle, ...style }, onLoad: handleLoad }));
|
|
110
|
-
});
|
|
111
|
-
FileViewer.displayName = 'FileViewer';
|
|
112
|
-
export default FileViewer;
|
|
1
|
+
export { FileViewer, default, } from '@file-viewer/react';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flyfish-group/file-viewer-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Private-deploy React component for Flyfish Viewer",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"file-viewer",
|
|
9
9
|
"document-preview",
|
|
10
10
|
"react",
|
|
11
|
-
"
|
|
11
|
+
"native-viewer",
|
|
12
12
|
"office-preview"
|
|
13
13
|
],
|
|
14
14
|
"publishConfig": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"repository": {
|
|
23
23
|
"type": "git",
|
|
24
24
|
"url": "git+https://github.com/flyfish-dev/file-viewer.git",
|
|
25
|
-
"directory": "packages/react"
|
|
25
|
+
"directory": "packages/compat/react"
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/flyfish-dev/file-viewer",
|
|
28
28
|
"bugs": {
|
|
@@ -41,13 +41,14 @@
|
|
|
41
41
|
},
|
|
42
42
|
"files": [
|
|
43
43
|
"dist",
|
|
44
|
-
"README.md"
|
|
44
|
+
"README.md",
|
|
45
|
+
"README.en.md"
|
|
45
46
|
],
|
|
46
47
|
"peerDependencies": {
|
|
47
48
|
"react": ">=17 <20"
|
|
48
49
|
},
|
|
49
50
|
"dependencies": {
|
|
50
|
-
"@
|
|
51
|
+
"@file-viewer/react": "^2.0.0"
|
|
51
52
|
},
|
|
52
53
|
"devDependencies": {
|
|
53
54
|
"@types/react": "^19.2.17",
|