From d50ad10ee42275f10ee154d8d16b6390f9bff44b Mon Sep 17 00:00:00 2001 From: Acbox liu <850625057@qq.com> Date: Tue, 13 Aug 2024 04:19:46 +0800 Subject: [PATCH] feat: add function type support for json importing. --- packages/json/src/import-widget.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/json/src/import-widget.ts b/packages/json/src/import-widget.ts index e07c34aa1..4bc91e401 100644 --- a/packages/json/src/import-widget.ts +++ b/packages/json/src/import-widget.ts @@ -4,7 +4,7 @@ import type { WidgetFormat } from './format' import { processAction } from './process-action' import { processResource } from './process-resource' -export function processColor(color: string | Array) { +export function processItem(color: string | Array) { if (Array.isArray(color)) { return Color.rgba(color[0], color[1], color[2], color[3] ?? 1) } @@ -14,6 +14,10 @@ export function processColor(color: string | Array) { else if (isString(color) && /shader\(.+\)/.test(color)) { return Shader.createColorShader(Color.WHITE) } + else if (isString(color) && /fn\(.+\)/.test(color)) { + // eslint-disable-next-line no-new-func + return Function(`return ${color.replace(/fn\(/, '').replace(/\)$/, '')}`)() + } else { return color } @@ -24,7 +28,7 @@ export function processOptions(options: WidgetOptions) { if (typeof (options as Record)[key] === 'object') (options as Record)[key] = processOptions((options as Record)[key]) const result1 = processResource((options as Record)[key]) - const result2 = processColor((options as Record)[key]) + const result2 = processItem((options as Record)[key]) if (isString(result2)) { if (isString(result1)) { (options as Record)[key] = (options as Record)[key] @@ -45,7 +49,7 @@ export function processArguments(args: unknown[]) { for (const arg of args) { if (isString(arg)) { const result1 = processResource(arg as string) - const result2 = processColor(arg as string) + const result2 = processItem(arg as string) if (isString(result1)) { if (isString(result2)) { result.push(arg) @@ -84,7 +88,7 @@ export function importWidget( if (widgetData.animations) { widgetData.animations.forEach((animation) => { widget.animate(anims[animation.type]().withAttr({ - ...animation.parameters, + ...processOptions(animation.parameters), by: easingFunctions[animation.parameters.by as string], })) })