UNPKG

@flatbiz/antd

Version:
157 lines (151 loc) 5.84 kB
/*! @flatjs/forge MIT @flatbiz/antd */ import { classNames } from '@dimjs/utils/class-names/class-names'; import { a as _slicedToArray } from './_rollupPluginBabelHelpers-BspM60Sw.js'; import { useState, useRef, useMemo } from 'react'; import { useMemoizedFn } from 'ahooks'; import { Result } from 'antd'; import { generateIntArray } from '@flatbiz/utils'; import { fbaHooks } from './fba-hooks/index.js'; import { Pdf } from './pdf/index.js'; import { R as RollLocationInView } from './in-view-XeG6cIRO.js'; import { jsx, jsxs } from 'react/jsx-runtime'; var Navigation = function Navigation(props) { var navigationWidth = typeof props.navigationWidth === 'undefined' ? 200 : props.navigationWidth; return /*#__PURE__*/jsx(RollLocationInView, { activeKey: "".concat(props.activeNumber), behavior: "auto", style: { width: navigationWidth }, renderList: generateIntArray(1, props.numPages + 1).map(function (pageNumer) { return { activeKey: "".concat(pageNumer), render: /*#__PURE__*/jsxs("div", { className: classNames('v-pdf-preview-navigation-content', { 'v-pdf-preview-navigation-active': pageNumer === props.activeNumber }), children: [/*#__PURE__*/jsx(Pdf.Page, { pageNumber: pageNumer, gap: 0, scale: props.scale || 0.2, className: classNames('v-pdf-preview-navigation-page'), onClick: function onClick() { props.onChangeActiveNumber(pageNumer); var scroll = document.querySelector("#".concat(props.pdfContentNodeId)); if (scroll) { if (pageNumer === 1) { scroll.scrollTo(0, 0); } else { var top = props.pdfPageListHeightScope[pageNumer - 1][0]; scroll.scrollTo(0, top); } } } }), /*#__PURE__*/jsxs("div", { className: "v-pdf-preview-navigation-pagination", children: ["- ", pageNumer, " -"] })] }, pageNumer) }; }) }); }; /** * pdf预览方式1 * ``` * 使用方式:在cdn.ts中引入 '//file.40017.cn/tcsk/react/pdf@3.2.146/pdf.min.js' * Git: https://github.com/mozilla/pdfjs-dist/tree/master * Demo: https://fex.qa.tcshuke.com/docs/admin/main/file/pdf * Demo: https://fex.qa.tcshuke.com/docs/admin/main/file/pdf-seal * ``` */ var PdfPreview = function PdfPreview(props) { var _useState = useState(0), _useState2 = _slicedToArray(_useState, 2), numPages = _useState2[0], setNumPages = _useState2[1]; var dpfDoucmentRef = useRef(null); var _useState3 = useState(1), _useState4 = _slicedToArray(_useState3, 2), activePagination = _useState4[0], setActivePagination = _useState4[1]; var _useState5 = useState(0), _useState6 = _slicedToArray(_useState5, 2), pdfPageHeight = _useState6[0], setPdfPageHeight = _useState6[1]; var pageGap = 10; var onDocumentLoadSuccess = useMemoizedFn(function (inst) { setNumPages(inst.numPages); }); /** 计算 page 在文档流中的高度范围,包含竖直方向间距 */ var pdfPageListHeightScope = useMemo(function () { if (!numPages) return []; var pageHeightScopeList = []; generateIntArray(0, numPages).forEach(function (item) { if (item === 0) { pageHeightScopeList.push([0, pdfPageHeight]); } else { var x = pdfPageHeight * item + item * pageGap; pageHeightScopeList.push([x, x + pdfPageHeight]); } }); return pageHeightScopeList; }, [numPages, pdfPageHeight]); var onDocumentCenterScroll = function onDocumentCenterScroll(event) { var _dpfDoucmentRef$curre; var scrollTop = event.target.scrollTop; var scrollHeight = (_dpfDoucmentRef$curre = dpfDoucmentRef.current) === null || _dpfDoucmentRef$curre === void 0 ? void 0 : _dpfDoucmentRef$curre.clientHeight; for (var index = 0; index < pdfPageListHeightScope.length; index++) { var element = pdfPageListHeightScope[index]; if (element[1] - scrollTop >= scrollHeight / 2) { setActivePagination(index + 1); break; } } }; var pdfContentNodeId = fbaHooks.useUuid(); return /*#__PURE__*/jsxs(Pdf.Document, { pdfUrl: props.pdfUrl, onLoadSuccess: onDocumentLoadSuccess, error: /*#__PURE__*/jsx(Result, { status: "500", title: "PDF\u52A0\u8F7D\u5F02\u5E38", subTitle: "PDF\u5730\u5740\uFF1A".concat(props.pdfUrl) }), className: classNames('v-pdf-preview-document', props.className), children: [!props.hiddenNavigation ? /*#__PURE__*/jsx("div", { className: "v-pdf-preview-navigation", children: /*#__PURE__*/jsx(Navigation, { numPages: numPages, activeNumber: activePagination, onChangeActiveNumber: setActivePagination, scale: props.navigationScale, navigationWidth: props.navigationWidth, pdfPageListHeightScope: pdfPageListHeightScope, pdfContentNodeId: pdfContentNodeId }) }) : null, /*#__PURE__*/jsx("div", { className: "v-pdf-preview-content", onScroll: onDocumentCenterScroll, ref: dpfDoucmentRef, id: pdfContentNodeId, children: numPages > 0 ? generateIntArray(1, numPages + 1).map(function (item) { return /*#__PURE__*/jsx(Pdf.Page, { pageNumber: item, scale: props.scale, gap: pageGap, onRenderSuccess: function onRenderSuccess(node) { var h = Math.floor(node.height); setPdfPageHeight(h); }, style: { display: 'flex', justifyContent: 'center' } }, item); }) : null })] }); }; export { PdfPreview as P }; //# sourceMappingURL=preview-B6Htn1sD.js.map