2019-02-05 04:59:03 +01:00
|
|
|
/* Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
|
|
|
|
|
2021-01-30 17:54:07 +01:00
|
|
|
import { filterHTML } from "./filterHtml";
|
2021-02-08 19:45:42 +01:00
|
|
|
import { nodeIsInline } from "./helpers";
|
2021-01-31 14:15:03 +01:00
|
|
|
import { bridgeCommand } from "./lib";
|
2021-02-08 19:45:42 +01:00
|
|
|
import { saveField } from "./changeTimer";
|
|
|
|
import { updateButtonState, maybeDisableButtons } from "./toolbar";
|
|
|
|
import { onInput, onKey, onKeyUp } from "./inputHandlers";
|
|
|
|
import { onFocus, onBlur } from "./focusHandlers";
|
2021-01-30 17:54:07 +01:00
|
|
|
|
2021-02-08 19:45:42 +01:00
|
|
|
export { setNoteId, getNoteId } from "./noteId";
|
|
|
|
export { preventButtonFocus, toggleEditorButton, setFGButton } from "./toolbar";
|
2021-02-08 20:28:02 +01:00
|
|
|
export { saveNow } from "./changeTimer";
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-30 18:32:36 +01:00
|
|
|
declare global {
|
|
|
|
interface Selection {
|
|
|
|
modify(s: string, t: string, u: string): void;
|
|
|
|
addRange(r: Range): void;
|
|
|
|
removeAllRanges(): void;
|
|
|
|
getRangeAt(n: number): Range;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:44:56 +01:00
|
|
|
export function getCurrentField(): EditingArea | null {
|
2021-02-08 17:00:27 +01:00
|
|
|
return document.activeElement instanceof EditingArea
|
|
|
|
? document.activeElement
|
|
|
|
: null;
|
2021-02-08 15:44:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 17:54:07 +01:00
|
|
|
export function focusField(n: number): void {
|
2021-01-30 14:20:14 +01:00
|
|
|
const field = getEditorField(n);
|
2021-01-28 19:52:49 +01:00
|
|
|
|
|
|
|
if (field) {
|
2021-01-30 14:20:14 +01:00
|
|
|
field.editingArea.focusEditable();
|
2017-08-05 07:15:19 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 17:54:07 +01:00
|
|
|
export function focusIfField(x: number, y: number): boolean {
|
2020-08-25 16:23:34 +02:00
|
|
|
const elements = document.elementsFromPoint(x, y);
|
|
|
|
for (let i = 0; i < elements.length; i++) {
|
2021-01-29 20:32:21 +01:00
|
|
|
let elem = elements[i] as EditingArea;
|
|
|
|
if (elem instanceof EditingArea) {
|
|
|
|
elem.focusEditable();
|
2020-08-25 16:23:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-08-19 06:37:14 +02:00
|
|
|
}
|
|
|
|
return false;
|
2017-08-01 09:40:51 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 17:00:27 +01:00
|
|
|
function onPaste(evt: ClipboardEvent): void {
|
2021-01-31 14:15:03 +01:00
|
|
|
bridgeCommand("paste");
|
2021-02-08 17:00:27 +01:00
|
|
|
evt.preventDefault();
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 19:45:42 +01:00
|
|
|
function onCutOrCopy(): boolean {
|
|
|
|
bridgeCommand("cutOrCopy");
|
|
|
|
return true;
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-28 17:24:07 +01:00
|
|
|
function containsInlineContent(field: Element): boolean {
|
2021-01-26 23:00:55 +01:00
|
|
|
if (field.childNodes.length === 0) {
|
|
|
|
// for now, for all practical purposes, empty fields are in block mode
|
|
|
|
return false;
|
2021-01-26 21:26:04 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 23:00:55 +01:00
|
|
|
for (const child of field.children) {
|
|
|
|
if (!nodeIsInline(child)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-01-26 21:26:04 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:32:21 +01:00
|
|
|
class Editable extends HTMLElement {
|
2021-01-28 17:16:10 +01:00
|
|
|
set fieldHTML(content: string) {
|
|
|
|
this.innerHTML = content;
|
|
|
|
|
2021-01-28 17:24:07 +01:00
|
|
|
if (containsInlineContent(this)) {
|
2021-01-28 17:16:10 +01:00
|
|
|
this.appendChild(document.createElement("br"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get fieldHTML(): string {
|
2021-01-28 17:24:07 +01:00
|
|
|
return containsInlineContent(this) && this.innerHTML.endsWith("<br>")
|
2021-01-28 17:16:10 +01:00
|
|
|
? this.innerHTML.slice(0, -4) // trim trailing <br>
|
|
|
|
: this.innerHTML;
|
|
|
|
}
|
2021-01-29 20:13:02 +01:00
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
this.setAttribute("contenteditable", "");
|
|
|
|
}
|
2021-01-28 15:42:37 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:32:21 +01:00
|
|
|
customElements.define("anki-editable", Editable);
|
2021-01-28 15:42:37 +01:00
|
|
|
|
2021-02-08 19:45:42 +01:00
|
|
|
export class EditingArea extends HTMLDivElement {
|
2021-01-29 20:32:21 +01:00
|
|
|
editable: Editable;
|
2021-01-29 15:50:34 +01:00
|
|
|
baseStyle: HTMLStyleElement;
|
2021-01-28 15:42:37 +01:00
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.attachShadow({ mode: "open" });
|
2021-01-28 17:16:10 +01:00
|
|
|
this.className = "field";
|
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
const rootStyle = document.createElement("link");
|
|
|
|
rootStyle.setAttribute("rel", "stylesheet");
|
2021-01-29 20:32:21 +01:00
|
|
|
rootStyle.setAttribute("href", "./_anki/css/editable.css");
|
2021-02-08 17:00:27 +01:00
|
|
|
this.shadowRoot!.appendChild(rootStyle);
|
2021-01-29 15:50:34 +01:00
|
|
|
|
|
|
|
this.baseStyle = document.createElement("style");
|
|
|
|
this.baseStyle.setAttribute("rel", "stylesheet");
|
2021-02-08 17:00:27 +01:00
|
|
|
this.shadowRoot!.appendChild(this.baseStyle);
|
2021-01-29 15:50:34 +01:00
|
|
|
|
2021-01-29 20:32:21 +01:00
|
|
|
this.editable = document.createElement("anki-editable") as Editable;
|
2021-02-08 17:00:27 +01:00
|
|
|
this.shadowRoot!.appendChild(this.editable);
|
2021-01-29 15:50:34 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 17:41:27 +01:00
|
|
|
get ord(): number {
|
|
|
|
return Number(this.getAttribute("ord"));
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:13:02 +01:00
|
|
|
set fieldHTML(content: string) {
|
2021-01-29 20:32:21 +01:00
|
|
|
this.editable.fieldHTML = content;
|
2021-01-29 20:13:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get fieldHTML(): string {
|
2021-01-29 20:32:21 +01:00
|
|
|
return this.editable.fieldHTML;
|
2021-01-29 20:13:02 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
connectedCallback(): void {
|
2021-01-28 17:16:10 +01:00
|
|
|
this.addEventListener("keydown", onKey);
|
|
|
|
this.addEventListener("keyup", onKeyUp);
|
|
|
|
this.addEventListener("input", onInput);
|
|
|
|
this.addEventListener("focus", onFocus);
|
|
|
|
this.addEventListener("blur", onBlur);
|
|
|
|
this.addEventListener("paste", onPaste);
|
|
|
|
this.addEventListener("copy", onCutOrCopy);
|
|
|
|
this.addEventListener("oncut", onCutOrCopy);
|
2021-02-08 20:49:33 +01:00
|
|
|
this.addEventListener("click", updateButtonState);
|
2021-01-28 17:16:10 +01:00
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
const baseStyleSheet = this.baseStyle.sheet as CSSStyleSheet;
|
2021-01-29 20:32:21 +01:00
|
|
|
baseStyleSheet.insertRule("anki-editable {}", 0);
|
2021-01-29 15:50:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback(): void {
|
|
|
|
this.removeEventListener("keydown", onKey);
|
|
|
|
this.removeEventListener("keyup", onKeyUp);
|
|
|
|
this.removeEventListener("input", onInput);
|
|
|
|
this.removeEventListener("focus", onFocus);
|
|
|
|
this.removeEventListener("blur", onBlur);
|
|
|
|
this.removeEventListener("paste", onPaste);
|
|
|
|
this.removeEventListener("copy", onCutOrCopy);
|
|
|
|
this.removeEventListener("oncut", onCutOrCopy);
|
2021-02-08 20:49:33 +01:00
|
|
|
this.removeEventListener("click", updateButtonState);
|
2021-01-28 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:00:33 +01:00
|
|
|
initialize(color: string, content: string): void {
|
2021-01-28 22:01:34 +01:00
|
|
|
this.setBaseColor(color);
|
2021-01-29 20:32:21 +01:00
|
|
|
this.editable.fieldHTML = content;
|
2021-01-28 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 22:01:34 +01:00
|
|
|
setBaseColor(color: string): void {
|
2021-01-29 15:50:34 +01:00
|
|
|
const styleSheet = this.baseStyle.sheet as CSSStyleSheet;
|
|
|
|
const firstRule = styleSheet.cssRules[0] as CSSStyleRule;
|
2021-01-28 22:01:34 +01:00
|
|
|
firstRule.style.color = color;
|
|
|
|
}
|
|
|
|
|
2021-01-28 18:37:38 +01:00
|
|
|
setBaseStyling(fontFamily: string, fontSize: string, direction: string): void {
|
2021-01-29 15:50:34 +01:00
|
|
|
const styleSheet = this.baseStyle.sheet as CSSStyleSheet;
|
|
|
|
const firstRule = styleSheet.cssRules[0] as CSSStyleRule;
|
2021-01-28 21:38:55 +01:00
|
|
|
firstRule.style.fontFamily = fontFamily;
|
|
|
|
firstRule.style.fontSize = fontSize;
|
|
|
|
firstRule.style.direction = direction;
|
|
|
|
}
|
|
|
|
|
2021-01-28 18:37:38 +01:00
|
|
|
isRightToLeft(): boolean {
|
2021-01-29 20:32:21 +01:00
|
|
|
return this.editable.style.direction === "rtl";
|
2021-01-28 18:37:38 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 18:32:36 +01:00
|
|
|
getSelection(): Selection {
|
2021-02-08 17:00:27 +01:00
|
|
|
return this.shadowRoot!.getSelection()!;
|
2021-01-28 17:43:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:32:21 +01:00
|
|
|
focusEditable(): void {
|
|
|
|
this.editable.focus();
|
2021-01-28 19:52:49 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:32:21 +01:00
|
|
|
blurEditable(): void {
|
|
|
|
this.editable.blur();
|
2021-01-28 19:52:49 +01:00
|
|
|
}
|
2021-01-28 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:32:21 +01:00
|
|
|
customElements.define("anki-editing-area", EditingArea, { extends: "div" });
|
2021-01-28 17:16:10 +01:00
|
|
|
|
|
|
|
class EditorField extends HTMLDivElement {
|
|
|
|
labelContainer: HTMLDivElement;
|
|
|
|
label: HTMLSpanElement;
|
2021-01-29 20:32:21 +01:00
|
|
|
editingArea: EditingArea;
|
2021-01-28 17:16:10 +01:00
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.labelContainer = document.createElement("div");
|
2021-01-28 15:42:37 +01:00
|
|
|
this.labelContainer.className = "fname";
|
2021-01-29 19:34:44 +01:00
|
|
|
this.appendChild(this.labelContainer);
|
2021-01-28 15:42:37 +01:00
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
this.label = document.createElement("span");
|
2021-01-28 15:42:37 +01:00
|
|
|
this.label.className = "fieldname";
|
2021-01-29 15:50:34 +01:00
|
|
|
this.labelContainer.appendChild(this.label);
|
2021-01-28 15:42:37 +01:00
|
|
|
|
2021-01-29 20:32:21 +01:00
|
|
|
this.editingArea = document.createElement("div", {
|
|
|
|
is: "anki-editing-area",
|
|
|
|
}) as EditingArea;
|
|
|
|
this.appendChild(this.editingArea);
|
2021-01-28 15:42:37 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 17:41:27 +01:00
|
|
|
static get observedAttributes(): string[] {
|
2021-01-29 19:34:44 +01:00
|
|
|
return ["ord"];
|
2021-01-29 17:41:27 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:13:02 +01:00
|
|
|
set ord(n: number) {
|
|
|
|
this.setAttribute("ord", String(n));
|
|
|
|
}
|
|
|
|
|
2021-01-29 17:41:27 +01:00
|
|
|
attributeChangedCallback(name: string, _oldValue: string, newValue: string): void {
|
|
|
|
switch (name) {
|
|
|
|
case "ord":
|
2021-01-29 20:32:21 +01:00
|
|
|
this.editingArea.setAttribute("ord", newValue);
|
2021-01-29 17:41:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:00:33 +01:00
|
|
|
initialize(label: string, color: string, content: string): void {
|
2021-01-28 15:42:37 +01:00
|
|
|
this.label.innerText = label;
|
2021-01-29 20:32:21 +01:00
|
|
|
this.editingArea.initialize(color, content);
|
2021-01-28 15:42:37 +01:00
|
|
|
}
|
2021-01-28 18:37:38 +01:00
|
|
|
|
|
|
|
setBaseStyling(fontFamily: string, fontSize: string, direction: string): void {
|
2021-01-29 20:32:21 +01:00
|
|
|
this.editingArea.setBaseStyling(fontFamily, fontSize, direction);
|
2021-01-28 18:37:38 +01:00
|
|
|
}
|
2021-01-28 15:42:37 +01:00
|
|
|
}
|
2021-01-26 23:00:55 +01:00
|
|
|
|
2021-01-29 20:32:21 +01:00
|
|
|
customElements.define("anki-editor-field", EditorField, { extends: "div" });
|
2021-01-28 15:42:37 +01:00
|
|
|
|
|
|
|
function adjustFieldAmount(amount: number): void {
|
2021-02-08 17:00:27 +01:00
|
|
|
const fieldsContainer = document.getElementById("fields")!;
|
2021-01-28 15:42:37 +01:00
|
|
|
|
|
|
|
while (fieldsContainer.childElementCount < amount) {
|
2021-01-29 19:34:44 +01:00
|
|
|
const newField = document.createElement("div", {
|
2021-01-29 20:32:21 +01:00
|
|
|
is: "anki-editor-field",
|
2021-01-29 19:34:44 +01:00
|
|
|
}) as EditorField;
|
2021-01-29 17:41:27 +01:00
|
|
|
newField.ord = fieldsContainer.childElementCount;
|
|
|
|
fieldsContainer.appendChild(newField);
|
2021-01-26 23:00:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 15:42:37 +01:00
|
|
|
while (fieldsContainer.childElementCount > amount) {
|
2021-02-08 17:00:27 +01:00
|
|
|
fieldsContainer.removeChild(fieldsContainer.lastElementChild as Node);
|
2021-01-28 15:42:37 +01:00
|
|
|
}
|
2021-01-26 23:00:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 20:56:28 +01:00
|
|
|
export function getEditorField(n: number): EditorField | null {
|
2021-02-08 17:00:27 +01:00
|
|
|
const fields = document.getElementById("fields")!.children;
|
2021-01-30 14:20:14 +01:00
|
|
|
return (fields[n] as EditorField) ?? null;
|
|
|
|
}
|
|
|
|
|
2021-01-31 20:56:28 +01:00
|
|
|
export function forEditorField<T>(
|
2021-01-28 19:13:39 +01:00
|
|
|
values: T[],
|
2021-01-29 19:34:44 +01:00
|
|
|
func: (field: EditorField, value: T) => void
|
2021-01-28 19:13:39 +01:00
|
|
|
): void {
|
2021-02-08 17:00:27 +01:00
|
|
|
const fields = document.getElementById("fields")!.children;
|
2021-01-29 19:34:44 +01:00
|
|
|
for (let i = 0; i < fields.length; i++) {
|
|
|
|
const field = fields[i] as EditorField;
|
|
|
|
func(field, values[i]);
|
2021-01-28 18:37:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:54:07 +01:00
|
|
|
export function setFields(fields: [string, string][]): void {
|
2020-12-21 08:56:20 +01:00
|
|
|
// webengine will include the variable after enter+backspace
|
|
|
|
// if we don't convert it to a literal colour
|
|
|
|
const color = window
|
|
|
|
.getComputedStyle(document.documentElement)
|
|
|
|
.getPropertyValue("--text-fg");
|
2021-01-26 23:00:55 +01:00
|
|
|
|
2021-01-28 15:42:37 +01:00
|
|
|
adjustFieldAmount(fields.length);
|
2021-01-29 19:34:44 +01:00
|
|
|
forEditorField(fields, (field, [name, fieldContent]) =>
|
2021-01-29 14:00:33 +01:00
|
|
|
field.initialize(name, color, fieldContent)
|
2021-01-28 19:13:39 +01:00
|
|
|
);
|
2021-01-26 23:00:55 +01:00
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
maybeDisableButtons();
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-30 17:54:07 +01:00
|
|
|
export function setBackgrounds(cols: ("dupe" | "")[]) {
|
2021-01-29 19:34:44 +01:00
|
|
|
forEditorField(cols, (field, value) =>
|
2021-01-29 20:32:21 +01:00
|
|
|
field.editingArea.classList.toggle("dupe", value === "dupe")
|
2021-01-28 19:13:39 +01:00
|
|
|
);
|
|
|
|
document
|
2021-02-08 17:00:27 +01:00
|
|
|
.getElementById("dupes")!
|
2021-01-28 19:13:39 +01:00
|
|
|
.classList.toggle("is-inactive", !cols.includes("dupe"));
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 17:54:07 +01:00
|
|
|
export function setFonts(fonts: [string, number, boolean][]): void {
|
2021-01-29 19:34:44 +01:00
|
|
|
forEditorField(fonts, (field, [fontFamily, fontSize, isRtl]) => {
|
2021-01-28 18:37:38 +01:00
|
|
|
field.setBaseStyling(fontFamily, `${fontSize}px`, isRtl ? "rtl" : "ltr");
|
|
|
|
});
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 19:45:42 +01:00
|
|
|
function wrappedExceptForWhitespace(text: string, front: string, back: string): string {
|
|
|
|
const match = text.match(/^(\s*)([^]*?)(\s*)$/)!;
|
|
|
|
return match[1] + front + match[2] + back + match[3];
|
2018-04-30 06:51:49 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 19:45:42 +01:00
|
|
|
export function wrap(front: string, back: string): void {
|
|
|
|
wrapInternal(front, back, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* currently unused */
|
|
|
|
export function wrapIntoText(front: string, back: string): void {
|
|
|
|
wrapInternal(front, back, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setFormat(cmd: string, arg?: any, nosave: boolean = false): void {
|
|
|
|
document.execCommand(cmd, false, arg);
|
|
|
|
if (!nosave) {
|
|
|
|
saveField(getCurrentField() as EditingArea, "key");
|
|
|
|
updateButtonState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapInternal(front: string, back: string, plainText: boolean): void {
|
|
|
|
const currentField = getCurrentField()!;
|
|
|
|
const s = currentField.getSelection();
|
|
|
|
let r = s.getRangeAt(0);
|
|
|
|
const content = r.cloneContents();
|
|
|
|
const span = document.createElement("span");
|
|
|
|
span.appendChild(content);
|
|
|
|
|
|
|
|
if (plainText) {
|
|
|
|
const new_ = wrappedExceptForWhitespace(span.innerText, front, back);
|
|
|
|
setFormat("inserttext", new_);
|
|
|
|
} else {
|
|
|
|
const new_ = wrappedExceptForWhitespace(span.innerHTML, front, back);
|
|
|
|
setFormat("inserthtml", new_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!span.innerHTML) {
|
|
|
|
// run with an empty selection; move cursor back past postfix
|
|
|
|
r = s.getRangeAt(0);
|
|
|
|
r.setStart(r.startContainer, r.startOffset - back.length);
|
|
|
|
r.collapse(true);
|
|
|
|
s.removeAllRanges();
|
|
|
|
s.addRange(r);
|
|
|
|
}
|
2021-02-08 17:00:27 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 17:54:07 +01:00
|
|
|
export let pasteHTML = function (
|
2021-01-19 01:08:15 +01:00
|
|
|
html: string,
|
|
|
|
internal: boolean,
|
|
|
|
extendedMode: boolean
|
|
|
|
): void {
|
2020-12-21 03:13:31 +01:00
|
|
|
html = filterHTML(html, internal, extendedMode);
|
|
|
|
|
2019-05-17 05:40:23 +02:00
|
|
|
if (html !== "") {
|
|
|
|
setFormat("inserthtml", html);
|
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
};
|