Merge pull request #1253 from hgiesel/codablesetfields

Fix Codable carrying on content from other notes in Browser
This commit is contained in:
Damien Elmes 2021-06-25 08:26:42 +10:00 committed by GitHub
commit d82b87e643
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View file

@ -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) {
if (this.active) {
this.codeMirror.setValue(content);
} else {
this.value = content; 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 {

View file

@ -92,7 +92,7 @@ export class EditingArea extends HTMLDivElement {
initialize(color: string, content: string): void { initialize(color: string, content: string): void {
this.setBaseColor(color); this.setBaseColor(color);
this.editable.fieldHTML = content; this.fieldHTML = content;
} }
setBaseColor(color: string): void { setBaseColor(color: string): void {
@ -155,7 +155,7 @@ export class EditingArea extends HTMLDivElement {
this.editable.hidden = false; this.editable.hidden = false;
} else { } else {
this.editable.hidden = true; this.editable.hidden = true;
this.codable.setup(this.fieldHTML); this.codable.setup(this.editable.fieldHTML);
} }
if (hadFocus) { if (hadFocus) {