Skip to content

Commit

Permalink
[add] Neo: support loading & setFieldsValue
Browse files Browse the repository at this point in the history
  • Loading branch information
MatrixAge committed Oct 24, 2023
1 parent 74c38d8 commit 184c442
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 28 deletions.
9 changes: 9 additions & 0 deletions packages/xgen/actions/Common/emitEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Action } from '@/types'

export default () => {
return async (payload: Action.EmitEvent) => {
const { key, value } = payload

await window.$app.Event.emit(key, value)
}
}
2 changes: 2 additions & 0 deletions packages/xgen/actions/Common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export { default as historyBack } from './historyBack'
export { default as confirm } from './confirm'
export { default as refetch } from './refetch'
export { default as reload } from './reload'
export { default as showMessage } from './showMessage'
export { default as emitEvent } from './emitEvent'
25 changes: 25 additions & 0 deletions packages/xgen/actions/Common/showMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { message } from 'antd'

import type { Action } from '@/types'

export default () => {
return (payload: Action.ShowMessage) => {
return new Promise<void>((resolve) => {
const { type, content } = payload

switch (type) {
case 'success':
message.success(content)
break
case 'warning':
message.warning(content)
break
case 'error':
message.error(content)
break
}

resolve()
})
}
}
18 changes: 17 additions & 1 deletion packages/xgen/actions/utils/handleActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { closeModal, confirm, historyBack, historyPush, openModal, refetch, reload } from '../Common'
import {
closeModal,
confirm,
historyBack,
historyPush,
openModal,
refetch,
reload,
showMessage,
emitEvent
} from '../Common'
import { delete as formDelete, find as formFind, fullscreen as formFullscreen, submit as formSubmit } from '../Form'
import { delete as TableDelete, save as tableSave, search as tableSearch } from '../Table'
import { Service, Studio } from '../Yao'
Expand Down Expand Up @@ -34,6 +44,12 @@ export default ({ namespace, primary, data_item, it, extra }: OnAction) => {
case 'Common.reload':
total.push({ task: reload(), ...flow_info })
break
case 'Common.showMessage':
total.push({ task: showMessage(), ...flow_info })
break
case 'Common.emitEvent':
total.push({ task: emitEvent(), ...flow_info })
break
case 'Table.search':
total.push({ task: tableSearch({ namespace }), ...flow_info })
break
Expand Down
28 changes: 23 additions & 5 deletions packages/xgen/components/base/PureForm/components/FormItem.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMemoizedFn } from 'ahooks'
import { Col, Popover, Input } from 'antd'
import { Col, Popover, Input, Button } from 'antd'
import clsx from 'clsx'
import { observer } from 'mobx-react-lite'
import { PaperPlaneTilt } from 'phosphor-react'
import { useMemo, useState, useRef } from 'react'
import { useMemo, useState, useRef, useEffect } from 'react'

import { X } from '@/components'
import { useGlobal } from '@/context/app'
Expand All @@ -19,6 +19,15 @@ const Index = (props: IPropsFormItem) => {
const global = useGlobal()
const input = useRef<any>(null)
const [visible, setVisible] = useState(false)
const [loading, setLoading] = useState(false)

const unLoading = useMemoizedFn(() => setLoading(false))

useEffect(() => {
window.$app.Event.on(`${namespace}/${item.bind}/unloading`, unLoading)

return () => window.$app.Event.off(`${namespace}/${item.bind}/unloading`, unLoading)
}, [namespace, item])

const show_ai = useMemo(
() => global.app_info.optional?.neo?.api && item.edit.props?.ai,
Expand All @@ -38,19 +47,23 @@ const Index = (props: IPropsFormItem) => {

const showAI = useMemoizedFn(() => setVisible(true))

const askAI = useMemoizedFn(() => {
const askAI = useMemoizedFn((e) => {
e.preventDefault()

const target = input?.current?.resizableTextArea?.textArea

if (!target) return
if (!target.value) return

window.$app.Event.emit('app/getField', {
title: item.name,
bind: item.bind,
text: target.value
text: target.value,
config: item
})

setVisible(false)
setLoading(true)
})

const Ask = (
Expand Down Expand Up @@ -83,6 +96,11 @@ const Index = (props: IPropsFormItem) => {
AI
</span>
)}
{loading && (
<Button className='ai_loading' type='ghost' size='small' loading>
<span className='mark_ai'>AI</span>
</Button>
)}
<X
type='edit'
name={item.edit.type}
Expand Down
14 changes: 14 additions & 0 deletions packages/xgen/components/base/PureForm/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@
opacity: 1;
}
}

.ai_loading{
position: absolute !important;
top: -2px;
right: 10px;
z-index: 10;
padding: 0;
border: none;

.mark_ai{
opacity: 1;
}
}

}

.xgen-tabs {
Expand Down
8 changes: 6 additions & 2 deletions packages/xgen/components/base/PureForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ const Index = (props: IPropsPureForm) => {

useLayoutEffect(() => {
window.$app.Event.on(`${namespace}/submit`, submit)
window.$app.Event.on(`${namespace}/setFieldsValue`, setFieldsValue)

return () => window.$app.Event.off(`${namespace}/submit`, submit)
}, [])
return () => {
window.$app.Event.off(`${namespace}/submit`, submit)
window.$app.Event.off(`${namespace}/setFieldsValue`, setFieldsValue)
}
}, [namespace])

useLayoutEffect(() => {
window.$app.Event.emit('app/getContext', { namespace, primary, data_item: data })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@
.confirm_wrap {
padding-top: 12px;

.text{
width: calc(100% - 60px);
}

.btn_yes {
width: 60px;

&:hover {
text-decoration: underline;
}
Expand Down
76 changes: 58 additions & 18 deletions packages/xgen/layouts/components/Neo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import clsx from 'clsx'
import { AnimatePresence, motion } from 'framer-motion'
import { ChatCircleText, PaperPlaneTilt, X, ArrowsOutSimple, ArrowsInSimple, Stop } from 'phosphor-react'
import { useLayoutEffect, useEffect, useRef, useState, useMemo } from 'react'
import { Else, If, Then } from 'react-if'
import { Else, If, Then, When } from 'react-if'

import { fuzzyQuery } from '@/knife'
import { useLocation, getLocale } from '@umijs/max'
Expand All @@ -15,7 +15,7 @@ import { useEventStream } from './hooks'
import styles from './index.less'

import type { IPropsNeo } from '../../types'
import type { App } from '@/types'
import type { App, Common } from '@/types'

const { TextArea } = Input

Expand All @@ -36,7 +36,8 @@ const Index = (props: IPropsNeo) => {
})
const [field, setField] = useState<App.Field>({
name: '',
bind: ''
bind: '',
config: {} as Common.FieldDetail
})
const ref = useRef<HTMLDivElement>(null)
const [value, { onChange }] = useEventTarget({ initialValue: '' })
Expand Down Expand Up @@ -66,7 +67,7 @@ const Index = (props: IPropsNeo) => {
}, [commands, value, visible_commands])

const getContext = useMemoizedFn((v: App.Context) => setContext(v))
const getField = useMemoizedFn((v: App.Field & { text: string }) => {
const getField = useMemoizedFn((v: App.Field & { text: string; config: Common.FieldDetail }) => {
if (loading) stop()

setField(v)
Expand All @@ -76,11 +77,18 @@ const Index = (props: IPropsNeo) => {
{
is_neo: false,
text: v.text,
context: { stack, pathname, formdata: context.data_item, field: { name: v.name, bind: v.bind } }
context: {
namespace: context.namespace,
stack,
pathname,
formdata: context.data_item,
field: { name: v.name, bind: v.bind },
config: v.config
}
}
])
})
const setNeoVisible = useMemoizedFn(() => setVisible(true))
const setNeoVisible = useMemoizedFn((v) => setVisible(v ?? true))

useLayoutEffect(() => {
window.$app.Event.on('app/getContext', getContext)
Expand Down Expand Up @@ -108,7 +116,18 @@ const Index = (props: IPropsNeo) => {

setMessages([
...messages,
{ is_neo: false, text: value, context: { stack, pathname, formdata: context.data_item, field } }
{
is_neo: false,
text: value,
context: {
namespace: context.namespace,
stack,
pathname,
formdata: context.data_item,
field: { name: field.name, bind: field.bind },
config: field.config
}
}
])

setTimeout(() => {
Expand Down Expand Up @@ -146,6 +165,18 @@ const Index = (props: IPropsNeo) => {
)
}, [commands, max, search_commands])

const exit = useMemoizedFn(() => {
if (cmd?.name) exitCmd()

if (field.name) {
setField({
name: '',
bind: '',
config: {} as Common.FieldDetail
})
}
})

return (
<div className={clsx('fixed flex flex_column align_end', styles._local)}>
<AnimatePresence>
Expand All @@ -163,18 +194,27 @@ const Index = (props: IPropsNeo) => {
>
<div className='chatbox_transition_wrap w_100 h_100 flex flex_column relative'>
<div className='header_wrap w_100 border_box flex justify_between align_center absolute top_0'>
<If condition={cmd?.name}>
<If condition={cmd?.name || field.name}>
<Then>
<div className='title flex flex_column'>
<span className='cmd_title'>
{is_cn ? '命令模式:' : 'Command mode:'}
</span>
<span className='cmd_name'>{cmd?.name}</span>
</div>
<span
className='btn_exit_cmd cursor_point'
onClick={exitCmd}
>
{cmd?.name && (
<div className='title flex flex_column'>
<span className='cmd_title'>
{is_cn ? '命令模式:' : 'Command mode:'}
</span>
<span className='cmd_name'>{cmd?.name}</span>
</div>
)}
{field.name && (
<div className='title flex flex_column'>
<span className='cmd_title'>
{is_cn ? '字段模式:' : 'Field mode:'}
</span>
<span className='cmd_name'>
{field.name}-{field.bind}
</span>
</div>
)}
<span className='btn_exit_cmd cursor_point' onClick={exit}>
{is_cn ? '退出' : 'Exit'}
</span>
</Then>
Expand Down
12 changes: 12 additions & 0 deletions packages/xgen/types/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ export declare namespace Action {
neo?: boolean
}

interface ShowMessage {
type: 'success' | 'warning' | 'error'
content: string
}

type EmitEvent = { key: string; value: any } | EventToggleNeo | EventSetFieldsValue | EventUnLoadingAI
type EventToggleNeo = { key: 'setNeoVisible'; value: boolean }
type EventSetFieldsValue = { key: `$namespace/setFieldsValue`; value: Record<string, any> }
type EventUnLoadingAI = { key: `$namespace/$item.bind/unloading` }

interface ActionMap {
'Common.openModal': OpenModal
'Common.closeModal': {}
Expand All @@ -44,6 +54,8 @@ export declare namespace Action {
'Common.confirm': Confirm
'Common.refetch': {}
'Common.reload': Reload
'Common.showMessage': ShowMessage
'Common.emitEvent': EmitEvent
'Table.search': {}
'Table.save': Global.StringObject
'Table.delete': {}
Expand Down
12 changes: 10 additions & 2 deletions packages/xgen/types/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Action } from '@/types'
import type { Action, Common } from '@/types'

export declare namespace App {
type Theme = 'light' | 'dark'
Expand Down Expand Up @@ -27,7 +27,14 @@ export declare namespace App {
type ChatHuman = {
is_neo: boolean
text: string
context?: { stack: string; pathname: string; formdata: any; field?: Field }
context?: {
namespace: string
stack: string
pathname: string
formdata: any
field?: Omit<Field, 'config'>
config?: Common.FieldDetail
}
}

type ChatInfo = ChatHuman | ChatAI
Expand All @@ -49,6 +56,7 @@ export declare namespace App {
interface Field {
name: string
bind: string
config: Common.FieldDetail
}

type Role = {
Expand Down
1 change: 1 addition & 0 deletions packages/xgen/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export declare namespace Common {
}

interface FieldDetail {
id: string
bind: string
view: {
bind?: string
Expand Down

0 comments on commit 184c442

Please sign in to comment.