mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-02 20:18:35 +00:00
refactor(core): trim color grading helpers
This commit is contained in:
@@ -234,25 +234,11 @@ function readColorGradingObject(raw: unknown): Record<string, unknown> | null {
|
||||
return isRecord(raw) ? raw : null;
|
||||
}
|
||||
|
||||
function hasOwnKey(value: HfColorGradingVariableMap, key: string): boolean {
|
||||
return Object.prototype.hasOwnProperty.call(value, key);
|
||||
}
|
||||
|
||||
function resolveStringVariableRef(value: string, variables: HfColorGradingVariableMap): unknown {
|
||||
const match = value.trim().match(VARIABLE_REF_RE);
|
||||
if (!match) return value;
|
||||
const key = match[1] ?? match[2] ?? "";
|
||||
return key && hasOwnKey(variables, key) ? variables[key] : value;
|
||||
}
|
||||
|
||||
function parseJsonColorGradingString(value: string): unknown {
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed.startsWith("{")) return value;
|
||||
try {
|
||||
return JSON.parse(trimmed) as unknown;
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
return key && Object.hasOwn(variables, key) ? variables[key] : value;
|
||||
}
|
||||
|
||||
export function resolveHfColorGradingVariables(
|
||||
@@ -262,8 +248,13 @@ export function resolveHfColorGradingVariables(
|
||||
if (typeof raw === "string") {
|
||||
const direct = resolveStringVariableRef(raw, variables);
|
||||
if (direct !== raw) return direct;
|
||||
const parsed = parseJsonColorGradingString(raw);
|
||||
return parsed === raw ? raw : resolveHfColorGradingVariables(parsed, variables);
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed.startsWith("{")) return raw;
|
||||
try {
|
||||
return resolveHfColorGradingVariables(JSON.parse(trimmed) as unknown, variables);
|
||||
} catch {
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
if (Array.isArray(raw)) {
|
||||
return raw.map((item) => resolveHfColorGradingVariables(item, variables));
|
||||
@@ -333,13 +324,8 @@ export function serializeHfColorGrading(
|
||||
): string {
|
||||
const normalized = normalizeHfColorGrading(grading);
|
||||
if (!normalized) return "";
|
||||
return JSON.stringify({
|
||||
preset: normalized.preset,
|
||||
intensity: normalized.intensity,
|
||||
adjust: normalized.adjust,
|
||||
lut: normalized.lut,
|
||||
colorSpace: normalized.colorSpace,
|
||||
});
|
||||
const { enabled: _enabled, ...serializable } = normalized;
|
||||
return JSON.stringify(serializable);
|
||||
}
|
||||
|
||||
export function isHfColorGradingActive(
|
||||
|
||||
@@ -221,10 +221,6 @@ function maybeInlineRelativeAssetUrl(urlValue: string, projectDir: string): stri
|
||||
return appendSuffixToUrl(dataUrl, suffix);
|
||||
}
|
||||
|
||||
function isJsonRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
// fallow-ignore-next-line complexity
|
||||
function rewriteLookLutWithInlinedAssets(value: string, projectDir: string): string {
|
||||
if (!value.trim().startsWith("{")) return value;
|
||||
@@ -234,19 +230,21 @@ function rewriteLookLutWithInlinedAssets(value: string, projectDir: string): str
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
if (!isJsonRecord(parsed)) return value;
|
||||
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return value;
|
||||
|
||||
const lut = parsed.lut;
|
||||
const lut = Reflect.get(parsed, "lut");
|
||||
if (typeof lut === "string") {
|
||||
const inlined = maybeInlineRelativeAssetUrl(lut, projectDir);
|
||||
if (!inlined) return value;
|
||||
parsed.lut = inlined;
|
||||
Reflect.set(parsed, "lut", inlined);
|
||||
return JSON.stringify(parsed);
|
||||
}
|
||||
if (!isJsonRecord(lut) || typeof lut.src !== "string") return value;
|
||||
const inlined = maybeInlineRelativeAssetUrl(lut.src, projectDir);
|
||||
if (typeof lut !== "object" || lut === null || Array.isArray(lut)) return value;
|
||||
const lutSrc = Reflect.get(lut, "src");
|
||||
if (typeof lutSrc !== "string") return value;
|
||||
const inlined = maybeInlineRelativeAssetUrl(lutSrc, projectDir);
|
||||
if (!inlined) return value;
|
||||
lut.src = inlined;
|
||||
Reflect.set(lut, "src", inlined);
|
||||
return JSON.stringify(parsed);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user