feat(pdf-fields): add logic to hide signers on ESC

This commit is contained in:
NostrDev 2024-11-01 10:50:54 +03:00
parent 33e7fc7771
commit d89aea96bc

View File

@ -9,8 +9,16 @@ import {
} from '@mui/material'
import styles from './style.module.scss'
import React, { useEffect, useState } from 'react'
import { ProfileMetadata, User, UserRole } from '../../types'
import { MouseState, PdfPage, DrawnField, DrawTool } from '../../types/drawing'
import {
ProfileMetadata,
User,
UserRole,
KeyboardCode,
MouseState,
PdfPage,
DrawnField,
DrawTool
} from '../../types'
import { hexToNpub, npubToHex, getProfileUsername } from '../../utils'
import { SigitFile } from '../../utils/file'
import { getToolboxLabelByMarkType } from '../../utils/mark'
@ -41,6 +49,10 @@ export const DrawPDFFields = (props: Props) => {
const signers = users.filter((u) => u.role === UserRole.signer)
const defaultSignerNpub = signers.length ? hexToNpub(signers[0].pubkey) : ''
const [lastSigner, setLastSigner] = useState(defaultSignerNpub)
const [hideSignersForDrawnField, setHideSignersForDrawnField] = useState<{
[key: number]: boolean
} | null>()
/**
* Return first pubkey that is present in the signers list
* @param pubkeys
@ -361,6 +373,7 @@ export const DrawPDFFields = (props: Props) => {
rect
}
}
/**
* Renders the pdf pages and drawing elements
*/
@ -492,13 +505,17 @@ export const DrawPDFFields = (props: Props) => {
fileIndex,
pageIndex,
drawnFieldIndex
) && (
) &&
(!hideSignersForDrawnField ||
!hideSignersForDrawnField[drawnFieldIndex]) && (
<div
onPointerDown={handleUserSelectPointerDown}
className={styles.userSelect}
>
<FormControl fullWidth size="small">
<InputLabel id="counterparts">Counterpart</InputLabel>
<InputLabel id="counterparts">
Counterpart
</InputLabel>
<Select
value={getAvailableSigner(drawnField.counterpart)}
onChange={(event) => {
@ -514,6 +531,30 @@ export const DrawPDFFields = (props: Props) => {
renderValue={(value) =>
renderCounterpartValue(value)
}
onClose={(event: React.SyntheticEvent) => {
// get native event
const { nativeEvent } = event
// check if event is a keyboard event
if (nativeEvent instanceof KeyboardEvent) {
// check if event code is Escape
if (
nativeEvent.code === KeyboardCode.Escape
) {
// set hide signers for this DrawnField
if (hideSignersForDrawnField) {
setHideSignersForDrawnField((prev) => ({
...prev,
[drawnFieldIndex]: true
}))
} else {
setHideSignersForDrawnField({
[drawnFieldIndex]: true
})
}
}
}
}}
>
{signers.map((signer, index) => {
const npub = hexToNpub(signer.pubkey)