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-05-06 23:33:28 +02:00
|
|
|
/* eslint
|
|
|
|
@typescript-eslint/no-non-null-assertion: "off",
|
|
|
|
*/
|
|
|
|
|
2021-07-21 02:15:10 +02:00
|
|
|
import ImageHandle from "./ImageHandle.svelte";
|
2021-07-21 01:32:09 +02:00
|
|
|
|
2021-07-19 15:27:11 +02:00
|
|
|
import type { EditableContainer } from "./editable-container";
|
2021-02-28 14:12:48 +01:00
|
|
|
import type { Editable } from "./editable";
|
2021-06-17 13:46:25 +02:00
|
|
|
import type { Codable } from "./codable";
|
2021-02-28 14:12:48 +01:00
|
|
|
|
2021-04-27 23:08:47 +02:00
|
|
|
import { updateActiveButtons } from "./toolbar";
|
2021-02-28 14:12:48 +01:00
|
|
|
import { bridgeCommand } from "./lib";
|
2021-06-30 19:55:56 +02:00
|
|
|
import { onInput, onKey, onKeyUp } from "./input-handlers";
|
|
|
|
import { onFocus, onBlur } from "./focus-handlers";
|
2021-07-21 01:32:09 +02:00
|
|
|
import { nightModeKey } from "components/context-keys";
|
2021-02-28 14:12:48 +01:00
|
|
|
|
2021-02-28 14:29:41 +01:00
|
|
|
function onCutOrCopy(): void {
|
2021-02-28 14:12:48 +01:00
|
|
|
bridgeCommand("cutOrCopy");
|
|
|
|
}
|
|
|
|
|
|
|
|
export class EditingArea extends HTMLDivElement {
|
2021-07-21 02:15:10 +02:00
|
|
|
imageHandle: ImageHandle;
|
2021-07-19 15:27:11 +02:00
|
|
|
editableContainer: EditableContainer;
|
2021-02-28 14:12:48 +01:00
|
|
|
editable: Editable;
|
2021-06-17 13:46:25 +02:00
|
|
|
codable: Codable;
|
2021-02-28 14:12:48 +01:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2021-04-15 16:44:07 +02:00
|
|
|
this.className = "field";
|
2021-02-28 14:12:48 +01:00
|
|
|
|
2021-07-19 15:27:11 +02:00
|
|
|
this.editableContainer = document.createElement("div", {
|
|
|
|
is: "anki-editable-container",
|
|
|
|
}) as EditableContainer;
|
2021-02-28 14:12:48 +01:00
|
|
|
this.editable = document.createElement("anki-editable") as Editable;
|
2021-07-19 15:27:11 +02:00
|
|
|
this.editableContainer.shadowRoot!.appendChild(this.editable);
|
|
|
|
this.appendChild(this.editableContainer);
|
2021-06-17 13:46:25 +02:00
|
|
|
|
2021-07-21 01:32:09 +02:00
|
|
|
const context = new Map();
|
|
|
|
context.set(
|
|
|
|
nightModeKey,
|
|
|
|
document.documentElement.classList.contains("night-mode")
|
|
|
|
);
|
|
|
|
|
2021-07-21 02:15:10 +02:00
|
|
|
this.imageHandle = new ImageHandle({
|
2021-07-21 01:32:09 +02:00
|
|
|
target: this,
|
|
|
|
anchor: this.editableContainer,
|
2021-07-21 14:13:56 +02:00
|
|
|
props: {
|
|
|
|
container: this.editable,
|
|
|
|
},
|
2021-07-21 01:32:09 +02:00
|
|
|
context,
|
|
|
|
} as any);
|
|
|
|
|
2021-06-17 17:18:00 +02:00
|
|
|
this.codable = document.createElement("textarea", {
|
|
|
|
is: "anki-codable",
|
|
|
|
}) as Codable;
|
2021-07-19 15:27:11 +02:00
|
|
|
this.appendChild(this.codable);
|
2021-06-17 23:12:15 +02:00
|
|
|
|
|
|
|
this.onPaste = this.onPaste.bind(this);
|
2021-07-21 01:32:09 +02:00
|
|
|
this.showImageHandle = this.showImageHandle.bind(this);
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:36:56 +02:00
|
|
|
get activeInput(): Editable | Codable {
|
|
|
|
return this.codable.active ? this.codable : this.editable;
|
|
|
|
}
|
|
|
|
|
2021-02-28 14:12:48 +01:00
|
|
|
get ord(): number {
|
|
|
|
return Number(this.getAttribute("ord"));
|
|
|
|
}
|
|
|
|
|
|
|
|
set fieldHTML(content: string) {
|
2021-06-17 21:36:56 +02:00
|
|
|
this.activeInput.fieldHTML = content;
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get fieldHTML(): string {
|
2021-06-17 21:36:56 +02:00
|
|
|
return this.activeInput.fieldHTML;
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback(): void {
|
|
|
|
this.addEventListener("keydown", onKey);
|
|
|
|
this.addEventListener("keyup", onKeyUp);
|
|
|
|
this.addEventListener("input", onInput);
|
2021-07-19 15:27:11 +02:00
|
|
|
this.addEventListener("focusin", onFocus);
|
|
|
|
this.addEventListener("focusout", onBlur);
|
2021-06-17 23:12:15 +02:00
|
|
|
this.addEventListener("paste", this.onPaste);
|
2021-02-28 14:12:48 +01:00
|
|
|
this.addEventListener("copy", onCutOrCopy);
|
|
|
|
this.addEventListener("oncut", onCutOrCopy);
|
2021-04-19 23:51:09 +02:00
|
|
|
this.addEventListener("mouseup", updateActiveButtons);
|
2021-07-21 01:32:09 +02:00
|
|
|
this.editable.addEventListener("click", this.showImageHandle);
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback(): void {
|
|
|
|
this.removeEventListener("keydown", onKey);
|
|
|
|
this.removeEventListener("keyup", onKeyUp);
|
|
|
|
this.removeEventListener("input", onInput);
|
2021-07-19 15:27:11 +02:00
|
|
|
this.removeEventListener("focusin", onFocus);
|
|
|
|
this.removeEventListener("focusout", onBlur);
|
2021-06-17 23:12:15 +02:00
|
|
|
this.removeEventListener("paste", this.onPaste);
|
2021-02-28 14:12:48 +01:00
|
|
|
this.removeEventListener("copy", onCutOrCopy);
|
|
|
|
this.removeEventListener("oncut", onCutOrCopy);
|
2021-04-19 23:51:09 +02:00
|
|
|
this.removeEventListener("mouseup", updateActiveButtons);
|
2021-07-21 01:32:09 +02:00
|
|
|
this.editable.removeEventListener("click", this.showImageHandle);
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
initialize(color: string, content: string): void {
|
|
|
|
this.setBaseColor(color);
|
2021-06-24 19:15:07 +02:00
|
|
|
this.fieldHTML = content;
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setBaseColor(color: string): void {
|
2021-07-19 15:27:11 +02:00
|
|
|
this.editableContainer.setBaseColor(color);
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
2021-08-19 21:31:11 +02:00
|
|
|
quoteFontFamily(fontFamily: string): string {
|
|
|
|
// generic families (e.g. sans-serif) must not be quoted
|
2021-09-02 13:04:39 +02:00
|
|
|
if (!/^[-a-z]+$/.test(fontFamily)) {
|
2021-08-19 21:31:11 +02:00
|
|
|
fontFamily = `"${fontFamily}"`;
|
|
|
|
}
|
|
|
|
return fontFamily;
|
|
|
|
}
|
|
|
|
|
2021-02-28 14:12:48 +01:00
|
|
|
setBaseStyling(fontFamily: string, fontSize: string, direction: string): void {
|
2021-07-19 15:27:11 +02:00
|
|
|
this.editableContainer.setBaseStyling(
|
|
|
|
this.quoteFontFamily(fontFamily),
|
|
|
|
fontSize,
|
|
|
|
direction,
|
|
|
|
);
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isRightToLeft(): boolean {
|
2021-07-19 15:27:11 +02:00
|
|
|
return this.editableContainer.isRightToLeft();
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:36:56 +02:00
|
|
|
focus(): void {
|
|
|
|
this.activeInput.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur(): void {
|
|
|
|
this.activeInput.blur();
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:36:56 +02:00
|
|
|
caretToEnd(): void {
|
|
|
|
this.activeInput.caretToEnd();
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|
2021-06-17 17:08:33 +02:00
|
|
|
|
2021-06-17 19:08:14 +02:00
|
|
|
hasFocus(): boolean {
|
2021-07-19 15:27:11 +02:00
|
|
|
return document.activeElement?.closest(".field") === this;
|
2021-06-17 19:08:14 +02:00
|
|
|
}
|
|
|
|
|
2021-06-18 02:33:56 +02:00
|
|
|
getSelection(): Selection {
|
2021-07-19 15:27:11 +02:00
|
|
|
const root = this.activeInput.getRootNode() as Document | ShadowRoot;
|
|
|
|
return root.getSelection()!;
|
2021-06-18 02:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
surroundSelection(before: string, after: string): void {
|
|
|
|
this.activeInput.surroundSelection(before, after);
|
|
|
|
}
|
|
|
|
|
2021-06-17 23:12:15 +02:00
|
|
|
onEnter(event: KeyboardEvent): void {
|
|
|
|
this.activeInput.onEnter(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
onPaste(event: ClipboardEvent): void {
|
|
|
|
this.activeInput.onPaste(event);
|
2021-06-17 22:02:06 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 01:32:09 +02:00
|
|
|
showImageHandle(event: MouseEvent): void {
|
|
|
|
if (event.target instanceof HTMLImageElement) {
|
|
|
|
(this.imageHandle as any).$set({
|
2021-07-21 02:15:10 +02:00
|
|
|
image: event.target,
|
2021-07-21 14:13:56 +02:00
|
|
|
imageRule: this.editableContainer.imageRule,
|
2021-07-21 01:32:09 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
(this.imageHandle as any).$set({
|
2021-07-21 02:15:10 +02:00
|
|
|
image: null,
|
2021-07-21 14:13:56 +02:00
|
|
|
imageRule: null,
|
2021-07-21 01:32:09 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:08:33 +02:00
|
|
|
toggleHtmlEdit(): void {
|
2021-06-17 19:08:14 +02:00
|
|
|
const hadFocus = this.hasFocus();
|
|
|
|
|
|
|
|
if (this.codable.active) {
|
2021-06-17 21:36:56 +02:00
|
|
|
this.fieldHTML = this.codable.teardown();
|
2021-06-17 19:08:14 +02:00
|
|
|
this.editable.hidden = false;
|
|
|
|
} else {
|
|
|
|
this.editable.hidden = true;
|
2021-06-24 19:15:07 +02:00
|
|
|
this.codable.setup(this.editable.fieldHTML);
|
2021-06-17 21:36:56 +02:00
|
|
|
}
|
2021-06-17 19:08:14 +02:00
|
|
|
|
2021-06-17 21:36:56 +02:00
|
|
|
if (hadFocus) {
|
|
|
|
this.focus();
|
|
|
|
this.caretToEnd();
|
2021-06-17 17:49:50 +02:00
|
|
|
}
|
2021-06-17 17:08:33 +02:00
|
|
|
}
|
2021-06-17 22:02:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated Use focus instead
|
|
|
|
*/
|
|
|
|
focusEditable(): void {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @deprecated Use blur instead
|
|
|
|
*/
|
|
|
|
blurEditable(): void {
|
|
|
|
blur();
|
|
|
|
}
|
2021-02-28 14:12:48 +01:00
|
|
|
}
|