feat(sdk): improve goView storage conversion

This commit is contained in:
ErSan 2026-01-27 19:01:45 +08:00
parent d42b210608
commit 8ba32270c7

View File

@ -3,37 +3,77 @@ import { convertGoViewTextOptionToNodeProps, type GoViewTextOption } from '../wi
export interface GoViewComponentLike { export interface GoViewComponentLike {
id?: string; id?: string;
key?: string;
// goView component identity
key?: string; // e.g. "TextCommon" (sometimes)
chartConfig?: { key?: string }; // goView standard location
// geometry
attr?: { x: number; y: number; w: number; h: number; zIndex?: number }; attr?: { x: number; y: number; w: number; h: number; zIndex?: number };
// state
status?: { lock?: boolean; hide?: boolean }; status?: { lock?: boolean; hide?: boolean };
// goView uses "option" for widget-specific config
option?: unknown; option?: unknown;
} }
export interface GoViewEditCanvasConfigLike {
projectName?: string;
width?: number;
height?: number;
background?: string;
}
export interface GoViewStorageLike {
editCanvasConfig?: GoViewEditCanvasConfigLike;
componentList?: GoViewComponentLike[];
}
export interface GoViewProjectLike { export interface GoViewProjectLike {
// very loose input shape; goView has different versions/branches. // very loose input shape; goView has different versions/branches.
width?: number; width?: number;
height?: number; height?: number;
canvas?: { width?: number; height?: number }; canvas?: { width?: number; height?: number };
componentList?: GoViewComponentLike[]; componentList?: GoViewComponentLike[];
// goView persisted store shape
editCanvasConfig?: GoViewEditCanvasConfigLike;
} }
export function convertGoViewProjectToScreen(input: GoViewProjectLike): Screen { function isTextCommon(c: GoViewComponentLike): boolean {
const width = input.canvas?.width ?? input.width ?? 1920; const k = (c.chartConfig?.key ?? c.key ?? '').toLowerCase();
const height = input.canvas?.height ?? input.height ?? 1080; if (k === 'textcommon') return true;
// fallback heuristic
return k.includes('text');
}
export function convertGoViewProjectToScreen(input: GoViewProjectLike | GoViewStorageLike): Screen {
const editCanvasConfig = (input as GoViewStorageLike).editCanvasConfig;
const width =
editCanvasConfig?.width ?? (input as GoViewProjectLike).canvas?.width ?? (input as GoViewProjectLike).width ?? 1920;
const height =
editCanvasConfig?.height ?? (input as GoViewProjectLike).canvas?.height ?? (input as GoViewProjectLike).height ?? 1080;
const name = editCanvasConfig?.projectName ?? 'Imported from goView';
const background = editCanvasConfig?.background;
const screen = createEmptyScreen({ const screen = createEmptyScreen({
version: ASTRALVIEW_SCHEMA_VERSION, version: ASTRALVIEW_SCHEMA_VERSION,
width, width,
height, height,
name: 'Imported from goView', name,
background: background ? { color: background } : undefined,
nodes: [], nodes: [],
}); });
const componentList = (input as GoViewStorageLike).componentList ?? (input as GoViewProjectLike).componentList ?? [];
const nodes: TextWidgetNode[] = []; const nodes: TextWidgetNode[] = [];
for (const c of input.componentList ?? []) { for (const c of componentList) {
// Only first: TextCommon-like // Only first: TextCommon-like
const key = c.key ?? ''; if (!isTextCommon(c)) continue;
if (!/text/i.test(key)) continue;
const rect = c.attr ? { x: c.attr.x, y: c.attr.y, w: c.attr.w, h: c.attr.h } : { x: 0, y: 0, w: 320, h: 60 }; const rect = c.attr ? { x: c.attr.x, y: c.attr.y, w: c.attr.w, h: c.attr.h } : { x: 0, y: 0, w: 320, h: 60 };
const props = convertGoViewTextOptionToNodeProps((c.option ?? {}) as GoViewTextOption); const props = convertGoViewTextOptionToNodeProps((c.option ?? {}) as GoViewTextOption);