Skip to content

Commit

Permalink
Refactor template value handling, improve action filtering, and enhan…
Browse files Browse the repository at this point in the history
…ce layout responsiveness
  • Loading branch information
trheyi committed Nov 7, 2024
1 parent f1daf9e commit b6f956c
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 17 deletions.
11 changes: 10 additions & 1 deletion packages/actionflow/src/utils/getTemplateValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ type FunctionType = <T>(template: Template & T, data: Record<string, any>) => T

const Index: FunctionType = (template, data) => {
if (typeof template === 'string') {
return decodeURIComponent(Mustache.render(template.replaceAll('[[', '{{').replaceAll(']]', '}}'), data))
return decodeURIComponent(
Mustache.render(
template
.replaceAll('[[[', '{{{')
.replaceAll(']]]', '}}}')
.replaceAll('[[', '{{')
.replaceAll(']]', '}}'),
data
)
)
}

if (typeof template === 'boolean' || typeof template === 'number') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const Index = (props: IPropsActions) => {
const when_add = id === 0
const when_view = type === 'view'
const handle_actions = getTemplateValue(actions!, data || {})
if (when_add) return handle_actions.filter((item) => item.showWhenAdd)
if (when_view) return handle_actions.filter((item) => item.showWhenView)
if (when_add) return handle_actions?.filter((item) => item.showWhenAdd)
if (when_view) return handle_actions?.filter((item) => item.showWhenView)

return handle_actions?.filter((item) => !item.hideWhenEdit)
}, [actions, data, id, type])
Expand Down
11 changes: 9 additions & 2 deletions packages/xgen/components/base/PureForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,20 @@ const Index = (props: IPropsPureForm) => {
Object.keys(data).map((key) => onValuesChange({ [key]: data[key] }), true)
}, [data, onLoadSync])

// Set fields value and trigger ${namespace}/setFieldsValue event
const onSetFieldsValue = useMemoizedFn((values: Record<string, any>) => {
setFieldsValue(values)
if (!Object.keys(values).length) return
Object.keys(values).map((key) => onValuesChange({ [key]: values[key] }), true)
})

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

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

Expand Down
1 change: 1 addition & 0 deletions packages/xgen/context/app/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default class GlobalModel {
menu_selected_keys: Array<string> = (local.menu_key_path || []) as Array<string>
loading: boolean = false
visible_menu: boolean = true
hide_nav: boolean = false

dataCache: Record<string, any> = {}

Expand Down
10 changes: 10 additions & 0 deletions packages/xgen/layouts/components/Container/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
width: calc(100vw - 80px);
}

&.no_nav {
margin-left: 0;
width: 100vw;
}

&.iframe {
padding-bottom: 0;
}
Expand All @@ -21,6 +26,11 @@
width: calc(100vw - 92px);
}

&.no_nav {
margin-left: 0;
width: 100vw;
}

&.iframe {
padding-bottom: 0;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/xgen/layouts/components/Container/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import styles from './index.less'
import type { IPropsContainer } from '../../types'

const Index = (props: IPropsContainer & { children: React.ReactNode }) => {
const { children, menu, visible_menu, show_name } = props
const { children, menu, visible_menu, show_name, hide_nav } = props
return (
<div
id='container'
className={clsx([
show_name ? styles._local_showname : styles._local,
(!menu?.length || !visible_menu) && styles.no_menu,
!hide_nav && (!menu?.length || !visible_menu) && styles.no_menu,
hide_nav && styles.no_nav,
history.location.pathname.indexOf('/iframe') !== -1 ? styles.iframe : ''
])}
>
Expand Down
6 changes: 3 additions & 3 deletions packages/xgen/layouts/components/Neo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const Index = (props: IPropsNeo) => {
const { messages, cmd, commands, loading, setMessages, stop, exitCmd } = useEventStream({ api, studio })
const is_cn = locale === 'zh-CN'

useKeyPress('enter', () => submit())
useKeyPress('enter', (event) => !event.shiftKey && submit())

useEffect(() => {
local.neo_max = max
Expand Down Expand Up @@ -156,7 +156,7 @@ const Index = (props: IPropsNeo) => {
return (
<div
className={clsx('border_box', styles.commands)}
style={{ width: `calc(${max ? 900 : 360}px - 9px * 2)`, maxHeight: max ? 720 : 360 }}
style={{ width: `calc(${max ? '80vw' : '360px'} - 9px * 2)`, maxHeight: max ? 720 : 360 }}
>
{(search_commands.length ? search_commands : commands).map((item) => (
<div
Expand Down Expand Up @@ -201,7 +201,7 @@ const Index = (props: IPropsNeo) => {
visible
? {
opacity: 1,
width: max ? 900 : 360,
width: max ? '80vw' : '360px',
height: max
? 'min(1200px,calc(100vh - 30px - 48px - 18px - 60px))'
: 480
Expand Down
21 changes: 16 additions & 5 deletions packages/xgen/layouts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import type {
const Index = () => {
const messages = useIntl()
const [global] = useState(() => container.resolve(GlobalModel))
const { pathname } = useLocation()
const { pathname, search } = useLocation()
const [hide_nav, setHideNav] = useState(false)
const menu = toJS(global.menu)
const is_login = pathname.indexOf('/login/') !== -1 || pathname === '/'
const is_auth = pathname === '/auth'
Expand All @@ -58,7 +59,8 @@ const Index = () => {

useLayoutEffect(() => {
global.visible_menu = true

global.hide_nav = search.indexOf('__hidemenu=1') !== -1
setHideNav(global.hide_nav)
global.stack.reset()
}, [pathname])

Expand Down Expand Up @@ -114,7 +116,8 @@ const Index = () => {
menu: menu_items,
visible_menu: global.visible_menu,
menu_layout: layout,
show_name: show_name
show_name: show_name,
hide_nav: hide_nav
}

return (
Expand All @@ -141,7 +144,15 @@ const Index = () => {
<If condition={!is_auth && !is_login}>
<Then>
<Fragment>
<If condition={layout === '2-columns'}>
<If condition={hide_nav}>
<Then>
<Loading {...props_loading}></Loading>
<Container {...props_container}>
<Outlet />
</Container>
</Then>
</If>
<If condition={!hide_nav && layout === '2-columns'}>
<Then>
<Loading {...props_loading}></Loading>
<Nav {...props_nav}></Nav>
Expand All @@ -152,7 +163,7 @@ const Index = () => {
</Then>
</If>

<If condition={layout === '1-column'}>
<If condition={!hide_nav && layout === '1-column'}>
<Then>
<Loading {...props_loading}></Loading>
<MenuColumnOne
Expand Down
1 change: 1 addition & 0 deletions packages/xgen/layouts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface IPropsContainer {
visible_menu: GlobalModel['visible_menu']
menu_layout: '1-column' | '2-columns'
show_name?: boolean
hide_nav?: boolean
}

export interface IPropsSettingModalContent {
Expand Down
4 changes: 2 additions & 2 deletions packages/xgen/pages/auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Index = () => {
await window.$app.Event.emit('app/getAppInfo')
// Parse data
try {
const text = decodeURIComponent(atob(base64 || ''))
const text = decodeURIComponent(Buffer.from(base64 || '', 'base64').toString())
const data = JSON.parse(text) as ResLogin
auth.afterLogin(data, undefined)
} catch (err: any) {
Expand All @@ -37,7 +37,7 @@ const Index = () => {
}
// Redirect to the custom logout page
if (local.logout_redirect) {
window.location = local.logout_redirect + `?error=${errStr}`
window.location.href = local.logout_redirect + `?error=${errStr}`
return
}
}
Expand Down

0 comments on commit b6f956c

Please sign in to comment.