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 */
|
|
|
|
|
2019-12-20 22:33:15 +01:00
|
|
|
let currentField = null;
|
|
|
|
let changeTimer = null;
|
|
|
|
let currentNoteId = null;
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2019-12-18 04:53:31 +01:00
|
|
|
declare interface String {
|
2021-01-18 17:42:29 +01:00
|
|
|
format(...args: string[]): string;
|
2019-12-18 04:53:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* kept for compatibility with add-ons */
|
2021-01-18 17:42:29 +01:00
|
|
|
String.prototype.format = function (...args: string[]): string {
|
2021-01-19 02:48:41 +01:00
|
|
|
return this.replace(/\{\d+\}/g, (m: string): void => {
|
2021-01-19 03:23:29 +01:00
|
|
|
const match = m.match(/\d+/);
|
2021-01-19 02:48:41 +01:00
|
|
|
|
2021-01-19 03:23:29 +01:00
|
|
|
return match ? args[match[0]] : "";
|
|
|
|
});
|
2017-07-28 08:19:06 +02:00
|
|
|
};
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function setFGButton(col: string): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
$("#forecolor")[0].style.backgroundColor = col;
|
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 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
|
2017-07-28 08:19:06 +02:00
|
|
|
currentField.blur();
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2020-08-11 02:32:58 +02:00
|
|
|
interface Selection {
|
|
|
|
modify(s: string, t: string, u: string): void;
|
|
|
|
}
|
|
|
|
|
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") {
|
2017-07-28 08:19:06 +02:00
|
|
|
currentField.blur();
|
|
|
|
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");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-11 02:09:03 +02:00
|
|
|
// fix Ctrl+right/left handling in RTL fields
|
|
|
|
if (currentField.dir === "rtl") {
|
|
|
|
const selection = window.getSelection();
|
|
|
|
let granularity = "character";
|
|
|
|
let alter = "move";
|
|
|
|
if (evt.ctrlKey) {
|
|
|
|
granularity = "word";
|
|
|
|
}
|
|
|
|
if (evt.shiftKey) {
|
|
|
|
alter = "extend";
|
|
|
|
}
|
2021-01-16 20:23:46 +01:00
|
|
|
if (evt.code === "ArrowRight") {
|
2020-08-11 02:09:03 +02:00
|
|
|
selection.modify(alter, "right", granularity);
|
|
|
|
evt.preventDefault();
|
|
|
|
return;
|
2021-01-16 20:23:46 +01:00
|
|
|
} else if (evt.code === "ArrowLeft") {
|
2020-08-11 02:09:03 +02:00
|
|
|
selection.modify(alter, "left", granularity);
|
|
|
|
evt.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-15 18:18:42 +01:00
|
|
|
const anchor = window.getSelection().anchorNode;
|
|
|
|
|
|
|
|
if (
|
|
|
|
nodeIsElement(anchor) &&
|
|
|
|
anchor.tagName === "DIV" &&
|
|
|
|
!anchor.classList.contains("field") &&
|
|
|
|
anchor.childElementCount === 1 &&
|
|
|
|
anchor.children[0].tagName === "BR"
|
|
|
|
) {
|
|
|
|
anchor.replaceWith(anchor.children[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 18:59:07 +01:00
|
|
|
function nodeIsElement(node: Node): node is Element {
|
2021-01-18 17:05:05 +01:00
|
|
|
return node.nodeType === Node.ELEMENT_NODE;
|
2021-01-14 18:59:07 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 23:42:04 +01:00
|
|
|
const INLINE_TAGS = [
|
|
|
|
"A",
|
|
|
|
"ABBR",
|
|
|
|
"ACRONYM",
|
|
|
|
"AUDIO",
|
|
|
|
"B",
|
|
|
|
"BDI",
|
|
|
|
"BDO",
|
|
|
|
"BIG",
|
|
|
|
"BR",
|
|
|
|
"BUTTON",
|
|
|
|
"CANVAS",
|
|
|
|
"CITE",
|
|
|
|
"CODE",
|
|
|
|
"DATA",
|
|
|
|
"DATALIST",
|
|
|
|
"DEL",
|
|
|
|
"DFN",
|
|
|
|
"EM",
|
|
|
|
"EMBED",
|
|
|
|
"I",
|
|
|
|
"IFRAME",
|
|
|
|
"IMG",
|
|
|
|
"INPUT",
|
|
|
|
"INS",
|
|
|
|
"KBD",
|
|
|
|
"LABEL",
|
|
|
|
"MAP",
|
|
|
|
"MARK",
|
|
|
|
"METER",
|
|
|
|
"NOSCRIPT",
|
|
|
|
"OBJECT",
|
|
|
|
"OUTPUT",
|
|
|
|
"PICTURE",
|
|
|
|
"PROGRESS",
|
|
|
|
"Q",
|
|
|
|
"RUBY",
|
|
|
|
"S",
|
|
|
|
"SAMP",
|
|
|
|
"SCRIPT",
|
|
|
|
"SELECT",
|
|
|
|
"SLOT",
|
|
|
|
"SMALL",
|
|
|
|
"SPAN",
|
|
|
|
"STRONG",
|
|
|
|
"SUB",
|
|
|
|
"SUP",
|
|
|
|
"SVG",
|
|
|
|
"TEMPLATE",
|
|
|
|
"TEXTAREA",
|
|
|
|
"TIME",
|
|
|
|
"U",
|
|
|
|
"TT",
|
|
|
|
"VAR",
|
|
|
|
"VIDEO",
|
|
|
|
"WBR",
|
|
|
|
];
|
|
|
|
|
2021-01-26 23:00:55 +01:00
|
|
|
function nodeIsInline(node: Node): boolean {
|
2021-01-26 23:49:48 +01:00
|
|
|
return !nodeIsElement(node) || INLINE_TAGS.includes(node.tagName);
|
2021-01-26 23:00:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 18:59:07 +01:00
|
|
|
function inListItem(): boolean {
|
|
|
|
const anchor = window.getSelection().anchorNode;
|
|
|
|
|
|
|
|
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 insertNewline(): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
if (!inPreEnvironment()) {
|
2017-07-31 05:22:35 +02:00
|
|
|
setFormat("insertText", "\n");
|
2017-07-28 08:19:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// in some cases inserting a newline will not show any changes,
|
|
|
|
// as a trailing newline at the end of a block does not render
|
|
|
|
// differently. so in such cases we note the height has not
|
|
|
|
// changed and insert an extra newline.
|
|
|
|
|
2019-12-20 22:33:15 +01:00
|
|
|
const r = window.getSelection().getRangeAt(0);
|
2017-07-28 08:19:06 +02:00
|
|
|
if (!r.collapsed) {
|
|
|
|
// delete any currently selected text first, making
|
|
|
|
// sure the delete is undoable
|
|
|
|
setFormat("delete");
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:33:15 +01:00
|
|
|
const oldHeight = currentField.clientHeight;
|
2017-07-31 05:22:35 +02:00
|
|
|
setFormat("inserthtml", "\n");
|
2017-07-28 08:48:49 +02:00
|
|
|
if (currentField.clientHeight === oldHeight) {
|
2017-07-31 05:22:35 +02:00
|
|
|
setFormat("inserthtml", "\n");
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// is the cursor in an environment that respects whitespace?
|
2021-01-14 18:59:07 +01:00
|
|
|
function inPreEnvironment(): boolean {
|
|
|
|
const anchor = window.getSelection().anchorNode;
|
|
|
|
const n = nodeIsElement(anchor) ? anchor : anchor.parentElement;
|
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
return window.getComputedStyle(n).whiteSpace.startsWith("pre");
|
|
|
|
}
|
|
|
|
|
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-18 17:05:05 +01:00
|
|
|
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-18 17:05:05 +01:00
|
|
|
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 {
|
|
|
|
const elem = evt.currentTarget as HTMLElement;
|
2018-04-30 07:26:54 +02:00
|
|
|
if (currentField === elem) {
|
|
|
|
// anki window refocused; current element unchanged
|
|
|
|
return;
|
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
currentField = elem;
|
2021-01-18 17:42:29 +01:00
|
|
|
pycmd(`focus:${currentFieldOrdinal()}`);
|
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-18 17:05:05 +01:00
|
|
|
function focusField(n: number): void {
|
2017-08-05 07:15:19 +02:00
|
|
|
if (n === null) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-18 17:42:29 +01:00
|
|
|
$(`#f${n}`).focus();
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
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++) {
|
|
|
|
let elem = elements[i] as HTMLElement;
|
|
|
|
if (elem.classList.contains("field")) {
|
|
|
|
elem.focus();
|
|
|
|
// 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-01-18 17:05:05 +01:00
|
|
|
function onPaste(): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
pycmd("paste");
|
|
|
|
window.event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function caretToEnd(): void {
|
2019-12-21 11:18:58 +01:00
|
|
|
const r = document.createRange();
|
2017-07-28 08:19:06 +02:00
|
|
|
r.selectNodeContents(currentField);
|
|
|
|
r.collapse(false);
|
2019-12-21 11:18:58 +01:00
|
|
|
const s = document.getSelection();
|
2017-07-28 08:19:06 +02:00
|
|
|
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 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-28 17:16:10 +01:00
|
|
|
pycmd(
|
|
|
|
`${type}:${currentFieldOrdinal()}:${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 currentFieldOrdinal(): string {
|
2017-08-05 07:15:19 +02:00
|
|
|
return currentField.id.substring(1);
|
|
|
|
}
|
|
|
|
|
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-20 17:01:16 +01:00
|
|
|
function preventButtonFocus(): void {
|
|
|
|
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 {
|
2020-04-23 04:10:21 +02:00
|
|
|
if (!document.activeElement || document.activeElement.className !== "field") {
|
2017-07-28 08:19:06 +02:00
|
|
|
disableButtons();
|
|
|
|
} else {
|
|
|
|
enableButtons();
|
|
|
|
}
|
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 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-01-18 17:05:05 +01:00
|
|
|
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 {
|
2019-12-21 11:18:58 +01:00
|
|
|
const s = window.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 {
|
2017-09-02 05:48:03 +02:00
|
|
|
pycmd("cutOrCopy");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:42:37 +01:00
|
|
|
class EditingArea extends HTMLElement {
|
|
|
|
connectedCallback() {
|
|
|
|
this.setAttribute("contenteditable", "true");
|
|
|
|
}
|
2021-01-28 17:16:10 +01:00
|
|
|
|
|
|
|
initialize(color: string): void {
|
|
|
|
this.style.color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-28 15:42:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("editing-area", EditingArea);
|
|
|
|
|
2021-01-28 17:16:10 +01:00
|
|
|
class EditingContainer extends HTMLDivElement {
|
|
|
|
editingArea: EditingArea;
|
2021-01-28 15:42:37 +01:00
|
|
|
|
2021-01-28 17:16:10 +01:00
|
|
|
connectedCallback(): void {
|
|
|
|
this.className = "field";
|
|
|
|
|
|
|
|
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 editingShadow = this.attachShadow({ mode: "open" });
|
|
|
|
|
|
|
|
const style = editingShadow.appendChild(document.createElement("link"));
|
|
|
|
style.setAttribute("rel", "stylesheet");
|
|
|
|
style.setAttribute("href", "./_anki/css/editing-area.css");
|
|
|
|
|
|
|
|
this.editingArea = editingShadow.appendChild(
|
|
|
|
document.createElement("editing-area")
|
|
|
|
) as EditingArea;
|
|
|
|
}
|
|
|
|
|
|
|
|
initialize(index: number, color: string, content: string): void {
|
|
|
|
this.id = `f${index}`;
|
|
|
|
this.editingArea.initialize(color);
|
|
|
|
this.editingArea.fieldHTML = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
set fieldHTML(content: string) {
|
|
|
|
this.editingArea.fieldHTML = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
get fieldHTML(): string {
|
|
|
|
return this.editingArea.fieldHTML;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("editing-container", EditingContainer, { extends: "div" });
|
|
|
|
|
|
|
|
class EditorField extends HTMLDivElement {
|
|
|
|
labelContainer: HTMLDivElement;
|
|
|
|
label: HTMLSpanElement;
|
|
|
|
editingContainer: EditingContainer;
|
|
|
|
|
|
|
|
connectedCallback(): void {
|
2021-01-28 15:42:37 +01:00
|
|
|
this.labelContainer = this.appendChild(document.createElement("div"));
|
|
|
|
this.labelContainer.className = "fname";
|
|
|
|
|
|
|
|
this.label = this.labelContainer.appendChild(document.createElement("span"));
|
|
|
|
this.label.className = "fieldname";
|
|
|
|
|
2021-01-28 17:16:10 +01:00
|
|
|
this.editingContainer = this.appendChild(
|
|
|
|
document.createElement("div", { is: "editing-container" })
|
|
|
|
) as EditingContainer;
|
2021-01-28 15:42:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
initialize(index: number, label: string, color: string, content: string): void {
|
|
|
|
this.labelContainer.id = `name${index}`;
|
|
|
|
this.label.innerText = label;
|
2021-01-28 17:16:10 +01:00
|
|
|
this.editingContainer.initialize(index, color, content);
|
2021-01-28 15:42:37 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 23:00:55 +01:00
|
|
|
|
2021-01-28 17:16:10 +01:00
|
|
|
customElements.define("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-28 17:16:10 +01:00
|
|
|
fieldsContainer.appendChild(
|
|
|
|
document.createElement("div", { is: "editor-field" })
|
|
|
|
);
|
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-18 17:05:05 +01:00
|
|
|
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-26 23:00:55 +01:00
|
|
|
|
2021-01-26 23:15:11 +01:00
|
|
|
const fieldsContainer = document.getElementById("fields");
|
2021-01-28 17:16:10 +01:00
|
|
|
const children = [...fieldsContainer.children] as EditorField[];
|
2021-01-28 15:42:37 +01:00
|
|
|
for (const [index, child] of children.entries()) {
|
|
|
|
const [name, fieldContent] = fields[index];
|
2021-01-28 17:16:10 +01:00
|
|
|
child.initialize(index, name, color, fieldContent);
|
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-19 01:08:15 +01:00
|
|
|
function setBackgrounds(cols: "dupe"[]) {
|
2019-12-20 22:33:15 +01:00
|
|
|
for (let i = 0; i < cols.length; i++) {
|
2021-01-19 03:23:29 +01:00
|
|
|
const element = document.querySelector(`#f${i}`);
|
|
|
|
element.classList.toggle("dupe", cols[i] === "dupe");
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function setFonts(fonts: [string, number, boolean][]): void {
|
2019-12-20 22:33:15 +01:00
|
|
|
for (let i = 0; i < fonts.length; i++) {
|
2021-01-18 17:42:29 +01:00
|
|
|
const n = $(`#f${i}`);
|
2019-12-18 07:12:39 +01:00
|
|
|
n.css("font-family", fonts[i][0]).css("font-size", fonts[i][1]);
|
2017-07-28 08:48:49 +02:00
|
|
|
n[0].dir = fonts[i][2] ? "rtl" : "ltr";
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function setNoteId(id: number): void {
|
2018-04-30 06:51:49 +02:00
|
|
|
currentNoteId = id;
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function showDupes(): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
$("#dupes").show();
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function hideDupes(): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
$("#dupes").hide();
|
|
|
|
}
|
|
|
|
|
2021-01-19 01:08:15 +01:00
|
|
|
let pasteHTML = function (
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-01-19 01:08:15 +01:00
|
|
|
let filterHTML = function (
|
|
|
|
html: string,
|
|
|
|
internal: boolean,
|
|
|
|
extendedMode: boolean
|
|
|
|
): string {
|
2017-07-28 08:19:06 +02:00
|
|
|
// wrap it in <top> as we aren't allowed to change top level elements
|
2021-01-19 03:23:29 +01:00
|
|
|
const top = document.createElement("ankitop");
|
2021-01-19 03:06:44 +01:00
|
|
|
top.innerHTML = html;
|
|
|
|
|
2017-10-25 09:51:22 +02:00
|
|
|
if (internal) {
|
|
|
|
filterInternalNode(top);
|
2019-12-18 07:12:39 +01:00
|
|
|
} else {
|
2017-10-25 12:20:28 +02:00
|
|
|
filterNode(top, extendedMode);
|
2017-10-25 09:51:22 +02:00
|
|
|
}
|
2019-12-18 04:53:31 +01:00
|
|
|
let outHtml = top.innerHTML;
|
2020-01-26 10:13:31 +01:00
|
|
|
if (!extendedMode && !internal) {
|
2018-06-15 03:36:12 +02:00
|
|
|
// collapse whitespace
|
|
|
|
outHtml = outHtml.replace(/[\n\t ]+/g, " ");
|
|
|
|
}
|
|
|
|
outHtml = outHtml.trim();
|
2017-07-28 08:19:06 +02:00
|
|
|
return outHtml;
|
|
|
|
};
|
|
|
|
|
2019-12-20 22:33:15 +01:00
|
|
|
let allowedTagsBasic = {};
|
|
|
|
let allowedTagsExtended = {};
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2019-12-20 22:33:15 +01:00
|
|
|
let TAGS_WITHOUT_ATTRS = ["P", "DIV", "BR", "SUB", "SUP"];
|
|
|
|
for (const tag of TAGS_WITHOUT_ATTRS) {
|
|
|
|
allowedTagsBasic[tag] = { attrs: [] };
|
2019-12-18 07:12:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TAGS_WITHOUT_ATTRS = [
|
2020-02-07 08:12:43 +01:00
|
|
|
"B",
|
|
|
|
"BLOCKQUOTE",
|
|
|
|
"CODE",
|
|
|
|
"DD",
|
|
|
|
"DL",
|
|
|
|
"DT",
|
|
|
|
"EM",
|
2019-12-18 07:12:39 +01:00
|
|
|
"H1",
|
|
|
|
"H2",
|
|
|
|
"H3",
|
2020-02-07 08:12:43 +01:00
|
|
|
"I",
|
2019-12-18 07:12:39 +01:00
|
|
|
"LI",
|
|
|
|
"OL",
|
|
|
|
"PRE",
|
2020-02-07 08:12:43 +01:00
|
|
|
"RP",
|
|
|
|
"RT",
|
|
|
|
"RUBY",
|
|
|
|
"STRONG",
|
2019-12-18 07:12:39 +01:00
|
|
|
"TABLE",
|
|
|
|
"U",
|
2020-02-07 08:12:43 +01:00
|
|
|
"UL",
|
2019-12-18 07:12:39 +01:00
|
|
|
];
|
2019-12-20 22:33:15 +01:00
|
|
|
for (const tag of TAGS_WITHOUT_ATTRS) {
|
|
|
|
allowedTagsExtended[tag] = { attrs: [] };
|
2017-10-25 12:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 07:12:39 +01:00
|
|
|
allowedTagsBasic["IMG"] = { attrs: ["SRC"] };
|
2017-10-25 12:20:28 +02:00
|
|
|
|
2019-12-18 07:12:39 +01:00
|
|
|
allowedTagsExtended["A"] = { attrs: ["HREF"] };
|
|
|
|
allowedTagsExtended["TR"] = { attrs: ["ROWSPAN"] };
|
|
|
|
allowedTagsExtended["TD"] = { attrs: ["COLSPAN", "ROWSPAN"] };
|
|
|
|
allowedTagsExtended["TH"] = { attrs: ["COLSPAN", "ROWSPAN"] };
|
|
|
|
allowedTagsExtended["FONT"] = { attrs: ["COLOR"] };
|
2017-10-25 12:20:28 +02:00
|
|
|
|
2019-12-06 04:39:05 +01:00
|
|
|
const allowedStyling = {
|
2019-12-18 07:12:39 +01:00
|
|
|
color: true,
|
|
|
|
"background-color": true,
|
|
|
|
"font-weight": true,
|
|
|
|
"font-style": true,
|
|
|
|
"text-decoration-line": true,
|
2019-12-06 04:39:05 +01:00
|
|
|
};
|
|
|
|
|
2020-06-24 13:54:38 +02:00
|
|
|
let isNightMode = function (): boolean {
|
2020-01-30 23:03:09 +01:00
|
|
|
return document.body.classList.contains("nightMode");
|
|
|
|
};
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
let filterExternalSpan = function (elem: HTMLElement) {
|
2019-12-06 04:39:05 +01:00
|
|
|
// filter out attributes
|
2021-01-19 02:48:41 +01:00
|
|
|
for (const attr of [...elem.attributes]) {
|
2019-12-20 22:33:15 +01:00
|
|
|
const attrName = attr.name.toUpperCase();
|
2021-01-18 17:05:05 +01:00
|
|
|
|
2019-12-18 07:12:39 +01:00
|
|
|
if (attrName !== "STYLE") {
|
2021-01-18 17:05:05 +01:00
|
|
|
elem.removeAttributeNode(attr);
|
2019-12-06 04:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-18 17:05:05 +01:00
|
|
|
|
2019-12-06 04:39:05 +01:00
|
|
|
// filter styling
|
2021-01-19 02:48:41 +01:00
|
|
|
for (const name of [...elem.style]) {
|
2021-01-19 01:08:15 +01:00
|
|
|
const value = elem.style.getPropertyValue(name);
|
2021-01-18 17:05:05 +01:00
|
|
|
|
|
|
|
if (
|
|
|
|
!allowedStyling.hasOwnProperty(name) ||
|
2019-12-06 05:08:59 +01:00
|
|
|
// google docs adds this unnecessarily
|
2021-01-19 01:08:15 +01:00
|
|
|
(name === "background-color" && value === "transparent") ||
|
2020-01-30 23:03:09 +01:00
|
|
|
// ignore coloured text in night mode for now
|
2021-01-19 01:08:15 +01:00
|
|
|
(isNightMode() && (name === "background-color" || name === "color"))
|
2021-01-18 17:05:05 +01:00
|
|
|
) {
|
|
|
|
elem.style.removeProperty(name);
|
2020-01-30 23:03:09 +01:00
|
|
|
}
|
2019-12-06 04:39:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
allowedTagsExtended["SPAN"] = filterExternalSpan;
|
|
|
|
|
2017-10-25 12:20:28 +02:00
|
|
|
// add basic tags to extended
|
|
|
|
Object.assign(allowedTagsExtended, allowedTagsBasic);
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function isHTMLElement(elem: Element): elem is HTMLElement {
|
|
|
|
return elem instanceof HTMLElement;
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:51:22 +02:00
|
|
|
// filtering from another field
|
2021-01-18 17:05:05 +01:00
|
|
|
let filterInternalNode = function (elem: Element) {
|
|
|
|
if (isHTMLElement(elem)) {
|
|
|
|
elem.style.removeProperty("background-color");
|
|
|
|
elem.style.removeProperty("font-size");
|
|
|
|
elem.style.removeProperty("font-family");
|
2017-10-25 09:51:22 +02:00
|
|
|
}
|
|
|
|
// recurse
|
2021-01-18 17:05:05 +01:00
|
|
|
for (let i = 0; i < elem.children.length; i++) {
|
|
|
|
const child = elem.children[i];
|
2019-12-20 22:33:15 +01:00
|
|
|
filterInternalNode(child);
|
2017-10-25 09:51:22 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// filtering from external sources
|
2021-01-18 17:05:05 +01:00
|
|
|
let filterNode = function (node: Node, extendedMode: boolean): void {
|
|
|
|
if (!nodeIsElement(node)) {
|
2017-07-28 08:19:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// descend first, and take a copy of the child nodes as the loop will skip
|
|
|
|
// elements due to node modifications otherwise
|
2021-01-19 02:48:41 +01:00
|
|
|
for (const child of [...node.children]) {
|
2019-12-20 22:33:15 +01:00
|
|
|
filterNode(child, extendedMode);
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-28 08:48:49 +02:00
|
|
|
if (node.tagName === "ANKITOP") {
|
2017-07-28 08:19:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
const tag = extendedMode
|
|
|
|
? allowedTagsExtended[node.tagName]
|
|
|
|
: allowedTagsBasic[node.tagName];
|
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
if (!tag) {
|
2019-12-18 07:12:39 +01:00
|
|
|
if (!node.innerHTML || node.tagName === "TITLE") {
|
2017-07-28 08:19:06 +02:00
|
|
|
node.parentNode.removeChild(node);
|
|
|
|
} else {
|
|
|
|
node.outerHTML = node.innerHTML;
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-18 07:12:39 +01:00
|
|
|
if (typeof tag === "function") {
|
2019-12-06 04:39:05 +01:00
|
|
|
// filtering function provided
|
|
|
|
tag(node);
|
|
|
|
} else {
|
|
|
|
// allowed, filter out attributes
|
2021-01-19 02:48:41 +01:00
|
|
|
for (const attr of [...node.attributes]) {
|
2019-12-20 22:33:15 +01:00
|
|
|
const attrName = attr.name.toUpperCase();
|
2019-12-06 04:39:05 +01:00
|
|
|
if (tag.attrs.indexOf(attrName) === -1) {
|
2021-01-18 17:05:05 +01:00
|
|
|
node.removeAttributeNode(attr);
|
2019-12-06 04:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|