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
|
2021-02-09 04:38:04 +01:00
|
|
|
|
2021-04-27 23:08:47 +02:00
|
|
|
import { updateActiveButtons } from "./toolbar";
|
2021-02-28 14:12:48 +01:00
|
|
|
import { EditingArea } from "./editingArea";
|
2021-04-20 03:55:59 +02:00
|
|
|
import { caretToEnd, nodeIsElement, getBlockElement } from "./helpers";
|
2021-02-08 19:45:42 +01:00
|
|
|
import { triggerChangeTimer } from "./changeTimer";
|
2021-04-23 02:16:40 +02:00
|
|
|
import { registerShortcut } from "lib/shortcuts";
|
2021-02-08 19:45:42 +01:00
|
|
|
|
|
|
|
export function onInput(event: Event): void {
|
|
|
|
// make sure IME changes get saved
|
|
|
|
triggerChangeTimer(event.currentTarget as EditingArea);
|
2021-04-28 13:09:25 +02:00
|
|
|
updateActiveButtons(event);
|
2021-02-08 19:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function onKey(evt: KeyboardEvent): void {
|
|
|
|
const currentField = evt.currentTarget as EditingArea;
|
|
|
|
|
|
|
|
// esc clears focus, allowing dialog to close
|
|
|
|
if (evt.code === "Escape") {
|
|
|
|
currentField.blurEditable();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefer <br> instead of <div></div>
|
2021-04-19 19:56:01 +02:00
|
|
|
if (
|
|
|
|
evt.code === "Enter" &&
|
2021-04-20 03:55:59 +02:00
|
|
|
!getBlockElement(currentField.shadowRoot!) !== evt.shiftKey
|
2021-04-19 19:56:01 +02:00
|
|
|
) {
|
2021-02-08 19:45:42 +01:00
|
|
|
evt.preventDefault();
|
|
|
|
document.execCommand("insertLineBreak");
|
|
|
|
}
|
|
|
|
|
|
|
|
// // 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
triggerChangeTimer(currentField);
|
|
|
|
}
|
|
|
|
|
2021-04-22 15:02:41 +02:00
|
|
|
function updateFocus(evt: FocusEvent) {
|
|
|
|
const newFocusTarget = evt.target;
|
|
|
|
if (newFocusTarget instanceof EditingArea) {
|
|
|
|
caretToEnd(newFocusTarget);
|
2021-04-28 13:09:25 +02:00
|
|
|
updateActiveButtons(evt);
|
2021-03-08 20:40:23 +01:00
|
|
|
}
|
2021-04-22 15:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
registerShortcut(
|
|
|
|
() => document.addEventListener("focusin", updateFocus, { once: true }),
|
2021-04-22 17:28:38 +02:00
|
|
|
"Tab",
|
|
|
|
["Shift"]
|
2021-04-22 15:02:41 +02:00
|
|
|
);
|
2021-03-08 20:40:23 +01:00
|
|
|
|
2021-02-08 19:45:42 +01:00
|
|
|
export function onKeyUp(evt: KeyboardEvent): void {
|
|
|
|
const currentField = evt.currentTarget as EditingArea;
|
|
|
|
|
|
|
|
// Avoid div element on remove
|
|
|
|
if (evt.code === "Enter" || evt.code === "Backspace") {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|