Anki/ts/routes/deck-options/DailyLimits.svelte
Damien Elmes 9f55cf26fc
Switch to SvelteKit (#3077)
* Update to latest Node LTS

* Add sveltekit

* Split tslib into separate @generated and @tslib components

SvelteKit's path aliases don't support multiple locations, so our old
approach of using @tslib to refer to both ts/lib and out/ts/lib will no
longer work. Instead, all generated sources and their includes are
placed in a separate out/ts/generated folder, and imported via @generated
instead. This also allows us to generate .ts files, instead of needing
to output separate .d.ts and .js files.

* Switch package.json to module type

* Avoid usage of baseUrl

Incompatible with SvelteKit

* Move sass into ts; use relative links

SvelteKit's default sass support doesn't allow overriding loadPaths

* jest->vitest, graphs example working with yarn dev

* most pages working in dev mode

* Some fixes after rebasing

* Fix/silence some svelte-check errors

* Get image-occlusion working with Fabric types

* Post-rebase lock changes

* Editor is now checked

* SvelteKit build integrated into ninja

* Use the new SvelteKit entrypoint for pages like congrats/deck options/etc

* Run eslint once for ts/**; fix some tests

* Fix a bunch of issues introduced when rebasing over latest main

* Run eslint fix

* Fix remaining eslint+pylint issues; tests now all pass

* Fix some issues with a clean build

* Latest bufbuild no longer requires @__PURE__ hack

* Add a few missed dependencies

* Add yarn.bat to fix Windows build

* Fix pages failing to show when ANKI_API_PORT not defined

* Fix svelte-check and vitest on Windows

* Set node path in ./yarn

* Move svelte-kit output to ts/.svelte-kit

Sadly, I couldn't figure out a way to store it in out/ if out/ is
a symlink, as it breaks module resolution when SvelteKit is run.

* Allow HMR inside Anki

* Skip SvelteKit build when HMR is defined

* Fix some post-rebase issues

I should have done a normal merge instead.
2024-03-31 09:16:31 +01:00

223 lines
7.5 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import * as tr from "@generated/ftl";
import { HelpPage } from "@tslib/help-page";
import type Carousel from "bootstrap/js/dist/carousel";
import type Modal from "bootstrap/js/dist/modal";
import DynamicallySlottable from "$lib/components/DynamicallySlottable.svelte";
import HelpModal from "$lib/components/HelpModal.svelte";
import Item from "$lib/components/Item.svelte";
import SettingTitle from "$lib/components/SettingTitle.svelte";
import SwitchRow from "$lib/components/SwitchRow.svelte";
import TitledContainer from "$lib/components/TitledContainer.svelte";
import type { HelpItem } from "$lib/components/types";
import GlobalLabel from "./GlobalLabel.svelte";
import type { DeckOptionsState } from "./lib";
import { ValueTab } from "./lib";
import SpinBoxRow from "./SpinBoxRow.svelte";
import TabbedValue from "./TabbedValue.svelte";
import Warning from "./Warning.svelte";
export let state: DeckOptionsState;
export let api: Record<string, never>;
export function onPresetChange() {
newTabs[0] = new ValueTab(
tr.deckConfigSharedPreset(),
$config.newPerDay,
(value) => ($config.newPerDay = value!),
$config.newPerDay,
null,
);
reviewTabs[0] = new ValueTab(
tr.deckConfigSharedPreset(),
$config.reviewsPerDay,
(value) => ($config.reviewsPerDay = value!),
$config.reviewsPerDay,
null,
);
}
const config = state.currentConfig;
const limits = state.deckLimits;
const defaults = state.defaults;
const newCardsIgnoreReviewLimit = state.newCardsIgnoreReviewLimit;
const applyAllParentLimits = state.applyAllParentLimits;
const v3Extra =
"\n\n" + tr.deckConfigLimitDeckV3() + "\n\n" + tr.deckConfigTabDescription();
const reviewV3Extra = "\n\n" + tr.deckConfigLimitInterdayBoundByReviews() + v3Extra;
const newCardsIgnoreReviewLimitHelp =
tr.deckConfigAffectsEntireCollection() +
"\n\n" +
tr.deckConfigNewCardsIgnoreReviewLimitTooltip();
const applyAllParentLimitsHelp =
tr.deckConfigAffectsEntireCollection() +
"\n\n" +
tr.deckConfigApplyAllParentLimitsTooltip();
$: reviewsTooLow =
Math.min(9999, newValue * 10) > reviewsValue
? tr.deckConfigReviewsTooLow({
cards: newValue,
expected: Math.min(9999, newValue * 10),
})
: "";
const newTabs: ValueTab[] = [
new ValueTab(
tr.deckConfigSharedPreset(),
$config.newPerDay,
(value) => ($config.newPerDay = value!),
$config.newPerDay,
null,
),
new ValueTab(
tr.deckConfigDeckOnly(),
$limits.new ?? null,
(value) => ($limits.new = value ?? undefined),
null,
null,
),
new ValueTab(
tr.deckConfigTodayOnly(),
$limits.newTodayActive ? $limits.newToday ?? null : null,
(value) => ($limits.newToday = value ?? undefined),
null,
$limits.newToday ?? null,
),
];
const reviewTabs: ValueTab[] = [
new ValueTab(
tr.deckConfigSharedPreset(),
$config.reviewsPerDay,
(value) => ($config.reviewsPerDay = value!),
$config.reviewsPerDay,
null,
),
new ValueTab(
tr.deckConfigDeckOnly(),
$limits.review ?? null,
(value) => ($limits.review = value ?? undefined),
null,
null,
),
new ValueTab(
tr.deckConfigTodayOnly(),
$limits.reviewTodayActive ? $limits.reviewToday ?? null : null,
(value) => ($limits.reviewToday = value ?? undefined),
null,
$limits.reviewToday ?? null,
),
];
let newValue = 0;
let reviewsValue = 0;
const settings = {
newLimit: {
title: tr.schedulingNewCardsday(),
help: tr.deckConfigNewLimitTooltip() + v3Extra,
url: HelpPage.DeckOptions.newCardsday,
},
reviewLimit: {
title: tr.schedulingMaximumReviewsday(),
help: tr.deckConfigReviewLimitTooltip() + reviewV3Extra,
url: HelpPage.DeckOptions.maximumReviewsday,
},
newCardsIgnoreReviewLimit: {
title: tr.deckConfigNewCardsIgnoreReviewLimit(),
help: newCardsIgnoreReviewLimitHelp,
url: HelpPage.DeckOptions.newCardsday,
},
applyAllParentLimits: {
title: tr.deckConfigApplyAllParentLimits(),
help: applyAllParentLimitsHelp,
url: HelpPage.DeckOptions.newCardsday,
},
};
const helpSections = Object.values(settings) as HelpItem[];
let modal: Modal;
let carousel: Carousel;
function openHelpModal(index: number): void {
modal.show();
carousel.to(index);
}
</script>
<TitledContainer title={tr.deckConfigDailyLimits()}>
<HelpModal
title={tr.deckConfigDailyLimits()}
url={HelpPage.DeckOptions.dailyLimits}
slot="tooltip"
{helpSections}
on:mount={(e) => {
modal = e.detail.modal;
carousel = e.detail.carousel;
}}
/>
<DynamicallySlottable slotHost={Item} {api}>
<Item>
<SpinBoxRow bind:value={newValue} defaultValue={defaults.newPerDay}>
<TabbedValue slot="tabs" tabs={newTabs} bind:value={newValue} />
<SettingTitle
on:click={() =>
openHelpModal(Object.keys(settings).indexOf("newLimit"))}
>
{settings.newLimit.title}
</SettingTitle>
</SpinBoxRow>
</Item>
<Item>
<SpinBoxRow bind:value={reviewsValue} defaultValue={defaults.reviewsPerDay}>
<TabbedValue slot="tabs" tabs={reviewTabs} bind:value={reviewsValue} />
<SettingTitle
on:click={() =>
openHelpModal(Object.keys(settings).indexOf("reviewLimit"))}
>
{settings.reviewLimit.title}
</SettingTitle>
</SpinBoxRow>
</Item>
<Item>
<Warning warning={reviewsTooLow} />
</Item>
<Item>
<SwitchRow bind:value={$newCardsIgnoreReviewLimit} defaultValue={false}>
<SettingTitle
on:click={() =>
openHelpModal(
Object.keys(settings).indexOf("newCardsIgnoreReviewLimit"),
)}
>
<GlobalLabel title={settings.newCardsIgnoreReviewLimit.title} />
</SettingTitle>
</SwitchRow>
</Item>
<Item>
<SwitchRow bind:value={$applyAllParentLimits} defaultValue={false}>
<SettingTitle
on:click={() =>
openHelpModal(
Object.keys(settings).indexOf("applyAllParentLimits"),
)}
>
<GlobalLabel title={settings.applyAllParentLimits.title} />
</SettingTitle>
</SwitchRow>
</Item>
</DynamicallySlottable>
</TitledContainer>