anki/ts/editor/index.ts

571 lines
16 KiB
TypeScript
Raw Normal View History

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";
import { nodeIsElement, nodeIsInline } from "./helpers";
2021-01-31 14:15:03 +01:00
import { bridgeCommand } from "./lib";
2021-01-30 17:54:07 +01:00
2021-01-29 00:43:45 +01:00
let changeTimer: number | null = null;
let currentNoteId: number | null = null;
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 setFGButton(col: string): void {
2021-02-08 17:00:27 +01:00
document.getElementById("forecolor")!.style.backgroundColor = col;
2017-07-28 08:48:49 +02:00
}
2021-01-30 17:54:07 +01:00
export function saveNow(keepFocus: boolean): void {
2021-02-08 17:00:27 +01:00
const currentField = getCurrentField();
if (!currentField) {
return;
}
clearChangeTimer();
if (keepFocus) {
2021-02-08 17:00:27 +01:00
saveField(currentField, "key");
} else {
// triggers onBlur, which saves
currentField.blurEditable();
}
2017-07-28 08:48:49 +02:00
}
2021-02-08 17:00:27 +01:00
function triggerKeyTimer(currentField: EditingArea): void {
clearChangeTimer();
2020-06-24 13:54:38 +02:00
changeTimer = setTimeout(function () {
updateButtonState();
2021-02-08 17:00:27 +01:00
saveField(currentField, "key");
}, 600);
}
function onKey(evt: KeyboardEvent): void {
2021-02-08 17:00:27 +01:00
const currentField = evt.currentTarget as EditingArea;
// esc clears focus, allowing dialog to close
if (evt.code === "Escape") {
currentField.blurEditable();
return;
}
2021-01-14 13:32:30 +01:00
// prefer <br> instead of <div></div>
2021-02-08 17:00:27 +01:00
if (evt.code === "Enter" && !inListItem(currentField)) {
2021-01-14 13:32:30 +01:00
evt.preventDefault();
document.execCommand("insertLineBreak");
}
2021-01-28 18:37:38 +01:00
// // fix Ctrl+right/left handling in RTL fields
if (currentField.isRightToLeft()) {
const selection = currentField.getSelection();
const granularity = evt.ctrlKey ? "word" : "character";
const alter = evt.shiftKey ? "extend" : "move";
switch (evt.code) {
case "ArrowRight":
selection.modify(alter, "right", granularity);
evt.preventDefault();
return;
case "ArrowLeft":
selection.modify(alter, "left", granularity);
evt.preventDefault();
return;
}
}
2021-02-08 17:00:27 +01:00
triggerKeyTimer(currentField);
2017-07-28 08:48:49 +02:00
}
function onKeyUp(evt: KeyboardEvent): void {
2021-02-08 17:00:27 +01:00
const currentField = evt.currentTarget as EditingArea;
// Avoid div element on remove
if (evt.code === "Enter" || evt.code === "Backspace") {
2021-02-08 17:00:27 +01:00
const anchor = currentField.getSelection().anchorNode as Node;
if (
nodeIsElement(anchor) &&
anchor.tagName === "DIV" &&
!(anchor instanceof EditingArea) &&
anchor.childElementCount === 1 &&
anchor.children[0].tagName === "BR"
) {
anchor.replaceWith(anchor.children[0]);
}
}
}
2021-02-08 17:00:27 +01:00
function inListItem(currentField: EditingArea): boolean {
const anchor = currentField.getSelection()!.anchorNode!;
let inList = false;
let n = nodeIsElement(anchor) ? anchor : anchor.parentElement;
while (n) {
inList = inList || window.getComputedStyle(n).display == "list-item";
n = n.parentElement;
}
return inList;
}
2021-02-08 17:00:27 +01:00
function onInput(event: Event): void {
// make sure IME changes get saved
2021-02-08 17:00:27 +01:00
triggerKeyTimer(event.currentTarget as EditingArea);
2017-07-28 08:48:49 +02:00
}
function updateButtonState(): void {
const buts = ["bold", "italic", "underline", "superscript", "subscript"];
for (const name of buts) {
const elem = document.querySelector(`#${name}`) as HTMLElement;
elem.classList.toggle("highlighted", document.queryCommandState(name));
}
// fixme: forecolor
2019-12-18 07:12:39 +01:00
// 'col': document.queryCommandValue("forecolor")
2017-07-28 08:48:49 +02:00
}
2021-01-30 17:54:07 +01:00
export function toggleEditorButton(buttonid: string): void {
const button = $(buttonid)[0];
button.classList.toggle("highlighted");
2017-07-28 08:48:49 +02:00
}
2021-01-30 17:54:07 +01:00
export function setFormat(cmd: string, arg?: any, nosave: boolean = false): void {
document.execCommand(cmd, false, arg);
if (!nosave) {
2021-02-08 17:00:27 +01:00
saveField(getCurrentField() as EditingArea, "key");
updateButtonState();
}
2017-07-28 08:48:49 +02:00
}
function clearChangeTimer(): void {
if (changeTimer) {
clearTimeout(changeTimer);
changeTimer = null;
}
2017-07-28 08:48:49 +02:00
}
function onFocus(evt: FocusEvent): void {
2021-02-08 17:00:27 +01:00
const currentField = evt.currentTarget as EditingArea;
currentField.focusEditable();
2021-01-31 14:15:03 +01:00
bridgeCommand(`focus:${currentField.ord}`);
enableButtons();
// do this twice so that there's no flicker on newer versions
2021-02-08 17:00:27 +01:00
caretToEnd(currentField);
// scroll if bottom of element off the screen
function pos(elem: HTMLElement): number {
let cur = 0;
do {
cur += elem.offsetTop;
elem = elem.offsetParent as HTMLElement;
} while (elem);
return cur;
}
2021-02-08 17:00:27 +01:00
const y = pos(currentField);
2019-12-18 07:12:39 +01:00
if (
2021-02-08 17:00:27 +01:00
window.pageYOffset + window.innerHeight < y + currentField.offsetHeight ||
2019-12-18 07:12:39 +01:00
window.pageYOffset > y
) {
2021-02-08 17:00:27 +01:00
window.scroll(0, y + currentField.offsetHeight - window.innerHeight);
}
}
2021-01-30 17:54:07 +01:00
export function focusField(n: number): void {
const field = getEditorField(n);
if (field) {
field.editingArea.focusEditable();
}
}
2021-01-30 17:54:07 +01:00
export function focusIfField(x: number, y: number): boolean {
const elements = document.elementsFromPoint(x, y);
for (let i = 0; i < elements.length; i++) {
let elem = elements[i] as EditingArea;
if (elem instanceof EditingArea) {
elem.focusEditable();
return true;
}
}
return false;
}
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();
}
2021-02-08 17:00:27 +01:00
function caretToEnd(currentField: EditingArea): void {
2021-01-29 00:43:45 +01:00
const range = document.createRange();
range.selectNodeContents(currentField.editable);
2021-01-29 00:43:45 +01:00
range.collapse(false);
const selection = currentField.getSelection();
selection.removeAllRanges();
selection.addRange(range);
2017-07-28 08:48:49 +02:00
}
2021-02-08 17:00:27 +01:00
function onBlur(evt: FocusEvent): void {
const currentField = evt.currentTarget as EditingArea;
if (document.activeElement === currentField) {
// other widget or window focused; current field unchanged
2021-02-08 17:00:27 +01:00
saveField(currentField, "key");
} else {
2021-02-08 17:00:27 +01:00
saveField(currentField, "blur");
disableButtons();
}
2017-07-28 08:48:49 +02: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;
}
2021-02-08 17:00:27 +01:00
function saveField(currentField: EditingArea, type: "blur" | "key"): void {
clearChangeTimer();
2021-01-31 14:15:03 +01:00
bridgeCommand(
2021-02-08 17:00:27 +01:00
`${type}:${currentField.ord}:${getCurrentNoteId()}:${currentField.fieldHTML}`
2021-01-31 14:15:03 +01:00
);
2017-07-28 08:48:49 +02:00
}
function wrappedExceptForWhitespace(text: string, front: string, back: string): string {
2021-02-08 17:00:27 +01:00
const match = text.match(/^(\s*)([^]*?)(\s*)$/)!;
return match[1] + front + match[2] + back + match[3];
2017-07-28 08:48:49 +02:00
}
2021-01-30 17:54:07 +01:00
export function preventButtonFocus(): void {
for (const element of document.querySelectorAll("button.linkb")) {
element.addEventListener("mousedown", (evt: Event) => {
evt.preventDefault();
});
}
}
function disableButtons(): void {
$("button.linkb:not(.perm)").prop("disabled", true);
2017-07-28 08:48:49 +02:00
}
function enableButtons(): void {
$("button.linkb").prop("disabled", false);
2017-07-28 08:48:49 +02:00
}
// disable the buttons if a field is not currently focused
function maybeDisableButtons(): void {
if (document.activeElement instanceof EditingArea) {
enableButtons();
} else {
disableButtons();
}
2017-07-28 08:48:49 +02:00
}
2021-01-30 17:54:07 +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);
}
function wrapInternal(front: string, back: string, plainText: boolean): void {
2021-02-08 17:00:27 +01:00
const currentField = getCurrentField()!;
const s = currentField.getSelection();
let r = s.getRangeAt(0);
const content = r.cloneContents();
const span = document.createElement("span");
span.appendChild(content);
2021-02-08 17:00:27 +01:00
if (plainText) {
const new_ = wrappedExceptForWhitespace(span.innerText, front, back);
setFormat("inserttext", new_);
} else {
const new_ = wrappedExceptForWhitespace(span.innerHTML, front, back);
setFormat("inserthtml", new_);
}
2021-02-08 17:00:27 +01:00
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);
}
2017-07-28 08:48:49 +02:00
}
function onCutOrCopy(): boolean {
2021-01-31 14:15:03 +01:00
bridgeCommand("cutOrCopy");
return true;
}
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-01-29 20:13:02 +01:00
connectedCallback() {
this.setAttribute("contenteditable", "");
}
}
customElements.define("anki-editable", Editable);
class EditingArea extends HTMLDivElement {
editable: Editable;
baseStyle: HTMLStyleElement;
constructor() {
super();
this.attachShadow({ mode: "open" });
this.className = "field";
const rootStyle = document.createElement("link");
rootStyle.setAttribute("rel", "stylesheet");
rootStyle.setAttribute("href", "./_anki/css/editable.css");
2021-02-08 17:00:27 +01:00
this.shadowRoot!.appendChild(rootStyle);
this.baseStyle = document.createElement("style");
this.baseStyle.setAttribute("rel", "stylesheet");
2021-02-08 17:00:27 +01:00
this.shadowRoot!.appendChild(this.baseStyle);
this.editable = document.createElement("anki-editable") as Editable;
2021-02-08 17:00:27 +01:00
this.shadowRoot!.appendChild(this.editable);
}
get ord(): number {
return Number(this.getAttribute("ord"));
}
2021-01-29 20:13:02 +01:00
set fieldHTML(content: string) {
this.editable.fieldHTML = content;
2021-01-29 20:13:02 +01:00
}
get fieldHTML(): string {
return this.editable.fieldHTML;
2021-01-29 20:13:02 +01:00
}
connectedCallback(): void {
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);
const baseStyleSheet = this.baseStyle.sheet as CSSStyleSheet;
baseStyleSheet.insertRule("anki-editable {}", 0);
}
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);
}
initialize(color: string, content: string): void {
this.setBaseColor(color);
this.editable.fieldHTML = content;
}
setBaseColor(color: string): void {
const styleSheet = this.baseStyle.sheet as CSSStyleSheet;
const firstRule = styleSheet.cssRules[0] as CSSStyleRule;
firstRule.style.color = color;
}
2021-01-28 18:37:38 +01:00
setBaseStyling(fontFamily: string, fontSize: string, direction: string): void {
const styleSheet = this.baseStyle.sheet as CSSStyleSheet;
const firstRule = styleSheet.cssRules[0] as CSSStyleRule;
firstRule.style.fontFamily = fontFamily;
firstRule.style.fontSize = fontSize;
firstRule.style.direction = direction;
}
2021-01-28 18:37:38 +01:00
isRightToLeft(): boolean {
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
}
focusEditable(): void {
this.editable.focus();
}
blurEditable(): void {
this.editable.blur();
}
}
customElements.define("anki-editing-area", EditingArea, { extends: "div" });
class EditorField extends HTMLDivElement {
labelContainer: HTMLDivElement;
label: HTMLSpanElement;
editingArea: EditingArea;
constructor() {
super();
this.labelContainer = document.createElement("div");
this.labelContainer.className = "fname";
this.appendChild(this.labelContainer);
this.label = document.createElement("span");
this.label.className = "fieldname";
this.labelContainer.appendChild(this.label);
this.editingArea = document.createElement("div", {
is: "anki-editing-area",
}) as EditingArea;
this.appendChild(this.editingArea);
}
static get observedAttributes(): string[] {
return ["ord"];
}
2021-01-29 20:13:02 +01:00
set ord(n: number) {
this.setAttribute("ord", String(n));
}
attributeChangedCallback(name: string, _oldValue: string, newValue: string): void {
switch (name) {
case "ord":
this.editingArea.setAttribute("ord", newValue);
}
}
initialize(label: string, color: string, content: string): void {
this.label.innerText = label;
this.editingArea.initialize(color, content);
}
2021-01-28 18:37:38 +01:00
setBaseStyling(fontFamily: string, fontSize: string, direction: string): void {
this.editingArea.setBaseStyling(fontFamily, fontSize, direction);
2021-01-28 18:37:38 +01:00
}
}
customElements.define("anki-editor-field", EditorField, { extends: "div" });
function adjustFieldAmount(amount: number): void {
2021-02-08 17:00:27 +01:00
const fieldsContainer = document.getElementById("fields")!;
while (fieldsContainer.childElementCount < amount) {
const newField = document.createElement("div", {
is: "anki-editor-field",
}) as EditorField;
newField.ord = fieldsContainer.childElementCount;
fieldsContainer.appendChild(newField);
}
while (fieldsContainer.childElementCount > amount) {
2021-02-08 17:00:27 +01:00
fieldsContainer.removeChild(fieldsContainer.lastElementChild as Node);
}
}
export function getEditorField(n: number): EditorField | null {
2021-02-08 17:00:27 +01:00
const fields = document.getElementById("fields")!.children;
return (fields[n] as EditorField) ?? null;
}
export function forEditorField<T>(
values: T[],
func: (field: EditorField, value: T) => void
): void {
2021-02-08 17:00:27 +01:00
const fields = document.getElementById("fields")!.children;
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 {
// 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");
adjustFieldAmount(fields.length);
forEditorField(fields, (field, [name, fieldContent]) =>
field.initialize(name, color, fieldContent)
);
maybeDisableButtons();
2017-07-28 08:48:49 +02:00
}
2021-01-30 17:54:07 +01:00
export function setBackgrounds(cols: ("dupe" | "")[]) {
forEditorField(cols, (field, value) =>
field.editingArea.classList.toggle("dupe", value === "dupe")
);
document
2021-02-08 17:00:27 +01:00
.getElementById("dupes")!
.classList.toggle("is-inactive", !cols.includes("dupe"));
}
2021-01-30 17:54:07 +01:00
export function setFonts(fonts: [string, number, boolean][]): void {
forEditorField(fonts, (field, [fontFamily, fontSize, isRtl]) => {
2021-01-28 18:37:38 +01:00
field.setBaseStyling(fontFamily, `${fontSize}px`, isRtl ? "rtl" : "ltr");
});
}
2021-01-30 17:54:07 +01:00
export function setNoteId(id: number): void {
currentNoteId = id;
}
2021-02-08 17:00:27 +01:00
export function getCurrentNoteId(): number | null {
return currentNoteId;
}
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 {
html = filterHTML(html, internal, extendedMode);
if (html !== "") {
setFormat("inserthtml", html);
}
};