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
|
2019-02-05 04:59:03 +01:00
|
|
|
|
2021-05-06 23:33:28 +02:00
|
|
|
/* eslint
|
|
|
|
@typescript-eslint/no-non-null-assertion: "off",
|
|
|
|
*/
|
|
|
|
|
2021-03-25 05:36:45 +01:00
|
|
|
import { filterHTML } from "html-filter";
|
2021-04-27 23:08:47 +02:00
|
|
|
import { updateActiveButtons, disableButtons } from "./toolbar";
|
2021-04-22 19:55:26 +02:00
|
|
|
import { setupI18n, ModuleName } from "lib/i18n";
|
2021-04-20 02:07:15 +02:00
|
|
|
|
2021-04-19 23:51:09 +02:00
|
|
|
import "./fields.css";
|
2021-03-25 00:43:01 +01:00
|
|
|
|
2021-02-28 14:12:48 +01:00
|
|
|
import { caretToEnd } from "./helpers";
|
2021-02-08 19:45:42 +01:00
|
|
|
import { saveField } from "./changeTimer";
|
2021-02-28 14:12:48 +01:00
|
|
|
|
|
|
|
import { EditorField } from "./editorField";
|
|
|
|
import { LabelContainer } from "./labelContainer";
|
|
|
|
import { EditingArea } from "./editingArea";
|
|
|
|
import { Editable } from "./editable";
|
2021-04-20 15:32:02 +02:00
|
|
|
import { initToolbar } from "./toolbar";
|
2021-01-30 17:54:07 +01:00
|
|
|
|
2021-02-08 19:45:42 +01:00
|
|
|
export { setNoteId, getNoteId } from "./noteId";
|
2021-02-08 20:28:02 +01:00
|
|
|
export { saveNow } from "./changeTimer";
|
2021-02-08 21:02:46 +01:00
|
|
|
export { wrap, wrapIntoText } from "./wrap";
|
2021-05-05 01:22:51 +02:00
|
|
|
export { editorToolbar } from "./toolbar";
|
2021-04-20 02:07:15 +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-28 14:12:48 +01:00
|
|
|
customElements.define("anki-editable", Editable);
|
|
|
|
customElements.define("anki-editing-area", EditingArea, { extends: "div" });
|
|
|
|
customElements.define("anki-label-container", LabelContainer, { extends: "div" });
|
|
|
|
customElements.define("anki-editor-field", EditorField, { extends: "div" });
|
|
|
|
|
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();
|
2021-03-08 20:40:23 +01:00
|
|
|
caretToEnd(field.editingArea);
|
2021-04-28 13:09:25 +02:00
|
|
|
updateActiveButtons(new Event("manualfocus"));
|
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-05-06 23:33:28 +02:00
|
|
|
const elem = elements[i] as EditingArea;
|
2021-01-29 20:32:21 +01:00
|
|
|
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 21:02:46 +01:00
|
|
|
export function pasteHTML(
|
|
|
|
html: string,
|
|
|
|
internal: boolean,
|
|
|
|
extendedMode: boolean
|
|
|
|
): void {
|
|
|
|
html = filterHTML(html, internal, extendedMode);
|
|
|
|
|
|
|
|
if (html !== "") {
|
|
|
|
setFormat("inserthtml", html);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-29 10:13:45 +02:00
|
|
|
/// forEachEditorFieldAndProvidedValue:
|
|
|
|
/// Values should be a list with the same length as the
|
|
|
|
/// number of fields. Func will be called with each field and
|
|
|
|
/// value in turn.
|
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-03-28 23:59:16 +02:00
|
|
|
forEditorField(
|
|
|
|
fields,
|
|
|
|
(field: EditorField, [name, fieldContent]: [string, string]): void =>
|
|
|
|
field.initialize(name, color, fieldContent)
|
2021-01-28 19:13:39 +01:00
|
|
|
);
|
2021-03-10 19:28:08 +01:00
|
|
|
|
|
|
|
if (!getCurrentField()) {
|
|
|
|
// when initial focus of the window is not on editor (e.g. browser)
|
2021-04-19 23:51:09 +02:00
|
|
|
disableButtons();
|
2021-03-10 19:28:08 +01:00
|
|
|
}
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-02-28 15:00:42 +01:00
|
|
|
export function setBackgrounds(cols: ("dupe" | "")[]): void {
|
2021-03-28 23:59:16 +02:00
|
|
|
forEditorField(cols, (field: EditorField, value: "dupe" | "") =>
|
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-03-28 23:59:16 +02:00
|
|
|
forEditorField(
|
|
|
|
fonts,
|
|
|
|
(
|
|
|
|
field: EditorField,
|
|
|
|
[fontFamily, fontSize, isRtl]: [string, number, boolean]
|
|
|
|
) => {
|
|
|
|
field.setBaseStyling(fontFamily, `${fontSize}px`, isRtl ? "rtl" : "ltr");
|
|
|
|
}
|
|
|
|
);
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:17:42 +01:00
|
|
|
export function setSticky(stickies: boolean[]): void {
|
2021-03-28 23:59:16 +02:00
|
|
|
forEditorField(stickies, (field: EditorField, isSticky: boolean) => {
|
2021-02-28 01:17:42 +01:00
|
|
|
field.labelContainer.activateSticky(isSticky);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-06 23:33:28 +02:00
|
|
|
export function setFormat(cmd: string, arg?: string, nosave = false): void {
|
2021-02-08 19:45:42 +01:00
|
|
|
document.execCommand(cmd, false, arg);
|
|
|
|
if (!nosave) {
|
|
|
|
saveField(getCurrentField() as EditingArea, "key");
|
2021-04-28 13:09:25 +02:00
|
|
|
updateActiveButtons(new Event(cmd));
|
2021-02-08 19:45:42 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-20 02:07:15 +02:00
|
|
|
|
2021-05-06 16:10:26 +02:00
|
|
|
const i18n = setupI18n({
|
|
|
|
modules: [ModuleName.EDITING, ModuleName.KEYBOARD, ModuleName.ACTIONS, ModuleName.BROWSING],
|
|
|
|
});
|
2021-04-20 02:07:15 +02:00
|
|
|
|
2021-04-27 23:08:47 +02:00
|
|
|
import type EditorToolbar from "./EditorToolbar.svelte";
|
|
|
|
|
|
|
|
export const $editorToolbar: Promise<EditorToolbar> = initToolbar(i18n);
|