feat(sdk): improve goView iframe/video import detection

This commit is contained in:
ErSan 2026-01-27 21:20:06 +08:00
parent 900a8cc8e9
commit 5f093ed157
2 changed files with 46 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import { convertGoViewImageOptionToNodeProps, type GoViewImageOption } from '../
import { convertGoViewIframeOptionToNodeProps, type GoViewIframeOption } from '../widgets/iframe'; import { convertGoViewIframeOptionToNodeProps, type GoViewIframeOption } from '../widgets/iframe';
import { convertGoViewVideoOptionToNodeProps, type GoViewVideoOption } from '../widgets/video'; import { convertGoViewVideoOptionToNodeProps, type GoViewVideoOption } from '../widgets/video';
import { convertGoViewTextOptionToNodeProps, type GoViewTextOption } from '../widgets/text'; import { convertGoViewTextOptionToNodeProps, type GoViewTextOption } from '../widgets/text';
import { pickUrlLike } from '../widgets/urlLike';
export interface GoViewComponentLike { export interface GoViewComponentLike {
id?: string; id?: string;
@ -117,6 +118,38 @@ function isVideo(c: GoViewComponentLike): boolean {
return k.includes('mp4') || k.includes('media') || k.includes('player') || k.includes('stream'); return k.includes('mp4') || k.includes('media') || k.includes('player') || k.includes('stream');
} }
function looksLikeIframeOption(option: unknown): boolean {
if (!option || typeof option !== 'object') return false;
const o = option as Record<string, unknown>;
// Prefer explicit iframe-ish keys.
if ('iframeUrl' in o || 'iframeSrc' in o || 'embedUrl' in o) return true;
const url = pickUrlLike(option);
if (!url) return false;
// If it isn't an obvious media URL, it's often an iframe/embed.
// (We deliberately keep this conservative; image/video are handled earlier.)
return /^https?:\/\//i.test(url) && !/\.(mp4|m3u8|flv)(\?|#|$)/i.test(url);
}
function looksLikeVideoOption(option: unknown): boolean {
if (!option || typeof option !== 'object') return false;
const o = option as Record<string, unknown>;
// Prefer explicit video-ish keys.
if ('videoUrl' in o || 'videoSrc' in o || 'mp4' in o || 'm3u8' in o || 'flv' in o || 'hls' in o || 'rtsp' in o) return true;
const url = pickUrlLike(option);
if (!url) return false;
// Common direct URL patterns.
if (/\.(mp4|m3u8|flv)(\?|#|$)/i.test(url)) return true;
if (/\bm3u8\b/i.test(url)) return true;
return false;
}
function pick<T>(...values: Array<T | undefined | null>): T | undefined { function pick<T>(...values: Array<T | undefined | null>): T | undefined {
for (const v of values) { for (const v of values) {
if (v !== undefined && v !== null) return v as T; if (v !== undefined && v !== null) return v as T;
@ -237,8 +270,10 @@ export function convertGoViewProjectToScreen(input: GoViewProjectLike | GoViewSt
continue; continue;
} }
if (isIframe(c)) { const option = optionOf(c);
const props = convertGoViewIframeOptionToNodeProps(optionOf(c) as GoViewIframeOption);
if (isIframe(c) || looksLikeIframeOption(option)) {
const props = convertGoViewIframeOptionToNodeProps(option as GoViewIframeOption);
nodes.push({ nodes.push({
id: c.id ?? `import_iframe_${Math.random().toString(16).slice(2)}`, id: c.id ?? `import_iframe_${Math.random().toString(16).slice(2)}`,
type: 'iframe', type: 'iframe',
@ -251,8 +286,8 @@ export function convertGoViewProjectToScreen(input: GoViewProjectLike | GoViewSt
continue; continue;
} }
if (isVideo(c)) { if (isVideo(c) || looksLikeVideoOption(option)) {
const props = convertGoViewVideoOptionToNodeProps(optionOf(c) as GoViewVideoOption); const props = convertGoViewVideoOptionToNodeProps(option as GoViewVideoOption);
nodes.push({ nodes.push({
id: c.id ?? `import_video_${Math.random().toString(16).slice(2)}`, id: c.id ?? `import_video_${Math.random().toString(16).slice(2)}`,
type: 'video', type: 'video',

View File

@ -50,6 +50,13 @@ function pickUrlLikeInner(input: unknown, depth: number): string {
'mp4', 'mp4',
'm3u8', 'm3u8',
'flv', 'flv',
// other streaming-ish keys
'hls',
'hlsUrl',
'stream',
'streamUrl',
'rtsp',
'rtspUrl',
]) { ]) {
const v = obj[key]; const v = obj[key];
if (typeof v === 'string' && v) return v; if (typeof v === 'string' && v) return v;