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-06-17 23:12:15 +02:00
|
|
|
import { bridgeCommand } from "./lib";
|
2021-06-17 22:02:06 +02:00
|
|
|
import { nodeIsInline, caretToEnd, getBlockElement } from "./helpers";
|
2021-06-18 00:27:07 +02:00
|
|
|
import { setEditableButtons } from "./toolbar";
|
2021-06-18 02:33:56 +02:00
|
|
|
import { wrap } from "./wrap";
|
2021-02-28 14:12:48 +01:00
|
|
|
|
|
|
|
function containsInlineContent(field: Element): boolean {
|
|
|
|
if (field.childNodes.length === 0) {
|
|
|
|
// for now, for all practical purposes, empty fields are in block mode
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const child of field.children) {
|
|
|
|
if (!nodeIsInline(child)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Editable extends HTMLElement {
|
|
|
|
set fieldHTML(content: string) {
|
|
|
|
this.innerHTML = content;
|
|
|
|
|
|
|
|
if (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 {
|
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();
|
|
|
|
setEditableButtons();
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
wrap(before, after);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|