diff --git a/ts/editor/codable.ts b/ts/editor/codable.ts index 568ef6aaf..073309031 100644 --- a/ts/editor/codable.ts +++ b/ts/editor/codable.ts @@ -28,24 +28,26 @@ const parser = new DOMParser(); function parseHTML(html: string): string { const doc = parser.parseFromString(html, "text/html"); - return doc.documentElement.innerHTML; + return doc.body.innerHTML; } export class Codable extends HTMLTextAreaElement { codeMirror: CodeMirror | undefined; - active: boolean; - constructor() { - super(); - this.active = false; + get active(): boolean { + return Boolean(this.codeMirror); } set fieldHTML(content: string) { - this.value = content; + if (this.active) { + this.codeMirror.setValue(content); + } else { + this.value = content; + } } get fieldHTML(): string { - return parseHTML(this.codeMirror.getValue()); + return parseHTML(this.active ? this.codeMirror.getValue() : this.value); } connectedCallback(): void { @@ -53,16 +55,14 @@ export class Codable extends HTMLTextAreaElement { } setup(html: string): void { - this.active = true; this.fieldHTML = html; this.codeMirror = CodeMirror.fromTextArea(this, codeMirrorOptions); } teardown(): string { - this.active = false; this.codeMirror.toTextArea(); this.codeMirror = undefined; - return parseHTML(this.value); + return this.fieldHTML; } focus(): void { diff --git a/ts/editor/editingArea.ts b/ts/editor/editingArea.ts index 35ac59fe7..6dbc222cd 100644 --- a/ts/editor/editingArea.ts +++ b/ts/editor/editingArea.ts @@ -92,7 +92,7 @@ export class EditingArea extends HTMLDivElement { initialize(color: string, content: string): void { this.setBaseColor(color); - this.editable.fieldHTML = content; + this.fieldHTML = content; } setBaseColor(color: string): void { @@ -155,7 +155,7 @@ export class EditingArea extends HTMLDivElement { this.editable.hidden = false; } else { this.editable.hidden = true; - this.codable.setup(this.fieldHTML); + this.codable.setup(this.editable.fieldHTML); } if (hadFocus) {