anki/ts/editor/rich-text-input/RichTextStyles.svelte
Henrik Giesel c1e54e6842
Fix field description (#1923)
* Remove most of the original description placeholder implementation

* Move description showing logic to RichTextInput

- there is no need to propagate it to ContentEditable

* Remove the @html from field-description

This actually worked - however I removed it in case we'd rather offer
markdown support or something else in the future.

* Do not remove placeholder already on focus

- Other editors do not do it either

* Hide via hidden attribute instead of unmounting

* Do not pass content to ContentEditable

* Sort imports

* Change placeholder text color (dae)

In day mode, slightly-grey is almost indistinguishable from black
(at least on the monitor I'm using here)
2022-06-22 09:53:10 +10:00

60 lines
1.9 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 { 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;
export let fontFamily: string;
export let fontSize: number;
export let direction: "ltr" | "rtl";
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} />