2021-07-19 15:27:11 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
/* eslint
|
|
|
|
@typescript-eslint/no-non-null-assertion: "off",
|
|
|
|
*/
|
|
|
|
|
2021-08-07 01:02:57 +02:00
|
|
|
function loadStyleLink(container: Node, href: string): Promise<void> {
|
|
|
|
const rootStyle = document.createElement("link");
|
|
|
|
rootStyle.setAttribute("rel", "stylesheet");
|
|
|
|
rootStyle.setAttribute("href", href);
|
|
|
|
|
|
|
|
let styleResolve: () => void;
|
|
|
|
const stylePromise = new Promise<void>((resolve) => (styleResolve = resolve));
|
|
|
|
|
|
|
|
rootStyle.addEventListener("load", () => styleResolve());
|
|
|
|
container.appendChild(rootStyle);
|
|
|
|
|
|
|
|
return stylePromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadStyleTag(container: Node): [HTMLStyleElement, Promise<void>] {
|
|
|
|
const style = document.createElement("style");
|
|
|
|
style.setAttribute("rel", "stylesheet");
|
|
|
|
|
|
|
|
let styleResolve: () => void;
|
|
|
|
const stylePromise = new Promise<void>((resolve) => (styleResolve = resolve));
|
|
|
|
|
|
|
|
style.addEventListener("load", () => styleResolve());
|
|
|
|
container.appendChild(style);
|
|
|
|
|
|
|
|
return [style, stylePromise];
|
|
|
|
}
|
|
|
|
|
2021-07-19 15:27:11 +02:00
|
|
|
export class EditableContainer extends HTMLDivElement {
|
|
|
|
baseStyle: HTMLStyleElement;
|
2021-08-07 01:02:57 +02:00
|
|
|
imageStyle: HTMLStyleElement;
|
|
|
|
|
|
|
|
imagePromise: Promise<void>;
|
|
|
|
stylePromise: Promise<void>;
|
|
|
|
|
2021-07-21 14:13:56 +02:00
|
|
|
baseRule?: CSSStyleRule;
|
2021-07-19 15:27:11 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
const shadow = this.attachShadow({ mode: "open" });
|
|
|
|
|
|
|
|
if (document.documentElement.classList.contains("night-mode")) {
|
|
|
|
this.classList.add("night-mode");
|
|
|
|
}
|
|
|
|
|
2021-08-07 01:02:57 +02:00
|
|
|
const rootPromise = loadStyleLink(shadow, "./_anki/css/editable-build.css");
|
|
|
|
const [baseStyle, basePromise] = loadStyleTag(shadow);
|
|
|
|
const [imageStyle, imagePromise] = loadStyleTag(shadow);
|
|
|
|
|
|
|
|
this.baseStyle = baseStyle;
|
|
|
|
this.imageStyle = imageStyle;
|
2021-07-19 15:27:11 +02:00
|
|
|
|
2021-08-07 01:02:57 +02:00
|
|
|
this.imagePromise = imagePromise;
|
|
|
|
this.stylePromise = Promise.all([
|
|
|
|
rootPromise,
|
|
|
|
basePromise,
|
|
|
|
imagePromise,
|
|
|
|
]) as unknown as Promise<void>;
|
2021-07-19 15:27:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback(): void {
|
2021-07-21 14:13:56 +02:00
|
|
|
const sheet = this.baseStyle.sheet as CSSStyleSheet;
|
|
|
|
const baseIndex = sheet.insertRule("anki-editable {}");
|
|
|
|
this.baseRule = sheet.cssRules[baseIndex] as CSSStyleRule;
|
2021-07-19 15:27:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
initialize(color: string): void {
|
|
|
|
this.setBaseColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
setBaseColor(color: string): void {
|
2021-07-21 14:13:56 +02:00
|
|
|
if (this.baseRule) {
|
|
|
|
this.baseRule.style.color = color;
|
|
|
|
}
|
2021-07-19 15:27:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setBaseStyling(fontFamily: string, fontSize: string, direction: string): void {
|
2021-07-21 14:13:56 +02:00
|
|
|
if (this.baseRule) {
|
|
|
|
this.baseRule.style.fontFamily = fontFamily;
|
|
|
|
this.baseRule.style.fontSize = fontSize;
|
|
|
|
this.baseRule.style.direction = direction;
|
|
|
|
}
|
2021-07-19 15:27:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isRightToLeft(): boolean {
|
2021-07-21 16:48:02 +02:00
|
|
|
return this.baseRule!.style.direction === "rtl";
|
2021-07-19 15:27:11 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-04 00:32:30 +02:00
|
|
|
|
|
|
|
customElements.define("anki-editable-container", EditableContainer, { extends: "div" });
|