2021-10-18 14:01:15 +02:00
|
|
|
<!--
|
|
|
|
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">
|
2021-11-05 02:29:02 +01:00
|
|
|
import type { Readable } from "svelte/store";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2022-02-04 09:36:34 +01:00
|
|
|
import type { EditingAreaAPI } from "./EditingArea.svelte";
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
export interface FieldData {
|
|
|
|
name: string;
|
|
|
|
fontFamily: string;
|
|
|
|
fontSize: number;
|
|
|
|
direction: "ltr" | "rtl";
|
2022-06-17 03:02:30 +02:00
|
|
|
description: string;
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface EditorFieldAPI {
|
2021-11-05 02:29:02 +01:00
|
|
|
element: Promise<HTMLElement>;
|
|
|
|
direction: Readable<"ltr" | "rtl">;
|
|
|
|
editingArea: EditingAreaAPI;
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2022-06-20 04:07:07 +02:00
|
|
|
import { registerPackage } from "../lib/runtime-require";
|
|
|
|
import contextProperty from "../sveltelib/context-property";
|
|
|
|
import lifecycleHooks from "../sveltelib/lifecycle-hooks";
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
const key = Symbol("editorField");
|
2022-02-03 05:52:11 +01:00
|
|
|
const [context, setContextProperty] = contextProperty<EditorFieldAPI>(key);
|
2022-06-20 04:07:07 +02:00
|
|
|
const [lifecycle, instances, setupLifecycleHooks] =
|
|
|
|
lifecycleHooks<EditorFieldAPI>();
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
export { context };
|
2022-06-20 04:07:07 +02:00
|
|
|
|
|
|
|
registerPackage("anki/EditorField", {
|
|
|
|
context,
|
|
|
|
lifecycle,
|
|
|
|
instances,
|
|
|
|
});
|
2021-10-18 14:01:15 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2021-11-05 02:29:02 +01:00
|
|
|
import { onDestroy, setContext } from "svelte";
|
2021-10-18 14:01:15 +02:00
|
|
|
import type { Writable } from "svelte/store";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { writable } from "svelte/store";
|
|
|
|
|
2022-08-15 05:34:16 +02:00
|
|
|
import { directionKey } from "../lib/context-keys";
|
2021-11-05 02:29:02 +01:00
|
|
|
import { promiseWithResolver } from "../lib/promise";
|
|
|
|
import type { Destroyable } from "./destroyable";
|
2022-02-04 09:36:34 +01:00
|
|
|
import EditingArea from "./EditingArea.svelte";
|
|
|
|
import FieldState from "./FieldState.svelte";
|
|
|
|
import LabelContainer from "./LabelContainer.svelte";
|
|
|
|
import LabelName from "./LabelName.svelte";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
export let content: Writable<string>;
|
|
|
|
export let field: FieldData;
|
|
|
|
|
2021-11-05 02:29:02 +01:00
|
|
|
const directionStore = writable<"ltr" | "rtl">();
|
2021-10-18 14:01:15 +02:00
|
|
|
setContext(directionKey, directionStore);
|
|
|
|
|
|
|
|
$: $directionStore = field.direction;
|
|
|
|
|
2021-11-05 02:29:02 +01:00
|
|
|
const editingArea: Partial<EditingAreaAPI> = {};
|
|
|
|
const [element, elementResolve] = promiseWithResolver<HTMLElement>();
|
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
let apiPartial: Partial<EditorFieldAPI> & Destroyable;
|
|
|
|
export { apiPartial as api };
|
|
|
|
|
|
|
|
const api: EditorFieldAPI & Destroyable = Object.assign(apiPartial, {
|
2021-11-05 02:29:02 +01:00
|
|
|
element,
|
|
|
|
direction: directionStore,
|
|
|
|
editingArea: editingArea as EditingAreaAPI,
|
|
|
|
});
|
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
setContextProperty(api);
|
2022-06-20 04:07:07 +02:00
|
|
|
setupLifecycleHooks(api);
|
2021-11-05 02:29:02 +01:00
|
|
|
|
|
|
|
onDestroy(() => api?.destroy());
|
2021-10-18 14:01:15 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<div
|
2021-11-05 02:29:02 +01:00
|
|
|
use:elementResolve
|
2021-10-18 14:01:15 +02:00
|
|
|
class="editor-field"
|
|
|
|
on:focusin
|
|
|
|
on:focusout
|
2021-11-05 02:29:02 +01:00
|
|
|
on:click={() => editingArea.focus?.()}
|
2021-10-18 14:01:15 +02:00
|
|
|
>
|
|
|
|
<LabelContainer>
|
2021-11-06 00:42:48 +01:00
|
|
|
<span>
|
|
|
|
<LabelName>
|
|
|
|
{field.name}
|
|
|
|
</LabelName>
|
|
|
|
</span>
|
2021-10-18 14:01:15 +02:00
|
|
|
<FieldState><slot name="field-state" /></FieldState>
|
|
|
|
</LabelContainer>
|
|
|
|
<EditingArea
|
|
|
|
{content}
|
|
|
|
fontFamily={field.fontFamily}
|
|
|
|
fontSize={field.fontSize}
|
2021-11-05 02:29:02 +01:00
|
|
|
api={editingArea}
|
2021-10-18 14:01:15 +02:00
|
|
|
>
|
|
|
|
<slot name="editing-inputs" />
|
|
|
|
</EditingArea>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.editor-field {
|
|
|
|
--border-color: var(--border);
|
|
|
|
|
|
|
|
border-radius: 5px;
|
|
|
|
border: 1px solid var(--border-color);
|
|
|
|
|
|
|
|
&:focus-within {
|
|
|
|
--border-color: var(--focus-border);
|
|
|
|
|
|
|
|
outline: none;
|
|
|
|
box-shadow: 0 0 0 3px var(--focus-shadow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|