mirror of
https://github.com/ankitects/anki.git
synced 2025-09-22 07:52:24 -04:00

This is to provide minimal closures for the mutable file-local variables: - changeTimer - previousActiveElement - currentNoteId This makes it clear, that they should not be used, but rather the functions which wrap them in an API
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import type { EditingArea } from ".";
|
|
|
|
import { getCurrentField } from ".";
|
|
import { bridgeCommand } from "./lib";
|
|
import { getNoteId } from "./noteId";
|
|
import { updateButtonState } from "./toolbar";
|
|
|
|
let changeTimer: number | null = null;
|
|
|
|
export function triggerChangeTimer(currentField: EditingArea): void {
|
|
clearChangeTimer();
|
|
changeTimer = setTimeout(function () {
|
|
updateButtonState();
|
|
saveField(currentField, "key");
|
|
}, 600);
|
|
}
|
|
|
|
function clearChangeTimer(): void {
|
|
if (changeTimer) {
|
|
clearTimeout(changeTimer);
|
|
changeTimer = null;
|
|
}
|
|
}
|
|
|
|
export function saveField(currentField: EditingArea, type: "blur" | "key"): void {
|
|
clearChangeTimer();
|
|
bridgeCommand(
|
|
`${type}:${currentField.ord}:${getNoteId()}:${currentField.fieldHTML}`
|
|
);
|
|
}
|
|
|
|
export function saveNow(keepFocus: boolean): void {
|
|
const currentField = getCurrentField();
|
|
|
|
if (!currentField) {
|
|
return;
|
|
}
|
|
|
|
clearChangeTimer();
|
|
|
|
if (keepFocus) {
|
|
saveField(currentField, "key");
|
|
} else {
|
|
// triggers onBlur, which saves
|
|
currentField.blurEditable();
|
|
}
|
|
}
|