Skip to content

Commit

Permalink
chore: 优化弹窗参数映射逻辑 (baidu#9730)
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop committed Mar 5, 2024
1 parent 1847bce commit b8e486f
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 184 deletions.
16 changes: 12 additions & 4 deletions packages/amis-core/src/renderers/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -763,17 +763,25 @@ export default class Form extends React.Component<FormProps, object> {
const initedAt = store.initedAt;

store.setInited(true);
const hooks = groupBy(this.hooks['init'] || [], item =>
const hooks = this.hooks['init'] || [];
const groupedHooks = groupBy(hooks, item =>
(item as any).__enforce === 'prev'
? 'prev'
: (item as any).__enforce === 'post'
? 'post'
: 'normal'
);

await Promise.all((hooks.prev || []).map(hook => hook(data)));
await Promise.all((hooks.normal || []).map(hook => hook(data)));
await Promise.all((hooks.post || []).map(hook => hook(data)));
await Promise.all((groupedHooks.prev || []).map(hook => hook(data)));
// 有可能在前面的步骤中删除了钩子,所以需要重新验证一下
await Promise.all(
(groupedHooks.normal || []).map(
hook => hooks.includes(hook) && hook(data)
)
);
await Promise.all(
(groupedHooks.post || []).map(hook => hooks.includes(hook) && hook(data))
);

if (!isAlive(store)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/amis-core/src/renderers/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ export class FormItemWrap extends React.Component<FormItemProps> {
onInit();
} else if (addHook) {
// 放在初始化的最后面
addHook(onInit, 'init', 'post');
this.toDispose.push(addHook(onInit, 'init', 'post'));
}
}

Expand Down
17 changes: 8 additions & 9 deletions packages/amis-editor-core/src/component/Panel/DialogList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export default observer(function DialogList({
const dialog = store.modals[index];
store.openSubEditor({
title: '编辑弹窗',
value: dialog,
value: {
type: 'dialog',
...(dialog as any)
},
onChange: (value: any, diff: any) => {
store.updateModal(dialog.$$id!, value);
}
Expand All @@ -62,14 +65,10 @@ export default observer(function DialogList({
const refsCount = store.countModalActionRefs(dialog.$$id!);

const confirmed = await confirm(
`确认删除弹窗「${
dialog.editorSetting?.displayName || dialog.title
}」?${
refsCount
? `<br/>当前弹窗已关联${refsCount} 个事件,删除后,所配置的事件动作将一起被删除。`
: ''
}`,
''
refsCount
? `当前弹窗已关联 ${refsCount} 个事件,删除后,所配置的事件动作将一起被删除。`
: '',
`确认删除弹窗「${dialog.editorSetting?.displayName || dialog.title}」?`
);

if (confirmed) {
Expand Down
22 changes: 18 additions & 4 deletions packages/amis-editor-core/src/component/base/SchemaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,27 @@ export function SchemaFrom({
const themeConfig = React.useMemo(() => getThemeConfig(), []);
const submitSubscribers = React.useRef<Array<Function>>([]);
const subscribeSubmit = React.useCallback(
(fn: (schema: any, value: any, id: string, diff?: any) => any) => {
submitSubscribers.current.push(fn);
return () => {
(
fn: (schema: any, value: any, id: string, diff?: any) => any,
once = false
) => {
let raw = fn;
const unsubscribe = () => {
submitSubscribers.current = submitSubscribers.current.filter(
item => item !== fn
item => ((item as any).__raw ?? item) !== raw
);
};

if (once) {
fn = (schema: any, value: any, id: string, diff?: any) => {
const ret = raw(schema, value, id, diff);
unsubscribe();
return ret;
};
(fn as any).__raw = raw;
}
submitSubscribers.current.push(fn);
return unsubscribe;
},
[]
);
Expand Down
2 changes: 1 addition & 1 deletion packages/amis-editor-core/src/store/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,7 @@ export const MainStore = types
args: undefined,
dialog: undefined,
drawer: undefined,
[newHostKey]: modal
[newHostKey]: JSONPipeIn(modal)
});
}

Expand Down
9 changes: 7 additions & 2 deletions packages/amis-editor/src/plugin/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
BuildPanelEventContext,
BasicPanelItem,
PluginEvent,
ChangeEventContext
ChangeEventContext,
JSONPipeOut
} from 'amis-editor-core';
import {getEventControlConfig} from '../renderer/event-control/helper';
import omit from 'lodash/omit';
Expand Down Expand Up @@ -577,7 +578,10 @@ export class DialogPlugin extends BasePlugin {
) {
const renderer = this.manager.store.getNodeById(node.id)?.getComponent();
const data = omit(renderer.props.$schema.data, '$$id');
let dataSchema: any = {};
const inputParams = JSONPipeOut(renderer.props.$schema.inputParams);
let dataSchema: any = {
...inputParams?.properties
};

if (renderer.props.$schema.data === undefined || !isEmpty(data)) {
// 静态数据
Expand Down Expand Up @@ -609,6 +613,7 @@ export class DialogPlugin extends BasePlugin {
return {
$id: 'dialog',
type: 'object',
...inputParams,
title: node.schema?.label || node.schema?.name,
properties: dataSchema
};
Expand Down
9 changes: 7 additions & 2 deletions packages/amis-editor/src/plugin/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
noop,
EditorNodeType,
isEmpty,
getI18nEnabled
getI18nEnabled,
JSONPipeOut
} from 'amis-editor-core';
import {getEventControlConfig} from '../renderer/event-control/helper';
import {tipedLabel} from 'amis-editor-core';
Expand Down Expand Up @@ -384,7 +385,10 @@ export class DrawerPlugin extends BasePlugin {
) {
const renderer = this.manager.store.getNodeById(node.id)?.getComponent();
const data = omit(renderer.props.$schema.data, '$$id');
let dataSchema: any = {};
const inputParams = JSONPipeOut(renderer.props.$schema.inputParams);
let dataSchema: any = {
...inputParams?.properties
};

if (renderer.props.$schema.data === undefined || !isEmpty(data)) {
// 静态数据
Expand Down Expand Up @@ -416,6 +420,7 @@ export class DrawerPlugin extends BasePlugin {
return {
$id: 'drawer',
type: 'object',
...inputParams,
title: node.schema?.label || node.schema?.name,
properties: dataSchema
};
Expand Down

0 comments on commit b8e486f

Please sign in to comment.