mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 09:16:38 -04:00
Make Codable abstract more over its textarea v codemirror when using fieldHTML
This commit is contained in:
parent
96e4e90a61
commit
4252898c78
1 changed files with 10 additions and 10 deletions
|
@ -28,24 +28,26 @@ const parser = new DOMParser();
|
||||||
|
|
||||||
function parseHTML(html: string): string {
|
function parseHTML(html: string): string {
|
||||||
const doc = parser.parseFromString(html, "text/html");
|
const doc = parser.parseFromString(html, "text/html");
|
||||||
return doc.documentElement.innerHTML;
|
return doc.body.innerHTML;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Codable extends HTMLTextAreaElement {
|
export class Codable extends HTMLTextAreaElement {
|
||||||
codeMirror: CodeMirror | undefined;
|
codeMirror: CodeMirror | undefined;
|
||||||
active: boolean;
|
|
||||||
|
|
||||||
constructor() {
|
get active(): boolean {
|
||||||
super();
|
return Boolean(this.codeMirror);
|
||||||
this.active = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
set fieldHTML(content: string) {
|
set fieldHTML(content: string) {
|
||||||
this.value = content;
|
if (this.active) {
|
||||||
|
this.codeMirror.setValue(content);
|
||||||
|
} else {
|
||||||
|
this.value = content;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get fieldHTML(): string {
|
get fieldHTML(): string {
|
||||||
return parseHTML(this.codeMirror.getValue());
|
return parseHTML(this.active ? this.codeMirror.getValue() : this.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback(): void {
|
connectedCallback(): void {
|
||||||
|
@ -53,16 +55,14 @@ export class Codable extends HTMLTextAreaElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
setup(html: string): void {
|
setup(html: string): void {
|
||||||
this.active = true;
|
|
||||||
this.fieldHTML = html;
|
this.fieldHTML = html;
|
||||||
this.codeMirror = CodeMirror.fromTextArea(this, codeMirrorOptions);
|
this.codeMirror = CodeMirror.fromTextArea(this, codeMirrorOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
teardown(): string {
|
teardown(): string {
|
||||||
this.active = false;
|
|
||||||
this.codeMirror.toTextArea();
|
this.codeMirror.toTextArea();
|
||||||
this.codeMirror = undefined;
|
this.codeMirror = undefined;
|
||||||
return parseHTML(this.value);
|
return this.fieldHTML;
|
||||||
}
|
}
|
||||||
|
|
||||||
focus(): void {
|
focus(): void {
|
||||||
|
|
Loading…
Reference in a new issue