import React, { useMemo } from 'react'
import classes from './styles.module.css'
import NiceModal, { useModal } from '@ebay/nice-modal-react'
import { debounce } from 'lodash-es'
import { BodyEditor } from '../../editor'
import i18n, { IsChina } from '../../i18n'
import { Helper } from '../../environments/online/helper'
import { SetCDNBase } from '../../utils/detect'
const { Root, ContextMenuContent, ContextMenuItem, RightSlot } = classes
const MyContextMenu = NiceModal.create<{
editor: BodyEditor
mouseX: number
mouseY: number
onChangeBackground: (url: string) => void
}>(({ editor, mouseX, mouseY, onChangeBackground }) => {
const helper = useMemo(() => new Helper(editor), [editor])
const modal = useModal()
return (
{
modal.hide()
}}
onContextMenu={(e) => {
e.preventDefault()
}}
>
{
editor.Undo()
}}
>
{i18n.t('Undo')}
⌘ Z
{
editor.Redo()
}}
>
{i18n.t('Redo')}
⇧ ⌘ Z
{
helper.CopySkeleton()
}}
>
{i18n.t('Duplicate Skeleton')}
⇧ D
{
helper.RemoveSkeleton()
}}
>
{i18n.t('Delete Skeleton')}
{i18n.t('Del')}
{
helper.CopyKeypointToClipboard()
}}
>
{i18n.t('Copy Keypoint Data')}
{
helper.SetRandomPose()
}}
>
{i18n.t('Set Random Pose')}
helper.DetectFromImage(onChangeBackground)}
>
{i18n.t('Detect From Image')}
{IsChina() ? (
{
SetCDNBase(false)
helper.DetectFromImage(onChangeBackground)
}}
>
{i18n.t('Detect From Image') + ' [中国]'}
) : undefined}
)
})
declare type NiceModalArgs = T extends
| keyof JSX.IntrinsicElements
| React.JSXElementConstructor
? Omit, 'id'>
: Record
export type ShowProps = NiceModalArgs
export function GetContextMenu(wait = 0) {
const show = debounce((props: ShowProps) => {
NiceModal.show(MyContextMenu, props)
}, wait)
return {
show: (props: ShowProps) => {
show(props)
},
hide: () => {
show.cancel()
HideContextMenu()
},
}
}
export async function ShowContextMenu(props: ShowProps): Promise {
return await NiceModal.show(MyContextMenu, props)
}
export function HideContextMenu() {
return NiceModal.hide(MyContextMenu)
}