mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 01:06:35 -04:00
Make Codable correctly update the editable state and its button
This commit is contained in:
parent
7f76a98546
commit
4daede2995
3 changed files with 29 additions and 11 deletions
|
@ -18,7 +18,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
import { wrap } from "./wrap";
|
import { wrap } from "./wrap";
|
||||||
import { appendInParentheses } from "./helpers";
|
import { appendInParentheses } from "./helpers";
|
||||||
import { forEditorField } from ".";
|
import { forEditorField, getEditorField } from ".";
|
||||||
import { paperclipIcon, micIcon, functionIcon, xmlIcon } from "./icons";
|
import { paperclipIcon, micIcon, functionIcon, xmlIcon } from "./icons";
|
||||||
|
|
||||||
export let api = {};
|
export let api = {};
|
||||||
|
@ -31,6 +31,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
bridgeCommand("record");
|
bridgeCommand("record");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkHtmlEdit() {
|
||||||
|
return getEditorField(0)!.editingArea.codable.active;
|
||||||
|
}
|
||||||
|
|
||||||
function onHtmlEdit() {
|
function onHtmlEdit() {
|
||||||
forEditorField([], (field) => {
|
forEditorField([], (field) => {
|
||||||
field.editingArea.toggleHtmlEdit();
|
field.editingArea.toggleHtmlEdit();
|
||||||
|
@ -170,10 +174,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
<WithShortcut shortcut={"Control+Shift+X"} let:createShortcut let:shortcutLabel>
|
<WithShortcut shortcut={"Control+Shift+X"} let:createShortcut let:shortcutLabel>
|
||||||
<WithState
|
<WithState
|
||||||
key="htmledit"
|
key="htmledit"
|
||||||
update={(event) => {
|
update={checkHtmlEdit}
|
||||||
console.log(event);
|
|
||||||
return true;
|
|
||||||
}}
|
|
||||||
let:state={active}
|
let:state={active}
|
||||||
let:updateState
|
let:updateState
|
||||||
>
|
>
|
||||||
|
@ -186,6 +187,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
updateState(event);
|
updateState(event);
|
||||||
}}
|
}}
|
||||||
on:mount={createShortcut}
|
on:mount={createShortcut}
|
||||||
|
disables={false}
|
||||||
>
|
>
|
||||||
{@html xmlIcon}
|
{@html xmlIcon}
|
||||||
</IconButton>
|
</IconButton>
|
||||||
|
|
|
@ -10,8 +10,16 @@ const codeMirrorOptions = {
|
||||||
theme: "monokai",
|
theme: "monokai",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const parser = new DOMParser();
|
||||||
|
|
||||||
export class Codable extends HTMLTextAreaElement {
|
export class Codable extends HTMLTextAreaElement {
|
||||||
codeMirror: any;
|
codeMirror: CodeMirror | undefined;
|
||||||
|
active: boolean;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.active = false;
|
||||||
|
}
|
||||||
|
|
||||||
connectedCallback(): void {
|
connectedCallback(): void {
|
||||||
this.setAttribute("hidden", "");
|
this.setAttribute("hidden", "");
|
||||||
|
@ -22,15 +30,18 @@ export class Codable extends HTMLTextAreaElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
setup(html: string): string {
|
setup(html: string): string {
|
||||||
this.innerHTML = html;
|
this.active = true;
|
||||||
|
this.value = html;
|
||||||
this.codeMirror = CodeMirror.fromTextArea(this, codeMirrorOptions);
|
this.codeMirror = CodeMirror.fromTextArea(this, codeMirrorOptions);
|
||||||
return html;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
teardown(): string {
|
teardown(): string {
|
||||||
|
this.active = false;
|
||||||
this.codeMirror.toTextArea();
|
this.codeMirror.toTextArea();
|
||||||
this.codeMirror = undefined;
|
this.codeMirror = undefined;
|
||||||
console.log(this.innerHTML);
|
|
||||||
return this.innerHTML;
|
const doc = parser.parseFromString(this.value, "text/html");
|
||||||
|
return doc.documentElement.textContent!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,6 +127,11 @@ export class EditingArea extends HTMLDivElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleHtmlEdit(): void {
|
toggleHtmlEdit(): void {
|
||||||
this.fieldHTML = this.codable.toggle(this.fieldHTML);
|
const output = this.codable.toggle(this.fieldHTML);
|
||||||
|
console.log("succ output", output, this.ord);
|
||||||
|
if (output) {
|
||||||
|
console.log("writo");
|
||||||
|
this.fieldHTML = output;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue