mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-12-27 02:21:27 +08:00
move resize, add metadata
This commit is contained in:
parent
197e15d6b6
commit
80173acd24
4 changed files with 25 additions and 18 deletions
|
|
@ -12,7 +12,6 @@ import {
|
|||
createErrorMessage,
|
||||
isObject,
|
||||
promiseWithResolvers,
|
||||
triggerResize,
|
||||
typedKeys,
|
||||
} from "./utils/misc";
|
||||
import * as ConfigSchemas from "@monkeytype/schemas/configs";
|
||||
|
|
@ -227,17 +226,13 @@ export function setConfig<T extends keyof Config>(
|
|||
if (!options?.nosave) saveToLocalStorage(key, options?.nosave);
|
||||
|
||||
if (!options?.noevent) {
|
||||
// @ts-expect-error i can't figure this out
|
||||
ConfigEvent.dispatch({
|
||||
key: key,
|
||||
newValue: value,
|
||||
nosave: options?.nosave ?? false,
|
||||
previousValue: previousValue as Config[T],
|
||||
});
|
||||
}
|
||||
|
||||
if (metadata.triggerResize && !options?.nosave) {
|
||||
triggerResize();
|
||||
previousValue: previousValue,
|
||||
metadata,
|
||||
} as ConfigEvent.ConfigEventOptions);
|
||||
}
|
||||
|
||||
metadata.afterSet?.({
|
||||
|
|
@ -276,6 +271,7 @@ export function toggleFunbox(funbox: FunboxName, nosave?: boolean): boolean {
|
|||
newValue: config.funbox,
|
||||
nosave,
|
||||
previousValue,
|
||||
metadata: configMetadata.funbox,
|
||||
});
|
||||
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { Config } from "@monkeytype/schemas/configs";
|
||||
import { ConfigMetadata } from "../config-metadata";
|
||||
|
||||
export type ConfigEventKey =
|
||||
| keyof Config
|
||||
|
|
@ -6,16 +7,26 @@ export type ConfigEventKey =
|
|||
| "fullConfigChangeFinished"
|
||||
| "configApplied";
|
||||
|
||||
type SubscribeParams = {
|
||||
export type ConfigEventOptions = {
|
||||
nosave?: boolean;
|
||||
fullConfig?: Config;
|
||||
} & {
|
||||
[K in ConfigEventKey]?: K extends keyof Config
|
||||
? { key: K; newValue: Config[K]; previousValue: Config[K] }
|
||||
: { key: K; newValue?: undefined; previousValue?: undefined };
|
||||
? {
|
||||
key: K;
|
||||
newValue: Config[K];
|
||||
previousValue: Config[K];
|
||||
metadata: ConfigMetadata<K>;
|
||||
}
|
||||
: {
|
||||
key: K;
|
||||
newValue?: undefined;
|
||||
previousValue?: undefined;
|
||||
metadata?: undefined;
|
||||
};
|
||||
}[ConfigEventKey];
|
||||
|
||||
type SubscribeFunction = (options: SubscribeParams) => void;
|
||||
type SubscribeFunction = (options: ConfigEventOptions) => void;
|
||||
|
||||
const subscribers: SubscribeFunction[] = [];
|
||||
|
||||
|
|
@ -23,7 +34,7 @@ export function subscribe(fn: SubscribeFunction): void {
|
|||
subscribers.push(fn);
|
||||
}
|
||||
|
||||
export function dispatch(options: SubscribeParams): void {
|
||||
export function dispatch(options: ConfigEventOptions): void {
|
||||
console.log("dispatching config event", options);
|
||||
subscribers.forEach((fn) => {
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -2032,7 +2032,11 @@ $("#wordsWrapper").on("click", () => {
|
|||
focusWords();
|
||||
});
|
||||
|
||||
ConfigEvent.subscribe(({ key, newValue }) => {
|
||||
ConfigEvent.subscribe(({ key, newValue, metadata }) => {
|
||||
if (metadata?.triggerResize) {
|
||||
document.dispatchEvent(new Event("resize"));
|
||||
}
|
||||
|
||||
if (key === "configApplied") {
|
||||
$(
|
||||
"#caret, #paceCaret, #liveStatsMini, #typingTest, #wordsInput, #compositionDisplay",
|
||||
|
|
|
|||
|
|
@ -680,10 +680,6 @@ export function debounceUntilResolved<TArgs extends unknown[], TResult>(
|
|||
};
|
||||
}
|
||||
|
||||
export function triggerResize(): void {
|
||||
$(window).trigger("resize");
|
||||
}
|
||||
|
||||
export type RequiredProperties<T, K extends keyof T> = Omit<T, K> &
|
||||
Required<Pick<T, K>>;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue