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 { Writable } from "svelte/store";
|
2022-02-04 09:36:34 +01:00
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
import contextProperty from "../sveltelib/context-property";
|
|
|
|
|
2022-05-13 05:02:03 +02:00
|
|
|
export interface FocusableInputAPI {
|
2021-10-18 14:01:15 +02:00
|
|
|
readonly name: string;
|
2022-02-05 06:58:31 +01:00
|
|
|
focusable: boolean;
|
|
|
|
/**
|
|
|
|
* The reaction to a user-initiated focus, e.g. by clicking on the
|
|
|
|
* editor label, or pressing Tab.
|
|
|
|
*/
|
2021-10-18 14:01:15 +02:00
|
|
|
focus(): void;
|
2022-02-05 06:58:31 +01:00
|
|
|
/**
|
|
|
|
* Behaves similar to a refresh, e.g. sync with content, put the caret
|
|
|
|
* into a neutral position, and/or clear selections.
|
|
|
|
*/
|
2021-10-18 14:01:15 +02:00
|
|
|
refocus(): void;
|
|
|
|
}
|
|
|
|
|
2022-05-13 05:02:03 +02:00
|
|
|
export interface EditingInputAPI extends FocusableInputAPI {
|
|
|
|
/**
|
|
|
|
* Check whether blurred target belongs to an editing input.
|
|
|
|
* The editing area can then restore focus to this input.
|
|
|
|
*
|
|
|
|
* @returns An editing input api that is associated with the event target.
|
|
|
|
*/
|
|
|
|
getInputAPI(target: EventTarget): Promise<FocusableInputAPI | null>;
|
|
|
|
}
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
export interface EditingAreaAPI {
|
|
|
|
content: Writable<string>;
|
|
|
|
editingInputs: Writable<EditingInputAPI[]>;
|
|
|
|
focus(): void;
|
|
|
|
refocus(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const key = Symbol("editingArea");
|
2022-02-03 05:52:11 +01:00
|
|
|
const [context, setContextProperty] = contextProperty<EditingAreaAPI>(key);
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
export { context };
|
2021-10-18 14:01:15 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2022-10-27 01:11:36 +02:00
|
|
|
import { setContext as svelteSetContext } from "svelte";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { writable } from "svelte/store";
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
import { fontFamilyKey, fontSizeKey } from "../lib/context-keys";
|
|
|
|
|
|
|
|
export let fontFamily: string;
|
|
|
|
const fontFamilyStore = writable(fontFamily);
|
|
|
|
$: $fontFamilyStore = fontFamily;
|
|
|
|
svelteSetContext(fontFamilyKey, fontFamilyStore);
|
|
|
|
|
|
|
|
export let fontSize: number;
|
|
|
|
const fontSizeStore = writable(fontSize);
|
|
|
|
$: $fontSizeStore = fontSize;
|
|
|
|
svelteSetContext(fontSizeKey, fontSizeStore);
|
|
|
|
|
|
|
|
export let content: Writable<string>;
|
|
|
|
|
|
|
|
let editingArea: HTMLElement;
|
|
|
|
|
|
|
|
const inputsStore = writable<EditingInputAPI[]>([]);
|
|
|
|
$: editingInputs = $inputsStore;
|
|
|
|
|
|
|
|
function getAvailableInput(): EditingInputAPI | undefined {
|
|
|
|
return editingInputs.find((input) => input.focusable);
|
|
|
|
}
|
|
|
|
|
|
|
|
function focus(): void {
|
2022-10-27 01:11:36 +02:00
|
|
|
editingArea.contains(document.activeElement);
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function refocus(): void {
|
|
|
|
const availableInput = getAvailableInput();
|
|
|
|
|
|
|
|
if (availableInput) {
|
|
|
|
availableInput.refocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
let apiPartial: Partial<EditingAreaAPI>;
|
|
|
|
export { apiPartial as api };
|
|
|
|
|
|
|
|
const api = Object.assign(apiPartial, {
|
|
|
|
content,
|
|
|
|
editingInputs: inputsStore,
|
|
|
|
focus,
|
|
|
|
refocus,
|
|
|
|
});
|
|
|
|
|
|
|
|
setContextProperty(api);
|
2021-10-18 14:01:15 +02:00
|
|
|
</script>
|
|
|
|
|
2022-10-27 01:11:36 +02:00
|
|
|
<div bind:this={editingArea} class="editing-area">
|
2021-10-18 14:01:15 +02:00
|
|
|
<slot />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.editing-area {
|
2022-01-08 02:46:01 +01:00
|
|
|
display: grid;
|
|
|
|
/* TODO allow configuration of grid #1503 */
|
|
|
|
/* grid-template-columns: repeat(2, 1fr); */
|
|
|
|
|
2022-10-27 01:11:36 +02:00
|
|
|
/* This defines the border between inputs */
|
|
|
|
grid-gap: 1px;
|
|
|
|
background-color: var(--border);
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
</style>
|