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",
|
|
|
|
*/
|
|
|
|
|
|
|
|
export class EditableContainer extends HTMLDivElement {
|
|
|
|
baseStyle: HTMLStyleElement;
|
2021-07-21 14:13:56 +02:00
|
|
|
baseRule?: CSSStyleRule;
|
2021-07-28 20:17:31 +02:00
|
|
|
imageStyle?: HTMLStyleElement;
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
const rootStyle = document.createElement("link");
|
|
|
|
rootStyle.setAttribute("rel", "stylesheet");
|
|
|
|
rootStyle.setAttribute("href", "./_anki/css/editable.css");
|
|
|
|
shadow.appendChild(rootStyle);
|
|
|
|
|
|
|
|
this.baseStyle = document.createElement("style");
|
|
|
|
this.baseStyle.setAttribute("rel", "stylesheet");
|
2021-07-28 20:17:31 +02:00
|
|
|
this.baseStyle.id = "baseStyle";
|
2021-07-19 15:27:11 +02:00
|
|
|
shadow.appendChild(this.baseStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|