mirror of
https://github.com/ankitects/anki.git
synced 2025-11-19 19:17: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
169 lines
4.8 KiB
Svelte
169 lines
4.8 KiB
Svelte
<!--
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
-->
|
|
<script lang="ts">
|
|
import { onMount, onDestroy, tick } from "svelte";
|
|
import { writable } from "svelte/store";
|
|
import WithDropdown from "../../components/WithDropdown.svelte";
|
|
import { noop } from "../../lib/functional";
|
|
import { on } from "../../lib/events";
|
|
import { Mathjax } from "../../editable/mathjax-element";
|
|
import HandleSelection from "../HandleSelection.svelte";
|
|
import HandleBackground from "../HandleBackground.svelte";
|
|
import HandleControl from "../HandleControl.svelte";
|
|
import { context } from "../rich-text-input";
|
|
import MathjaxMenu from "./MathjaxMenu.svelte";
|
|
|
|
const { container, api } = context.get();
|
|
const { flushCaret, preventResubscription } = api;
|
|
|
|
const code = writable("");
|
|
|
|
let activeImage: HTMLImageElement | null = null;
|
|
let mathjaxElement: HTMLElement | null = null;
|
|
let allow = noop;
|
|
let unsubscribe = noop;
|
|
|
|
function showHandle(image: HTMLImageElement): void {
|
|
allow = preventResubscription();
|
|
|
|
activeImage = image;
|
|
mathjaxElement = activeImage.closest(Mathjax.tagName)!;
|
|
|
|
code.set(mathjaxElement.dataset.mathjax ?? "");
|
|
unsubscribe = code.subscribe((value: string) => {
|
|
mathjaxElement!.dataset.mathjax = value;
|
|
});
|
|
}
|
|
|
|
let selectAll = false;
|
|
|
|
function placeHandle(after: boolean): void {
|
|
flushCaret();
|
|
|
|
if (after) {
|
|
(mathjaxElement as any).placeCaretAfter();
|
|
} else {
|
|
(mathjaxElement as any).placeCaretBefore();
|
|
}
|
|
}
|
|
|
|
async function resetHandle(): Promise<void> {
|
|
selectAll = false;
|
|
|
|
if (activeImage && mathjaxElement) {
|
|
unsubscribe();
|
|
activeImage = null;
|
|
mathjaxElement = null;
|
|
}
|
|
|
|
await tick();
|
|
allow();
|
|
}
|
|
|
|
async function maybeShowHandle({ target }: Event): Promise<void> {
|
|
await resetHandle();
|
|
if (target instanceof HTMLImageElement && target.dataset.anki === "mathjax") {
|
|
showHandle(target);
|
|
}
|
|
}
|
|
|
|
async function showAutofocusHandle({
|
|
detail,
|
|
}: CustomEvent<HTMLImageElement>): Promise<void> {
|
|
await resetHandle();
|
|
showHandle(detail);
|
|
}
|
|
|
|
async function showSelectAll({
|
|
detail,
|
|
}: CustomEvent<HTMLImageElement>): Promise<void> {
|
|
await resetHandle();
|
|
selectAll = true;
|
|
showHandle(detail);
|
|
}
|
|
|
|
onMount(() => {
|
|
const removeClick = on(container, "click", maybeShowHandle);
|
|
const removeCaretAfter = on(
|
|
container,
|
|
"movecaretafter" as any,
|
|
showAutofocusHandle,
|
|
);
|
|
const removeSelectAll = on(container, "selectall" as any, showSelectAll);
|
|
|
|
return () => {
|
|
removeClick();
|
|
removeCaretAfter();
|
|
removeSelectAll();
|
|
};
|
|
});
|
|
|
|
let updateSelection: () => Promise<void>;
|
|
let errorMessage: string;
|
|
let dropdownApi: any;
|
|
|
|
async function onImageResize(): Promise<void> {
|
|
errorMessage = activeImage!.title;
|
|
await updateSelection();
|
|
dropdownApi.update();
|
|
}
|
|
|
|
const resizeObserver = new ResizeObserver(onImageResize);
|
|
|
|
let clearResize = noop;
|
|
function handleImageResizing(activeImage: HTMLImageElement | null) {
|
|
if (activeImage) {
|
|
resizeObserver.observe(container);
|
|
clearResize = on(activeImage, "resize", onImageResize);
|
|
} else {
|
|
resizeObserver.unobserve(container);
|
|
clearResize();
|
|
}
|
|
}
|
|
|
|
$: handleImageResizing(activeImage);
|
|
|
|
onDestroy(() => {
|
|
resizeObserver.disconnect();
|
|
clearResize();
|
|
});
|
|
</script>
|
|
|
|
<WithDropdown
|
|
drop="down"
|
|
autoOpen={true}
|
|
autoClose={false}
|
|
distance={4}
|
|
let:createDropdown
|
|
>
|
|
{#if activeImage && mathjaxElement}
|
|
<MathjaxMenu
|
|
element={mathjaxElement}
|
|
{code}
|
|
{selectAll}
|
|
bind:updateSelection
|
|
on:reset={resetHandle}
|
|
on:moveoutstart={() => {
|
|
placeHandle(false);
|
|
resetHandle();
|
|
}}
|
|
on:moveoutend={() => {
|
|
placeHandle(true);
|
|
resetHandle();
|
|
}}
|
|
>
|
|
<HandleSelection
|
|
image={activeImage}
|
|
{container}
|
|
bind:updateSelection
|
|
on:mount={(event) =>
|
|
(dropdownApi = createDropdown(event.detail.selection))}
|
|
>
|
|
<HandleBackground tooltip={errorMessage} />
|
|
<HandleControl offsetX={1} offsetY={1} />
|
|
</HandleSelection>
|
|
</MathjaxMenu>
|
|
{/if}
|
|
</WithDropdown>
|