-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move utility functions to other files
- Loading branch information
Showing
4 changed files
with
114 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export function OpacityAnimation({ | ||
index, | ||
total, | ||
partDuration, | ||
switchDuration, | ||
}: { | ||
readonly index: number; | ||
readonly total: number; | ||
readonly partDuration: number; | ||
readonly switchDuration: number; | ||
}) { | ||
if (total <= 1) { | ||
return <></>; | ||
} | ||
|
||
const totalDuration = total * partDuration; | ||
const showSwitchStart = index * partDuration; | ||
const showSwitchFinish = showSwitchStart + switchDuration; | ||
const hideSwitchStart = showSwitchStart + partDuration - switchDuration; | ||
const hideSwitchFinish = showSwitchStart + partDuration; | ||
const keyTimes = [ | ||
0, | ||
showSwitchStart / totalDuration, | ||
showSwitchFinish / totalDuration, | ||
hideSwitchStart / totalDuration, | ||
hideSwitchFinish / totalDuration, | ||
1, | ||
]; | ||
const values = [0, 0, 1, 1, 0, 0]; | ||
if (index === 0) { | ||
values.shift(); | ||
keyTimes.shift(); | ||
} else if (index === total - 1) { | ||
values.pop(); | ||
keyTimes.pop(); | ||
} | ||
|
||
return ( | ||
<animate | ||
attributeName="opacity" | ||
values={values.join(";")} | ||
keyTimes={keyTimes.join(";")} | ||
dur={`${totalDuration}s`} | ||
repeatCount="indefinite" | ||
/> | ||
); | ||
} |
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,27 @@ | ||
import type { PreferencesSchema } from "./schemas"; | ||
|
||
export function formatDateTime( | ||
datetime: string, | ||
language: string, | ||
timeFormat: Exclude<PreferencesSchema["time_format"], "auto"> | ||
): [weekday: string, dateTime: string] { | ||
const date = new Date(datetime + "Z"); | ||
return [ | ||
date.toLocaleString(language, { | ||
timeZone: "UTC", | ||
weekday: "long", | ||
}), | ||
date.toLocaleString(language, { | ||
timeZone: "UTC", | ||
...(timeFormat === "native" | ||
? { dateStyle: "medium", timeStyle: "short" } | ||
: { | ||
month: "short", | ||
day: "numeric", | ||
hour: timeFormat === "24h" ? "2-digit" : "numeric", | ||
hour12: timeFormat === "12h", | ||
minute: "2-digit", | ||
}), | ||
}), | ||
]; | ||
} |
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,37 @@ | ||
import type { JSX as PreactJSX } from "preact"; | ||
|
||
export function isRTLLanguage(language: string): boolean { | ||
const locale = new Intl.Locale(language); | ||
// @ts-expect-error | ||
Check failure on line 5 in src/widget/rtl.tsx GitHub Actions / deploy
|
||
const textInfo = locale.getTextInfo?.() ?? locale.textInfo; | ||
return textInfo?.direction === "rtl"; | ||
} | ||
|
||
export function createLogicalComponent<T extends "rect" | "text" | "use">( | ||
tagName: T, | ||
boxWidth: number, | ||
flip: boolean | ||
) { | ||
const Tag = tagName; | ||
if (!flip) { | ||
return (props: PreactJSX.SVGAttributes<SVGElementTagNameMap[T]>) => ( | ||
// @ts-expect-error | ||
Check failure on line 18 in src/widget/rtl.tsx GitHub Actions / deploy
|
||
<Tag {...props} /> | ||
); | ||
} | ||
|
||
return ({ ...props }) => { | ||
const x = boxWidth - Number(props.x ?? "0") - Number(props.width ?? "0"); | ||
const textAnchor = | ||
tagName === "text" ? props["text-anchor"] ?? "start" : undefined; | ||
const flippedTextAnchor = | ||
textAnchor === "start" | ||
? "end" | ||
: textAnchor === "end" | ||
? "start" | ||
: textAnchor; | ||
|
||
// @ts-expect-error | ||
Check failure on line 34 in src/widget/rtl.tsx GitHub Actions / deploy
|
||
return <Tag {...props} x={x} text-anchor={flippedTextAnchor} />; | ||
}; | ||
} |