2021-02-09 04:38:04 +01:00
|
|
|
/* Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
|
|
|
|
|
2021-02-09 01:09:16 +01:00
|
|
|
import type { EditingArea, EditorField } from ".";
|
2021-02-08 19:45:42 +01:00
|
|
|
|
|
|
|
import { bridgeCommand } from "./lib";
|
|
|
|
import { enableButtons, disableButtons } from "./toolbar";
|
|
|
|
import { saveField } from "./changeTimer";
|
|
|
|
|
2021-02-09 01:09:16 +01:00
|
|
|
enum ViewportRelativePosition {
|
|
|
|
Contained,
|
|
|
|
ExceedTop,
|
|
|
|
ExceedBottom,
|
|
|
|
}
|
|
|
|
|
|
|
|
function isFieldInViewport(
|
|
|
|
element: Element,
|
|
|
|
toolbarHeight: number
|
|
|
|
): ViewportRelativePosition {
|
2021-02-08 19:45:42 +01:00
|
|
|
const rect = element.getBoundingClientRect();
|
|
|
|
|
2021-02-09 01:09:16 +01:00
|
|
|
return rect.top <= toolbarHeight
|
|
|
|
? ViewportRelativePosition.ExceedTop
|
|
|
|
: rect.bottom >= (window.innerHeight || document.documentElement.clientHeight)
|
|
|
|
? ViewportRelativePosition.ExceedBottom
|
|
|
|
: ViewportRelativePosition.Contained;
|
2021-02-08 19:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function caretToEnd(currentField: EditingArea): void {
|
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(currentField.editable);
|
|
|
|
range.collapse(false);
|
|
|
|
const selection = currentField.getSelection();
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For distinguishing focus by refocusing window from deliberate focus
|
|
|
|
let previousActiveElement: EditingArea | null = null;
|
|
|
|
|
|
|
|
export function onFocus(evt: FocusEvent): void {
|
|
|
|
const currentField = evt.currentTarget as EditingArea;
|
|
|
|
|
|
|
|
if (currentField === previousActiveElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-09 01:09:16 +01:00
|
|
|
const editorField = currentField.parentElement! as EditorField;
|
|
|
|
const toolbarHeight = document.getElementById("topbutsOuter")!.clientHeight;
|
|
|
|
switch (isFieldInViewport(editorField, toolbarHeight)) {
|
|
|
|
case ViewportRelativePosition.ExceedBottom:
|
|
|
|
editorField.scrollIntoView(false);
|
|
|
|
break;
|
|
|
|
case ViewportRelativePosition.ExceedTop:
|
|
|
|
editorField.scrollIntoView(true);
|
|
|
|
window.scrollBy(0, -toolbarHeight);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-08 19:45:42 +01:00
|
|
|
currentField.focusEditable();
|
|
|
|
bridgeCommand(`focus:${currentField.ord}`);
|
|
|
|
enableButtons();
|
|
|
|
// do this twice so that there's no flicker on newer versions
|
|
|
|
caretToEnd(currentField);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function onBlur(evt: FocusEvent): void {
|
|
|
|
const currentField = evt.currentTarget as EditingArea;
|
|
|
|
|
2021-02-09 01:09:16 +01:00
|
|
|
if (currentField === document.activeElement) {
|
2021-02-08 19:45:42 +01:00
|
|
|
// other widget or window focused; current field unchanged
|
|
|
|
saveField(currentField, "key");
|
|
|
|
previousActiveElement = currentField;
|
|
|
|
} else {
|
|
|
|
saveField(currentField, "blur");
|
|
|
|
disableButtons();
|
|
|
|
previousActiveElement = null;
|
|
|
|
}
|
|
|
|
}
|