-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support React exercise picker (#3)
* refactor: separate exercise selector responsibilities to delegates * fix: support new React exercise picker Fixes #2
- Loading branch information
Showing
13 changed files
with
443 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { OnObserverDestroyFunct } from "../models/OnObserverDestroyFunct"; | ||
import ReactHelper from "../helpers/ReactHelper"; | ||
import ExerciseSelectorReactDelegate from "../models/ExerciseSelectorReactDelegate"; | ||
|
||
export function takeoverWorkoutExerciseEditor(container: HTMLElement): OnObserverDestroyFunct { | ||
try { | ||
const props = ReactHelper.closestProps(container, ["flattenedExerciseTypes", "onChange"], 5); | ||
|
||
if (props) { | ||
const reactExerciseSelector = new ExerciseSelectorReactDelegate(props, container); | ||
container.parentElement!.append(reactExerciseSelector.exerciseSelector); | ||
} | ||
} catch (e) { | ||
// do nothing, invalid element | ||
return false; | ||
} | ||
|
||
return () => {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
export default class ReactHelper { | ||
static closestProps( | ||
elem: HTMLElement, | ||
propKeys: string[], | ||
maxDepth: number | ||
): Record<string, unknown> | null { | ||
let currentParent: HTMLElement | null = elem; | ||
do { | ||
const reactFiberKey = this.getReactFiberKey(currentParent); | ||
|
||
if (reactFiberKey && this.isReactFiber(currentParent[reactFiberKey])) { | ||
const memoizedProps = (currentParent[reactFiberKey] as unknown as ReactFiber).memoizedProps, | ||
matchingProps = this.closestPropsRecursive(memoizedProps, propKeys); | ||
|
||
if (matchingProps) { | ||
return matchingProps; | ||
} | ||
} | ||
|
||
currentParent = currentParent.parentElement; | ||
maxDepth--; | ||
} while (currentParent && maxDepth > 0); | ||
|
||
return null; | ||
} | ||
|
||
private static closestPropsRecursive(props: ReactProps, propKeys: string[]): Record<string, unknown> | null { | ||
if ( | ||
"props" in props && | ||
propKeys.filter((e) => e in props.props).length === propKeys.length | ||
) { | ||
return props.props; | ||
} | ||
|
||
if ("children" in props && props.children) { | ||
const childProps = Array.isArray(props.children) ? props.children : [props.children]; | ||
|
||
for (const props of childProps) { | ||
if (typeof props === "object" && props && !Array.isArray(props)) { | ||
const value = this.closestPropsRecursive(props, propKeys); | ||
|
||
if (value) { | ||
return value; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static getReactFiberKey<T extends object>(elem: T): keyof T | null { | ||
const objKeys = Object.keys(elem) as (keyof T)[]; | ||
|
||
return objKeys.find((e) => (e as string).startsWith("__reactFiber")) || null; | ||
} | ||
|
||
private static isReactFiber(obj: unknown): obj is ReactFiber { | ||
return Boolean(typeof obj === "object" && obj && !Array.isArray(obj) && | ||
"memoizedProps" in obj && typeof obj.memoizedProps === "object" && !Array.isArray(obj.memoizedProps) && obj.memoizedProps && | ||
"children" in obj.memoizedProps && typeof obj.memoizedProps.children === "object"); | ||
} | ||
} | ||
|
||
type ReactFiber = { | ||
memoizedProps: ReactProps; | ||
}; | ||
type ReactProps = { | ||
children?: ReactProps | ReactProps[]; | ||
props: Record<string, unknown> & ReactProps; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import ExerciseOption from "./ExerciseOption"; | ||
|
||
export default class BasicExerciseOption extends ExerciseOption { | ||
constructor(optionElem: HTMLOptionElement) { | ||
super( | ||
BasicExerciseOption.findValue(optionElem) || "", | ||
BasicExerciseOption.findCategory(optionElem) || "", | ||
optionElem.innerText, | ||
optionElem.parentElement!.matches("[label='Suggested']") | ||
); | ||
} | ||
|
||
static findExerciseOption(exerciseOptions: readonly ExerciseOption[], option: HTMLOptionElement): ExerciseOption | null { | ||
const value = this.findValue(option), | ||
category = this.findCategory(option); | ||
|
||
if (!value || !category) { | ||
return null; | ||
} | ||
|
||
return exerciseOptions.find((e) => e.value === value && e.categoryValue === category) ?? null; | ||
} | ||
|
||
private static findValue(option: HTMLOptionElement): string { | ||
return option.value; | ||
} | ||
|
||
private static findCategory(option: HTMLOptionElement): string | null { | ||
return option.dataset["exerciseCategory"] ?? null; | ||
} | ||
} |
Oops, something went wrong.