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 20:32:21 +01:00
|
|
|
let currentField: EditingArea | null = null;
|
2021-01-29 00:43:45 +01:00
|
|
|
let changeTimer: number | null = null;
|
|
|
|
let currentNoteId: number | null = null;
|
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-01-30 17:54:07 +01:00
|
|
|
export function setFGButton(col: string): void {
|
2021-01-28 19:52:49 +01:00
|
|
|
document.getElementById("forecolor").style.backgroundColor = col;
|
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 saveNow(keepFocus: boolean): void {
|
2018-05-28 05:40:35 +02:00
|
|
|
if (!currentField) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
clearChangeTimer();
|
2018-05-28 05:40:35 +02:00
|
|
|
|
|
|
|
if (keepFocus) {
|
|
|
|
saveField("key");
|
|
|
|
} else {
|
2019-02-05 01:46:47 +01:00
|
|
|
// triggers onBlur, which saves
|
2021-01-29 20:32:21 +01:00
|
|
|
currentField.blurEditable();
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function triggerKeyTimer(): void {
|
2018-07-23 04:23:38 +02:00
|
|
|
clearChangeTimer();
|
2020-06-24 13:54:38 +02:00
|
|
|
changeTimer = setTimeout(function () {
|
2018-07-23 04:23:38 +02:00
|
|
|
updateButtonState();
|
|
|
|
saveField("key");
|
|
|
|
}, 600);
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function onKey(evt: KeyboardEvent): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
// esc clears focus, allowing dialog to close
|
2021-01-16 20:23:46 +01:00
|
|
|
if (evt.code === "Escape") {
|
2021-01-29 20:32:21 +01:00
|
|
|
currentField.blurEditable();
|
2017-07-28 08:19:06 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-08-11 02:09:03 +02:00
|
|
|
|
2021-01-14 13:32:30 +01:00
|
|
|
// prefer <br> instead of <div></div>
|
2021-01-16 20:23:46 +01:00
|
|
|
if (evt.code === "Enter" && !inListItem()) {
|
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;
|
2020-08-11 02:09:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:38 +02:00
|
|
|
triggerKeyTimer();
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function onKeyUp(evt: KeyboardEvent): void {
|
2021-01-15 18:18:42 +01:00
|
|
|
// Avoid div element on remove
|
2021-01-16 20:23:46 +01:00
|
|
|
if (evt.code === "Enter" || evt.code === "Backspace") {
|
2021-01-28 20:19:04 +01:00
|
|
|
const anchor = currentField.getSelection().anchorNode;
|
2021-01-15 18:18:42 +01:00
|
|
|
|
|
|
|
if (
|
|
|
|
nodeIsElement(anchor) &&
|
|
|
|
anchor.tagName === "DIV" &&
|
2021-01-29 20:32:21 +01:00
|
|
|
!(anchor instanceof EditingArea) &&
|
2021-01-15 18:18:42 +01:00
|
|
|
anchor.childElementCount === 1 &&
|
|
|
|
anchor.children[0].tagName === "BR"
|
|
|
|
) {
|
|
|
|
anchor.replaceWith(anchor.children[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 18:59:07 +01:00
|
|
|
function inListItem(): boolean {
|
2021-01-28 17:43:56 +01:00
|
|
|
const anchor = currentField.getSelection().anchorNode;
|
2021-01-14 18:59:07 +01:00
|
|
|
|
|
|
|
let inList = false;
|
2021-01-26 23:49:48 +01:00
|
|
|
let n = nodeIsElement(anchor) ? anchor : anchor.parentElement;
|
2021-01-14 18:59:07 +01:00
|
|
|
while (n) {
|
|
|
|
inList = inList || window.getComputedStyle(n).display == "list-item";
|
|
|
|
n = n.parentElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inList;
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function onInput(): void {
|
2018-07-23 04:23:38 +02:00
|
|
|
// make sure IME changes get saved
|
|
|
|
triggerKeyTimer();
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function updateButtonState(): void {
|
2019-12-20 22:33:15 +01:00
|
|
|
const buts = ["bold", "italic", "underline", "superscript", "subscript"];
|
|
|
|
for (const name of buts) {
|
2021-01-19 03:06:44 +01:00
|
|
|
const elem = document.querySelector(`#${name}`) as HTMLElement;
|
|
|
|
elem.classList.toggle("highlighted", document.queryCommandState(name));
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// fixme: forecolor
|
2019-12-18 07:12:39 +01:00
|
|
|
// 'col': document.queryCommandValue("forecolor")
|
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 toggleEditorButton(buttonid: string): void {
|
2021-01-19 03:06:44 +01:00
|
|
|
const button = $(buttonid)[0];
|
|
|
|
button.classList.toggle("highlighted");
|
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 setFormat(cmd: string, arg?: any, nosave: boolean = false): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
document.execCommand(cmd, false, arg);
|
|
|
|
if (!nosave) {
|
2019-12-18 07:12:39 +01:00
|
|
|
saveField("key");
|
2017-07-28 08:19:06 +02:00
|
|
|
updateButtonState();
|
|
|
|
}
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function clearChangeTimer(): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
if (changeTimer) {
|
|
|
|
clearTimeout(changeTimer);
|
|
|
|
changeTimer = null;
|
|
|
|
}
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-26 23:00:55 +01:00
|
|
|
function onFocus(evt: FocusEvent): void {
|
2021-01-29 20:32:21 +01:00
|
|
|
const elem = evt.currentTarget as EditingArea;
|
2018-04-30 07:26:54 +02:00
|
|
|
if (currentField === elem) {
|
|
|
|
// anki window refocused; current element unchanged
|
|
|
|
return;
|
|
|
|
}
|
2021-01-29 20:32:21 +01:00
|
|
|
elem.focusEditable();
|
2017-07-28 08:19:06 +02:00
|
|
|
currentField = elem;
|
2021-01-31 14:15:03 +01:00
|
|
|
bridgeCommand(`focus:${currentField.ord}`);
|
2017-07-28 08:19:06 +02:00
|
|
|
enableButtons();
|
|
|
|
// do this twice so that there's no flicker on newer versions
|
|
|
|
caretToEnd();
|
|
|
|
// scroll if bottom of element off the screen
|
2021-01-18 17:05:05 +01:00
|
|
|
function pos(elem: HTMLElement): number {
|
2019-12-20 22:33:15 +01:00
|
|
|
let cur = 0;
|
2017-07-28 08:19:06 +02:00
|
|
|
do {
|
2021-01-18 17:05:05 +01:00
|
|
|
cur += elem.offsetTop;
|
|
|
|
elem = elem.offsetParent as HTMLElement;
|
|
|
|
} while (elem);
|
2017-07-28 08:19:06 +02:00
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:33:15 +01:00
|
|
|
const y = pos(elem);
|
2019-12-18 07:12:39 +01:00
|
|
|
if (
|
|
|
|
window.pageYOffset + window.innerHeight < y + elem.offsetHeight ||
|
|
|
|
window.pageYOffset > y
|
|
|
|
) {
|
2017-07-28 08:19:06 +02:00
|
|
|
window.scroll(0, y + elem.offsetHeight - window.innerHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// the focus event may not fire if the window is not active, so make sure
|
|
|
|
// the current field is set
|
|
|
|
currentField = elem;
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-19 06:37:14 +02:00
|
|
|
}
|
|
|
|
return false;
|
2017-08-01 09:40:51 +02:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:42:49 +01:00
|
|
|
function onPaste(event: ClipboardEvent): void {
|
2021-01-31 14:15:03 +01:00
|
|
|
bridgeCommand("paste");
|
2021-02-05 13:42:49 +01:00
|
|
|
event.preventDefault();
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function caretToEnd(): void {
|
2021-01-29 00:43:45 +01:00
|
|
|
const range = document.createRange();
|
2021-01-29 20:32:21 +01:00
|
|
|
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
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function onBlur(): void {
|
2019-02-05 01:46:47 +01:00
|
|
|
if (!currentField) {
|
|
|
|
return;
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
2019-01-29 00:51:54 +01:00
|
|
|
|
|
|
|
if (document.activeElement === currentField) {
|
|
|
|
// other widget or window focused; current field unchanged
|
2019-02-05 01:46:47 +01:00
|
|
|
saveField("key");
|
|
|
|
} else {
|
|
|
|
saveField("blur");
|
|
|
|
currentField = null;
|
|
|
|
disableButtons();
|
2019-01-29 00:51:54 +01:00
|
|
|
}
|
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-18 17:05:05 +01:00
|
|
|
function saveField(type: "blur" | "key"): void {
|
2019-02-05 01:46:47 +01:00
|
|
|
clearChangeTimer();
|
2017-07-28 08:19:06 +02:00
|
|
|
if (!currentField) {
|
|
|
|
// no field has been focused yet
|
|
|
|
return;
|
|
|
|
}
|
2021-01-26 21:26:04 +01:00
|
|
|
|
2021-01-31 14:15:03 +01:00
|
|
|
bridgeCommand(
|
|
|
|
`${type}:${currentField.ord}:${currentNoteId}:${currentField.fieldHTML}`
|
|
|
|
);
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function wrappedExceptForWhitespace(text: string, front: string, back: string): string {
|
2019-12-20 22:33:15 +01:00
|
|
|
const match = text.match(/^(\s*)([^]*?)(\s*)$/);
|
2017-07-28 08:19:06 +02:00
|
|
|
return match[1] + front + match[2] + back + match[3];
|
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 preventButtonFocus(): void {
|
2021-01-20 17:01:16 +01:00
|
|
|
for (const element of document.querySelectorAll("button.linkb")) {
|
|
|
|
element.addEventListener("mousedown", (evt: Event) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function disableButtons(): void {
|
2017-12-08 12:06:16 +01:00
|
|
|
$("button.linkb:not(.perm)").prop("disabled", true);
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function enableButtons(): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
$("button.linkb").prop("disabled", false);
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
|
|
|
// disable the buttons if a field is not currently focused
|
2021-01-18 17:05:05 +01:00
|
|
|
function maybeDisableButtons(): void {
|
2021-01-29 20:32:21 +01:00
|
|
|
if (document.activeElement instanceof EditingArea) {
|
2017-07-28 08:19:06 +02:00
|
|
|
enableButtons();
|
2021-01-29 17:41:27 +01:00
|
|
|
} else {
|
|
|
|
disableButtons();
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
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 wrap(front: string, back: string): void {
|
2019-12-17 09:27:41 +01:00
|
|
|
wrapInternal(front, back, false);
|
|
|
|
}
|
|
|
|
|
2020-01-16 01:33:36 +01:00
|
|
|
/* currently unused */
|
2021-02-05 13:41:35 +01:00
|
|
|
export function wrapIntoText(front: string, back: string): void {
|
2019-12-17 09:27:41 +01:00
|
|
|
wrapInternal(front, back, true);
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function wrapInternal(front: string, back: string, plainText: boolean): void {
|
2021-01-28 20:19:04 +01:00
|
|
|
const s = currentField.getSelection();
|
2019-12-20 22:33:15 +01:00
|
|
|
let r = s.getRangeAt(0);
|
2019-12-21 11:18:58 +01:00
|
|
|
const content = r.cloneContents();
|
|
|
|
const span = document.createElement("span");
|
2017-07-28 08:19:06 +02:00
|
|
|
span.appendChild(content);
|
2019-12-17 09:27:41 +01:00
|
|
|
if (plainText) {
|
2019-12-20 22:33:15 +01:00
|
|
|
const new_ = wrappedExceptForWhitespace(span.innerText, front, back);
|
2019-12-17 09:27:41 +01:00
|
|
|
setFormat("inserttext", new_);
|
|
|
|
} else {
|
2019-12-20 22:33:15 +01:00
|
|
|
const new_ = wrappedExceptForWhitespace(span.innerHTML, front, back);
|
2019-12-17 09:27:41 +01:00
|
|
|
setFormat("inserthtml", new_);
|
|
|
|
}
|
2017-07-28 08:19:06 +02: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
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function onCutOrCopy(): boolean {
|
2021-01-31 14:15:03 +01:00
|
|
|
bridgeCommand("cutOrCopy");
|
2017-09-02 05:48:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-01-29 20:32:21 +01:00
|
|
|
class EditingArea extends HTMLDivElement {
|
|
|
|
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-01-29 19:34:44 +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-01-29 19:34:44 +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;
|
|
|
|
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-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-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-01-29 15:50:34 +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 {
|
|
|
|
const fieldsContainer = document.getElementById("fields");
|
|
|
|
|
|
|
|
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) {
|
|
|
|
fieldsContainer.removeChild(fieldsContainer.lastElementChild);
|
|
|
|
}
|
2021-01-26 23:00:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 20:56:28 +01:00
|
|
|
export function getEditorField(n: number): EditorField | null {
|
2021-01-30 14:20:14 +01:00
|
|
|
const fields = document.getElementById("fields").children;
|
|
|
|
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-01-29 19:34:44 +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 {
|
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
|
|
|
|
.querySelector("#dupes")
|
|
|
|
.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-01-30 17:54:07 +01:00
|
|
|
export function setNoteId(id: number): void {
|
2018-04-30 06:51:49 +02:00
|
|
|
currentNoteId = id;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|