anki/ts/editor/focus-handlers.ts

35 lines
1.0 KiB
TypeScript
Raw Normal View History

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
/* eslint
@typescript-eslint/no-non-null-assertion: "off",
*/
2021-07-05 18:15:03 +02:00
import { fieldFocused } from "./toolbar";
import type { EditingArea } from "./editing-area";
import { saveField } from "./change-timer";
import { bridgeCommand } from "./lib";
import { getCurrentField } from "./helpers";
export function onFocus(evt: FocusEvent): void {
const currentField = evt.currentTarget as EditingArea;
currentField.focus();
if (currentField.getSelection().anchorNode === null) {
// selection is not inside editable after focusing
currentField.caretToEnd();
}
bridgeCommand(`focus:${currentField.ord}`);
2021-07-05 18:15:03 +02:00
fieldFocused.set(true);
}
export function onBlur(evt: FocusEvent): void {
const previousFocus = evt.currentTarget as EditingArea;
const currentFieldUnchanged = previousFocus === getCurrentField();
saveField(previousFocus, currentFieldUnchanged ? "key" : "blur");
2021-07-05 18:15:03 +02:00
fieldFocused.set(false);
}