UNPKG

@orca-fe/pdf-viewer

Version:
129 lines (128 loc) 6.03 kB
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } /* eslint-disable react/no-unused-prop-types */ import React, { useContext, useImperativeHandle } from 'react'; import { useControllableValue, useMemoizedFn } from 'ahooks'; import { changeArr } from '@orca-fe/tools'; import { useHotkeyListener } from '@orca-fe/hooks'; import ToolbarButton from "../../ToolbarButton"; import { IconAddTooltip } from "../../icon/icon"; import PDFTooltipPainter from "./PDFTooltipPainter"; import PDFViewerContext, { usePageCoverRenderer } from "../../context"; import ToolbarPortal from "../../ToolbarPortal"; import { useLocale } from "../../locale/context"; import zhCN from "../../locale/zh_CN"; import { jsx as _jsx } from "react/jsx-runtime"; import { Fragment as _Fragment } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var eArr = []; var ef = () => undefined; var drawingNamePDFTooltipPlugin = 'PDFTooltipPlugin'; var PDFTooltipPlugin = /*#__PURE__*/React.forwardRef((props, pRef) => { var _useLocale = useLocale(zhCN), _useLocale2 = _slicedToArray(_useLocale, 1), l = _useLocale2[0]; var _props$autoCheck = props.autoCheck, autoCheck = _props$autoCheck === void 0 ? true : _props$autoCheck, initialAttr = props.initialAttr, _props$onChangeStart = props.onChangeStart, _onChangeStart = _props$onChangeStart === void 0 ? ef : _props$onChangeStart, _props$buttonName = props.buttonName, buttonName = _props$buttonName === void 0 ? l.tooltip : _props$buttonName; var renderPageCover = usePageCoverRenderer(); var _useContext = useContext(PDFViewerContext), pdfViewer = _useContext.pdfViewer, internalState = _useContext.internalState, setInternalState = _useContext.setInternalState; /* 绘图功能 */ var drawing = internalState.drawingPluginName === drawingNamePDFTooltipPlugin; var setDrawing = useMemoizedFn(b => { setInternalState({ drawingPluginName: b ? drawingNamePDFTooltipPlugin : '' }); }); var _useControllableValue = useControllableValue(props, { defaultValuePropName: 'defaultChecked', trigger: 'onCheck', valuePropName: 'checked' }), _useControllableValue2 = _slicedToArray(_useControllableValue, 2), checked = _useControllableValue2[0], setChecked = _useControllableValue2[1]; var _useControllableValue3 = useControllableValue(props, { defaultValuePropName: 'defaultData', trigger: 'onDataChange', valuePropName: 'data' }), _useControllableValue4 = _slicedToArray(_useControllableValue3, 2), _useControllableValue5 = _useControllableValue4[0], dataList = _useControllableValue5 === void 0 ? eArr : _useControllableValue5, setDataList = _useControllableValue4[1]; var drawTooltip = useMemoizedFn(() => { setDrawing(true); }); var cancelDraw = useMemoizedFn(() => { setDrawing(false); }); useImperativeHandle(pRef, () => ({ drawTooltip, cancelDraw })); useHotkeyListener('Escape', () => { if (checked) { setChecked(undefined); return false; } if (drawing) { cancelDraw(); return false; } return true; }); return /*#__PURE__*/_jsxs(_Fragment, { children: [/*#__PURE__*/_jsx(ToolbarPortal, { children: /*#__PURE__*/_jsx(ToolbarButton, { checked: drawing, onClick: e => { setDrawing(!drawing); }, icon: /*#__PURE__*/_jsx(IconAddTooltip, {}), children: buttonName }) }), renderPageCover((pageIndex, { viewport, zoom }) => /*#__PURE__*/_jsx(PDFTooltipPainter, { initialAttr: initialAttr, autoCheck: autoCheck, data: dataList[pageIndex] || eArr, onChangeStart: index => { _onChangeStart(pageIndex, index); }, onDataChange: (pageData, action, index) => { setDataList(changeArr(dataList, pageIndex, pageData), action, pageIndex, index); }, onDrawChange: setDrawing, checked: (checked === null || checked === void 0 ? void 0 : checked[0]) === pageIndex ? checked[1] : undefined, onCheck: index => { setChecked(checked => { if (index >= 0) { return [pageIndex, index]; } if (checked && checked[0] !== pageIndex) { return checked; } return undefined; }); }, drawing: drawing, zoom: zoom, getPopupContainer: () => pdfViewer.getRoot() || document.body }))] }); }); export default PDFTooltipPlugin;