apply prettier config update

This commit is contained in:
Michael Peters 2024-07-21 10:51:53 -07:00
parent 892f8ca09d
commit 9514790f79
3 changed files with 31 additions and 50 deletions

View File

@ -19,7 +19,7 @@ export interface IAlignment {
export function alignContextElement( export function alignContextElement(
element: HTMLElement, element: HTMLElement,
relativeTo: HTMLElement | { x: number; y: number }, relativeTo: HTMLElement | { x: number; y: number },
alignment: IAlignment alignment: IAlignment,
): void { ): void {
function getOffset(alignment: string, name: string) { function getOffset(alignment: string, name: string) {
const added = name + ' +'; const added = name + ' +';
@ -76,10 +76,7 @@ export function alignContextElement(
const offset = getOffset(alignment.centerY, 'bottom'); const offset = getOffset(alignment.centerY, 'bottom');
element.style.top = rt + (rh - eh / 2) + offset + 'px'; element.style.top = rt + (rh - eh / 2) + offset + 'px';
} else { } else {
throw new Error( throw new Error('illegal centerY alignment: ' + JSON.stringify(alignment.centerY));
'illegal centerY alignment: ' +
JSON.stringify(alignment.centerY)
);
} }
} else if (alignment.top) { } else if (alignment.top) {
if (alignment.top.startsWith('centerY')) { if (alignment.top.startsWith('centerY')) {
@ -95,9 +92,7 @@ export function alignContextElement(
const offset = getOffset(alignment.top, 'bottom'); const offset = getOffset(alignment.top, 'bottom');
element.style.top = rt + rh + offset + 'px'; element.style.top = rt + rh + offset + 'px';
} else { } else {
throw new Error( throw new Error('illegal top alignment: ' + JSON.stringify(alignment.top));
'illegal top alignment: ' + JSON.stringify(alignment.top)
);
} }
} else if (alignment.bottom) { } else if (alignment.bottom) {
if (alignment.bottom.startsWith('centerY')) { if (alignment.bottom.startsWith('centerY')) {
@ -113,9 +108,7 @@ export function alignContextElement(
const offset = getOffset(alignment.bottom, 'bottom'); const offset = getOffset(alignment.bottom, 'bottom');
element.style.top = rb - eh + offset + 'px'; element.style.top = rb - eh + offset + 'px';
} else { } else {
throw new Error( throw new Error('illegal bottom alignment: ' + JSON.stringify(alignment.bottom));
'illegal bottom alignment: ' + JSON.stringify(alignment.bottom)
);
} }
} else { } else {
throw new Error('y-alignment not specified'); throw new Error('y-alignment not specified');
@ -135,10 +128,7 @@ export function alignContextElement(
const offset = getOffset(alignment.centerX, 'right'); const offset = getOffset(alignment.centerX, 'right');
element.style.left = rl + (rw - ew / 2) + offset + 'px'; element.style.left = rl + (rw - ew / 2) + offset + 'px';
} else { } else {
throw new Error( throw new Error('illegal centerX alignment: ' + JSON.stringify(alignment.centerX));
'illegal centerX alignment: ' +
JSON.stringify(alignment.centerX)
);
} }
} else if (alignment.left) { } else if (alignment.left) {
if (alignment.left.startsWith('centerX')) { if (alignment.left.startsWith('centerX')) {
@ -151,9 +141,7 @@ export function alignContextElement(
const offset = getOffset(alignment.left, 'right'); const offset = getOffset(alignment.left, 'right');
element.style.left = rl + rw + offset + 'px'; element.style.left = rl + rw + offset + 'px';
} else { } else {
throw new Error( throw new Error('illegal left alignment: ' + JSON.stringify(alignment.left));
'illegal left alignment: ' + JSON.stringify(alignment.left)
);
} }
} else if (alignment.right) { } else if (alignment.right) {
if (alignment.right.startsWith('centerX')) { if (alignment.right.startsWith('centerX')) {
@ -166,9 +154,7 @@ export function alignContextElement(
const offset = getOffset(alignment.right, 'right'); const offset = getOffset(alignment.right, 'right');
element.style.left = rr - ew + offset + 'px'; element.style.left = rr - ew + offset + 'px';
} else { } else {
throw new Error( throw new Error('illegal right alignment: ' + JSON.stringify(alignment.right));
'illegal right alignment: ' + JSON.stringify(alignment.right)
);
} }
} else { } else {
throw new Error('x-alignment not defined'); throw new Error('x-alignment not defined');

View File

@ -14,7 +14,7 @@ import { IAlignment, alignContextElement } from './align';
class ShouldNeverHappenError extends Error {} class ShouldNeverHappenError extends Error {}
function sleep(ms: number): Promise<void> { function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
/** returns a ref that is true if the component is mounted and false otherwise. Very useful for async stuff */ /** returns a ref that is true if the component is mounted and false otherwise. Very useful for async stuff */
@ -51,16 +51,14 @@ function useShake(ms: number): [shaking: boolean, doShake: () => void] {
export function useOneTimeAsyncAction<T, V>( export function useOneTimeAsyncAction<T, V>(
actionFunc: () => Promise<T>, actionFunc: () => Promise<T>,
deps: DependencyList, deps: DependencyList,
initialValue: V initialValue: V,
): [value: T | V, error: unknown | null] { ): [value: T | V, error: unknown | null] {
const isMounted = useRef(false); const isMounted = useIsMountedRef();
const [value, setValue] = useState<T | V>(initialValue); const [value, setValue] = useState<T | V>(initialValue);
const [error, setError] = useState<unknown | null>(null); const [error, setError] = useState<unknown | null>(null);
useEffect(() => { useEffect(() => {
isMounted.current = true;
(async () => { (async () => {
try { try {
const value = await actionFunc(); const value = await actionFunc();
@ -73,10 +71,6 @@ export function useOneTimeAsyncAction<T, V>(
setError(e); setError(e);
} }
})(); })();
return () => {
isMounted.current = false;
};
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [actionFunc, ...deps]); }, [actionFunc, ...deps]);
@ -86,9 +80,9 @@ export function useOneTimeAsyncAction<T, V>(
/** creates a callable async function that will not double-trigger and gives a result, error message, and shaking boolean */ /** creates a callable async function that will not double-trigger and gives a result, error message, and shaking boolean */
export function useAsyncCallback<ResultType>( export function useAsyncCallback<ResultType>(
actionFunc: ( actionFunc: (
isMounted: MutableRefObject<boolean> isMounted: MutableRefObject<boolean>,
) => Promise<{ errorMessage: string | null; result: ResultType | null }>, ) => Promise<{ errorMessage: string | null; result: ResultType | null }>,
deps: DependencyList deps: DependencyList,
): [callable: () => void, result: ResultType | null, errorMessage: string | null, shaking: boolean, pending: boolean] { ): [callable: () => void, result: ResultType | null, errorMessage: string | null, shaking: boolean, pending: boolean] {
const isMounted = useIsMountedRef(); const isMounted = useIsMountedRef();
@ -126,7 +120,7 @@ export function useAsyncCallback<ResultType>(
/** creates a callable async function that does not need to return a value (or shake) */ /** creates a callable async function that does not need to return a value (or shake) */
export function useAsyncVoidCallback( export function useAsyncVoidCallback(
actionFunc: (isMounted: MutableRefObject<boolean>) => Promise<void>, actionFunc: (isMounted: MutableRefObject<boolean>) => Promise<void>,
deps: DependencyList deps: DependencyList,
): [callable: () => void] { ): [callable: () => void] {
const [callable] = useAsyncCallback(async (isMounted: MutableRefObject<boolean>) => { const [callable] = useAsyncCallback(async (isMounted: MutableRefObject<boolean>) => {
await actionFunc(isMounted); await actionFunc(isMounted);
@ -139,7 +133,7 @@ export function useAsyncVoidCallback(
/** creates useful state variables for a button intended call a submit action */ /** creates useful state variables for a button intended call a submit action */
export function useAsyncSubmitButton<ResultType>( export function useAsyncSubmitButton<ResultType>(
actionFunc: ( actionFunc: (
isMounted: MutableRefObject<boolean> isMounted: MutableRefObject<boolean>,
) => Promise<{ errorMessage: string | null; result: ResultType | null }>, ) => Promise<{ errorMessage: string | null; result: ResultType | null }>,
deps: DependencyList, deps: DependencyList,
stateTextMapping?: { stateTextMapping?: {
@ -147,7 +141,7 @@ export function useAsyncSubmitButton<ResultType>(
pending?: string; pending?: string;
error?: string; error?: string;
done?: string; done?: string;
} },
): [callable: () => void, text: string, shaking: boolean, result: ResultType | null, errorMessage: string | null] { ): [callable: () => void, text: string, shaking: boolean, result: ResultType | null, errorMessage: string | null] {
const textMapping = { const textMapping = {
...{ ...{
@ -169,7 +163,7 @@ export function useAsyncSubmitButton<ResultType>(
return actionReturnValue; return actionReturnValue;
}, },
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
[...deps, actionFunc] [...deps, actionFunc],
); );
const text = useMemo(() => { const text = useMemo(() => {
@ -186,7 +180,7 @@ export function useAsyncSubmitButton<ResultType>(
/** calls the close action when you hit escape or click outside of the ref element */ /** calls the close action when you hit escape or click outside of the ref element */
export function useActionWhenEscapeOrClickedOrContextOutsideEffect( export function useActionWhenEscapeOrClickedOrContextOutsideEffect(
refs: RefObject<HTMLElement>[], refs: RefObject<HTMLElement>[],
actionFunc: () => void actionFunc: () => void,
) { ) {
// have to use a ref here and not states since we can't re-assign state between mouseup and click // have to use a ref here and not states since we can't re-assign state between mouseup and click
const data = useRef< const data = useRef<
@ -228,7 +222,7 @@ export function useActionWhenEscapeOrClickedOrContextOutsideEffect(
actionFunc(); actionFunc();
}, },
[hadMouseDown, actionFunc] [hadMouseDown, actionFunc],
); );
const handleMouseDown = useCallback((event: MouseEvent) => { const handleMouseDown = useCallback((event: MouseEvent) => {
@ -263,7 +257,7 @@ export function useActionWhenEscapeOrClickedOrContextOutsideEffect(
// context menu is fired on mouse-down so no need to do special checks. // context menu is fired on mouse-down so no need to do special checks.
actionFunc(); actionFunc();
}, },
[actionFunc] [actionFunc],
); );
const handleKeyDown = useCallback( const handleKeyDown = useCallback(
@ -275,7 +269,7 @@ export function useActionWhenEscapeOrClickedOrContextOutsideEffect(
actionFunc(); actionFunc();
}, },
[actionFunc] [actionFunc],
); );
useEffect(() => { useEffect(() => {
@ -321,7 +315,7 @@ export function useAlignment(
relativeToPos: { x: number; y: number } | null, relativeToPos: { x: number; y: number } | null,
alignment: IAlignment, alignment: IAlignment,
realignDeps: DependencyList, realignDeps: DependencyList,
baseClassName: string baseClassName: string,
): [className: string] { ): [className: string] {
const [aligned, setAligned] = useState<boolean>(false); const [aligned, setAligned] = useState<boolean>(false);
@ -342,7 +336,7 @@ export function useAlignment(
/** creates useful context menu state */ /** creates useful context menu state */
export function useContextMenu( export function useContextMenu(
createContextMenu: (close: () => void) => ReactNode, createContextMenu: (close: () => void) => ReactNode,
createContextMenuDeps: DependencyList createContextMenuDeps: DependencyList,
): [contextMenu: ReactNode, toggle: () => void, close: () => void, open: () => void, isOpen: boolean] { ): [contextMenu: ReactNode, toggle: () => void, close: () => void, open: () => void, isOpen: boolean] {
const [isOpen, setIsOpen] = useState<boolean>(false); const [isOpen, setIsOpen] = useState<boolean>(false);
@ -354,7 +348,7 @@ export function useContextMenu(
const contextMenu = useMemo(() => createContextMenu(close), [close, createContextMenu, ...createContextMenuDeps]); const contextMenu = useMemo(() => createContextMenu(close), [close, createContextMenu, ...createContextMenuDeps]);
const toggle = useCallback(() => { const toggle = useCallback(() => {
setIsOpen((oldIsOpen) => !!contextMenu && !oldIsOpen); setIsOpen(oldIsOpen => !!contextMenu && !oldIsOpen);
}, [contextMenu]); }, [contextMenu]);
const open = useCallback(() => { const open = useCallback(() => {
setIsOpen(true); setIsOpen(true);
@ -366,7 +360,7 @@ export function useContextMenu(
/** creates context menu state for right-clicks */ /** creates context menu state for right-clicks */
export function useContextClickContextMenu( export function useContextClickContextMenu(
createContextMenu: (alignment: IAlignment, relativeToPos: { x: number; y: number }, close: () => void) => ReactNode, createContextMenu: (alignment: IAlignment, relativeToPos: { x: number; y: number }, close: () => void) => ReactNode,
createContextMenuDeps: DependencyList createContextMenuDeps: DependencyList,
): [contextMenu: ReactNode, onContextMenu: (event: React.MouseEvent<HTMLElement>) => void] { ): [contextMenu: ReactNode, onContextMenu: (event: React.MouseEvent<HTMLElement>) => void] {
const [relativeToPos, setRelativeToPos] = useState<{ const [relativeToPos, setRelativeToPos] = useState<{
x: number; x: number;
@ -377,12 +371,12 @@ export function useContextClickContextMenu(
const createContextMenuWithRelativeToPos = useCallback( const createContextMenuWithRelativeToPos = useCallback(
(close: () => void) => createContextMenu(alignment, relativeToPos, close), (close: () => void) => createContextMenu(alignment, relativeToPos, close),
[alignment, createContextMenu, relativeToPos] [alignment, createContextMenu, relativeToPos],
); );
const [contextMenu, _toggle, _close, open] = useContextMenu( const [contextMenu, _toggle, _close, open] = useContextMenu(
createContextMenuWithRelativeToPos, createContextMenuWithRelativeToPos,
createContextMenuDeps createContextMenuDeps,
); );
const onContextMenu = useCallback( const onContextMenu = useCallback(
@ -390,7 +384,7 @@ export function useContextClickContextMenu(
setRelativeToPos({ x: event.clientX, y: event.clientY }); setRelativeToPos({ x: event.clientX, y: event.clientY });
open(); open();
}, },
[open] [open],
); );
return [contextMenu, onContextMenu]; return [contextMenu, onContextMenu];
@ -399,14 +393,14 @@ export function useContextClickContextMenu(
/** creates context state for hovering over an element */ /** creates context state for hovering over an element */
export function useContextHover( export function useContextHover(
createContextHover: () => ReactNode, createContextHover: () => ReactNode,
createContextHoverDeps: DependencyList createContextHoverDeps: DependencyList,
): [contextHover: ReactNode, mouseEnterCallable: () => void, mouseLeaveCallable: () => void] { ): [contextHover: ReactNode, mouseEnterCallable: () => void, mouseLeaveCallable: () => void] {
const [isOpen, setIsOpen] = useState<boolean>(false); const [isOpen, setIsOpen] = useState<boolean>(false);
const contextHover = useMemo( const contextHover = useMemo(
() => createContextHover(), () => createContextHover(),
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
[createContextHover, ...createContextHoverDeps] [createContextHover, ...createContextHoverDeps],
); );
const mouseEnterCallable = useCallback(() => { const mouseEnterCallable = useCallback(() => {

View File

@ -1,7 +1,8 @@
<html> <html>
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<link rel="icon" href="data:,"><!-- disable favicon --> <!-- disable favicon -->
<link rel="icon" href="data:," />
<title>Webpack Starter</title> <title>Webpack Starter</title>
</head> </head>
<body> <body>