@kne/react-pdf-sign 1.0.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { pdfjs, Document, Page } from 'react-pdf';
2
- import { useMemo, useState, useRef, useEffect, forwardRef, useCallback, useImperativeHandle } from 'react';
2
+ import { useMemo, useState, useRef, useEffect, createElement, forwardRef, useCallback, useImperativeHandle } from 'react';
3
3
  import { usePreset } from '@kne/global-context';
4
4
  import useResize from '@kne/use-resize';
5
5
  import classnames from 'classnames';
@@ -8,7 +8,7 @@ import { LeftOutlined, RightOutlined } from '@ant-design/icons';
8
8
  import 'react-pdf/dist/Page/TextLayer.css';
9
9
  import 'react-pdf/dist/Page/AnnotationLayer.css';
10
10
  import { jsxs, jsx } from 'react/jsx-runtime';
11
- import { Stage, Layer, Group, Image as Image$1, Rect, Text, Transformer } from 'react-konva';
11
+ import { Layer, Group, Image as Image$1, Rect, Text, Transformer, Circle, Line, Stage } from 'react-konva';
12
12
  import useControlValue from '@kne/use-control-value';
13
13
  import useRefCallback from '@kne/use-ref-callback';
14
14
  import useImage from 'use-image';
@@ -38,7 +38,7 @@ function _objectWithoutPropertiesLoose(r, e) {
38
38
 
39
39
  var style = {"pdf-view-container":"_RgZXN","pdf-view":"_PAju1","pdf-view-children":"_N11Pj","pdf-view-page-control-left":"_IbVpf","pdf-view-page-control-right":"_IIgqj","pdf-view-page-control-current":"_JW3CT","signature-container":"_6TDb-","signature-canvas":"_2CGF4","signature-modal":"_kWe3k","signature-mask":"_FSvLv"};
40
40
 
41
- const _excluded$3 = ["numPages"];
41
+ const _excluded$4 = ["numPages"];
42
42
  const PDFViewer = ({
43
43
  className,
44
44
  defaultPage,
@@ -91,7 +91,7 @@ const PDFViewer = ({
91
91
  let {
92
92
  numPages
93
93
  } = _ref;
94
- _objectWithoutPropertiesLoose(_ref, _excluded$3);
94
+ _objectWithoutPropertiesLoose(_ref, _excluded$4);
95
95
  setPageSize(numPages);
96
96
  if (!Number.isInteger(defaultPage)) {
97
97
  setCurrentPage(numPages);
@@ -144,8 +144,10 @@ const locale$1 = {
144
144
  signatureDefaultTitle: '签名',
145
145
  signatureConfirmText: '确认',
146
146
  signatureCancelText: '取消',
147
+ signatureCleanText: '清空',
147
148
  signatureEmptyError: '签名内容不能为空',
148
- signatureSuccess: '签名成功'
149
+ signatureSuccess: '签名成功',
150
+ signatureAdd: '请添加签名'
149
151
  };
150
152
 
151
153
  const locale = {
@@ -154,8 +156,10 @@ const locale = {
154
156
  signatureDefaultTitle: 'Signature',
155
157
  signatureConfirmText: 'Confirm',
156
158
  signatureCancelText: 'Cancel',
159
+ signatureCleanText: 'Clean',
157
160
  signatureEmptyError: 'Signature content cannot be empty',
158
- signatureSuccess: 'Signature Success'
161
+ signatureSuccess: 'Signature Success',
162
+ signatureAdd: 'Please add signature'
159
163
  };
160
164
 
161
165
  const withLocale = createWithIntlProvider({
@@ -167,8 +171,30 @@ const withLocale = createWithIntlProvider({
167
171
  namespace: 'react-pdf-sign'
168
172
  });
169
173
 
170
- const _excluded$2 = ["width", "height", "padding", "stageWidth", "stageHeight", "placeholder", "signature"];
171
- const LocationLayer = withLocale(p => {
174
+ const getInitLocation = ({
175
+ stageWidth,
176
+ stageHeight,
177
+ width,
178
+ height
179
+ }) => {
180
+ return {
181
+ scaleX: 1,
182
+ scaleY: 1,
183
+ x: Math.round((stageWidth - width) / 2),
184
+ y: Math.round((stageHeight - height) / 2),
185
+ size: {
186
+ width,
187
+ height,
188
+ x: Math.round((stageWidth - width) / 2),
189
+ y: Math.round((stageHeight - height) / 2)
190
+ }
191
+ };
192
+ };
193
+
194
+ const _excluded$3 = ["width", "height", "padding", "stageWidth", "stageHeight", "placeholder", "signature", "active", "onClose", "onClick"],
195
+ _excluded2 = ["stageWidth", "stageHeight"],
196
+ _excluded3 = ["stageWidth", "stageHeight", "isEdit", "onClick", "currentPage"];
197
+ const LocationLayerInner = withLocale(p => {
172
198
  const {
173
199
  formatMessage
174
200
  } = useIntl();
@@ -181,9 +207,12 @@ const LocationLayer = withLocale(p => {
181
207
  placeholder = formatMessage({
182
208
  id: 'locationLayerPlaceholder'
183
209
  }),
184
- signature
210
+ signature,
211
+ active,
212
+ onClose,
213
+ onClick
185
214
  } = p,
186
- props = _objectWithoutPropertiesLoose(p, _excluded$2);
215
+ props = _objectWithoutPropertiesLoose(p, _excluded$3);
187
216
  const [value, setValue] = useControlValue(props);
188
217
  const [isInit, setIsInit] = useState(false);
189
218
  const [signatureImage] = useImage(signature);
@@ -193,7 +222,7 @@ const LocationLayer = withLocale(p => {
193
222
  const computedSignLocation = () => {
194
223
  const absolutePosition = signRef.current.absolutePosition();
195
224
  const size = signRef.current.getClientRect();
196
- setValue({
225
+ setValue(value => Object.assign({}, value, {
197
226
  size: {
198
227
  width: Math.round(size.width),
199
228
  height: Math.round(size.height),
@@ -204,22 +233,16 @@ const LocationLayer = withLocale(p => {
204
233
  scaleY: Number(groupRef.current.attrs.scaleY.toFixed(2)),
205
234
  x: Math.round(groupRef.current.attrs.x),
206
235
  y: Math.round(groupRef.current.attrs.y)
207
- });
236
+ }));
208
237
  };
209
238
  const initValue = useRefCallback(() => {
210
239
  if (['scaleX', 'scaleY', 'x', 'y', 'size'].some(name => !Object.assign({}, value).hasOwnProperty(name))) {
211
- setValue({
212
- scaleX: 1,
213
- scaleY: 1,
214
- x: Math.round((stageWidth - width) / 2),
215
- y: Math.round((stageHeight - height) / 2),
216
- size: {
217
- width,
218
- height,
219
- x: Math.round((stageWidth - width) / 2),
220
- y: Math.round((stageHeight - height) / 2)
221
- }
222
- });
240
+ setValue(value => Object.assign({}, value, getInitLocation({
241
+ stageWidth,
242
+ stageHeight,
243
+ width,
244
+ height
245
+ })));
223
246
  }
224
247
  setIsInit(true);
225
248
  });
@@ -242,607 +265,186 @@ const LocationLayer = withLocale(p => {
242
265
  if (!(isInit && value)) {
243
266
  return null;
244
267
  }
268
+ return /*#__PURE__*/jsxs(Layer, {
269
+ children: [/*#__PURE__*/jsx(Group, {
270
+ x: value.x,
271
+ y: value.y,
272
+ draggable: active !== false,
273
+ ref: groupRef,
274
+ onDragMove: computedSignLocation,
275
+ onDragEnd: computedSignLocation,
276
+ scaleX: value.scaleX,
277
+ scaleY: value.scaleY,
278
+ onTransform: computedSignLocation,
279
+ onTransformEnd: computedSignLocation,
280
+ onTap: onClick,
281
+ onClick: onClick,
282
+ children: signatureImage ? /*#__PURE__*/jsx(Image$1, {
283
+ width: width,
284
+ height: height,
285
+ image: signatureImage,
286
+ cornerRadius: 8,
287
+ ref: signRef
288
+ }) : /*#__PURE__*/jsx(Rect, {
289
+ width: width,
290
+ height: height,
291
+ fill: "#f0f0f0",
292
+ cornerRadius: 8,
293
+ ref: signRef
294
+ })
295
+ }), /*#__PURE__*/jsx(Text, {
296
+ listening: false,
297
+ x: value.x,
298
+ y: value.y,
299
+ text: signatureImage ? '' : placeholder,
300
+ fontSize: 16,
301
+ fill: "#666666",
302
+ fontFamily: "Arial",
303
+ align: "center",
304
+ verticalAlign: "middle",
305
+ width: width * value.scaleX,
306
+ height: height * value.scaleY
307
+ }), /*#__PURE__*/jsx(Transformer, {
308
+ ref: transformerRef,
309
+ visible: active !== false,
310
+ keepRatio: true,
311
+ flipEnabled: false,
312
+ rotateEnabled: false,
313
+ borderStroke: themeColor,
314
+ rotateAnchorStroke: themeColor,
315
+ anchorStroke: themeColor,
316
+ padding: padding
317
+ }), active === true && /*#__PURE__*/jsxs(Group, {
318
+ x: value.x + width * value.scaleX - 4,
319
+ y: value.y + 4,
320
+ onClick: () => {
321
+ onClose && onClose();
322
+ },
323
+ children: [/*#__PURE__*/jsx(Circle, {
324
+ radius: 6,
325
+ stroke: themeColor,
326
+ strokeWidth: 1,
327
+ fill: "white"
328
+ }), /*#__PURE__*/jsx(Line, {
329
+ points: [-2, -2, 2, 2],
330
+ stroke: themeColor,
331
+ strokeWidth: 1,
332
+ lineCap: "round"
333
+ }), /*#__PURE__*/jsx(Line, {
334
+ points: [-2, 2, 2, -2],
335
+ stroke: themeColor,
336
+ strokeWidth: 1,
337
+ lineCap: "round"
338
+ })]
339
+ })]
340
+ });
341
+ });
342
+ const LocationLayer = _ref => {
343
+ let {
344
+ stageWidth,
345
+ stageHeight
346
+ } = _ref,
347
+ props = _objectWithoutPropertiesLoose(_ref, _excluded2);
245
348
  return /*#__PURE__*/jsx(Stage, {
246
349
  width: stageWidth,
247
350
  height: stageHeight,
248
- children: /*#__PURE__*/jsxs(Layer, {
249
- children: [/*#__PURE__*/jsxs(Group, {
250
- x: value.x,
251
- y: value.y,
252
- draggable: true,
253
- ref: groupRef,
254
- onDragEnd: computedSignLocation,
255
- scaleX: value.scaleX,
256
- scaleY: value.scaleY,
257
- onTransformEnd: computedSignLocation,
258
- children: [signatureImage ? /*#__PURE__*/jsx(Image$1, {
259
- width: width,
260
- height: height,
261
- image: signatureImage,
262
- cornerRadius: 8,
263
- ref: signRef
264
- }) : /*#__PURE__*/jsx(Rect, {
265
- width: width,
266
- height: height,
267
- fill: "#f0f0f0",
268
- cornerRadius: 8,
269
- ref: signRef
270
- }), /*#__PURE__*/jsx(Text, {
271
- text: signatureImage ? '' : placeholder,
272
- fontSize: 16,
273
- fill: "#666666",
274
- fontFamily: "Arial",
275
- align: "center",
276
- verticalAlign: "middle",
277
- width: width,
278
- height: height
279
- })]
280
- }), /*#__PURE__*/jsx(Transformer, {
281
- ref: transformerRef,
282
- keepRatio: true,
283
- flipEnabled: false,
284
- rotateEnabled: false,
285
- borderStroke: themeColor,
286
- rotateAnchorStroke: themeColor,
287
- anchorStroke: themeColor,
288
- padding: padding
289
- })]
351
+ children: /*#__PURE__*/jsx(LocationLayerInner, _extends({}, props, {
352
+ stageWidth: stageWidth,
353
+ stageHeight: stageHeight
354
+ }))
355
+ });
356
+ };
357
+ const LocationGroup = _ref2 => {
358
+ let {
359
+ stageWidth,
360
+ stageHeight,
361
+ isEdit = true,
362
+ onClick,
363
+ currentPage
364
+ } = _ref2,
365
+ props = _objectWithoutPropertiesLoose(_ref2, _excluded3);
366
+ const [value, onChange] = useControlValue(Object.assign({}, {
367
+ defaultValue: []
368
+ }, props));
369
+ const [active, setActive] = useState(0);
370
+ return /*#__PURE__*/jsx(Stage, {
371
+ width: stageWidth,
372
+ height: stageHeight,
373
+ children: value.map((item, index) => {
374
+ if (item.page && item.page !== currentPage) {
375
+ return null;
376
+ }
377
+ return /*#__PURE__*/createElement(LocationLayerInner, _extends({}, props, {
378
+ key: index,
379
+ stageWidth: stageWidth,
380
+ stageHeight: stageHeight,
381
+ value: item,
382
+ signature: item.signature,
383
+ active: isEdit && active === index,
384
+ onClick: () => {
385
+ if (isEdit) {
386
+ if (active !== index) {
387
+ setActive(index);
388
+ }
389
+ } else {
390
+ onClick && onClick({
391
+ value: value[index],
392
+ index,
393
+ allValue: value
394
+ });
395
+ }
396
+ },
397
+ onClose: () => {
398
+ onChange(value => {
399
+ const newValue = value.slice(0);
400
+ newValue.splice(index, 1);
401
+ return newValue;
402
+ });
403
+ },
404
+ onChange: item => {
405
+ onChange(value => {
406
+ const newValue = value.slice(0);
407
+ newValue[index] = item;
408
+ return newValue;
409
+ });
410
+ }
411
+ }));
290
412
  })
291
413
  });
292
- });
293
-
294
- var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
295
-
296
- function createCommonjsModule(fn) {
297
- var module = { exports: {} };
298
- return fn(module, module.exports), module.exports;
299
- }
300
-
301
- /** Detect free variable `global` from Node.js. */
302
-
303
- var freeGlobal = typeof commonjsGlobal == 'object' && commonjsGlobal && commonjsGlobal.Object === Object && commonjsGlobal;
304
-
305
- var _freeGlobal = freeGlobal;
306
-
307
- /** Detect free variable `self`. */
308
- var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
309
-
310
- /** Used as a reference to the global object. */
311
- var root = _freeGlobal || freeSelf || Function('return this')();
312
-
313
- var _root = root;
314
-
315
- /** Built-in value references. */
316
- var Symbol = _root.Symbol;
317
-
318
- var _Symbol = Symbol;
319
-
320
- /** Used for built-in method references. */
321
- var objectProto$3 = Object.prototype;
322
-
323
- /** Used to check objects for own properties. */
324
- var hasOwnProperty$2 = objectProto$3.hasOwnProperty;
325
-
326
- /**
327
- * Used to resolve the
328
- * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
329
- * of values.
330
- */
331
- var nativeObjectToString$1 = objectProto$3.toString;
332
-
333
- /** Built-in value references. */
334
- var symToStringTag$1 = _Symbol ? _Symbol.toStringTag : undefined;
335
-
336
- /**
337
- * A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values.
338
- *
339
- * @private
340
- * @param {*} value The value to query.
341
- * @returns {string} Returns the raw `toStringTag`.
342
- */
343
- function getRawTag(value) {
344
- var isOwn = hasOwnProperty$2.call(value, symToStringTag$1),
345
- tag = value[symToStringTag$1];
346
-
347
- try {
348
- value[symToStringTag$1] = undefined;
349
- var unmasked = true;
350
- } catch (e) {}
351
-
352
- var result = nativeObjectToString$1.call(value);
353
- if (unmasked) {
354
- if (isOwn) {
355
- value[symToStringTag$1] = tag;
356
- } else {
357
- delete value[symToStringTag$1];
358
- }
359
- }
360
- return result;
361
- }
362
-
363
- var _getRawTag = getRawTag;
364
-
365
- /** Used for built-in method references. */
366
- var objectProto$2 = Object.prototype;
367
-
368
- /**
369
- * Used to resolve the
370
- * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
371
- * of values.
372
- */
373
- var nativeObjectToString = objectProto$2.toString;
374
-
375
- /**
376
- * Converts `value` to a string using `Object.prototype.toString`.
377
- *
378
- * @private
379
- * @param {*} value The value to convert.
380
- * @returns {string} Returns the converted string.
381
- */
382
- function objectToString(value) {
383
- return nativeObjectToString.call(value);
384
- }
385
-
386
- var _objectToString = objectToString;
387
-
388
- /** `Object#toString` result references. */
389
- var nullTag = '[object Null]',
390
- undefinedTag = '[object Undefined]';
391
-
392
- /** Built-in value references. */
393
- var symToStringTag = _Symbol ? _Symbol.toStringTag : undefined;
394
-
395
- /**
396
- * The base implementation of `getTag` without fallbacks for buggy environments.
397
- *
398
- * @private
399
- * @param {*} value The value to query.
400
- * @returns {string} Returns the `toStringTag`.
401
- */
402
- function baseGetTag(value) {
403
- if (value == null) {
404
- return value === undefined ? undefinedTag : nullTag;
405
- }
406
- return (symToStringTag && symToStringTag in Object(value))
407
- ? _getRawTag(value)
408
- : _objectToString(value);
409
- }
410
-
411
- var _baseGetTag = baseGetTag;
412
-
413
- /**
414
- * Checks if `value` is the
415
- * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
416
- * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
417
- *
418
- * @static
419
- * @memberOf _
420
- * @since 0.1.0
421
- * @category Lang
422
- * @param {*} value The value to check.
423
- * @returns {boolean} Returns `true` if `value` is an object, else `false`.
424
- * @example
425
- *
426
- * _.isObject({});
427
- * // => true
428
- *
429
- * _.isObject([1, 2, 3]);
430
- * // => true
431
- *
432
- * _.isObject(_.noop);
433
- * // => true
434
- *
435
- * _.isObject(null);
436
- * // => false
437
- */
438
- function isObject(value) {
439
- var type = typeof value;
440
- return value != null && (type == 'object' || type == 'function');
441
- }
442
-
443
- var isObject_1 = isObject;
444
-
445
- /** `Object#toString` result references. */
446
- var asyncTag = '[object AsyncFunction]',
447
- funcTag = '[object Function]',
448
- genTag = '[object GeneratorFunction]',
449
- proxyTag = '[object Proxy]';
450
-
451
- /**
452
- * Checks if `value` is classified as a `Function` object.
453
- *
454
- * @static
455
- * @memberOf _
456
- * @since 0.1.0
457
- * @category Lang
458
- * @param {*} value The value to check.
459
- * @returns {boolean} Returns `true` if `value` is a function, else `false`.
460
- * @example
461
- *
462
- * _.isFunction(_);
463
- * // => true
464
- *
465
- * _.isFunction(/abc/);
466
- * // => false
467
- */
468
- function isFunction(value) {
469
- if (!isObject_1(value)) {
470
- return false;
471
- }
472
- // The use of `Object#toString` avoids issues with the `typeof` operator
473
- // in Safari 9 which returns 'object' for typed arrays and other constructors.
474
- var tag = _baseGetTag(value);
475
- return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;
476
- }
477
-
478
- var isFunction_1 = isFunction;
479
-
480
- /** Used to detect overreaching core-js shims. */
481
- var coreJsData = _root['__core-js_shared__'];
482
-
483
- var _coreJsData = coreJsData;
484
-
485
- /** Used to detect methods masquerading as native. */
486
- var maskSrcKey = (function() {
487
- var uid = /[^.]+$/.exec(_coreJsData && _coreJsData.keys && _coreJsData.keys.IE_PROTO || '');
488
- return uid ? ('Symbol(src)_1.' + uid) : '';
489
- }());
490
-
491
- /**
492
- * Checks if `func` has its source masked.
493
- *
494
- * @private
495
- * @param {Function} func The function to check.
496
- * @returns {boolean} Returns `true` if `func` is masked, else `false`.
497
- */
498
- function isMasked(func) {
499
- return !!maskSrcKey && (maskSrcKey in func);
500
- }
501
-
502
- var _isMasked = isMasked;
503
-
504
- /** Used for built-in method references. */
505
- var funcProto$1 = Function.prototype;
506
-
507
- /** Used to resolve the decompiled source of functions. */
508
- var funcToString$1 = funcProto$1.toString;
509
-
510
- /**
511
- * Converts `func` to its source code.
512
- *
513
- * @private
514
- * @param {Function} func The function to convert.
515
- * @returns {string} Returns the source code.
516
- */
517
- function toSource(func) {
518
- if (func != null) {
519
- try {
520
- return funcToString$1.call(func);
521
- } catch (e) {}
522
- try {
523
- return (func + '');
524
- } catch (e) {}
525
- }
526
- return '';
527
- }
528
-
529
- var _toSource = toSource;
530
-
531
- /**
532
- * Used to match `RegExp`
533
- * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
534
- */
535
- var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
536
-
537
- /** Used to detect host constructors (Safari). */
538
- var reIsHostCtor = /^\[object .+?Constructor\]$/;
539
-
540
- /** Used for built-in method references. */
541
- var funcProto = Function.prototype,
542
- objectProto$1 = Object.prototype;
543
-
544
- /** Used to resolve the decompiled source of functions. */
545
- var funcToString = funcProto.toString;
546
-
547
- /** Used to check objects for own properties. */
548
- var hasOwnProperty$1 = objectProto$1.hasOwnProperty;
549
-
550
- /** Used to detect if a method is native. */
551
- var reIsNative = RegExp('^' +
552
- funcToString.call(hasOwnProperty$1).replace(reRegExpChar, '\\$&')
553
- .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
554
- );
414
+ };
555
415
 
556
- /**
557
- * The base implementation of `_.isNative` without bad shim checks.
558
- *
559
- * @private
560
- * @param {*} value The value to check.
561
- * @returns {boolean} Returns `true` if `value` is a native function,
562
- * else `false`.
563
- */
564
- function baseIsNative(value) {
565
- if (!isObject_1(value) || _isMasked(value)) {
566
- return false;
416
+ const signMultiPdfFile = async ({
417
+ filename,
418
+ url,
419
+ signatureList
420
+ }) => {
421
+ const response = await window.fetch(url);
422
+ const pdfBytes = await response.arrayBuffer();
423
+ const pdfDoc = await PDFDocument.load(pdfBytes);
424
+ for (let item of signatureList) {
425
+ const {
426
+ x,
427
+ y,
428
+ page,
429
+ signature,
430
+ width,
431
+ height
432
+ } = item;
433
+ const pdfPage = pdfDoc.getPage(page - 1);
434
+ const signatureBytes = await window.fetch(signature).then(res => res.arrayBuffer());
435
+ const signatureImageEmbed = await pdfDoc.embedPng(signatureBytes);
436
+ pdfPage.drawImage(signatureImageEmbed, {
437
+ x,
438
+ y,
439
+ width,
440
+ height
441
+ });
567
442
  }
568
- var pattern = isFunction_1(value) ? reIsNative : reIsHostCtor;
569
- return pattern.test(_toSource(value));
570
- }
571
-
572
- var _baseIsNative = baseIsNative;
573
-
574
- /**
575
- * Gets the value at `key` of `object`.
576
- *
577
- * @private
578
- * @param {Object} [object] The object to query.
579
- * @param {string} key The key of the property to get.
580
- * @returns {*} Returns the property value.
581
- */
582
- function getValue(object, key) {
583
- return object == null ? undefined : object[key];
584
- }
585
-
586
- var _getValue = getValue;
587
-
588
- /**
589
- * Gets the native function at `key` of `object`.
590
- *
591
- * @private
592
- * @param {Object} object The object to query.
593
- * @param {string} key The key of the method to get.
594
- * @returns {*} Returns the function if it's native, else `undefined`.
595
- */
596
- function getNative(object, key) {
597
- var value = _getValue(object, key);
598
- return _baseIsNative(value) ? value : undefined;
599
- }
600
-
601
- var _getNative = getNative;
602
-
603
- /* Built-in method references that are verified to be native. */
604
- var Map = _getNative(_root, 'Map');
605
-
606
- var _Map = Map;
607
-
608
- /* Built-in method references that are verified to be native. */
609
- _getNative(Object, 'create');
610
-
611
- /**
612
- * Checks if `value` is object-like. A value is object-like if it's not `null`
613
- * and has a `typeof` result of "object".
614
- *
615
- * @static
616
- * @memberOf _
617
- * @since 4.0.0
618
- * @category Lang
619
- * @param {*} value The value to check.
620
- * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
621
- * @example
622
- *
623
- * _.isObjectLike({});
624
- * // => true
625
- *
626
- * _.isObjectLike([1, 2, 3]);
627
- * // => true
628
- *
629
- * _.isObjectLike(_.noop);
630
- * // => false
631
- *
632
- * _.isObjectLike(null);
633
- * // => false
634
- */
635
- function isObjectLike(value) {
636
- return value != null && typeof value == 'object';
637
- }
638
-
639
- var isObjectLike_1 = isObjectLike;
640
-
641
- /** `Object#toString` result references. */
642
- var argsTag = '[object Arguments]';
643
-
644
- /**
645
- * The base implementation of `_.isArguments`.
646
- *
647
- * @private
648
- * @param {*} value The value to check.
649
- * @returns {boolean} Returns `true` if `value` is an `arguments` object,
650
- */
651
- function baseIsArguments(value) {
652
- return isObjectLike_1(value) && _baseGetTag(value) == argsTag;
653
- }
654
-
655
- var _baseIsArguments = baseIsArguments;
656
-
657
- /** Used for built-in method references. */
658
- var objectProto = Object.prototype;
659
-
660
- /** Used to check objects for own properties. */
661
- var hasOwnProperty = objectProto.hasOwnProperty;
662
-
663
- /** Built-in value references. */
664
- var propertyIsEnumerable = objectProto.propertyIsEnumerable;
665
-
666
- /**
667
- * Checks if `value` is likely an `arguments` object.
668
- *
669
- * @static
670
- * @memberOf _
671
- * @since 0.1.0
672
- * @category Lang
673
- * @param {*} value The value to check.
674
- * @returns {boolean} Returns `true` if `value` is an `arguments` object,
675
- * else `false`.
676
- * @example
677
- *
678
- * _.isArguments(function() { return arguments; }());
679
- * // => true
680
- *
681
- * _.isArguments([1, 2, 3]);
682
- * // => false
683
- */
684
- _baseIsArguments(function() { return arguments; }()) ? _baseIsArguments : function(value) {
685
- return isObjectLike_1(value) && hasOwnProperty.call(value, 'callee') &&
686
- !propertyIsEnumerable.call(value, 'callee');
443
+ const modifiedPdfBytes = await pdfDoc.save();
444
+ return new window.File([modifiedPdfBytes], filename, {
445
+ type: 'application/pdf'
446
+ });
687
447
  };
688
-
689
- /**
690
- * This method returns `false`.
691
- *
692
- * @static
693
- * @memberOf _
694
- * @since 4.13.0
695
- * @category Util
696
- * @returns {boolean} Returns `false`.
697
- * @example
698
- *
699
- * _.times(2, _.stubFalse);
700
- * // => [false, false]
701
- */
702
- function stubFalse() {
703
- return false;
704
- }
705
-
706
- var stubFalse_1 = stubFalse;
707
-
708
- createCommonjsModule(function (module, exports) {
709
- /** Detect free variable `exports`. */
710
- var freeExports = exports && !exports.nodeType && exports;
711
-
712
- /** Detect free variable `module`. */
713
- var freeModule = freeExports && 'object' == 'object' && module && !module.nodeType && module;
714
-
715
- /** Detect the popular CommonJS extension `module.exports`. */
716
- var moduleExports = freeModule && freeModule.exports === freeExports;
717
-
718
- /** Built-in value references. */
719
- var Buffer = moduleExports ? _root.Buffer : undefined;
720
-
721
- /* Built-in method references for those with the same name as other `lodash` methods. */
722
- var nativeIsBuffer = Buffer ? Buffer.isBuffer : undefined;
723
-
724
- /**
725
- * Checks if `value` is a buffer.
726
- *
727
- * @static
728
- * @memberOf _
729
- * @since 4.3.0
730
- * @category Lang
731
- * @param {*} value The value to check.
732
- * @returns {boolean} Returns `true` if `value` is a buffer, else `false`.
733
- * @example
734
- *
735
- * _.isBuffer(new Buffer(2));
736
- * // => true
737
- *
738
- * _.isBuffer(new Uint8Array(2));
739
- * // => false
740
- */
741
- var isBuffer = nativeIsBuffer || stubFalse_1;
742
-
743
- module.exports = isBuffer;
744
- });
745
-
746
- createCommonjsModule(function (module, exports) {
747
- /** Detect free variable `exports`. */
748
- var freeExports = exports && !exports.nodeType && exports;
749
-
750
- /** Detect free variable `module`. */
751
- var freeModule = freeExports && 'object' == 'object' && module && !module.nodeType && module;
752
-
753
- /** Detect the popular CommonJS extension `module.exports`. */
754
- var moduleExports = freeModule && freeModule.exports === freeExports;
755
-
756
- /** Detect free variable `process` from Node.js. */
757
- var freeProcess = moduleExports && _freeGlobal.process;
758
-
759
- /** Used to access faster Node.js helpers. */
760
- var nodeUtil = (function() {
761
- try {
762
- // Use `util.types` for Node.js 10+.
763
- var types = freeModule && freeModule.require && freeModule.require('util').types;
764
-
765
- if (types) {
766
- return types;
767
- }
768
-
769
- // Legacy `process.binding('util')` for Node.js < 10.
770
- return freeProcess && freeProcess.binding && freeProcess.binding('util');
771
- } catch (e) {}
772
- }());
773
-
774
- module.exports = nodeUtil;
775
- });
776
-
777
- /* Built-in method references that are verified to be native. */
778
- var DataView = _getNative(_root, 'DataView');
779
-
780
- var _DataView = DataView;
781
-
782
- /* Built-in method references that are verified to be native. */
783
- var Promise$1 = _getNative(_root, 'Promise');
784
-
785
- var _Promise = Promise$1;
786
-
787
- /* Built-in method references that are verified to be native. */
788
- var Set = _getNative(_root, 'Set');
789
-
790
- var _Set = Set;
791
-
792
- /* Built-in method references that are verified to be native. */
793
- var WeakMap = _getNative(_root, 'WeakMap');
794
-
795
- var _WeakMap = WeakMap;
796
-
797
- /** `Object#toString` result references. */
798
- var mapTag = '[object Map]',
799
- objectTag = '[object Object]',
800
- promiseTag = '[object Promise]',
801
- setTag = '[object Set]',
802
- weakMapTag = '[object WeakMap]';
803
-
804
- var dataViewTag = '[object DataView]';
805
-
806
- /** Used to detect maps, sets, and weakmaps. */
807
- var dataViewCtorString = _toSource(_DataView),
808
- mapCtorString = _toSource(_Map),
809
- promiseCtorString = _toSource(_Promise),
810
- setCtorString = _toSource(_Set),
811
- weakMapCtorString = _toSource(_WeakMap);
812
-
813
- /**
814
- * Gets the `toStringTag` of `value`.
815
- *
816
- * @private
817
- * @param {*} value The value to query.
818
- * @returns {string} Returns the `toStringTag`.
819
- */
820
- var getTag = _baseGetTag;
821
-
822
- // Fallback for data views, maps, sets, and weak maps in IE 11 and promises in Node.js < 6.
823
- if ((_DataView && getTag(new _DataView(new ArrayBuffer(1))) != dataViewTag) ||
824
- (_Map && getTag(new _Map) != mapTag) ||
825
- (_Promise && getTag(_Promise.resolve()) != promiseTag) ||
826
- (_Set && getTag(new _Set) != setTag) ||
827
- (_WeakMap && getTag(new _WeakMap) != weakMapTag)) {
828
- getTag = function(value) {
829
- var result = _baseGetTag(value),
830
- Ctor = result == objectTag ? value.constructor : undefined,
831
- ctorString = Ctor ? _toSource(Ctor) : '';
832
-
833
- if (ctorString) {
834
- switch (ctorString) {
835
- case dataViewCtorString: return dataViewTag;
836
- case mapCtorString: return mapTag;
837
- case promiseCtorString: return promiseTag;
838
- case setCtorString: return setTag;
839
- case weakMapCtorString: return weakMapTag;
840
- }
841
- }
842
- return result;
843
- };
844
- }
845
-
846
448
  const signPdfFile = async pdfSignature => {
847
449
  const {
848
450
  x,
@@ -854,25 +456,43 @@ const signPdfFile = async pdfSignature => {
854
456
  url,
855
457
  filename
856
458
  } = pdfSignature;
857
- const response = await window.fetch(url);
858
- const pdfBytes = await response.arrayBuffer();
859
- const pdfDoc = await PDFDocument.load(pdfBytes);
860
- const pdfPage = pdfDoc.getPage(page - 1);
861
- const signatureBytes = await window.fetch(signature).then(res => res.arrayBuffer());
862
- const signatureImageEmbed = await pdfDoc.embedPng(signatureBytes);
863
- pdfPage.drawImage(signatureImageEmbed, {
864
- x,
865
- y,
866
- width,
867
- height
868
- });
869
- const modifiedPdfBytes = await pdfDoc.save();
870
- return new window.File([modifiedPdfBytes], filename, {
871
- type: 'application/pdf'
459
+ return signMultiPdfFile({
460
+ filename,
461
+ url,
462
+ signatureList: [{
463
+ x,
464
+ y,
465
+ page,
466
+ signature,
467
+ width,
468
+ height
469
+ }]
872
470
  });
873
471
  };
874
472
 
875
- const _excluded$1 = ["placeholder", "signature", "url", "width", "height", "padding", "filename", "defaultLocation", "onChange"];
473
+ const computedPDFSignLocation = ({
474
+ size,
475
+ location
476
+ }) => {
477
+ const scaleX = size.width / size.originalWidth;
478
+ const scaleY = size.height / size.originalHeight;
479
+ const pdfX = Math.round(location.size.x / scaleX);
480
+ const pdfY = Math.round(size.originalHeight - location.size.y / scaleY);
481
+ const signWidth = Math.round(location.size.width / scaleX);
482
+ const signHeight = Math.round(location.size.height / scaleY);
483
+ return {
484
+ scaleX,
485
+ scaleY,
486
+ pdfX,
487
+ pdfY,
488
+ width: signWidth,
489
+ height: signHeight,
490
+ x: pdfX,
491
+ y: pdfY - signHeight
492
+ };
493
+ };
494
+
495
+ const _excluded$2 = ["placeholder", "signature", "url", "width", "height", "padding", "filename", "defaultLocation", "onChange"];
876
496
  const PDFSignInner = /*#__PURE__*/forwardRef(({
877
497
  size,
878
498
  currentPage,
@@ -887,42 +507,29 @@ const PDFSignInner = /*#__PURE__*/forwardRef(({
887
507
  onChange
888
508
  }, ref) => {
889
509
  const initLocation = useMemo(() => {
890
- return {
891
- scaleX: 1,
892
- scaleY: 1,
893
- x: Math.round((size.width - _width) / 2),
894
- y: Math.round((size.height - _height) / 2),
895
- size: {
896
- width: _width,
897
- height: _height,
898
- x: Math.round((size.width - _width) / 2),
899
- y: Math.round((size.height - _height) / 2)
900
- }
901
- };
510
+ return getInitLocation({
511
+ stageWidth: size.width,
512
+ stageHeight: size.height,
513
+ width: _width,
514
+ height: _height
515
+ });
902
516
  }, [size, _width, _height]);
903
517
  const [location, setLocationOrigin] = useState(Object.assign({}, initLocation, defaultLocation));
904
518
  const setLocation = useRefCallback(value => {
905
519
  setLocationOrigin(Object.assign({}, initLocation, value));
906
520
  });
907
521
  const pdfSignature = useMemo(() => {
908
- const scaleX = size.width / size.originalWidth;
909
- const scaleY = size.height / size.originalHeight;
910
- const pdfX = Math.round(location.size.x / scaleX);
911
- const pdfY = Math.round(size.originalHeight - location.size.y / scaleY);
912
- const signWidth = Math.round(location.size.width / scaleX);
913
- const signHeight = Math.round(location.size.height / scaleY);
914
- return {
915
- x: pdfX,
916
- y: pdfY - signHeight,
917
- page: currentPage,
918
- pageWidth: Math.round(size.originalWidth),
919
- pageHeight: Math.round(size.originalHeight),
920
- width: signWidth,
921
- height: signHeight,
522
+ return Object.assign({}, computedPDFSignLocation({
523
+ location,
524
+ size
525
+ }), {
922
526
  signature,
923
527
  url,
924
- filename: _filename
925
- };
528
+ filename: _filename,
529
+ page: currentPage,
530
+ pageWidth: Math.round(size.originalWidth),
531
+ pageHeight: Math.round(size.originalHeight)
532
+ });
926
533
  }, [location, signature, url, _filename, size, currentPage]);
927
534
  const signPdf = useCallback(async () => {
928
535
  return await signPdfFile(pdfSignature);
@@ -964,7 +571,7 @@ const PDFSign = withLocale(/*#__PURE__*/forwardRef((_ref, ref) => {
964
571
  defaultLocation,
965
572
  onChange
966
573
  } = _ref,
967
- props = _objectWithoutPropertiesLoose(_ref, _excluded$1);
574
+ props = _objectWithoutPropertiesLoose(_ref, _excluded$2);
968
575
  return /*#__PURE__*/jsx(PDFViewer, _extends({}, props, {
969
576
  url: url,
970
577
  children: ({
@@ -989,6 +596,131 @@ const PDFSign = withLocale(/*#__PURE__*/forwardRef((_ref, ref) => {
989
596
  }));
990
597
  }));
991
598
 
599
+ const _excluded$1 = ["placeholder", "url", "width", "height", "padding", "filename", "defaultSignatureList", "onSign", "onChange", "isEdit"];
600
+ const PDFSignMultiInner = /*#__PURE__*/forwardRef(({
601
+ size,
602
+ currentPage,
603
+ placeholder,
604
+ url,
605
+ width: _width = 200,
606
+ height: _height = 80,
607
+ padding,
608
+ filename: _filename = 'signed-document.pdf',
609
+ defaultSignatureList,
610
+ isEdit,
611
+ onSign,
612
+ onChange
613
+ }, ref) => {
614
+ const [signatureList, setSignatureList] = useState(defaultSignatureList || []);
615
+ const {
616
+ formatMessage
617
+ } = useIntl();
618
+ const pdfSignatureList = useMemo(() => {
619
+ return signatureList.filter(location => location.signature).map(location => {
620
+ return Object.assign({}, computedPDFSignLocation({
621
+ location,
622
+ size
623
+ }), {
624
+ page: location.page,
625
+ signature: location.signature
626
+ });
627
+ });
628
+ }, [signatureList, size]);
629
+ useImperativeHandle(ref, () => ({
630
+ getSignatureList: () => signatureList,
631
+ setSignatureList: value => setSignatureList(value),
632
+ getPdfSignatureList: () => {
633
+ return pdfSignatureList;
634
+ },
635
+ sign: () => {
636
+ if (!pdfSignatureList.length) {
637
+ return Promise.reject(new Error(formatMessage({
638
+ id: 'signatureAdd'
639
+ })));
640
+ }
641
+ return signMultiPdfFile({
642
+ url,
643
+ filename: _filename,
644
+ signatureList: pdfSignatureList
645
+ });
646
+ },
647
+ addSignLocation: () => {
648
+ setSignatureList(signatureList => {
649
+ return [...signatureList, {
650
+ page: currentPage
651
+ }];
652
+ });
653
+ }
654
+ }));
655
+ return /*#__PURE__*/jsx(LocationGroup, {
656
+ isEdit: isEdit,
657
+ currentPage: currentPage,
658
+ stageWidth: size.width,
659
+ stageHeight: size.height,
660
+ width: _width,
661
+ height: _height,
662
+ padding: padding,
663
+ placeholder: placeholder,
664
+ value: signatureList,
665
+ onChange: setSignatureList,
666
+ onClick: ({
667
+ index,
668
+ value
669
+ }) => {
670
+ onSign && onSign({
671
+ size: value.size,
672
+ callback: signature => {
673
+ setSignatureList(value => {
674
+ const newValue = value.slice(0);
675
+ newValue[index] = Object.assign({}, newValue[index], {
676
+ signature
677
+ });
678
+ return newValue;
679
+ });
680
+ }
681
+ });
682
+ }
683
+ });
684
+ });
685
+ const PDFSignMulti = withLocale(/*#__PURE__*/forwardRef((_ref, ref) => {
686
+ let {
687
+ placeholder,
688
+ url,
689
+ width,
690
+ height,
691
+ padding,
692
+ filename = 'signed-document.pdf',
693
+ defaultSignatureList,
694
+ onSign,
695
+ onChange,
696
+ isEdit
697
+ } = _ref,
698
+ props = _objectWithoutPropertiesLoose(_ref, _excluded$1);
699
+ return /*#__PURE__*/jsx(PDFViewer, _extends({}, props, {
700
+ url: url,
701
+ children: ({
702
+ size,
703
+ currentPage
704
+ }) => {
705
+ return /*#__PURE__*/jsx(PDFSignMultiInner, {
706
+ ref: ref,
707
+ size: size,
708
+ currentPage: currentPage,
709
+ url: url,
710
+ filename: filename,
711
+ defaultSignatureList: defaultSignatureList,
712
+ width: width,
713
+ height: height,
714
+ padding: padding,
715
+ placeholder: placeholder,
716
+ onChange: onChange,
717
+ onSign: onSign,
718
+ isEdit: isEdit
719
+ });
720
+ }
721
+ }));
722
+ }));
723
+
992
724
  const _excluded = ["filename", "onSuccess", "width", "height", "mask"];
993
725
  const dataURLtoBlob = dataURL => {
994
726
  let arr = dataURL.split(',');
@@ -1021,6 +753,7 @@ const Signature = withLocale(({
1021
753
  } = App.useApp();
1022
754
  const signatureCanvasRef = useRef(null);
1023
755
  const maskRef = useRef(null);
756
+ const [loading, setLoading] = useState(false);
1024
757
  return /*#__PURE__*/jsxs(Flex, {
1025
758
  vertical: true,
1026
759
  gap: 12,
@@ -1028,7 +761,7 @@ const Signature = withLocale(({
1028
761
  children: [/*#__PURE__*/jsxs("div", {
1029
762
  className: classnames(style['signature-container'], 'signature-container'),
1030
763
  style: {
1031
- width: '368px',
764
+ width: '100%',
1032
765
  height: `${Math.round(height * 368 / width)}px`
1033
766
  },
1034
767
  children: [/*#__PURE__*/jsx("div", {
@@ -1047,43 +780,54 @@ const Signature = withLocale(({
1047
780
  gap: 10,
1048
781
  children: [/*#__PURE__*/jsx(Button, {
1049
782
  onClick: () => {
1050
- onClose();
783
+ signatureCanvasRef.current.clear();
1051
784
  },
1052
785
  children: formatMessage({
1053
- id: 'signatureCancelText'
786
+ id: 'signatureCleanText'
1054
787
  })
1055
788
  }), /*#__PURE__*/jsx(Button, {
789
+ loading: loading,
1056
790
  type: "primary",
1057
791
  onClick: async () => {
1058
- if (signatureCanvasRef.current.isEmpty()) {
1059
- message.error(formatMessage({
1060
- id: 'signatureEmptyError'
1061
- }));
1062
- return;
1063
- }
1064
- let result = signatureCanvasRef.current.toDataURL('image/png');
1065
- if (_mask) {
1066
- const maskPng = await snapdom.toPng(maskRef.current, {
1067
- scale: 2
792
+ setLoading(true);
793
+ try {
794
+ if (signatureCanvasRef.current.isEmpty()) {
795
+ message.error(formatMessage({
796
+ id: 'signatureEmptyError'
797
+ }));
798
+ return;
799
+ }
800
+ let result = signatureCanvasRef.current.toDataURL('image/png');
801
+ if (_mask) {
802
+ const maskPng = await snapdom.toPng(maskRef.current, {
803
+ scale: 2
804
+ });
805
+ const canvas = document.createElement('canvas');
806
+ canvas.width = width * 2;
807
+ canvas.height = height * 2;
808
+ const ctx = canvas.getContext('2d');
809
+ ctx.drawImage(maskPng, 0, 0, canvas.width, canvas.height);
810
+ const resultImage = new Image();
811
+ resultImage.src = result;
812
+ await new Promise(resolve => {
813
+ resultImage.onload = resolve;
814
+ });
815
+ ctx.drawImage(resultImage, 0, 0, canvas.width, canvas.height);
816
+ result = canvas.toDataURL('image/png');
817
+ }
818
+ const file = new window.File([dataURLtoBlob(result)], filename, {
819
+ type: 'image/png'
1068
820
  });
1069
- const canvas = document.createElement('canvas');
1070
- canvas.width = width * 2;
1071
- canvas.height = height * 2;
1072
- const ctx = canvas.getContext('2d');
1073
- ctx.drawImage(maskPng, 0, 0, canvas.width, canvas.height);
1074
- const resultImage = new Image();
1075
- resultImage.src = result;
1076
- await new Promise(resolve => {
1077
- resultImage.onload = resolve;
1078
- });
1079
- ctx.drawImage(resultImage, 0, 0, canvas.width, canvas.height);
1080
- result = canvas.toDataURL('image/png');
821
+ const successResult = onSuccess && (await onSuccess(file));
822
+ setLoading(false);
823
+ if (successResult === false) {
824
+ return;
825
+ }
826
+ onClose();
827
+ } catch (e) {
828
+ message.error(e.message);
829
+ setLoading(false);
1081
830
  }
1082
- const file = new window.File([dataURLtoBlob(result)], filename, {
1083
- type: 'image/png'
1084
- });
1085
- onClose();
1086
- onSuccess(file);
1087
831
  },
1088
832
  children: formatMessage({
1089
833
  id: 'signatureConfirmText'
@@ -1120,6 +864,7 @@ const useSignature = () => {
1120
864
  title: /*#__PURE__*/jsx(Title, {}),
1121
865
  icon: null,
1122
866
  footer: null,
867
+ closable: true,
1123
868
  wrapClassName: style['signature-modal'],
1124
869
  classNames: {
1125
870
  content: style['signature-modal']
@@ -1140,5 +885,5 @@ const useSignature = () => {
1140
885
  };
1141
886
  };
1142
887
 
1143
- export { LocationLayer, PDFSign, PDFViewer, PDFSign as default, useSignature };
888
+ export { LocationGroup, LocationLayer, PDFSign, PDFSignMulti, PDFViewer, PDFSign as default, signPdfFile, useSignature };
1144
889
  //# sourceMappingURL=index.modern.js.map