Make code typecheck, fix issue with day labels in nightMode

This commit is contained in:
Henrik Giesel 2021-01-22 19:02:05 +01:00
parent ebd3ca8a8f
commit 17ebb69151
4 changed files with 31 additions and 18 deletions

View file

@ -3,12 +3,13 @@
import AxisTicks from "./AxisTicks.svelte"; import AxisTicks from "./AxisTicks.svelte";
import { defaultGraphBounds, RevlogRange } from "./graph-helpers"; import { defaultGraphBounds, RevlogRange } from "./graph-helpers";
import { gatherData, renderCalendar } from "./calendar"; import { gatherData, renderCalendar } from "./calendar";
import type { PreferenceStore } from "./preferences";
import type { GraphData } from "./calendar"; import type { GraphData } from "./calendar";
import type pb from "anki/backend_proto"; import type pb from "anki/backend_proto";
import type { I18n } from "anki/i18n"; import type { I18n } from "anki/i18n";
export let sourceData: pb.BackendProto.GraphsOut | null = null; export let sourceData: pb.BackendProto.GraphsOut | null = null;
export let preferences: pb.BackendProto.GraphsPreferencesOut | null = null; export let preferences: PreferenceStore | null = null;
export let revlogRange: RevlogRange; export let revlogRange: RevlogRange;
export let i18n: I18n; export let i18n: I18n;
export let nightMode: boolean; export let nightMode: boolean;

View file

@ -178,7 +178,7 @@ export function renderCalendar(
.attr("height", height - 2) .attr("height", height - 2)
.attr("x", x(1)! - 3) .attr("x", x(1)! - 3)
.attr("y", (_d, index) => bounds.marginTop + index * height) .attr("y", (_d, index) => bounds.marginTop + index * height)
.attr("fill", emptyColour) .attr("fill", nightMode ? "#ddd" : "black")
.attr("dominant-baseline", "hanging") .attr("dominant-baseline", "hanging")
.attr("text-anchor", "end") .attr("text-anchor", "end")
.attr("font-size", "small") .attr("font-size", "small")

View file

@ -8,6 +8,7 @@
import pb from "anki/backend_proto"; import pb from "anki/backend_proto";
import type { Selection } from "d3-selection"; import type { Selection } from "d3-selection";
import type { PreferencePayload } from "./preferences";
import { postRequest } from "anki/postrequest"; import { postRequest } from "anki/postrequest";
export async function getGraphData( export async function getGraphData(
@ -25,10 +26,8 @@ export async function getGraphPreferences(): Promise<pb.BackendProto.GraphsPrefe
); );
} }
export async function setGraphPreferences( export async function setGraphPreferences(prefs: PreferencePayload): Promise<void> {
prefs: pb.BackendProto.GraphsPreferencesOut return (async (): Promise<void> => {
): Promise<void> {
return (async () => {
await postRequest( await postRequest(
"/_anki/setGraphPreferences", "/_anki/setGraphPreferences",
pb.BackendProto.GraphsPreferencesOut.encode(prefs).finish() pb.BackendProto.GraphsPreferencesOut.encode(prefs).finish()

View file

@ -1,28 +1,39 @@
import type pb from "anki/backend_proto"; import type pb from "anki/backend_proto";
import { getGraphPreferences, setGraphPreferences } from "./graph-helpers"; import { getGraphPreferences, setGraphPreferences } from "./graph-helpers";
import { writable, get } from "svelte/store"; import { Writable, writable, get } from "svelte/store";
export interface CustomStore<T> { export interface CustomStore<T> extends Writable<T> {
subscribe: (getter: (value: T) => void) => () => void; subscribe: (getter: (value: T) => void) => () => void;
set: (value: T) => void; set: (value: T) => void;
} }
export type PreferenceStore = { export type PreferenceStore = {
[K in keyof pb.BackendProto.GraphsPreferencesOut]: CustomStore< [K in keyof Omit<pb.BackendProto.GraphsPreferencesOut, "toJSON">]: CustomStore<
pb.BackendProto.GraphsPreferencesOut[K] pb.BackendProto.GraphsPreferencesOut[K]
>; >;
}; };
export type PreferencePayload = {
[K in keyof Omit<
pb.BackendProto.GraphsPreferencesOut,
"toJSON"
>]: pb.BackendProto.GraphsPreferencesOut[K];
};
function createPreference<T>( function createPreference<T>(
initialValue: T, initialValue: T,
savePreferences: () => void savePreferences: () => void
): CustomStore<T> { ): CustomStore<T> {
const { subscribe, set } = writable(initialValue); const { subscribe, set, update } = writable(initialValue);
return { return {
subscribe, subscribe,
set: (v: T): void => { set: (value: T): void => {
set(v); set(value);
savePreferences();
},
update: (updater: (value: T) => T): void => {
update(updater);
savePreferences(); savePreferences();
}, },
}; };
@ -33,12 +44,14 @@ function preparePreferences(
): PreferenceStore { ): PreferenceStore {
const preferences: Partial<PreferenceStore> = {}; const preferences: Partial<PreferenceStore> = {};
function constructPreferences(): pb.BackendProto.GraphsPreferencesOut { function constructPreferences(): PreferencePayload {
const payload: Partial<pb.BackendProto.GraphsPreferencesOut> = {}; const payload: Partial<PreferencePayload> = {};
for (const [key, pref] of Object.entries(preferences as PreferenceStore)) {
payload[key] = get(pref as any); for (const key in preferences as PreferenceStore) {
payload[key] = get(preferences[key]);
} }
return payload as pb.BackendProto.GraphsPreferencesOut;
return payload as PreferencePayload;
} }
function savePreferences(): void { function savePreferences(): void {
@ -52,7 +65,7 @@ function preparePreferences(
return preferences as PreferenceStore; return preferences as PreferenceStore;
} }
export async function getPreferences() { export async function getPreferences(): Promise<PreferenceStore> {
const initialPreferences = await getGraphPreferences(); const initialPreferences = await getGraphPreferences();
return preparePreferences(initialPreferences); return preparePreferences(initialPreferences);
} }