Skip to content

Commit

Permalink
CaptionsRenderer: Added check to fallback if originX and originY do n…
Browse files Browse the repository at this point in the history
…ot provide a measure unit
  • Loading branch information
alexandercerutti committed Feb 11, 2024
1 parent f6baa87 commit 3808bc0
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/captions-renderer/src/TreeOrchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface OrchestratorSettings {
const LINES_TRANSITION_TIME_MS = 250;
const ROOT_CLASS_NAME = "region";

const UNIT_REGEX = /\d+\.?\d+?[a-zA-Z%]+$/;

export default class TreeOrchestrator {
private static DEFAULT_SETTINGS: OrchestratorSettings = {
lines: 2,
Expand Down Expand Up @@ -63,16 +65,24 @@ export default class TreeOrchestrator {
trackRegionSettings?.lines || settings?.lines || TreeOrchestrator.DEFAULT_SETTINGS.lines,
};

const [originX, originY] = trackRegionSettings?.getOrigin(
let [originX, originY] = trackRegionSettings?.getOrigin(
parent.offsetWidth,
parent.offsetHeight,
) ?? ["0%", "70%"];

if (typeof originX === "number" || !UNIT_REGEX.test(originX)) {
originX = `${originX}%`;
}

if (typeof originY === "number" || !UNIT_REGEX.test(originY)) {
originY = `${originY}%`;
}

const rootStyles: Partial<CSSStyleDeclaration> = {
width: `${trackRegionSettings?.width ?? 100}%`,
height: `${this.settings.lines * 1.5}em`,
left: `${originX}%`,
top: `${originY}%`,
left: originX,
top: originY,
};

Object.assign(root.style, rootStyles);
Expand Down

0 comments on commit 3808bc0

Please sign in to comment.