2021-08-05 20:54:25 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
/**
|
|
|
|
* decorated elements know three states:
|
|
|
|
* - stored, which is stored in the DB, e.g. `\(\alpha + \beta\)`
|
|
|
|
* - undecorated, which is displayed to the user in Codable, e.g. `<anki-mathjax>\alpha + \beta</anki-mathjax>`
|
|
|
|
* - decorated, which is displayed to the user in Editable, e.g. `<anki-mathjax data-mathjax="\alpha + \beta"><img src="data:..."></anki-mathjax>`
|
|
|
|
*/
|
|
|
|
|
|
|
|
export interface DecoratedElement extends HTMLElement {
|
|
|
|
/**
|
|
|
|
* Transforms itself from undecorated to decorated state.
|
|
|
|
* Should be called in connectedCallback.
|
|
|
|
*/
|
|
|
|
decorate(): void;
|
|
|
|
/**
|
|
|
|
* Transforms itself from decorated to undecorated state.
|
|
|
|
*/
|
|
|
|
undecorate(): void;
|
|
|
|
}
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
interface WithTagName {
|
2021-08-05 20:54:25 +02:00
|
|
|
tagName: string;
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface DecoratedElementConstructor
|
|
|
|
extends CustomElementConstructor,
|
|
|
|
WithTagName {
|
|
|
|
prototype: DecoratedElement;
|
2021-08-05 20:54:25 +02:00
|
|
|
/**
|
|
|
|
* Transforms elements in input HTML from undecorated to stored state.
|
|
|
|
*/
|
|
|
|
toStored(undecorated: string): string;
|
|
|
|
/**
|
|
|
|
* Transforms elements in input HTML from stored to undecorated state.
|
|
|
|
*/
|
|
|
|
toUndecorated(stored: string): string;
|
|
|
|
}
|
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
export class CustomElementArray extends Array<DecoratedElementConstructor> {
|
|
|
|
push(...elements: DecoratedElementConstructor[]): number {
|
2021-08-05 20:54:25 +02:00
|
|
|
for (const element of elements) {
|
|
|
|
customElements.define(element.tagName, element);
|
|
|
|
}
|
|
|
|
return super.push(...elements);
|
|
|
|
}
|
2022-02-25 02:14:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms any decorated elements in input HTML from undecorated to stored state.
|
|
|
|
*/
|
|
|
|
toStored(html: string): string {
|
|
|
|
let result = html;
|
|
|
|
|
|
|
|
for (const element of this) {
|
|
|
|
result = element.toStored(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms any decorated elements in input HTML from stored to undecorated state.
|
|
|
|
*/
|
|
|
|
toUndecorated(html: string): string {
|
|
|
|
let result = html;
|
|
|
|
|
|
|
|
for (const element of this) {
|
|
|
|
result = element.toUndecorated(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2021-08-05 20:54:25 +02:00
|
|
|
}
|