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-04 00:32:30 +02:00
|
|
|
import { bridgeCommand } from "lib/bridgecommand";
|
|
|
|
import { elementIsBlock, getBlockElement } from "lib/dom";
|
|
|
|
// import { inCodable } from "./toolbar";
|
|
|
|
// import { wrap } from "./wrap";
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2021-02-28 14:12:48 +01:00
|
|
|
|
2021-08-31 16:42:16 +02:00
|
|
|
function containsInlineContent(element: Element): boolean {
|
|
|
|
for (const child of element.children) {
|
|
|
|
if (elementIsBlock(child) || !containsInlineContent(child)) {
|
2021-02-28 14:12:48 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-05 06:01:43 +02:00
|
|
|
interface DecoratedElement extends HTMLElement {
|
|
|
|
decorate(): void;
|
|
|
|
undecorate(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const decoratedTags = ["anki-mathjax"];
|
2021-08-04 04:21:35 +02:00
|
|
|
|
2021-02-28 14:12:48 +01:00
|
|
|
export class Editable extends HTMLElement {
|
|
|
|
set fieldHTML(content: string) {
|
|
|
|
this.innerHTML = content;
|
|
|
|
|
2021-08-31 16:42:16 +02:00
|
|
|
if (content.length > 0 && containsInlineContent(this)) {
|
2021-02-28 14:12:48 +01:00
|
|
|
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 06:01:43 +02:00
|
|
|
for (const component of decoratedTags) {
|
2021-08-04 04:21:35 +02:00
|
|
|
for (const element of clone.getElementsByTagName(component)) {
|
2021-08-05 06:01:43 +02:00
|
|
|
(element as DecoratedElement).undecorate();
|
2021-08-04 04:21:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 19:25:35 +02:00
|
|
|
const result =
|
|
|
|
containsInlineContent(clone) && clone.innerHTML.endsWith("<br>")
|
|
|
|
? clone.innerHTML.slice(0, -4) // trim trailing <br>
|
|
|
|
: clone.innerHTML;
|
|
|
|
|
|
|
|
return result;
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 23:33:28 +02:00
|
|
|
connectedCallback(): void {
|
2021-02-28 14:12:48 +01:00
|
|
|
this.setAttribute("contenteditable", "");
|
|
|
|
}
|
2021-06-17 21:36:56 +02:00
|
|
|
|
2021-06-18 02:44:15 +02:00
|
|
|
focus(): void {
|
2021-06-18 00:27:07 +02:00
|
|
|
super.focus();
|
2021-08-04 00:32:30 +02:00
|
|
|
// TODO
|
|
|
|
// inCodable.set(false);
|
2021-06-18 00:27:07 +02:00
|
|
|
}
|
|
|
|
|
2021-06-18 02:44:15 +02:00
|
|
|
caretToEnd(): void {
|
2021-06-17 21:36:56 +02:00
|
|
|
caretToEnd(this);
|
|
|
|
}
|
2021-06-17 22:02:06 +02:00
|
|
|
|
2021-06-18 02:33:56 +02:00
|
|
|
surroundSelection(before: string, after: string): void {
|
2021-08-04 00:32:30 +02:00
|
|
|
// TODO
|
|
|
|
// wrap(before, after);
|
2021-06-18 02:33:56 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 23:12:15 +02:00
|
|
|
onEnter(event: KeyboardEvent): void {
|
2021-06-17 22:02:06 +02:00
|
|
|
if (
|
|
|
|
!getBlockElement(this.getRootNode() as Document | ShadowRoot) !==
|
|
|
|
event.shiftKey
|
|
|
|
) {
|
|
|
|
event.preventDefault();
|
|
|
|
document.execCommand("insertLineBreak");
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 23:12:15 +02:00
|
|
|
|
|
|
|
onPaste(event: ClipboardEvent): void {
|
|
|
|
bridgeCommand("paste");
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
2021-08-04 00:32:30 +02:00
|
|
|
|
|
|
|
customElements.define("anki-editable", Editable);
|