anki/ts/editor/editable.ts

66 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-04-13 10:57:08 +02:00
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { bridgeCommand } from "./lib";
import { elementIsBlock, caretToEnd, getBlockElement } from "./helpers";
2021-07-05 18:15:03 +02:00
import { inCodable } from "./toolbar";
import { wrap } from "./wrap";
function containsInlineContent(element: Element): boolean {
for (const child of element.children) {
if (elementIsBlock(child) || !containsInlineContent(child)) {
return false;
}
}
return true;
}
export class Editable extends HTMLElement {
set fieldHTML(content: string) {
this.innerHTML = content;
if (content.length > 0 && containsInlineContent(this)) {
this.appendChild(document.createElement("br"));
}
}
get fieldHTML(): string {
return containsInlineContent(this) && this.innerHTML.endsWith("<br>")
? this.innerHTML.slice(0, -4) // trim trailing <br>
: this.innerHTML;
}
2021-05-06 23:33:28 +02:00
connectedCallback(): void {
this.setAttribute("contenteditable", "");
}
2021-06-18 02:44:15 +02:00
focus(): void {
2021-06-18 00:27:07 +02:00
super.focus();
2021-07-05 18:15:03 +02:00
inCodable.set(false);
2021-06-18 00:27:07 +02:00
}
2021-06-18 02:44:15 +02:00
caretToEnd(): void {
caretToEnd(this);
}
surroundSelection(before: string, after: string): void {
wrap(before, after);
}
onEnter(event: KeyboardEvent): void {
if (
!getBlockElement(this.getRootNode() as Document | ShadowRoot) !==
event.shiftKey
) {
event.preventDefault();
document.execCommand("insertLineBreak");
}
}
onPaste(event: ClipboardEvent): void {
bridgeCommand("paste");
event.preventDefault();
}
}