Anki/ts/editor/rich-text-input/RichTextStyles.svelte
Henrik Giesel 3969487e77
Join RichTextAPI and RichTextContextAPI + Expose anki/RichTextInput (#1918)
* Format scss correctly so it passes ts:format

* Use on and singleCallback in ImageHandle and MathjaxHandle

* Add a few comments

* Fix relict of partial commit

* Fix 'element not found' in ImageHandle

* Remove setting css on image handle twice

* Remove use of container in ImageHandle

* Remove use of container in MathjaxHandle

* Use unprefixed properties of RichTextInputAPI

* Inline api to get to RichTextInputAPI

* Join customStyles into RichTextInputAPI

* Export RichTextInput; Remove SetContext

* Address eslint and svelte_check
2022-06-20 16:11:27 +10:00

63 lines
2.2 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 { getContext } from "svelte";
import type { Readable } from "svelte/store";
import { directionKey, fontFamilyKey, fontSizeKey } from "../../lib/context-keys";
import { promiseWithResolver } from "../../lib/promise";
import type { StyleLinkType, StyleObject } from "./CustomStyles.svelte";
import CustomStyles from "./CustomStyles.svelte";
export let callback: (styles: CustomStyles) => void;
const [userBaseStyle, userBaseResolve] = promiseWithResolver<StyleObject>();
const [userBaseRule, userBaseRuleResolve] = promiseWithResolver<CSSStyleRule>();
const stylesDidLoad: Promise<unknown> = Promise.all([userBaseStyle, userBaseRule]);
userBaseStyle.then((baseStyle: StyleObject) => {
const sheet = baseStyle.element.sheet as CSSStyleSheet;
const baseIndex = sheet.insertRule("anki-editable {}");
userBaseRuleResolve(sheet.cssRules[baseIndex] as CSSStyleRule);
});
export let color: string;
const fontFamily = getContext<Readable<string>>(fontFamilyKey);
const fontSize = getContext<Readable<number>>(fontSizeKey);
const direction = getContext<Readable<"ltr" | "rtl">>(directionKey);
async function setStyling(property: string, value: unknown): Promise<void> {
const rule = await userBaseRule;
rule.style[property] = value;
}
$: setStyling("color", color);
$: setStyling("fontFamily", $fontFamily);
$: setStyling("fontSize", $fontSize + "px");
$: setStyling("direction", $direction);
const styles = [
{
id: "rootStyle",
type: "link" as "link",
href: "./_anki/css/editable.css",
} as StyleLinkType,
];
function attachToShadow(element: Element) {
const customStyles = new CustomStyles({
target: element.shadowRoot as any,
props: { styles },
});
customStyles.addStyleTag("userBase").then((styleTag) => {
userBaseResolve(styleTag);
callback(customStyles);
});
}
</script>
<slot {attachToShadow} {stylesDidLoad} />