anki/ts/editable/editable.ts

84 lines
2.4 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
2021-08-05 20:54:25 +02:00
import type { DecoratedElement } from "./decorated";
import { decoratedComponents } from "./decorated";
2021-08-04 00:32:30 +02:00
import { bridgeCommand } from "lib/bridgecommand";
import { elementIsBlock, getBlockElement } from "lib/dom";
import { wrapInternal } from "lib/wrap";
2021-08-04 00:32:30 +02:00
export function caretToEnd(node: Node): void {
const range = document.createRange();
range.selectNodeContents(node);
range.collapse(false);
const selection = (node.getRootNode() as Document | ShadowRoot).getSelection()!;
selection.removeAllRanges();
selection.addRange(range);
}
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 {
2021-08-04 04:21:35 +02:00
const clone = this.cloneNode(true) as Element;
2021-08-05 20:54:25 +02:00
for (const component of decoratedComponents) {
for (const element of clone.getElementsByTagName(component.tagName)) {
(element as DecoratedElement).undecorate();
2021-08-04 04:21:35 +02:00
}
}
const result =
containsInlineContent(clone) && clone.innerHTML.endsWith("<br>")
? clone.innerHTML.slice(0, -4) // trim trailing <br>
: clone.innerHTML;
return result;
}
2021-05-06 23:33:28 +02:00
connectedCallback(): void {
this.setAttribute("contenteditable", "");
}
2021-06-18 02:44:15 +02:00
caretToEnd(): void {
caretToEnd(this);
}
surroundSelection(before: string, after: string): void {
wrapInternal(this.getRootNode() as ShadowRoot, before, after, false);
}
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();
}
}
2021-08-04 00:32:30 +02:00
customElements.define("anki-editable", Editable);