forked from baidu/amis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request baidu#9726 from allenve/master
feat:amis支持手写签名面板
- Loading branch information
Showing
13 changed files
with
408 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
--- | ||
title: inputSignature 签名面板 | ||
description: | ||
type: 0 | ||
group: null | ||
menuName: inputSignature | ||
icon: | ||
order: 62 | ||
--- | ||
|
||
## 基本用法 | ||
|
||
```schema: scope="body" | ||
{ | ||
"type": "form", | ||
"api": "/api/mock2/form/saveForm", | ||
"body": [ | ||
{ | ||
"name": "signature", | ||
"type": "input-signature", | ||
"label": "手写签名", | ||
"height": 200 | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## 水平展示 | ||
|
||
```schema: scope="body" | ||
{ | ||
"type": "form", | ||
"api": "/api/mock2/form/saveForm", | ||
"body": [ | ||
{ | ||
"name": "signature", | ||
"type": "input-signature", | ||
"horiz": true, | ||
"height": 300 | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## 自定义按钮名称 | ||
|
||
```schema: scope="body" | ||
{ | ||
"type": "form", | ||
"api": "/api/mock2/form/saveForm", | ||
"body": [ | ||
{ | ||
"name": "signature", | ||
"type": "input-signature", | ||
"height": 160, | ||
"confirmText": "确定", | ||
"undoText": "上一步", | ||
"clearText": "重置" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## 自定义颜色 | ||
|
||
```schema: scope="body" | ||
{ | ||
"type": "form", | ||
"api": "/api/mock2/form/saveForm", | ||
"body": [ | ||
{ | ||
"name": "signature", | ||
"type": "input-signature", | ||
"label": "手写签名", | ||
"height": 200, | ||
"color": "#ff0000", | ||
"bgColor": "#fff" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## 配合图片组件实现实时预览 | ||
|
||
```schema: scope="body" | ||
{ | ||
"type": "form", | ||
"api": "/api/mock2/form/saveForm", | ||
"body": [ | ||
{ | ||
"name": "signature", | ||
"type": "input-signature", | ||
"label": "手写签名", | ||
"height": 200 | ||
}, | ||
{ | ||
"type": "image", | ||
"name": "signature" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## 属性表 | ||
|
||
| 属性名 | 类型 | 默认值 | 说明 | | ||
| ----------- | --------- | --------- | ------------------ | | ||
| width | `number` | | 组件宽度,最小 300 | | ||
| height | `number` | | 组件高度,最小 160 | | ||
| color | `string` | `#000` | 手写字体颜色 | | ||
| bgColor | `string` | `#EFEFEF` | 面板背景颜色 | | ||
| clearText | `string` | `清空` | 清空按钮名称 | | ||
| undoText | `string` | `撤销` | 撤销按钮名称 | | ||
| confirmText | `string` | `确认` | 确认按钮名称 | | ||
| horiz | `boolean` | | 是否水平展示 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.#{$ns}Signature { | ||
width: 100%; | ||
height: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-between; | ||
&-canvas { | ||
border: 1px dashed #aaa; | ||
} | ||
&-Tool { | ||
text-align: center; | ||
height: 40px; | ||
margin-top: 8px; | ||
button { | ||
margin-left: 5px; | ||
} | ||
} | ||
|
||
&.is-horizontal { | ||
flex-direction: row-reverse; | ||
align-items: center; | ||
.#{$ns}Signature-Tool { | ||
width: 32px; | ||
height: 100%; | ||
margin-top: 0; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
.actions { | ||
white-space: nowrap; | ||
transform: rotate(90deg); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* @file Signature.tsx 签名组件 | ||
* | ||
* @created: 2024/03/04 | ||
*/ | ||
|
||
import React from 'react'; | ||
import {themeable, ThemeProps} from 'amis-core'; | ||
import {LocaleProps, localeable} from 'amis-core'; | ||
import {resizeSensor} from 'amis-core'; | ||
import SmoothSignature from 'smooth-signature'; | ||
import Button from './Button'; | ||
|
||
export interface ISignatureProps extends LocaleProps, ThemeProps { | ||
width?: number; | ||
height?: number; | ||
color?: string; | ||
bgColor?: string; | ||
clearText?: string; | ||
undoText?: string; | ||
confirmText?: string; | ||
horizontal?: boolean; | ||
onChange?: (value?: string) => void; | ||
} | ||
|
||
const Signature: React.FC<ISignatureProps> = props => { | ||
const {classnames: cx, horizontal, width, height} = props; | ||
const [sign, setSign] = React.useState<SmoothSignature | null>(null); | ||
const wrapper = React.useRef<HTMLDivElement>(null); | ||
const canvas = React.useRef<HTMLCanvasElement>(null); | ||
|
||
React.useEffect(() => { | ||
if (!wrapper.current || !canvas.current) { | ||
return; | ||
} | ||
|
||
initCanvas(canvas.current); | ||
const unSensor = resizeSensor(wrapper.current, resize); | ||
|
||
return () => { | ||
setSign(null); | ||
unSensor(); | ||
}; | ||
}, []); | ||
|
||
const clear = React.useCallback(() => { | ||
if (sign) { | ||
sign.clear(); | ||
props.onChange?.(undefined); | ||
} | ||
}, [sign]); | ||
const undo = React.useCallback(() => { | ||
if (sign) { | ||
sign.undo(); | ||
} | ||
}, [sign]); | ||
const confirm = React.useCallback(() => { | ||
if (sign) { | ||
const base64 = sign.toDataURL(); | ||
props.onChange?.(base64); | ||
} | ||
}, [sign]); | ||
const resize = React.useCallback(() => { | ||
setSign(null); | ||
initCanvas(canvas.current!); | ||
}, []); | ||
const initCanvas = React.useCallback( | ||
(element: HTMLCanvasElement) => { | ||
const {width, height} = props; | ||
const rect = element.parentElement!.getBoundingClientRect(); | ||
const clientWidth = Math.floor(rect.width); | ||
const defaultWidth = width || clientWidth - (horizontal ? 40 : 0); | ||
const defaultHeight = height || clientWidth / 2 - (horizontal ? 0 : 40); | ||
const signature = new SmoothSignature(element, { | ||
width: Math.max(defaultWidth, 300), | ||
height: Math.max(defaultHeight, 160), | ||
color: props.color || '#000', | ||
bgColor: props.bgColor || '#efefef' | ||
}); | ||
setSign(signature); | ||
}, | ||
[width, height, horizontal] | ||
); | ||
|
||
function renderTool() { | ||
const {translate: __, clearText, undoText, confirmText} = props; | ||
return ( | ||
<div className={cx('Signature-Tool')}> | ||
<div className="actions"> | ||
<Button onClick={clear}>{clearText || __('Signature.clear')}</Button> | ||
<Button onClick={undo}>{undoText || __('Signature.undo')}</Button> | ||
<Button onClick={confirm}> | ||
{confirmText || __('Signature.confirm')} | ||
</Button> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div | ||
className={cx('Signature', { | ||
'is-horizontal': horizontal | ||
})} | ||
ref={wrapper} | ||
> | ||
<canvas className={cx('Signature-canvas')} ref={canvas} /> | ||
{renderTool()} | ||
</div> | ||
); | ||
}; | ||
|
||
export default themeable(localeable(Signature)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.