chore: debug 功能改成在 env 里初始化,方便针对特定 amis 渲染区域关闭 (#3738)

* chore: debug 功能改成在 env 里初始化,方便针对特定 amis 渲染区域关闭

* 补回 warning
This commit is contained in:
吴多益 2022-03-10 13:59:50 +08:00 committed by GitHub
parent 72d5a8bc26
commit 62611cc6ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 28 deletions

View File

@ -8,10 +8,11 @@ amis 内置了调试工具,可以查看组件内部运行日志,方便分析
## 开启方法
默认不会开启这个功能,可以通过下面种方式开启:
默认不会开启这个功能,可以通过下面种方式开启:
1. render 的 env 里设置 `enableAMISDebug`
1. 配置全局变量 `enableAMISDebug` 的值为 `true`,比如 `window.enableAMISDebug = true`
2. 在页面 URL 参数中加上 `amisDebug=1`,比如 `http://xxx.com/?amisDebug=1`
1. 在页面 URL 参数中加上 `amisDebug=1`,比如 `http://xxx.com/?amisDebug=1`
开启之后,在页面右侧就会显示。

View File

@ -15,7 +15,7 @@ import {asFormItem} from './renderers/Form/Item';
import {renderChild, renderChildren} from './Root';
import {IScopedContext, ScopedContext} from './Scoped';
import {Schema, SchemaNode} from './types';
import {DebugWrapper, enableAMISDebug} from './utils/debug';
import {DebugWrapper} from './utils/debug';
import getExprProperties from './utils/filter-schema';
import {anyChanged, chainEvents, autobind} from './utils/helper';
import {SimpleMap} from './utils/SimpleMap';
@ -417,7 +417,7 @@ export class SchemaRenderer extends React.Component<SchemaRendererProps, any> {
/>
);
return enableAMISDebug ? (
return this.props.env.enableAMISDebug ? (
<DebugWrapper renderer={renderer}>{component}</DebugWrapper>
) : (
component

View File

@ -42,6 +42,7 @@ import {
RendererEvent
} from './utils/renderer-event';
import {runActions} from './actions/Action';
import {enableDebug} from './utils/debug';
export interface TestFunc {
(
@ -152,6 +153,10 @@ export interface RenderOptions {
* html xss
*/
filterHtml?: (input: string) => string;
/**
* amis
*/
enableAMISDebug?: boolean;
[propName: string]: any;
}
@ -271,6 +276,10 @@ const defaultOptions: RenderOptions = {
affixOffsetBottom: 0,
richTextToken: '',
useMobileUI: true, // 是否启用移动端原生 UI
enableAMISDebug:
(window as any).enableAMISDebug ??
location.search.indexOf('amisDebug=1') !== -1 ??
false,
loadRenderer,
rendererEventListeners: [],
fetcher() {
@ -492,6 +501,10 @@ export function render(
translate
} as any;
if (options.enableAMISDebug) {
enableDebug();
}
store = RendererStore.create({}, options);
stores[options.session || 'global'] = store;
}

View File

@ -284,24 +284,6 @@ const AMISDebug = observer(({store}: {store: AMISDebugStore}) => {
);
});
export let enableAMISDebug = false;
// 开启 debug 有两种方法,一个是设置 enableAMISDebug 全局变量,另一个是通过 amisDebug=1 query
if (
(window as any).enableAMISDebug ||
location.search.indexOf('amisDebug=1') !== -1
) {
enableAMISDebug = true;
// 页面只有一个
if (!(window as any).amisDebugElement) {
const amisDebugElement = document.createElement('div');
document.body.appendChild(amisDebugElement);
const element = <AMISDebug store={store} />;
render(element, amisDebugElement);
(window as any).amisDebugElement = true;
}
}
/**
*
*/
@ -365,7 +347,20 @@ autorun(() => {
}
});
if (enableAMISDebug) {
// 页面中只能有一个实例
let isEnabled = false;
export function enableDebug() {
if (isEnabled) {
return;
}
isEnabled = true;
const amisDebugElement = document.createElement('div');
document.body.appendChild(amisDebugElement);
const element = <AMISDebug store={store} />;
render(element, amisDebugElement);
document.body.appendChild(amisHoverBox);
document.body.appendChild(amisActiveBox);
document.addEventListener('mousemove', handleMouseMove);
@ -378,9 +373,6 @@ interface DebugWrapperProps {
export class DebugWrapper extends Component<DebugWrapperProps> {
componentDidMount() {
if (!enableAMISDebug) {
return;
}
const root = findDOMNode(this) as HTMLElement;
if (!root) {
return;
@ -407,7 +399,7 @@ type Category = 'api' | 'event';
* @param ext
*/
export function debug(cat: Category, msg: string, ext?: object) {
if (!enableAMISDebug) {
if (!isEnabled) {
return;
}
const log = {
@ -426,7 +418,7 @@ export function debug(cat: Category, msg: string, ext?: object) {
* @param ext
*/
export function warning(cat: Category, msg: string, ext?: object) {
if (!enableAMISDebug) {
if (!isEnabled) {
return;
}
store.logs.push({