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-06-30 19:55:56 +02:00
|
|
|
import type { EditingArea } from "./editing-area";
|
2021-02-08 19:45:42 +01:00
|
|
|
|
2021-07-19 15:27:11 +02:00
|
|
|
import { getCurrentField } from "./helpers";
|
2021-02-08 19:45:42 +01:00
|
|
|
import { bridgeCommand } from "./lib";
|
2021-06-30 19:55:56 +02:00
|
|
|
import { getNoteId } from "./note-id";
|
2021-02-08 19:45:42 +01:00
|
|
|
|
|
|
|
let changeTimer: number | null = null;
|
|
|
|
|
|
|
|
export function triggerChangeTimer(currentField: EditingArea): void {
|
|
|
|
clearChangeTimer();
|
2021-03-08 20:40:23 +01:00
|
|
|
changeTimer = setTimeout(() => saveField(currentField, "key"), 600);
|
2021-02-08 19:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearChangeTimer(): void {
|
|
|
|
if (changeTimer) {
|
|
|
|
clearTimeout(changeTimer);
|
|
|
|
changeTimer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function saveField(currentField: EditingArea, type: "blur" | "key"): void {
|
|
|
|
clearChangeTimer();
|
2021-07-21 01:32:09 +02:00
|
|
|
const command = `${type}:${currentField.ord}:${getNoteId()}:${
|
|
|
|
currentField.fieldHTML
|
|
|
|
}`;
|
2021-07-19 15:27:11 +02:00
|
|
|
bridgeCommand(command);
|
2021-02-08 19:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function saveNow(keepFocus: boolean): void {
|
|
|
|
const currentField = getCurrentField();
|
|
|
|
|
|
|
|
if (!currentField) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearChangeTimer();
|
|
|
|
|
|
|
|
if (keepFocus) {
|
|
|
|
saveField(currentField, "key");
|
|
|
|
} else {
|
|
|
|
// triggers onBlur, which saves
|
2021-06-17 22:02:06 +02:00
|
|
|
currentField.blur();
|
2021-02-08 19:45:42 +01:00
|
|
|
}
|
|
|
|
}
|