anki/ts/editor/EditorField.svelte
Matthias Metelka 35431c5944
Collapsible patch (#2052)
* Animate Collapsible in both directions

* Fix field margin issues

* Fix code style issues

* Make duration prop optional

* Implement reduced motion mode for Collapsible

* Refactor Collapsible and add comments

* Fix LabelContainer badges disappearing when field is still hovered

* Remove reducedMotion store and use body class instead

* Export optional animated boolean

* Do not export duration

* Add 5px top padding to Fields.svelte

to make it look like it used to.

* Revert "Add 5px top padding to Fields.svelte"

This reverts commit f30026149a89f3d3b289c5030cd1ca34f728b036.

* Add top margin of 5px to Fields.svelte
2022-09-14 15:26:07 +10:00

118 lines
3.5 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 class="field-container" on:mouseenter on:mouseleave>
<slot name="field-label" />
<Collapsible collapse={collapsed} let:collapsed={hidden}>
<div use:elementResolve class="editor-field" 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">
.editor-field {
position: relative;
padding: 0 3px;
--border-color: var(--border);
}
</style>