From c080f40a983d1ba5050112236804ad4ce4dfec2d Mon Sep 17 00:00:00 2001 From: 2betop <2698393+2betop@users.noreply.github.com> Date: Fri, 29 Nov 2024 11:16:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20debug=20=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=89=93=E5=8D=B0=E4=B8=8D=E5=85=A8=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis-core/src/utils/debug.tsx | 245 ++++++++++-------- .../amis-core/src/utils/renderer-event.ts | 10 + 2 files changed, 148 insertions(+), 107 deletions(-) diff --git a/packages/amis-core/src/utils/debug.tsx b/packages/amis-core/src/utils/debug.tsx index 06de32844ec..c2b3e4f8aba 100644 --- a/packages/amis-core/src/utils/debug.tsx +++ b/packages/amis-core/src/utils/debug.tsx @@ -12,6 +12,7 @@ import {uuidv4, importLazyComponent} from './helper'; import position from './position'; import {resolveVariableAndFilter} from './resolveVariableAndFilter'; import {callStrFunction} from './api'; +import isPlainObject from 'lodash/isPlainObject'; export const JsonView = React.lazy(() => import('react-json-view').then(importLazyComponent) @@ -102,21 +103,25 @@ const LogView = observer(({store}: {store: AMISDebugStore}) => { return ( <> {logs.map((log, index) => { + let ext = + typeof log.ext === 'string' && + (log.ext.startsWith('{') || log.ext.startsWith('[')) + ? parseJson(log.ext) + : typeof log.ext === 'object' + ? normalizeDataForLog(log.ext) + : log.ext; + return (
[{log.cat}] {log.msg}
- {(typeof log.ext === 'string' && - (log.ext.startsWith('{') || log.ext.startsWith('['))) || - typeof log.ext === 'object' ? ( + {typeof ext === 'object' ? ( Loading...
}> { /> ) : ( -
{JSON.stringify(log.ext)}
+
{JSON.stringify(ext)}
)} ); @@ -260,119 +265,121 @@ const AMISDebug = observer(({store}: {store: AMISDebugStore}) => { )} -
-
- -
-
{ - setStartX(event.clientX); - setPanelWidth( - parseInt( - getComputedStyle(panelRef.current!).getPropertyValue('width'), - 10 - ) - ); - setResizing(true); - }} - >
-
- - -
-
- {store.position === 'right' ? ( - { - store.position = 'left'; - }} - /> - ) : ( -
+
+
- {store.tab === 'log' ? ( -
+ > + Log + -
- ) : null} - {store.tab === 'inspect' ? ( -
- - - - {store.inspectMode ? ( - Select an element in the page to inspect it. +
+ {store.position === 'right' ? ( + { + store.position = 'left'; + }} + /> ) : ( - Click to inspect an element. + { + store.position = 'right'; + }} + /> )} - {activeId ? ( - <> -

- Component:{' '} - - {activeComponentInspect?.name} - -

- {stackDataView} - - ) : null}
- ) : null} - {activeId ? ( -
- - + + +
+ ) : null} + {store.tab === 'inspect' ? ( +
+ - - -
- ) : null} -
+ viewBox="0 0 1024 1024" + className={`AMISDebug-inspectIcon ${ + store.inspectMode ? 'is-active' : '' + }`} + onClick={store.toggleInspectMode} + > + + + {store.inspectMode ? ( + Select an element in the page to inspect it. + ) : ( + Click to inspect an element. + )} + {activeId ? ( + <> +

+ Component:{' '} + + {activeComponentInspect?.name} + +

+ {stackDataView} + + ) : null} +
+ ) : null} + {activeId ? ( +
+ + + + +
+ ) : null} + + ) : null} ); }); @@ -557,6 +564,30 @@ function parseJson(str: string) { } } +function normalizeDataForLog(data: any) { + try { + return parseJson(JSON.stringify(data)); + } catch { + let ret: any = {}; + + Object.keys(data).forEach(key => { + const value = data[key]; + + if ( + typeof value === 'object' && + value !== null && + !isPlainObject(value) + ) { + ret[key] = '[complicated data]'; + } else { + ret[key] = value; + } + }); + + return ret; + } +} + /** * 一般调试日志 * @param msg 简单消息 diff --git a/packages/amis-core/src/utils/renderer-event.ts b/packages/amis-core/src/utils/renderer-event.ts index ac30b801f37..62a54efc301 100644 --- a/packages/amis-core/src/utils/renderer-event.ts +++ b/packages/amis-core/src/utils/renderer-event.ts @@ -8,6 +8,7 @@ import debounce from 'lodash/debounce'; import {resolveVariableAndFilterForAsync} from './resolveVariableAndFilterForAsync'; import {evalExpression, evalExpressionWithConditionBuilderAsync} from './tpl'; import type {PlainObject} from '../types'; +import {debug} from './debug'; export interface debounceConfig { maxWait?: number; @@ -273,6 +274,15 @@ export async function dispatchEvent( let unbindEvent: ((eventName?: string) => void) | null | undefined = null; const eventName = typeof e === 'string' ? e : e.type; + const from = renderer?.props.id || renderer?.props.name || ''; + debug( + 'event', + `dispatch \`${eventName}\` from 「${renderer?.props.type || 'unknown'}${ + from ? `#${from}` : '' + }」`, + data + ); + renderer?.props?.env?.beforeDispatchEvent?.( e, renderer,