Skip to content

Commit

Permalink
fix: allow reload state tree & display pageStore in variable tree (#135)
Browse files Browse the repository at this point in the history
* fix: update state tree display

* fix: allow reload state tree

* fix: update action props

* fix: display pageStore in variable tree
  • Loading branch information
wwsun committed Apr 19, 2024
1 parent e39a913 commit 2664613
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
7 changes: 7 additions & 0 deletions packages/context/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ export const useWorkspaceData = () => {
const modelVariables: IVariableTreeNode[] = []; // 绑定变量列表
const storeActionVariables: IVariableTreeNode[] = []; // 模型中的所有 actions
const storeVariables: IVariableTreeNode[] = []; // 模型中的所有变量
const pageStoreVariables: IVariableTreeNode[] = []; // 页面中的组件实例变量
const serviceVariables: IVariableTreeNode[] = []; // 服务中的所有变量

pageStoreVariables.push({
title: 'pageStore',
key: 'pageStore',
});

Object.values(workspace.storeModules).forEach((file) => {
const prefix = `stores.${file.name}`;
const states: IVariableTreeNode[] = file.states.map((item) => ({
Expand Down Expand Up @@ -109,6 +115,7 @@ export const useWorkspaceData = () => {
}

let expressionVariables: IVariableTreeNode[] = [
buildVariableOptions('当前页面组件实例', '$pageStore', pageStoreVariables),
buildVariableOptions('数据模型', '$stores', storeVariables),
buildVariableOptions('服务函数', '$services', serviceVariables),
];
Expand Down
6 changes: 1 addition & 5 deletions packages/designer/src/sidebar/outline-panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ export function OutlinePanel({ showStateView = true, ...treeProps }: OutlineView
>
<ComponentsTree {...treeProps} />
</CollapsePanel>
{showStateView && (
<CollapsePanel title="页面状态" stickyHeader maxHeight="90%" overflowY="auto">
<StateTree />
</CollapsePanel>
)}
{showStateView && <StateTree />}
</Box>
);
}
40 changes: 30 additions & 10 deletions packages/designer/src/sidebar/outline-panel/state-tree.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useEffect, useReducer } from 'react';
import { observer } from '@music163/tango-context';
import { isFunction, isObject, pick } from '@music163/tango-helpers';
import { JsonView } from '@music163/tango-ui';
import { CollapsePanel, IconButton, JsonView } from '@music163/tango-ui';
import { ReloadOutlined } from '@ant-design/icons';
import { useSandboxQuery } from '../../context';

function processTango(obj: any = {}, index = 0) {
Expand All @@ -26,14 +27,15 @@ export const StateTree = observer(() => {
const [, forceUpdate] = useReducer((x) => x + 1, 0);
const tangoContext = pick(sandboxQuery.window['tango'] || {}, [
'stores',
'page',
'pageStore',
'services',
'config',
]);
const callback = () => {
forceUpdate();
};
useEffect(() => {
// FIXME: 优化这段代码,应该监听 tangoContext 的变化,来刷新
const settingForm = document.querySelector('.SettingPanel');
const ob = new MutationObserver(callback);
if (settingForm) {
Expand All @@ -48,13 +50,31 @@ export const StateTree = observer(() => {
};
}, []);
return (
<JsonView
src={processTango(tangoContext)}
collapsed={1}
name="tango"
displayObjectSize={false}
indentWidth={2}
enableCopy
/>
<CollapsePanel
title="页面状态"
stickyHeader
maxHeight="90%"
overflowY="auto"
extra={
<IconButton
tooltip="同步状态"
size="small"
icon={<ReloadOutlined />}
onClick={(e) => {
e.stopPropagation();
forceUpdate();
}}
/>
}
>
<JsonView
src={processTango(tangoContext)}
collapsed={1}
name="tango"
displayObjectSize={false}
indentWidth={2}
enableCopy
/>
</CollapsePanel>
);
});
2 changes: 1 addition & 1 deletion packages/helpers/src/helpers/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function isNil(val: any) {
* @returns
*/
export function isStoreVariablePath(key: string) {
return /^stores\.[a-zA-Z0-9]+\.\w+$/.test(key);
return key === 'pageStore' || /^stores\.[a-zA-Z0-9]+\.\w+$/.test(key);
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export interface ActionProps extends HTMLCoralProps<any> {
*/
className?: string;
children?: React.ReactNode;
onClick?: React.MouseEventHandler;
}

export function Action({
Expand Down

0 comments on commit 2664613

Please sign in to comment.