mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 05:52:22 -04:00

* 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.
155 lines
4.3 KiB
Svelte
155 lines
4.3 KiB
Svelte
<!--
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
-->
|
|
<script context="module" lang="ts">
|
|
import type { Readable } from "svelte/store";
|
|
|
|
import type { EditingAreaAPI } from "./EditingArea.svelte";
|
|
|
|
export interface FieldData {
|
|
name: string;
|
|
fontFamily: string;
|
|
fontSize: number;
|
|
direction: "ltr" | "rtl";
|
|
plainText: boolean;
|
|
description: string;
|
|
collapsed: boolean;
|
|
hidden: boolean;
|
|
}
|
|
|
|
export interface EditorFieldAPI {
|
|
element: Promise<HTMLElement>;
|
|
direction: Readable<"ltr" | "rtl">;
|
|
editingArea: EditingAreaAPI;
|
|
}
|
|
|
|
import { registerPackage } from "@tslib/runtime-require";
|
|
|
|
import contextProperty from "$lib/sveltelib/context-property";
|
|
import lifecycleHooks from "$lib/sveltelib/lifecycle-hooks";
|
|
|
|
const key = Symbol("editorField");
|
|
const [context, setContextProperty] = contextProperty<EditorFieldAPI>(key);
|
|
const [lifecycle, instances, setupLifecycleHooks] =
|
|
lifecycleHooks<EditorFieldAPI>();
|
|
|
|
export { context };
|
|
|
|
registerPackage("anki/EditorField", {
|
|
context,
|
|
lifecycle,
|
|
instances,
|
|
});
|
|
</script>
|
|
|
|
<script lang="ts">
|
|
import { collapsedKey, directionKey } from "@tslib/context-keys";
|
|
import { promiseWithResolver } from "@tslib/promise";
|
|
import { onDestroy, setContext } from "svelte";
|
|
import type { Writable } from "svelte/store";
|
|
import { writable } from "svelte/store";
|
|
|
|
import Collapsible from "$lib/components/Collapsible.svelte";
|
|
|
|
import type { Destroyable } from "./destroyable";
|
|
import EditingArea from "./EditingArea.svelte";
|
|
|
|
export let content: Writable<string>;
|
|
export let field: FieldData;
|
|
export let collapsed = false;
|
|
export let flipInputs = false;
|
|
export let dupe = false;
|
|
|
|
const directionStore = writable<"ltr" | "rtl">();
|
|
setContext(directionKey, directionStore);
|
|
|
|
$: $directionStore = field.direction;
|
|
|
|
const collapsedStore = writable<boolean>();
|
|
setContext(collapsedKey, collapsedStore);
|
|
|
|
$: $collapsedStore = collapsed;
|
|
|
|
const editingArea: Partial<EditingAreaAPI> = {};
|
|
const [element, elementResolve] = promiseWithResolver<HTMLElement>();
|
|
|
|
let apiPartial: Partial<EditorFieldAPI> & Destroyable;
|
|
export { apiPartial as api };
|
|
|
|
const api: EditorFieldAPI & Destroyable = Object.assign(apiPartial, {
|
|
element,
|
|
direction: directionStore,
|
|
editingArea: editingArea as EditingAreaAPI,
|
|
});
|
|
|
|
setContextProperty(api);
|
|
setupLifecycleHooks(api);
|
|
|
|
onDestroy(() => api?.destroy());
|
|
</script>
|
|
|
|
<div class="field-container" class:hide={field.hidden} on:mouseenter on:mouseleave>
|
|
<slot name="field-label" />
|
|
|
|
<Collapsible collapse={collapsed} let:collapsed={hidden}>
|
|
<div
|
|
use:elementResolve
|
|
class="editor-field"
|
|
class:dupe
|
|
on:focusin
|
|
on:focusout
|
|
{hidden}
|
|
>
|
|
<EditingArea
|
|
{content}
|
|
fontFamily={field.fontFamily}
|
|
fontSize={field.fontSize}
|
|
api={editingArea}
|
|
>
|
|
{#if flipInputs}
|
|
<slot name="plain-text-input" />
|
|
<slot name="rich-text-input" />
|
|
{:else}
|
|
<slot name="rich-text-input" />
|
|
<slot name="plain-text-input" />
|
|
{/if}
|
|
</EditingArea>
|
|
</div>
|
|
</Collapsible>
|
|
</div>
|
|
|
|
<style lang="scss">
|
|
@use "../lib/sass/elevation" as *;
|
|
|
|
/* Make sure labels are readable on custom Qt backgrounds */
|
|
.field-container {
|
|
background: var(--canvas);
|
|
border-radius: var(--border-radius);
|
|
overflow: hidden;
|
|
}
|
|
|
|
.field-container.hide {
|
|
display: none;
|
|
}
|
|
|
|
.editor-field {
|
|
overflow: hidden;
|
|
/* make room for thicker focus border */
|
|
margin: 1px;
|
|
|
|
border-radius: var(--border-radius);
|
|
border: 1px solid var(--border);
|
|
|
|
@include elevation(1);
|
|
|
|
outline-offset: -1px;
|
|
&.dupe,
|
|
&.dupe:focus-within {
|
|
outline: 2px solid var(--accent-danger);
|
|
}
|
|
&:focus-within {
|
|
outline: 2px solid var(--border-focus);
|
|
}
|
|
}
|
|
</style>
|