UNPKG

@orca-fe/pdf-viewer

Version:
317 lines (312 loc) 13.5 kB
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } 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, useState } from 'react'; import { IconButton, Trigger } from '@orca-fe/pocket'; import Painter from '@orca-fe/painter'; import { useControllableValue, useMemoizedFn } from 'ahooks'; import { CopyOutlined, DeleteOutlined } from '@ant-design/icons'; import { changeArr } from '@orca-fe/tools'; import produce from 'immer'; import rfdc from 'rfdc'; import { useHotkeyListener } from '@orca-fe/hooks'; import ToolbarPortal from "../../ToolbarPortal"; import { IconAddShape } from "../../icon/icon"; import ToolbarButton from "../../ToolbarButton"; import PDFViewerContext, { usePageCoverRenderer } from "../../context"; import SimplePropsEditor from "../SimplePropsEditor"; import PopupBox from "../PopupBox"; import useStyle from "./PDFMarkPlugin.style"; import { useLocale } from "../../locale/context"; import zhCN from "../../locale/zh_CN"; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; import { Fragment as _Fragment } from "react/jsx-runtime"; var deepClone = rfdc(); var eArr = []; var ef = () => {}; /** * PDFMarkPlugin 标注插件属性 */ var drawingNamePDFPainterPlugin = 'PDFMarkPlugin'; /** * PDFMarkPlugin 标注插件 */ var PDFMarkPlugin = /*#__PURE__*/React.forwardRef((props, pRef) => { var _useLocale = useLocale(zhCN), _useLocale2 = _slicedToArray(_useLocale, 1), l = _useLocale2[0]; var disabledButton = props.disabledButton, _props$autoCheck = props.autoCheck, autoCheck = _props$autoCheck === void 0 ? true : _props$autoCheck, _props$onChangeStart = props.onChangeStart, _onChangeStart = _props$onChangeStart === void 0 ? ef : _props$onChangeStart, _props$buttonName = props.buttonName, buttonName = _props$buttonName === void 0 ? l.paint : _props$buttonName, _props$drawingVisible = props.drawingVisible, drawingVisible = _props$drawingVisible === void 0 ? true : _props$drawingVisible, _props$drawingPluginI = props.drawingPluginId, drawingPluginId = _props$drawingPluginI === void 0 ? drawingNamePDFPainterPlugin : _props$drawingPluginI; var styles = useStyle(); var _useContext = useContext(PDFViewerContext), internalState = _useContext.internalState, setInternalState = _useContext.setInternalState; var propsDef = [{ key: 'stroke', type: 'color', name: l.color }, { key: 'strokeWidth', type: 'number', min: 0.1, max: 20, step: 0.1, name: l.border }]; 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 drawing = internalState.drawingPluginName === drawingPluginId; var setDrawing = useMemoizedFn(b => { setInternalState({ // 这里设置的时候,已经是全局的了 drawingPluginName: b ? drawingPluginId : '' }); }); var _useState = useState({ shapeType: 'mark' }), _useState2 = _slicedToArray(_useState, 2), drawMode = _useState2[0], setDrawMode = _useState2[1]; var _useState3 = useState({ refs: [] }), _useState4 = _slicedToArray(_useState3, 1), _painter = _useState4[0]; useHotkeyListener('Escape', () => { if (checked) { setChecked(undefined); return false; } if (drawing) { setDrawing(false); return false; } return true; }); var renderPageCover = usePageCoverRenderer(); var drawMark = useMemoizedFn((shapeType, attr) => { setDrawMode({ attr, shapeType }); if (!drawing) { setDrawing(true); } }); var cancelDraw = useMemoizedFn(() => { setDrawing(false); }); useImperativeHandle(pRef, () => ({ drawMark, cancelDraw })); return /*#__PURE__*/_jsxs(_Fragment, { children: [!disabledButton && /*#__PURE__*/_jsx(ToolbarPortal, { children: /*#__PURE__*/_jsx("div", { className: styles.root, style: { display: drawingVisible ? 'block' : 'none' }, children: /*#__PURE__*/_jsx("span", { className: styles.root, children: /*#__PURE__*/_jsx(ToolbarButton, { checked: drawing, onClick: e => { if (drawing) { setDrawing(false); } else { drawMark(drawMode.shapeType || 'mark', drawMode.attr || { strokeWidth: 1, stroke: '#CC0000' }); } }, icon: /*#__PURE__*/_jsx(IconAddShape, {}), children: buttonName }) }) }) }), renderPageCover((pageIndex, { viewport, zoom }) => /*#__PURE__*/_jsx(Painter, { ref: _ref => _painter.refs[pageIndex] = _ref, autoCheck: autoCheck, draggable: false, className: `${styles.painter} ${drawing ? styles.drawing : ''}`, style: { height: '100%', '--painter-scale': 'var(--scale-factor-origin, 1)', '--transformer-layout-scale': 'var(--scale-factor-origin, 1)' }, zoom: zoom, defaultDrawMode: drawing ? drawMode : undefined, drawMode: drawing ? drawMode : undefined, onChangeStart: index => { _onChangeStart(pageIndex, index); }, onDrawModeChange: drawMode => { if (drawMode) { setDrawMode(drawMode); } else { setDrawing(false); } }, dataList: dataList, data: dataList[pageIndex] || eArr, onDataChange: (pageData, action, index) => { setDataList(changeArr(dataList, pageIndex, pageData), action, pageIndex, index); }, 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; }); }, renderTransformingRect: (shape, index) => { // 是否圖片類型 var isImage = shape.type === 'image'; var deleteButton = /*#__PURE__*/_jsx(IconButton, { size: "small", onClick: () => { // 删除 setDataList(produce(dataList, _dataList => { if (_dataList[pageIndex]) { // eslint-disable-next-line no-param-reassign _dataList[pageIndex].splice(index, 1); } }), 'delete', pageIndex, index); }, style: { color: '#C00' }, children: /*#__PURE__*/_jsx(DeleteOutlined, {}) }); var shapePopup = /*#__PURE__*/_jsxs(PopupBox, { style: { pointerEvents: 'initial' }, onClick: e => { e.stopPropagation(); }, children: [/*#__PURE__*/_jsx(SimplePropsEditor, { value: { stroke: shape['stroke'], strokeWidth: shape['strokeWidth'] }, propsDef: propsDef, onChange: newProps => { var newShape = _objectSpread(_objectSpread({}, shape), newProps); setDataList(produce(dataList, _dataList => { if (_dataList[pageIndex]) { // eslint-disable-next-line no-param-reassign _dataList[pageIndex][index] = newShape; } }), 'change', pageIndex, index); }, colorTriggerProps: { getPopupContainer: () => { var _painter$refs$pageInd, _painter$refs$pageInd2; return (_painter$refs$pageInd = (_painter$refs$pageInd2 = _painter.refs[pageIndex]) === null || _painter$refs$pageInd2 === void 0 ? void 0 : _painter$refs$pageInd2.getRoot()) !== null && _painter$refs$pageInd !== void 0 ? _painter$refs$pageInd : document.body; }, popupAlign: { overflow: { adjustY: true, shiftX: true } } } }), /*#__PURE__*/_jsxs("div", { style: { display: 'flex', alignItems: 'center', justifyContent: 'flex-end' }, children: [/*#__PURE__*/_jsx(IconButton, { size: "small", onClick: () => { // 复制 setDataList(produce(dataList, _dataList => { if (_dataList[pageIndex]) { // eslint-disable-next-line no-param-reassign var _shape = deepClone(_dataList[pageIndex][index]); var offset = 16; if (_shape.type === 'mark') { _shape.x += offset; _shape.y += offset; } _dataList[pageIndex].push(_shape); } }), 'add', pageIndex, dataList[pageIndex].length); setChecked([pageIndex, dataList[pageIndex].length]); }, style: { color: '#333' }, children: /*#__PURE__*/_jsx(CopyOutlined, {}) }), deleteButton] })] }); return /*#__PURE__*/_jsx(Trigger, { popupVisible: !shape.disabled, getPopupContainer: dom => dom, popupAlign: { points: isImage ? ['tl', 'tr'] : ['bl', 'tl'], offset: isImage ? [5, -5] : [0, -5] }, popup: shapePopup, children: /*#__PURE__*/_jsx("div", { style: { position: 'absolute', width: '100%', height: '100%' } }) }); } }))] }); }); export default PDFMarkPlugin;