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">
|
|
|
|
import type { EditingAreaAPI } from "./EditingArea.svelte";
|
|
|
|
import contextProperty from "../sveltelib/context-property";
|
2021-11-05 02:29:02 +01:00
|
|
|
import type { Readable } from "svelte/store";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
export interface FieldData {
|
|
|
|
name: string;
|
|
|
|
fontFamily: string;
|
|
|
|
fontSize: number;
|
|
|
|
direction: "ltr" | "rtl";
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const key = Symbol("editorField");
|
|
|
|
const [set, getEditorField, hasEditorField] = contextProperty<EditorFieldAPI>(key);
|
|
|
|
|
|
|
|
export { getEditorField, hasEditorField };
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import EditingArea from "./EditingArea.svelte";
|
|
|
|
import LabelContainer from "./LabelContainer.svelte";
|
|
|
|
import LabelName from "./LabelName.svelte";
|
|
|
|
import FieldState from "./FieldState.svelte";
|
|
|
|
|
2021-11-05 02:29:02 +01:00
|
|
|
import { onDestroy, setContext } from "svelte";
|
2021-10-18 14:01:15 +02:00
|
|
|
import { writable } from "svelte/store";
|
|
|
|
import type { Writable } from "svelte/store";
|
|
|
|
import { directionKey } from "../lib/context-keys";
|
2021-11-05 02:29:02 +01:00
|
|
|
import { promiseWithResolver } from "../lib/promise";
|
|
|
|
import type { Destroyable } from "./destroyable";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
export let content: Writable<string>;
|
|
|
|
export let field: FieldData;
|
|
|
|
export let autofocus = false;
|
|
|
|
|
2021-11-05 02:29:02 +01:00
|
|
|
export let api: (Partial<EditorFieldAPI> & Destroyable) | undefined = undefined;
|
|
|
|
|
|
|
|
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>();
|
|
|
|
|
|
|
|
const editorFieldApi = set({
|
|
|
|
element,
|
|
|
|
direction: directionStore,
|
|
|
|
editingArea: editingArea as EditingAreaAPI,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (api) {
|
|
|
|
Object.assign(api, editorFieldApi);
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
<LabelName>{field.name}</LabelName>
|
|
|
|
<FieldState><slot name="field-state" /></FieldState>
|
|
|
|
</LabelContainer>
|
|
|
|
<EditingArea
|
|
|
|
{content}
|
|
|
|
{autofocus}
|
|
|
|
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);
|
|
|
|
|
|
|
|
min-width: 0;
|
|
|
|
|
|
|
|
&:focus-within {
|
|
|
|
--border-color: var(--focus-border);
|
|
|
|
|
|
|
|
outline: none;
|
|
|
|
box-shadow: 0 0 0 3px var(--focus-shadow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|