mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00

* Introduce setting to collapse field by default * Fix schema order * Change wording from adjective to imperative sounds a bit less clunky * Update rslib/src/notetype/schema11.rs (dae) * Keep settings in single column * Add back Toggle Visual Editor string * Add RichTextBadge component and show it conditionally * Reverse input order depending on default setting * Make PlainTextInput border-radius responsive to toggle states * Prevent first Collapsible transition differently * Focus inputs after Collapsible transition The double tick calls are just a temporary solution until I find the exact moment an input is focusable again. * Use requestAnimationFrame to await focusable state Note: Svelte tick doesn't seem to work in this scenario.
122 lines
3.4 KiB
Svelte
122 lines
3.4 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;
|
|
}
|
|
|
|
export interface EditorFieldAPI {
|
|
element: Promise<HTMLElement>;
|
|
direction: Readable<"ltr" | "rtl">;
|
|
editingArea: EditingAreaAPI;
|
|
}
|
|
|
|
import { registerPackage } from "../lib/runtime-require";
|
|
import contextProperty from "../sveltelib/context-property";
|
|
import lifecycleHooks from "../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 { onDestroy, setContext } from "svelte";
|
|
import type { Writable } from "svelte/store";
|
|
import { writable } from "svelte/store";
|
|
|
|
import Collapsible from "../components/Collapsible.svelte";
|
|
import { collapsedKey, directionKey } from "../lib/context-keys";
|
|
import { promiseWithResolver } from "../lib/promise";
|
|
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;
|
|
|
|
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
|
|
use:elementResolve
|
|
class="editor-field"
|
|
on:focusin
|
|
on:focusout
|
|
on:click={() => editingArea.focus?.()}
|
|
on:mouseenter
|
|
on:mouseleave
|
|
>
|
|
<slot name="field-label" />
|
|
|
|
<Collapsible {collapsed}>
|
|
<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>
|
|
</Collapsible>
|
|
</div>
|
|
|
|
<style lang="scss">
|
|
.editor-field {
|
|
position: relative;
|
|
--border-color: var(--border);
|
|
}
|
|
</style>
|