From 9328a366cb1e794833931be5570f4e46b96a8b4b Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Thu, 26 Mar 2026 14:09:11 +0900 Subject: [PATCH] Support typography from lib --- src/utils.ts | 46 ++++++++++------------------------------------ 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 7e4016a..5245783 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,22 +1,6 @@ import { toCamel } from './utils/to-camel' import { toPascal } from './utils/to-pascal' -// Cache for figma.getLocalTextStylesAsync() — called once per codegen run -let localTextStyleIdsCache: Promise> | null = null -let localTextStyleIdsResolved: Set | null = null - -function getLocalTextStyleIds(): Promise> { - if (localTextStyleIdsCache) return localTextStyleIdsCache - localTextStyleIdsCache = Promise.resolve( - figma.getLocalTextStylesAsync(), - ).then((styles) => { - const set = new Set(styles.map((s) => s.id)) - localTextStyleIdsResolved = set - return set - }) - return localTextStyleIdsCache -} - // Cache for figma.getStyleByIdAsync() — keyed by style ID const styleByIdCache = new Map>() const styleByIdResolved = new Map() @@ -35,8 +19,6 @@ function getStyleByIdCached(styleId: string): Promise { } export function resetTextStyleCache(): void { - localTextStyleIdsCache = null - localTextStyleIdsResolved = null styleByIdCache.clear() styleByIdResolved.clear() } @@ -63,26 +45,18 @@ export async function propsToPropsWithTypography( delete ret.w delete ret.h - // Sync fast path: if both caches are resolved, skip await entirely - if (localTextStyleIdsResolved !== null) { - if (textStyleId && localTextStyleIdsResolved.has(textStyleId)) { - const style = styleByIdResolved.get(textStyleId) - if (style !== undefined) { - if (style) applyTypographyStyle(ret, style) - return ret - } - // Style not yet resolved — fall through to async - } else { - return ret - } - } + if (!textStyleId) return ret - // Async fallback (first call or style not yet in resolved cache) - const localStyleIds = await getLocalTextStyleIds() - if (textStyleId && localStyleIds.has(textStyleId)) { - const style = await getStyleByIdCached(textStyleId) - if (style) applyTypographyStyle(ret, style) + // Sync fast path: if style already resolved, skip await entirely + const resolvedStyle = styleByIdResolved.get(textStyleId) + if (resolvedStyle !== undefined) { + if (resolvedStyle) applyTypographyStyle(ret, resolvedStyle) + return ret } + + // Async fallback: resolve style by ID (works for both local and library styles) + const style = await getStyleByIdCached(textStyleId) + if (style) applyTypographyStyle(ret, style) return ret }