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">
|
2022-08-17 08:00:37 +02:00
|
|
|
import { writable } from "svelte/store";
|
|
|
|
|
2022-01-24 02:43:09 +01:00
|
|
|
import type { ContentEditableAPI } from "../../editable/ContentEditable.svelte";
|
2022-02-25 01:59:06 +01:00
|
|
|
import type { InputHandlerAPI } from "../../sveltelib/input-handler";
|
2022-05-13 05:02:03 +02:00
|
|
|
import type { EditingInputAPI, FocusableInputAPI } from "../EditingArea.svelte";
|
2022-08-15 05:34:16 +02:00
|
|
|
import type { SurroundedAPI } from "../surround";
|
2022-02-04 09:36:34 +01:00
|
|
|
import type CustomStyles from "./CustomStyles.svelte";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2022-08-15 05:34:16 +02:00
|
|
|
export interface RichTextInputAPI extends EditingInputAPI, SurroundedAPI {
|
2021-10-18 14:01:15 +02:00
|
|
|
name: "rich-text";
|
2022-06-20 08:11:27 +02:00
|
|
|
/** This is the contentEditable anki-editable element */
|
2021-11-18 10:18:39 +01:00
|
|
|
element: Promise<HTMLElement>;
|
2021-10-18 14:01:15 +02:00
|
|
|
moveCaretToEnd(): void;
|
|
|
|
toggle(): boolean;
|
|
|
|
preventResubscription(): () => void;
|
2022-02-25 01:59:06 +01:00
|
|
|
inputHandler: InputHandlerAPI;
|
2022-06-20 08:11:27 +02:00
|
|
|
/** The API exposed by the editable component */
|
2022-03-31 03:17:13 +02:00
|
|
|
editable: ContentEditableAPI;
|
2022-06-20 08:11:27 +02:00
|
|
|
customStyles: Promise<CustomStyles>;
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-15 05:34:16 +02:00
|
|
|
function editingInputIsRichText(
|
2022-02-03 05:52:11 +01:00
|
|
|
editingInput: EditingInputAPI | null,
|
|
|
|
): editingInput is RichTextInputAPI {
|
|
|
|
return editingInput?.name === "rich-text";
|
|
|
|
}
|
|
|
|
|
2022-06-20 08:11:27 +02:00
|
|
|
import { registerPackage } from "../../lib/runtime-require";
|
|
|
|
import contextProperty from "../../sveltelib/context-property";
|
|
|
|
import lifecycleHooks from "../../sveltelib/lifecycle-hooks";
|
2022-08-15 05:34:16 +02:00
|
|
|
import { Surrounder } from "../surround";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
const key = Symbol("richText");
|
2022-06-20 08:11:27 +02:00
|
|
|
const [context, setContextProperty] = contextProperty<RichTextInputAPI>(key);
|
2022-02-25 01:59:06 +01:00
|
|
|
const [globalInputHandler, setupGlobalInputHandler] = useInputHandler();
|
2022-06-20 08:11:27 +02:00
|
|
|
const [lifecycle, instances, setupLifecycleHooks] =
|
|
|
|
lifecycleHooks<RichTextInputAPI>();
|
2022-08-17 08:00:37 +02:00
|
|
|
const apiStore = writable<SurroundedAPI | null>(null);
|
|
|
|
const surrounder = Surrounder.make(apiStore);
|
2022-06-20 08:11:27 +02:00
|
|
|
|
|
|
|
registerPackage("anki/RichTextInput", {
|
|
|
|
context,
|
2022-08-15 05:34:16 +02:00
|
|
|
surrounder,
|
2022-06-20 08:11:27 +02:00
|
|
|
lifecycle,
|
|
|
|
instances,
|
|
|
|
});
|
2022-01-08 02:46:01 +01:00
|
|
|
|
2022-08-15 05:34:16 +02:00
|
|
|
export {
|
|
|
|
context,
|
|
|
|
editingInputIsRichText,
|
|
|
|
globalInputHandler as inputHandler,
|
|
|
|
surrounder,
|
|
|
|
};
|
2021-10-18 14:01:15 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2022-06-22 01:53:10 +02:00
|
|
|
import { getAllContexts, getContext, onMount } from "svelte";
|
|
|
|
import type { Readable } from "svelte/store";
|
2022-02-04 09:36:34 +01:00
|
|
|
|
|
|
|
import { placeCaretAfterContent } from "../../domlib/place-caret";
|
|
|
|
import ContentEditable from "../../editable/ContentEditable.svelte";
|
2022-08-15 05:34:16 +02:00
|
|
|
import { directionKey, fontFamilyKey, fontSizeKey } from "../../lib/context-keys";
|
2022-06-20 08:11:27 +02:00
|
|
|
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";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { context as editingAreaContext } from "../EditingArea.svelte";
|
2022-02-25 01:59:06 +01:00
|
|
|
import { context as noteEditorContext } from "../NoteEditor.svelte";
|
2022-03-31 03:17:13 +02:00
|
|
|
import getNormalizingNodeStore from "./normalizing-node-store";
|
|
|
|
import useRichTextResolve from "./rich-text-resolve";
|
2022-01-24 02:43:09 +01:00
|
|
|
import RichTextStyles from "./RichTextStyles.svelte";
|
2022-03-31 03:17:13 +02:00
|
|
|
import { fragmentToStored, storedToFragment } from "./transform";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
export let hidden: boolean;
|
|
|
|
|
2022-02-25 01:59:06 +01:00
|
|
|
const { focusedInput } = noteEditorContext.get();
|
2022-02-03 05:52:11 +01:00
|
|
|
const { content, editingInputs } = editingAreaContext.get();
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2022-06-22 01:53:10 +02:00
|
|
|
const fontFamily = getContext<Readable<string>>(fontFamilyKey);
|
|
|
|
const fontSize = getContext<Readable<number>>(fontSizeKey);
|
|
|
|
const direction = getContext<Readable<"ltr" | "rtl">>(directionKey);
|
|
|
|
|
2022-03-31 03:17:13 +02:00
|
|
|
const nodes = getNormalizingNodeStore();
|
|
|
|
const [richTextPromise, resolve] = useRichTextResolve();
|
|
|
|
const { mirror, preventResubscription } = useDOMMirror();
|
|
|
|
const [inputHandler, setupInputHandler] = useInputHandler();
|
2022-06-20 08:11:27 +02:00
|
|
|
const [customStyles, stylesResolve] = promiseWithResolver<CustomStyles>();
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2022-03-31 03:17:13 +02:00
|
|
|
export function attachShadow(element: Element): void {
|
|
|
|
element.attachShadow({ mode: "open" });
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2022-03-31 03:17:13 +02:00
|
|
|
async function moveCaretToEnd(): Promise<void> {
|
|
|
|
const richText = await richTextPromise;
|
2022-05-26 02:47:55 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-03-31 03:17:13 +02:00
|
|
|
placeCaretAfterContent(richText);
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2022-03-31 03:17:13 +02:00
|
|
|
async function focus(): Promise<void> {
|
|
|
|
const richText = await richTextPromise;
|
|
|
|
richText.focus();
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2022-03-31 03:17:13 +02:00
|
|
|
async function refocus(): Promise<void> {
|
|
|
|
const richText = await richTextPromise;
|
|
|
|
richText.blur();
|
|
|
|
richText.focus();
|
|
|
|
moveCaretToEnd();
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2022-03-31 03:17:13 +02:00
|
|
|
function toggle(): boolean {
|
|
|
|
hidden = !hidden;
|
|
|
|
return hidden;
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 05:02:03 +02:00
|
|
|
const className = "rich-text-editable";
|
|
|
|
let richTextDiv: HTMLElement;
|
|
|
|
|
|
|
|
async function getInputAPI(target: EventTarget): Promise<FocusableInputAPI | null> {
|
|
|
|
if (target === richTextDiv) {
|
|
|
|
return api;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-31 03:17:13 +02:00
|
|
|
export const api: RichTextInputAPI = {
|
2021-10-18 14:01:15 +02:00
|
|
|
name: "rich-text",
|
2021-11-18 10:18:39 +01:00
|
|
|
element: richTextPromise,
|
2022-03-31 03:17:13 +02:00
|
|
|
focus,
|
|
|
|
refocus,
|
2021-10-18 14:01:15 +02:00
|
|
|
focusable: !hidden,
|
2022-03-31 03:17:13 +02:00
|
|
|
toggle,
|
2022-05-13 05:02:03 +02:00
|
|
|
getInputAPI,
|
2021-12-13 05:00:35 +01:00
|
|
|
moveCaretToEnd,
|
2021-10-18 14:01:15 +02:00
|
|
|
preventResubscription,
|
2022-02-25 01:59:06 +01:00
|
|
|
inputHandler,
|
2022-03-31 03:17:13 +02:00
|
|
|
editable: {} as ContentEditableAPI,
|
2022-06-20 08:11:27 +02:00
|
|
|
customStyles,
|
2022-03-31 03:17:13 +02:00
|
|
|
};
|
2022-01-08 02:46:01 +01:00
|
|
|
|
|
|
|
const allContexts = getAllContexts();
|
|
|
|
|
|
|
|
function attachContentEditable(element: Element, { stylesDidLoad }): void {
|
2022-03-31 03:17:13 +02:00
|
|
|
(async () => {
|
|
|
|
await stylesDidLoad;
|
|
|
|
|
|
|
|
new ContentEditable({
|
|
|
|
target: element.shadowRoot!,
|
|
|
|
props: {
|
|
|
|
nodes,
|
|
|
|
resolve,
|
|
|
|
mirrors: [mirror],
|
|
|
|
inputHandlers: [setupInputHandler, setupGlobalInputHandler],
|
|
|
|
api: api.editable,
|
|
|
|
},
|
|
|
|
context: allContexts,
|
|
|
|
});
|
|
|
|
})();
|
2022-01-08 02:46:01 +01:00
|
|
|
}
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2022-03-31 03:17:13 +02:00
|
|
|
function pushUpdate(isFocusable: boolean): void {
|
|
|
|
api.focusable = isFocusable;
|
2021-10-18 14:01:15 +02:00
|
|
|
$editingInputs = $editingInputs;
|
|
|
|
}
|
|
|
|
|
2022-06-22 01:53:10 +02:00
|
|
|
function setFocus(): void {
|
|
|
|
$focusedInput = api;
|
2022-08-17 08:00:37 +02:00
|
|
|
$apiStore = api;
|
|
|
|
}
|
2022-06-22 01:53:10 +02:00
|
|
|
|
2022-08-17 08:00:37 +02:00
|
|
|
function removeFocus(): void {
|
2022-06-22 01:53:10 +02:00
|
|
|
// 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.
|
|
|
|
|
2022-08-17 08:00:37 +02:00
|
|
|
$apiStore = null;
|
2022-08-15 05:34:16 +02:00
|
|
|
}
|
|
|
|
|
2022-03-31 03:17:13 +02:00
|
|
|
$: pushUpdate(!hidden);
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
$editingInputs.push(api);
|
|
|
|
$editingInputs = $editingInputs;
|
|
|
|
|
2022-03-31 03:17:13 +02:00
|
|
|
return singleCallback(
|
|
|
|
content.subscribe((html: string): void =>
|
|
|
|
nodes.setUnprocessed(storedToFragment(html)),
|
|
|
|
),
|
|
|
|
nodes.subscribe((fragment: DocumentFragment): void =>
|
|
|
|
content.set(fragmentToStored(fragment)),
|
|
|
|
),
|
|
|
|
);
|
2021-10-18 14:01:15 +02:00
|
|
|
});
|
2022-06-20 08:11:27 +02:00
|
|
|
|
|
|
|
setContextProperty(api);
|
|
|
|
setupLifecycleHooks(api);
|
2021-10-18 14:01:15 +02:00
|
|
|
</script>
|
|
|
|
|
2022-08-15 05:34:16 +02:00
|
|
|
<div class="rich-text-input" {hidden} on:focusin={setFocus} on:focusout={removeFocus}>
|
2022-01-08 02:46:01 +01:00
|
|
|
<RichTextStyles
|
|
|
|
color={$pageTheme.isDark ? "white" : "black"}
|
2022-06-22 01:53:10 +02:00
|
|
|
fontFamily={$fontFamily}
|
|
|
|
fontSize={$fontSize}
|
|
|
|
direction={$direction}
|
2022-06-20 08:11:27 +02:00
|
|
|
callback={stylesResolve}
|
2022-01-08 02:46:01 +01:00
|
|
|
let:attachToShadow={attachStyles}
|
|
|
|
let:stylesDidLoad
|
|
|
|
>
|
|
|
|
<div
|
2022-05-13 05:02:03 +02:00
|
|
|
bind:this={richTextDiv}
|
|
|
|
class={className}
|
2022-01-08 02:46:01 +01:00
|
|
|
class:night-mode={$pageTheme.isDark}
|
|
|
|
use:attachShadow
|
|
|
|
use:attachStyles
|
|
|
|
use:attachContentEditable={{ stylesDidLoad }}
|
|
|
|
on:focusin
|
|
|
|
on:focusout
|
|
|
|
/>
|
|
|
|
|
2022-06-20 08:11:27 +02:00
|
|
|
{#await Promise.all([richTextPromise, stylesDidLoad]) then _}
|
|
|
|
<div class="rich-text-widgets">
|
|
|
|
<slot />
|
|
|
|
</div>
|
|
|
|
{/await}
|
2022-01-08 02:46:01 +01:00
|
|
|
</RichTextStyles>
|
|
|
|
</div>
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
<style lang="scss">
|
2022-06-22 01:53:10 +02:00
|
|
|
.rich-text-input {
|
|
|
|
position: relative;
|
2022-08-15 05:34:16 +02:00
|
|
|
padding: 6px;
|
2022-06-22 01:53:10 +02:00
|
|
|
}
|
|
|
|
|
2022-08-15 05:34:16 +02:00
|
|
|
.hidden {
|
|
|
|
display: none;
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
</style>
|