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-08-02 23:12:00 +02:00
|
|
|
@typescript-eslint/no-explicit-any: "off",
|
2021-05-06 23:33:28 +02:00
|
|
|
*/
|
|
|
|
|
2021-03-25 05:36:45 +01:00
|
|
|
import { filterHTML } from "html-filter";
|
2021-07-05 18:15:03 +02:00
|
|
|
import { updateActiveButtons } from "./toolbar";
|
2021-04-22 19:55:26 +02:00
|
|
|
import { setupI18n, ModuleName } from "lib/i18n";
|
2021-07-31 03:43:10 +02:00
|
|
|
import { isApplePlatform } from "lib/platform";
|
|
|
|
import { registerShortcut } from "lib/shortcuts";
|
|
|
|
import { bridgeCommand } from "lib/bridgecommand";
|
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-06-30 19:55:56 +02:00
|
|
|
import { saveField } from "./change-timer";
|
2021-02-28 14:12:48 +01:00
|
|
|
|
2021-06-30 19:55:56 +02:00
|
|
|
import { EditorField } from "./editor-field";
|
|
|
|
import { LabelContainer } from "./label-container";
|
|
|
|
import { EditingArea } from "./editing-area";
|
2021-02-28 14:12:48 +01:00
|
|
|
import { Editable } from "./editable";
|
2021-06-17 13:46:25 +02:00
|
|
|
import { Codable } from "./codable";
|
2021-07-05 18:15:03 +02:00
|
|
|
import { initToolbar, fieldFocused } from "./toolbar";
|
2021-01-30 17:54:07 +01:00
|
|
|
|
2021-06-30 19:55:56 +02:00
|
|
|
export { setNoteId, getNoteId } from "./note-id";
|
|
|
|
export { saveNow } from "./change-timer";
|
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-08-03 05:52:07 +02:00
|
|
|
export { activateStickyShortcuts } from "./label-container";
|
2021-07-05 16:19:03 +02:00
|
|
|
export { components } from "./Components.svelte";
|
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);
|
2021-06-17 13:46:25 +02:00
|
|
|
customElements.define("anki-codable", Codable, { extends: "textarea" });
|
2021-02-28 14:12:48 +01:00
|
|
|
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-07-31 03:43:10 +02:00
|
|
|
if (isApplePlatform()) {
|
|
|
|
registerShortcut(() => bridgeCommand("paste"), "Control+Shift+V");
|
|
|
|
}
|
|
|
|
|
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-06-17 22:02:06 +02:00
|
|
|
field.editingArea.focus();
|
2021-06-17 21:36:56 +02:00
|
|
|
field.editingArea.caretToEnd();
|
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) {
|
2021-06-17 22:02:06 +02:00
|
|
|
elem.focus();
|
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-07-05 18:15:03 +02:00
|
|
|
fieldFocused.set(false);
|
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-06-14 10:21:42 +02:00
|
|
|
export function setClozeHint(cloze_hint: string): void {
|
|
|
|
document.getElementById("cloze-hint")!.innerHTML = cloze_hint;
|
2021-06-12 17:35:40 +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-08-02 23:12:00 +02:00
|
|
|
export function setColorButtons([textColor, highlightColor]: [string, string]): void {
|
|
|
|
$editorToolbar.then((editorToolbar) =>
|
|
|
|
(editorToolbar as any).$set({ textColor, highlightColor })
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-08-03 06:02:29 +02:00
|
|
|
export const i18n = setupI18n({
|
2021-05-07 02:14:54 +02:00
|
|
|
modules: [
|
|
|
|
ModuleName.EDITING,
|
|
|
|
ModuleName.KEYBOARD,
|
|
|
|
ModuleName.ACTIONS,
|
|
|
|
ModuleName.BROWSING,
|
|
|
|
],
|
2021-05-06 16:10:26 +02:00
|
|
|
});
|
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);
|