Loading...
Loading...
Compare original and translation side by side
CogniteFileViewerCogniteFileViewerpackage.jsonpackageManagervite.config.tspackage.jsonpackageManagervite.config.tspnpm add "github:cognitedata/dune-industrial-components#semver:*" react-pdfnpm install "github:cognitedata/dune-industrial-components#semver:*" react-pdfyarn add "github:cognitedata/dune-industrial-components#semver:*" react-pdfpdfjs-distreact-pdfpnpm add "github:cognitedata/dune-industrial-components#semver:*" react-pdfnpm install "github:cognitedata/dune-industrial-components#semver:*" react-pdfyarn add "github:cognitedata/dune-industrial-components#semver:*" react-pdfpdfjs-distreact-pdfpdfjs-distreact-pdfCogniteFileViewerimport { pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url,
).toString();pnpm users: pnpm's strict linking may prevent the browser from resolving. Either addpdfjs-distas a direct dependency (pdfjs-dist), or addpnpm add pdfjs-disttopublic-hoist-pattern[]=pdfjs-dist..npmrc
react-pdfpdfjs-distCogniteFileViewerimport { pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url,
).toString();pnpm用户注意: pnpm的严格链接机制可能导致浏览器无法解析。可选择将pdfjs-dist添加为直接依赖(pdfjs-dist),或在pnpm add pdfjs-dist中添加.npmrc。public-hoist-pattern[]=pdfjs-dist
optimizeDeps.exclude: ['pdfjs-dist']vite.config.tsexport default defineConfig({
// ... existing config ...
optimizeDeps: {
exclude: ['pdfjs-dist'],
},
});vite.config.tsoptimizeDeps.exclude: ['pdfjs-dist']export default defineConfig({
// ... 现有配置 ...
optimizeDeps: {
exclude: ['pdfjs-dist'],
},
});CogniteFileViewerimport { CogniteFileViewer } from '@cognite/dune-industrial-components/file-viewer';sdkuseDune()import { useDune } from '@cognite/dune';
const { sdk } = useDune();CogniteFileViewerimport { CogniteFileViewer } from '@cognite/dune-industrial-components/file-viewer';useDune()sdkimport { useDune } from '@cognite/dune';
const { sdk } = useDune();| Type | Formats |
|---|---|
| |
| Office documents | Word, PowerPoint, Excel, ODS, ODP, ODT, RTF, TSV — converted to PDF via the CDF Document Preview API, then rendered identically to PDF |
| Image | JPEG, PNG, WebP, SVG, TIFF — zoom, pan, rotation |
| Text | |
| Other | Falls back to |
| 类型 | 格式 |
|---|---|
| |
| 办公文档 | Word、PowerPoint、Excel、ODS、ODP、ODT、RTF、TSV — 通过CDF文档预览API转换为PDF,然后以PDF格式渲染 |
| 图片 | JPEG、PNG、WebP、SVG、TIFF — 支持缩放、平移、旋转 |
| 文本 | |
| 其他 | 回退到 |
<CogniteFileViewer
source={{ type: 'internalId', id: file.id }}
client={sdk}
style={{ width: '100%', height: '600px' }}
/>The component needs a defined height. If the parent has no explicit height, the viewer will collapse to zero. Always set aviaheight,style, or the parent container.className
<CogniteFileViewer
source={{ type: 'internalId', id: file.id }}
client={sdk}
style={{ width: '100%', height: '600px' }}
/>组件需要设置明确高度。如果父容器没有显式高度,查看器会收缩为零高度。请始终通过、style或父容器设置className。height
// By instance ID (data-modelled file — enables annotations)
<CogniteFileViewer
source={{ type: 'instanceId', space: 'my-space', externalId: 'my-file' }}
client={sdk}
/>
// By CDF internal ID
<CogniteFileViewer
source={{ type: 'internalId', id: 12345 }}
client={sdk}
/>
// By direct URL
<CogniteFileViewer
source={{ type: 'url', url: 'https://...', mimeType: 'application/pdf' }}
/>instanceIdsdk.files.list()file.instanceIdsource={
file.instanceId
? { type: 'instanceId', space: file.instanceId.space, externalId: file.instanceId.externalId }
: { type: 'internalId', id: file.id }
}// 通过实例ID(数据建模文件——支持批注)
<CogniteFileViewer
source={{ type: 'instanceId', space: 'my-space', externalId: 'my-file' }}
client={sdk}
/>
// 通过CDF内部ID
<CogniteFileViewer
source={{ type: 'internalId', id: 12345 }}
client={sdk}
/>
// 通过直接URL
<CogniteFileViewer
source={{ type: 'url', url: 'https://...', mimeType: 'application/pdf' }}
/>instanceIdsdk.files.list()file.instanceIdsource={
file.instanceId
? { type: 'instanceId', space: file.instanceId.space, externalId: file.instanceId.externalId }
: { type: 'internalId', id: file.id }
}<CogniteFileViewer
// Required
source={source}
client={sdk} // required for instanceId and internalId sources
// PDF pagination
page={page} // controlled current page (1-indexed)
onPageChange={setPage}
onDocumentLoad={({ numPages }) => setNumPages(numPages)}
// Zoom & pan (works on PDF and images)
zoom={zoom} // 1 = 100%; Ctrl/Cmd+wheel, pinch-to-zoom, and middle-click drag built in
onZoomChange={setZoom}
minZoom={0.25} // default
maxZoom={5} // default
panOffset={pan} // controlled pan offset; resets on page change
onPanChange={setPan}
// Fit mode
fitMode="width" // 'width' fits to container width; 'page' fits entire page in container
// Rotation (PDFs and images)
rotation={rotation} // 0 | 90 | 180 | 270
// Diagram annotations (instanceId sources only)
showAnnotations={true} // default
onAnnotationClick={(annotation) => { /* annotation.linkedResource has space + externalId */ }}
onAnnotationHover={(annotation) => {}}
// Custom annotation tooltip (replaces native <title> tooltip)
renderAnnotationTooltip={(annotation, rect) => (
<div style={{
position: 'absolute',
left: rect.x + rect.width,
top: rect.y,
zIndex: 11,
}}>
{annotation.text}
</div>
)}
// Custom overlay (SVG paths, highlights, drawings — works on PDF and images)
renderOverlay={({ width, height, originalWidth, originalHeight, pageNumber, rotation }) => (
<svg
width={width}
height={height}
viewBox={`0 0 ${originalWidth} ${originalHeight}`}
preserveAspectRatio="none"
style={{ position: 'absolute', top: 0, left: 0, pointerEvents: 'all' }}
>
<path d="..." stroke="cyan" fill="none" />
</svg>
)}
// Custom renderers (all optional)
renderLoading={() => <MySpinner />}
renderError={(error) => <MyError message={error.message} />}
renderUnsupported={(mimeType) => <div>Cannot preview {mimeType}</div>}
// Layout
className="..."
style={{ width: '100%', height: '100%' }}
/><CogniteFileViewer
// 必填项
source={source}
client={sdk} // 使用instanceId和internalId源时必填
// PDF分页
page={page} // 受控当前页码(从1开始)
onPageChange={setPage}
onDocumentLoad={({ numPages }) => setNumPages(numPages)}
// 缩放与平移(支持PDF和图片)
zoom={zoom} // 1 = 100%;内置Ctrl/Cmd+滚轮、捏合缩放、中键拖动功能
onZoomChange={setZoom}
minZoom={0.25} // 默认值
maxZoom={5} // 默认值
panOffset={pan} // 受控平移偏移量;切换页面时会重置
onPanChange={setPan}
// 适配模式
fitMode="width" // 'width' 适配容器宽度;'page' 使整个页面适配容器
// 旋转(支持PDF和图片)
rotation={rotation} // 0 | 90 | 180 | 270
// 图表批注(仅instanceId源支持)
showAnnotations={true} // 默认值
onAnnotationClick={(annotation) => { /* annotation.linkedResource包含space + externalId */ }}
onAnnotationHover={(annotation) => {}}
// 自定义批注提示框(替换原生<title>提示框)
renderAnnotationTooltip={(annotation, rect) => (
<div style={{
position: 'absolute',
left: rect.x + rect.width,
top: rect.y,
zIndex: 11,
}}>
{annotation.text}
</div>
)}
// 自定义叠加层(SVG路径、高亮、绘图——支持PDF和图片)
renderOverlay={({ width, height, originalWidth, originalHeight, pageNumber, rotation }) => (
<svg
width={width}
height={height}
viewBox={`0 0 ${originalWidth} ${originalHeight}`}
preserveAspectRatio="none"
style={{ position: 'absolute', top: 0, left: 0, pointerEvents: 'all' }}
>
<path d="..." stroke="cyan" fill="none" />
</svg>
)}
// 自定义渲染器(均为可选)
renderLoading={() => <MySpinner />}
renderError={(error) => <MyError message={error.message} />}
renderUnsupported={(mimeType) => <div>无法预览 {mimeType}</div>}
// 布局
className="..."
style={{ width: '100%', height: '100%' }}
/>const navigateToFile = (file: FileInfo) => {
setSelectedFile(file);
setPage(1);
setZoom(1);
setRotation(0);
};numPages > 0onDocumentLoad{numPages > 0 && (
<>
<button disabled={page <= 1} onClick={() => setPage(p => p - 1)}>‹</button>
<span>{page} / {numPages}</span>
<button disabled={page >= numPages} onClick={() => setPage(p => p + 1)}>›</button>
</>
)}annotation.linkedResourcespaceexternalIdfile.instanceIdonAnnotationClick={(annotation) => {
if (!annotation.linkedResource) return;
const { space, externalId } = annotation.linkedResource;
const linked = files.find(
f => f.instanceId?.space === space && f.instanceId?.externalId === externalId
);
if (linked) navigateToFile(linked);
}}zoomonZoomChangerenderOverlayoriginalWidthoriginalHeightviewBoxconst navigateToFile = (file: FileInfo) => {
setSelectedFile(file);
setPage(1);
setZoom(1);
setRotation(0);
};numPages > 0onDocumentLoad{numPages > 0 && (
<>
<button disabled={page <= 1} onClick={() => setPage(p => p - 1)}>‹</button>
<span>{page} / {numPages}</span>
<button disabled={page >= numPages} onClick={() => setPage(p => p + 1)}>›</button>
</>
)}annotation.linkedResourcespaceexternalIdfile.instanceIdonAnnotationClick={(annotation) => {
if (!annotation.linkedResource) return;
const { space, externalId } = annotation.linkedResource;
const linked = files.find(
f => f.instanceId?.space === space && f.instanceId?.externalId === externalId
);
if (linked) navigateToFile(linked);
}}zoomonZoomChangerenderOverlayoriginalWidthoriginalHeightviewBox| Problem | Cause | Fix |
|---|---|---|
| Worker not configured | Add the worker setup from Step 3 in the same file that uses |
| | Do not install |
| Annotations never show | | Use |
| Annotations show but are empty | File has no | Expected — only P&ID/diagram files synced to the data model have annotations |
| Viewer collapses to zero height | Parent has no explicit height | Set |
| 问题 | 原因 | 解决方法 |
|---|---|---|
| 未配置Worker | 在使用 |
| 应用与 | 请勿单独安装 |
| 批注始终不显示 | | 使用 |
| 批注显示但为空 | CDF中该文件没有 | 属于正常情况——仅同步到数据模型的P&ID/图表文件才有批注 |
| 查看器收缩为零高度 | 父容器没有显式高度 | 通过 |