Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

优化代码编辑体验,仅在切换模式时同步文件 AST 信息 #188

Closed
wants to merge 11 commits into from
4 changes: 2 additions & 2 deletions apps/playground/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Toolbar,
WorkspacePanel,
WorkspaceView,
CodeEditor,
LiveCodeEditor,
Sandbox,
DndQuery,
themeLight,
Expand Down Expand Up @@ -188,7 +188,7 @@ export default function App() {
</Sidebar>
<WorkspacePanel>
<WorkspaceView mode="code">
<CodeEditor />
<LiveCodeEditor />
</WorkspaceView>
<WorkspaceView mode="design">
<Sandbox
Expand Down
4 changes: 4 additions & 0 deletions packages/context/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const useDesigner = () => {
return useTangoEngine()?.engine.designer;
};

export const useEditorState = () => {
return useTangoEngine()?.engine.workspace.editorState;
};

export const useWorkspaceData = () => {
const ctx = useTangoEngine();
const workspace = useWorkspace();
Expand Down
15 changes: 4 additions & 11 deletions packages/core/src/helpers/ast/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import generator, { GeneratorOptions } from '@babel/generator';
import * as t from '@babel/types';
import { logger, wrapCode } from '@music163/tango-helpers';
import { Dict, logger, wrapCode } from '@music163/tango-helpers';
import { formatCode } from '../string';

const defaultGeneratorOptions: GeneratorOptions = {
Expand All @@ -25,19 +25,12 @@ export function ast2code(ast: t.Node, options: GeneratorOptions = defaultGenerat
return code;
}

const bracketPattern = /^\(.+\)$/s;

/**
* 是否被 () 包裹
*
* @example ({ foo: 'foo' }) -> true
* @example { foo: 'foo' } -> false
*
* @param str 目标字符串
*/
function isWrappingWithBrackets(str: string) {
return bracketPattern.test(str);
}
const bracketPattern = /^\(.+\)$/;

/**
* 将表达式生成为块级代码
Expand All @@ -54,7 +47,7 @@ export function expression2code(node: t.Expression) {

const isWrappingExpression = t.isObjectExpression(node) || t.isFunctionExpression(node);

if (isWrappingExpression && isWrappingWithBrackets(ret)) {
if (isWrappingExpression && bracketPattern.test(ret)) {
// 如果是对象,输出包含 ({}),则去掉首尾的括号
ret = ret.slice(1, -1);
}
Expand Down Expand Up @@ -195,7 +188,7 @@ export function node2value(node: t.Node, isWrapCode = true): any {
);
if (isSimpleObject) {
// simple object: { key1, key2, key3 }
ret = node.properties.reduce((prev, propertyNode) => {
ret = node.properties.reduce<Dict>((prev, propertyNode) => {
if (propertyNode.type === 'ObjectProperty') {
const key = keyNode2value(propertyNode.key);
const value = node2value(propertyNode.value, isWrapCode);
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/helpers/ast/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ export function isValidExpressionCode(code: string) {
* @returns
*/
export function code2ast(code: string): t.File {
try {
return parse(code, babelParserConfig);
} catch (err) {
logger.error('[code2ast failed!]', err);
}
return parse(code, babelParserConfig);
}

/**
Expand Down Expand Up @@ -175,7 +171,7 @@ export function value2node(
* 将 js 普通对象解析为 t.Node
*/
export function object2node(
obj: object,
obj: Dict<any>,
getValueNode: (value: any, key?: string) => t.Expression = value2node,
) {
if (!isPlainObject(obj)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/helpers/ast/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,15 +1009,15 @@ export function serviceConfig2Node(payload: object) {
});
}

export function updateServiceConfigToServiceFile(ast: t.File, config: Dict<object>) {
export function updateServiceConfigToServiceFile(ast: t.File, config: Dict<Dict>) {
traverse(ast, {
CallExpression(path) {
const calleeName = keyNode2value(path.node.callee) as string;
if (isDefineService(calleeName) && path.node.arguments.length) {
const configNode = path.node.arguments[0];

if (t.isObjectExpression(configNode)) {
const newPropertiesNodeMap = Object.keys(config).reduce((properties, key) => {
const newPropertiesNodeMap: Dict = Object.keys(config).reduce<Dict>((properties, key) => {
const serviceConfig = config[key];
const property = t.objectProperty(t.identifier(key), serviceConfig2Node(serviceConfig));
properties[key] = property;
Expand Down
15 changes: 13 additions & 2 deletions packages/core/src/models/designer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { action, computed, makeObservable, observable, toJS } from 'mobx';
import { IWorkspace } from './interfaces';
import { MenuDataType } from '@music163/tango-helpers';
import { IWorkspace } from './interfaces';

export type SimulatorNameType = 'desktop' | 'phone';

Expand Down Expand Up @@ -185,6 +185,7 @@ export class Designer {
// 默认激活的视图
if (defaultActiveView) {
this.setActiveView(defaultActiveView);
this.workspace.setMode(defaultActiveView === 'design' ? 'design' : 'code');
}

makeObservable(this, {
Expand Down Expand Up @@ -238,14 +239,24 @@ export class Designer {
}

setActiveView(view: DesignerViewType) {
this._activeView = view;
const prevView = this._activeView;
if (view === prevView) {
return;
}

if (view === 'dual') {
this.setActiveSidebarPanel('');
this.toggleRightPanel(false);
this.toggleIsPreview(true);
this.workspace.setMode('code');
} else if (view === 'design') {
this.toggleIsPreview(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

切回设计视图的时候,默认不激活 RightPanel,在用户点击画布节点的时候(use-dnd onClick)中可以自动激活下 RightPanel ?

this.workspace.setMode('design');
} else if (view === 'code') {
this.workspace.setMode('code');
}

this._activeView = view;
}

setActiveSidebarPanel(panel: string) {
Expand Down
Loading
Loading