@flatbiz/antd
Version:
192 lines (185 loc) • 6.79 kB
JavaScript
/* eslint-disable */
import './../fba-hooks/index.css';
import './index.css';
/*! @flatjs/forge MIT @flatbiz/antd */
import { classNames } from '@dimjs/utils/class-names/class-names';
import { a as _slicedToArray, b as _objectSpread2 } from '../_rollupPluginBabelHelpers-BYm17lo8.js';
import { useState } from 'react';
import { Result, Spin } from 'antd';
import { fbaHooks } from '../fba-hooks/index.js';
import { createCtx } from '@wove/react/create-ctx';
import { jsx, jsxs } from 'react/jsx-runtime';
import { hooks } from '@wove/react/hooks';
import '@flatbiz/utils';
import '../use-responsive-point-Bp3D3lZT.js';
var _createCtx = createCtx(),
_createCtx2 = _slicedToArray(_createCtx, 2),
getCtx = _createCtx2[0],
CtxProvider = _createCtx2[1];
var PdfDocument = function PdfDocument(props) {
var _useState = useState(),
_useState2 = _slicedToArray(_useState, 2),
pdfInstance = _useState2[0],
setPdfInstance = _useState2[1];
var _useState3 = useState(),
_useState4 = _slicedToArray(_useState3, 2),
errorInst = _useState4[0],
setErrorInst = _useState4[1];
fbaHooks.useEffectCustomAsync(function () {
return new Promise(function ($return, $error) {
var _ref, pdfjsLib, _props$onLoadSuccess, loadingTask, _pdfInstance, _props$onLoadError;
_ref = globalThis, pdfjsLib = _ref.pdfjsLib;
pdfjsLib.GlobalWorkerOptions.workerSrc = '//file.40017.cn/tcsk/react/pdf@3.2.146/pdf.worker.min.js';
var $Try_1_Post = function () {
try {
return $return();
} catch ($boundEx) {
return $error($boundEx);
}
};
var $Try_1_Catch = function (error) {
try {
console.error(error);
(_props$onLoadError = props.onLoadError) === null || _props$onLoadError === void 0 || _props$onLoadError.call(props, error);
setErrorInst(error);
return $Try_1_Post();
} catch ($boundEx) {
return $error($boundEx);
}
};
try {
loadingTask = pdfjsLib.getDocument({
url: props.pdfUrl,
// 避免服务端动态填充的字体不展示
cMapUrl: 'https://file.40017.cn/tcsk/react/pdf@3.2.146/bcmaps/',
cMapPacked: true
});
return Promise.resolve(loadingTask.promise).then(function ($await_2) {
try {
_pdfInstance = $await_2;
setPdfInstance(_pdfInstance);
(_props$onLoadSuccess = props.onLoadSuccess) === null || _props$onLoadSuccess === void 0 || _props$onLoadSuccess.call(props, _pdfInstance);
return $Try_1_Post();
} catch ($boundEx) {
return $Try_1_Catch($boundEx);
}
}, $Try_1_Catch);
} catch (error) {
$Try_1_Catch(error);
}
});
}, [props.pdfUrl]);
var getPdfInstance = function getPdfInstance() {
return pdfInstance;
};
if (errorInst) {
if (props.error) {
return typeof props.error === 'function' ? props.error(errorInst) : props.error;
}
return /*#__PURE__*/jsx(Result, {
status: "error",
title: "PDF\u52A0\u8F7D\u5F02\u5E38",
subTitle: errorInst === null || errorInst === void 0 ? void 0 : errorInst.message
});
}
if (pdfInstance) {
return /*#__PURE__*/jsx(CtxProvider, {
value: {
getPdfInstance: getPdfInstance
},
children: /*#__PURE__*/jsx("div", {
className: classNames('v-pdf-document', props.className),
children: props.children
})
});
}
return /*#__PURE__*/jsx("div", {
className: "v-pdf-document-init-loading",
children: /*#__PURE__*/jsx(Spin, {})
});
};
var PdfPage = function PdfPage(props) {
var ctx = getCtx();
var pdfInstance = ctx.getPdfInstance();
var id = hooks.useId(undefined, "v-pdf-page-".concat(props.pageNumber));
var _useState = useState(true),
_useState2 = _slicedToArray(_useState, 2),
spinning = _useState2[0],
setSpinning = _useState2[1];
var numPages = pdfInstance.numPages;
fbaHooks.useEffectCustom(function () {
pdfInstance.getPage(props.pageNumber).then(function (page) {
var scale = props.scale || 1;
var viewport = page.getViewport({
scale: scale
});
var customWidth = props.width;
var canvas = document.getElementById(id);
var context = canvas.getContext('2d');
if (customWidth && !props.scale) {
var ratio = customWidth / viewport.width;
viewport = page.getViewport({
scale: ratio
});
}
var cssUnits = window.devicePixelRatio;
cssUnits = cssUnits <= 1 ? 96 / 72 : cssUnits;
canvas.width = Math.floor(viewport.width * cssUnits);
canvas.height = Math.floor(viewport.height * cssUnits);
canvas.style.width = viewport.width + 'px';
canvas.style.height = viewport.height + 'px';
var renderContext = {
transform: [cssUnits, 0, 0, cssUnits, 0, 0],
canvasContext: context,
viewport: viewport
};
var renderTask = page.render(renderContext);
renderTask.promise.then(function () {
var _props$onRenderSucces;
(_props$onRenderSucces = props.onRenderSuccess) === null || _props$onRenderSucces === void 0 || _props$onRenderSucces.call(props, {
width: viewport.width,
height: viewport.height,
scale: scale,
pageNumber: props.pageNumber,
page: page
}, pdfInstance);
setSpinning(false);
}).catch(function (error) {
var _props$onRenderError;
console.error(error === null || error === void 0 ? void 0 : error.message);
(_props$onRenderError = props.onRenderError) === null || _props$onRenderError === void 0 || _props$onRenderError.call(props, error, pdfInstance);
setSpinning(false);
});
});
}, [pdfInstance, props.scale, props.pageNumber]);
var gap = typeof props.gap === 'undefined' ? 10 : props.gap;
var style = numPages !== props.pageNumber ? {
marginBottom: gap
} : {};
return /*#__PURE__*/jsxs("div", {
className: classNames('v-pdf-page', props.className),
style: _objectSpread2(_objectSpread2({}, style), props.style),
onClick: props.onClick,
children: [/*#__PURE__*/jsx(Spin, {
spinning: spinning,
children: /*#__PURE__*/jsx("canvas", {
id: id
})
}), props.children]
});
};
/**
* pdf预览
* ```
* 使用方式:在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 Pdf = {
Document: PdfDocument,
Page: PdfPage
};
export { Pdf };
//# sourceMappingURL=index.js.map