From daa880fc94b723f694d83e84beddaf214d6c3608 Mon Sep 17 00:00:00 2001 From: zestack Date: Wed, 29 Nov 2023 13:01:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E6=B8=B2?= =?UTF-8?q?=E6=9F=93=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 widget 和 view 分散到不同的函数里面 --- src/engineer/render.ts | 84 ++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/src/engineer/render.ts b/src/engineer/render.ts index 5c15d00..31ea3f5 100644 --- a/src/engineer/render.ts +++ b/src/engineer/render.ts @@ -18,7 +18,8 @@ import { provideBlockId } from './context' import { align, background, - bordering, clip, + bordering, + clip, gap, insets, radii, @@ -41,11 +42,26 @@ export type WidgetRenderFunction = (widget: Widget, view: View, axis?: Axis) => */ const widgets: Record = {} +const views: Record = { + text: DddTextView, + image: DddImageView, + audio: DddAudioView, + video: DddAudioView, +} + export function registerWidget(some: Record): void { for (const [key, func] of Object.entries(some)) widgets[key] = func } +function renderWidget(widget: Widget, view: View, axis?: Axis): VNodeChild { + const func = widgets[widget.name] + if (typeof func !== 'function') + throw new Error(`unknown widget: ${widget.name}`) + + return func(widget, view, axis) +} + const isWidget = (s: any): s is Widget => s != null && 'name' in s function isRefer(s: any): s is DataRefer { @@ -56,6 +72,24 @@ function isRefer(s: any): s is DataRefer { && typeof s.key === 'string' } +function renderRefer(refer: DataRefer, axis?: Axis): VNodeChild { + const { key, type, ...attrs } = refer + const component = views[type] + if (component) { + return h(component, { + source: key, + axis, + ...attrs, + }) + } + return h('b', { + style: { + color: 'red', + fontSize: '36px', + }, + }, 'unimplemented') +} + export function isFlexible(theme: Theme) { return theme.axis != null || theme.mainAlign != null @@ -74,51 +108,11 @@ export function render(view?: ViewChildren, axis?: Axis): VNodeChild { if (Array.isArray(view)) return view.map(v => render(v, axis)) - if (isRefer(view)) { - const { key, ...attrs } = view - let component: Component | undefined - switch (view.type) { - case 'text': - component = DddTextView - return h(DddTextView, { - source: view.key, - }) - case 'image': - component = DddImageView - return h(DddImageView, { - source: key, - ...attrs, - }) - case 'audio': - component = DddAudioView - return h(DddAudioView, { - source: view.key, - title: view.title, - }) - case 'video': - // todo ... - } - if (component) { - return h(component, { - source: key, - ...attrs, - }) - } - return h('b', { - style: { - color: 'red', - fontSize: '36px', - }, - }, 'unimplemented') - } - if (isWidget(view.theme)) { - const func = widgets[view.theme.name] - if (typeof func !== 'function') - throw new Error(`unknown widget: ${view.theme.name}`) + if (isRefer(view)) + return renderRefer(view) - const widget = view.theme - return func(widget, view, axis) - } + if (isWidget(view.theme)) + return renderWidget(view.theme, view, axis) const theme = view.theme const css: CSSProperties = {