mirror of
https://github.com/ankitects/anki.git
synced 2025-11-08 21:57:12 -05:00
* Add componentHook functionality
* Register package NoteEditor
* Rename OldEditorAdapter to NoteEditor
* Expose instances in component-hook as well
* Rename NoteTypeButtons to NotetypeButtons
* Move PreviewButton initialization to BrowserEditor.svelte
* Remove focusInRichText
- Same thing can be done by inspecting activeInput
* Satisfy formatter
* Fix remaining rebase issues
* Add .bazel to .prettierignore
* Rename currentField and activeInput to focused{Field,Input}
* Move identifier to lib and registration to sveltelib
* Fix Dynamic component insertion
* Simplify editingInputIsRichText
* Give extra warning in svelte/svelte.ts
- This was caused by doing a rename of a files, that only differed in
case: NoteTypeButtons.svelte to NotetypeButtons.svelte
- It was quite tough to figure out, and this console.log might make it
easier if it ever happens again
* Change signature of contextProperty
* Add ts/typings for add-on definition files
* Add Anki types in typings/common/index.d.ts
* Export without .svelte suffix
It conflicts with how Svelte types its packages
* Fix left over .svelte import from editor.py
* Rename NoteTypeButtons to unrelated to ensure case-only rename
* Rename back to NotetypeButtons.svelte
* Remove unused component-hook.ts, Fix typing in lifecycle-hooks
* Merge runtime-require and register-package into one file
+ Give some preliminary types to require
* Rename uiDidLoad to loaded
* Fix eslint / svelte-check
* Rename context imports to noteEditorContext
* Fix import name mismatch
- I wonder why these issues are not caught by svelte-check?
* Rename two missed usages of uiDidLoad
* Fix ButtonDropdown from having wrong border-radius
* Uniformly rename libraries to packages
- I don't have a strong opinion on whether to name them libraries or
packages, I just think we should have a uniform name.
- JS/TS only uses the terms "module" and "namespace", however `package`
is a reserved keyword for future use, whereas `library` is not.
* Refactor registration.ts into dynamic-slotting
- This is part of an effort to refactor the dynamic slotting (extending
buttons) functionality out of components like ButtonGroup.
* Remove dynamically-slottable logic from ButtonToolbar
* Use DynamicallySlottable in editor-toolbar
* Fix no border radius on indentation button dropdown
* Fix AddonButtons
* Remove Item/ButtonGroupItem in deck-options, where it's not necessary
* Remove unnecessary uses of Item and ButtonGroupItem
* Fix remaining tests
* Fix relative imports
* Revert change return value of remapBinToSrcDir to ./bazel/out...
* Remove typings directory
* Adjust comments for dynamic-slottings
159 lines
4.2 KiB
Svelte
159 lines
4.2 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 { Writable } from "svelte/store";
|
|
import contextProperty from "../sveltelib/context-property";
|
|
|
|
export interface EditingInputAPI {
|
|
readonly name: string;
|
|
focus(): void;
|
|
refocus(): void;
|
|
focusable: boolean;
|
|
moveCaretToEnd(): void;
|
|
}
|
|
|
|
export interface EditingAreaAPI {
|
|
content: Writable<string>;
|
|
editingInputs: Writable<EditingInputAPI[]>;
|
|
focus(): void;
|
|
refocus(): void;
|
|
}
|
|
|
|
const key = Symbol("editingArea");
|
|
const [context, setContextProperty] = contextProperty<EditingAreaAPI>(key);
|
|
|
|
export { context };
|
|
</script>
|
|
|
|
<script lang="ts">
|
|
import FocusTrap from "./FocusTrap.svelte";
|
|
import { writable } from "svelte/store";
|
|
import { onMount, setContext as svelteSetContext } from "svelte";
|
|
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>;
|
|
export let autofocus = false;
|
|
|
|
let editingArea: HTMLElement;
|
|
let focusTrap: FocusTrap;
|
|
|
|
const inputsStore = writable<EditingInputAPI[]>([]);
|
|
$: editingInputs = $inputsStore;
|
|
|
|
function getAvailableInput(): EditingInputAPI | undefined {
|
|
return editingInputs.find((input) => input.focusable);
|
|
}
|
|
|
|
function focusEditingInputIfAvailable(): boolean {
|
|
const availableInput = getAvailableInput();
|
|
|
|
if (availableInput) {
|
|
availableInput.focus();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function focusEditingInputIfFocusTrapFocused(): void {
|
|
if (focusTrap && focusTrap.isFocusTrap(document.activeElement!)) {
|
|
focusEditingInputIfAvailable();
|
|
}
|
|
}
|
|
|
|
$: {
|
|
$inputsStore;
|
|
/**
|
|
* Triggers when all editing inputs are hidden,
|
|
* the editor field has focus, and then some
|
|
* editing input is shown
|
|
*/
|
|
focusEditingInputIfFocusTrapFocused();
|
|
}
|
|
|
|
function focus(): void {
|
|
if (editingArea.contains(document.activeElement)) {
|
|
// do nothing
|
|
} else if (!focusEditingInputIfAvailable()) {
|
|
focusTrap.focus();
|
|
}
|
|
}
|
|
|
|
function refocus(): void {
|
|
const availableInput = getAvailableInput();
|
|
|
|
if (availableInput) {
|
|
availableInput.refocus();
|
|
} else {
|
|
focusTrap.blur();
|
|
focusTrap.focus();
|
|
}
|
|
}
|
|
|
|
function focusEditingInputInsteadIfAvailable(event: FocusEvent): void {
|
|
if (focusEditingInputIfAvailable()) {
|
|
event.preventDefault();
|
|
}
|
|
}
|
|
|
|
// prevents editor field being entirely deselected when
|
|
// closing active field
|
|
function trapFocusOnBlurOut(event: FocusEvent): void {
|
|
if (!event.relatedTarget && editingInputs.every((input) => !input.focusable)) {
|
|
focusTrap.focus();
|
|
event.preventDefault();
|
|
}
|
|
}
|
|
|
|
let apiPartial: Partial<EditingAreaAPI>;
|
|
export { apiPartial as api };
|
|
|
|
const api = Object.assign(apiPartial, {
|
|
content,
|
|
editingInputs: inputsStore,
|
|
focus,
|
|
refocus,
|
|
});
|
|
|
|
setContextProperty(api);
|
|
|
|
onMount(() => {
|
|
if (autofocus) {
|
|
focus();
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<FocusTrap bind:this={focusTrap} on:focus={focusEditingInputInsteadIfAvailable} />
|
|
|
|
<div bind:this={editingArea} class="editing-area" on:focusout={trapFocusOnBlurOut}>
|
|
<slot />
|
|
</div>
|
|
|
|
<style lang="scss">
|
|
.editing-area {
|
|
display: grid;
|
|
/* TODO allow configuration of grid #1503 */
|
|
/* grid-template-columns: repeat(2, 1fr); */
|
|
|
|
position: relative;
|
|
background: var(--frame-bg);
|
|
border-radius: 0 0 5px 5px;
|
|
|
|
&:focus {
|
|
outline: none;
|
|
}
|
|
}
|
|
</style>
|