3642dc6245
* Allow passing in reference into WithFloating as prop * Fix WithAutocomplete * Fix WithFloating for MathjaxOverlay * Add resize-store * Allow passing debug=True to jest_test for debugger support (#2013) * Disable auto-closing of HTML tags https://forums.ankiweb.net/t/set-html-editor-as-a-default-editor-instead-of-visual-editor/20988/3 Closes #1963 * Add slight margin to MathjaxEditor * Enable passing offset and shift to WithFloating * Hide overflow of mathjax editor * Add automatic hide functionality to sveltelib/position * Last polishes for Surrounder class (#2017) * Make private properties in Surrounder truly private * Fix remove logic of Surrounder * No reason for toggleTriggerRemove to be async * Allow using alt-shift to set all remove formats but this one * modifyFormat => updateFormat * Fix formatting * Fix field descriptions blocking cursor from being set (#2018) - happens when focus is in HTML editor * Remove hiding functionality again until it's really useful * Add support for autoPlacement * Implement new WithFloating that supports manually calling position() * Implement hide mechanisms * Add option in math dropdown to toggle MathJax rendering (#2014) * Add option in math dropdown to toggle MathJax rendering Closes #1942 * Hackily redraw the page when toggling MathJax * Add Fluent string * Default input setting in fields dialog (#1987) (kleinerpirat) * Introduce field setting to use plain text editor by default (kleinerpirat) * Remove leftover function from #1476 * Use boolean instead of string * Simplify clear_other_field_duplicates * Convert plain text key to camelCase * Move HTML item below the existing checkbox, instead of to the right (dae) Showing it on the right is more space efficient, but feels a bit cluttered IMHO. * Fix not being able to scroll when mouse hovers PlainTextInput (#2019) * Remove overscroll-behavior: none for * (all elements) * Revert "Remove overscroll-behavior: none for * (all elements)" This reverts commit 189358908cecd03027e19d8fe47822735319ec17. * Use body instead of *, but keep CSS rule * Unify two CSS rules * Remove console.logs * Reposition mathjax menu on switching between inline/block * Implement WithOverlay * Implement FloatingArrow * Display overlay with padding and brighter background * Rename to MathjaxOverlay * Simplify MathjaxOverlay component overall * Rename ImageHandle to image overlay * Generally fix ImageOverlay again * Increase z-index of StickyContainer * Fix setting block or inline on mathjax * Add reasons in closing-{click,keyup} * Have both WithFloating and WithOverlay use a simple show flag instead of a store * Remove subscribe-trigger * Fix clicking from one mathjax element to another * Check before executing cleanup * Do not wait for elements to mount before slotting in With{Floating,Overlay} * Allow using reference slot for WithFloating and WithOveray * Add inline argument to options * Add support for inline slot in WithOvelay * Use WithFloating for RemoveFormatButton * Remove last uses of DropdownMenu and WithDropdown * Remove all of the bootstrap dropdown components * Fix closing behavior of several buttons and ImageOverlay * Increase popover padding to 6px * Find a different way to create some padding at the bottom of the fields ...before the tag editor @kleinerpirat I think is what this css what trying to achieve? * Satisfy tests * Use removeStyleProperties in ImageOverlay * Use notify function in WithOverlay and WithFloating * Do not use portal for WithFloating and WithOverlay Allows for scrolling * Set hidden to default false in Rich/Plain TextInput * Reset handle when changing mathjax elements via click * Restrict size of empty mathjax image * Prevent sticky labels from obscuring menus * Remove several overflow-hidden * Fix empty string being falsy bug when editing mathjax * Do not import portal anymore * Use { reason, originalEvent } instead of symbol as update to modified event store * Fix closing behavior of image overlay (do not close after resize) * Simplify Collapsible * Use removeStyleProperties in Collapsible * Satisfy eslint * Fix latex shortcuts being mounted * Fix mathjax overlay not focusable in first field * Neither hide image overlay on escaped * Fix Block ButtonDropdown wrapping * Bring back portal to fix tag editor
250 lines
8.1 KiB
Svelte
250 lines
8.1 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 { writable } from "svelte/store";
|
|
|
|
import type { ContentEditableAPI } from "../../editable/ContentEditable.svelte";
|
|
import type { InputHandlerAPI } from "../../sveltelib/input-handler";
|
|
import type { EditingInputAPI, FocusableInputAPI } from "../EditingArea.svelte";
|
|
import type { SurroundedAPI } from "../surround";
|
|
import type CustomStyles from "./CustomStyles.svelte";
|
|
|
|
export interface RichTextInputAPI extends EditingInputAPI, SurroundedAPI {
|
|
name: "rich-text";
|
|
/** This is the contentEditable anki-editable element */
|
|
element: Promise<HTMLElement>;
|
|
moveCaretToEnd(): void;
|
|
toggle(): boolean;
|
|
preventResubscription(): () => void;
|
|
inputHandler: InputHandlerAPI;
|
|
/** The API exposed by the editable component */
|
|
editable: ContentEditableAPI;
|
|
customStyles: Promise<CustomStyles>;
|
|
}
|
|
|
|
function editingInputIsRichText(
|
|
editingInput: EditingInputAPI | null,
|
|
): editingInput is RichTextInputAPI {
|
|
return editingInput?.name === "rich-text";
|
|
}
|
|
|
|
import { registerPackage } from "../../lib/runtime-require";
|
|
import contextProperty from "../../sveltelib/context-property";
|
|
import lifecycleHooks from "../../sveltelib/lifecycle-hooks";
|
|
import { Surrounder } from "../surround";
|
|
|
|
const key = Symbol("richText");
|
|
const [context, setContextProperty] = contextProperty<RichTextInputAPI>(key);
|
|
const [globalInputHandler, setupGlobalInputHandler] = useInputHandler();
|
|
const [lifecycle, instances, setupLifecycleHooks] =
|
|
lifecycleHooks<RichTextInputAPI>();
|
|
const apiStore = writable<SurroundedAPI | null>(null);
|
|
const surrounder = Surrounder.make(apiStore);
|
|
|
|
registerPackage("anki/RichTextInput", {
|
|
context,
|
|
surrounder,
|
|
lifecycle,
|
|
instances,
|
|
});
|
|
|
|
export {
|
|
context,
|
|
editingInputIsRichText,
|
|
globalInputHandler as inputHandler,
|
|
surrounder,
|
|
};
|
|
</script>
|
|
|
|
<script lang="ts">
|
|
import { getAllContexts, getContext, onMount } from "svelte";
|
|
import type { Readable } from "svelte/store";
|
|
|
|
import { placeCaretAfterContent } from "../../domlib/place-caret";
|
|
import ContentEditable from "../../editable/ContentEditable.svelte";
|
|
import { directionKey, fontFamilyKey, fontSizeKey } from "../../lib/context-keys";
|
|
import { promiseWithResolver } from "../../lib/promise";
|
|
import { singleCallback } from "../../lib/typing";
|
|
import useDOMMirror from "../../sveltelib/dom-mirror";
|
|
import useInputHandler from "../../sveltelib/input-handler";
|
|
import { pageTheme } from "../../sveltelib/theme";
|
|
import { context as editingAreaContext } from "../EditingArea.svelte";
|
|
import { context as noteEditorContext } from "../NoteEditor.svelte";
|
|
import getNormalizingNodeStore from "./normalizing-node-store";
|
|
import useRichTextResolve from "./rich-text-resolve";
|
|
import RichTextStyles from "./RichTextStyles.svelte";
|
|
import { fragmentToStored, storedToFragment } from "./transform";
|
|
|
|
export let hidden = false;
|
|
|
|
const { focusedInput } = noteEditorContext.get();
|
|
const { content, editingInputs } = editingAreaContext.get();
|
|
|
|
const fontFamily = getContext<Readable<string>>(fontFamilyKey);
|
|
const fontSize = getContext<Readable<number>>(fontSizeKey);
|
|
const direction = getContext<Readable<"ltr" | "rtl">>(directionKey);
|
|
|
|
const nodes = getNormalizingNodeStore();
|
|
const [richTextPromise, resolve] = useRichTextResolve();
|
|
const { mirror, preventResubscription } = useDOMMirror();
|
|
const [inputHandler, setupInputHandler] = useInputHandler();
|
|
const [customStyles, stylesResolve] = promiseWithResolver<CustomStyles>();
|
|
|
|
export function attachShadow(element: Element): void {
|
|
element.attachShadow({ mode: "open" });
|
|
}
|
|
|
|
async function moveCaretToEnd(): Promise<void> {
|
|
const richText = await richTextPromise;
|
|
if (richText.textContent?.length === 0) {
|
|
// Calling this method when richText is empty will cause the first keystroke of
|
|
// ibus-based input methods with candidates to go double. For example, if you
|
|
// type "a" it becomes "aa". This problem exists in many linux distributions.
|
|
// When richText is empty, there is no need to place the caret, just return.
|
|
return;
|
|
}
|
|
|
|
placeCaretAfterContent(richText);
|
|
}
|
|
|
|
async function focus(): Promise<void> {
|
|
const richText = await richTextPromise;
|
|
richText.focus();
|
|
}
|
|
|
|
async function refocus(): Promise<void> {
|
|
const richText = await richTextPromise;
|
|
richText.blur();
|
|
richText.focus();
|
|
moveCaretToEnd();
|
|
}
|
|
|
|
function toggle(): boolean {
|
|
hidden = !hidden;
|
|
return hidden;
|
|
}
|
|
|
|
const className = "rich-text-editable";
|
|
let richTextDiv: HTMLElement;
|
|
|
|
async function getInputAPI(target: EventTarget): Promise<FocusableInputAPI | null> {
|
|
if (target === richTextDiv) {
|
|
return api;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export const api: RichTextInputAPI = {
|
|
name: "rich-text",
|
|
element: richTextPromise,
|
|
focus,
|
|
refocus,
|
|
focusable: !hidden,
|
|
toggle,
|
|
getInputAPI,
|
|
moveCaretToEnd,
|
|
preventResubscription,
|
|
inputHandler,
|
|
editable: {} as ContentEditableAPI,
|
|
customStyles,
|
|
};
|
|
|
|
const allContexts = getAllContexts();
|
|
|
|
function attachContentEditable(element: Element, { stylesDidLoad }): void {
|
|
(async () => {
|
|
await stylesDidLoad;
|
|
|
|
new ContentEditable({
|
|
target: element.shadowRoot!,
|
|
props: {
|
|
nodes,
|
|
resolve,
|
|
mirrors: [mirror],
|
|
inputHandlers: [setupInputHandler, setupGlobalInputHandler],
|
|
api: api.editable,
|
|
},
|
|
context: allContexts,
|
|
});
|
|
})();
|
|
}
|
|
|
|
function pushUpdate(isFocusable: boolean): void {
|
|
api.focusable = isFocusable;
|
|
$editingInputs = $editingInputs;
|
|
}
|
|
|
|
function setFocus(): void {
|
|
$focusedInput = api;
|
|
$apiStore = api;
|
|
}
|
|
|
|
function removeFocus(): void {
|
|
// We do not unset focusedInput here.
|
|
// If we did, UI components for the input would react the store
|
|
// being unset, even though most likely it will be set to some other
|
|
// field right away.
|
|
|
|
$apiStore = null;
|
|
}
|
|
|
|
$: pushUpdate(!hidden);
|
|
|
|
onMount(() => {
|
|
$editingInputs.push(api);
|
|
$editingInputs = $editingInputs;
|
|
|
|
return singleCallback(
|
|
content.subscribe((html: string): void =>
|
|
nodes.setUnprocessed(storedToFragment(html)),
|
|
),
|
|
nodes.subscribe((fragment: DocumentFragment): void =>
|
|
content.set(fragmentToStored(fragment)),
|
|
),
|
|
);
|
|
});
|
|
|
|
setContextProperty(api);
|
|
setupLifecycleHooks(api);
|
|
</script>
|
|
|
|
<div class="rich-text-input" on:focusin={setFocus} on:focusout={removeFocus} {hidden}>
|
|
<RichTextStyles
|
|
color={$pageTheme.isDark ? "white" : "black"}
|
|
fontFamily={$fontFamily}
|
|
fontSize={$fontSize}
|
|
direction={$direction}
|
|
callback={stylesResolve}
|
|
let:attachToShadow={attachStyles}
|
|
let:stylesDidLoad
|
|
>
|
|
<div
|
|
bind:this={richTextDiv}
|
|
class={className}
|
|
class:night-mode={$pageTheme.isDark}
|
|
use:attachShadow
|
|
use:attachStyles
|
|
use:attachContentEditable={{ stylesDidLoad }}
|
|
on:focusin
|
|
on:focusout
|
|
/>
|
|
|
|
{#await Promise.all([richTextPromise, stylesDidLoad]) then _}
|
|
<div class="rich-text-widgets">
|
|
<slot />
|
|
</div>
|
|
{/await}
|
|
</RichTextStyles>
|
|
<slot name="plain-text-badge" />
|
|
</div>
|
|
|
|
<style lang="scss">
|
|
.rich-text-input {
|
|
position: relative;
|
|
margin: 6px;
|
|
}
|
|
</style>
|