mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00

Contains the shadow root, and references to the styles. Is ignorant of Editable. Is necessary, so our we editable.scss does not need to contain information about Codable, ImageHandle or all those other things which have nothing to do with Editable
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
import type { EditingArea } from "./editing-area";
|
|
|
|
import { getCurrentField } from "./helpers";
|
|
import { bridgeCommand } from "./lib";
|
|
import { getNoteId } from "./note-id";
|
|
|
|
let changeTimer: number | null = null;
|
|
|
|
export function triggerChangeTimer(currentField: EditingArea): void {
|
|
clearChangeTimer();
|
|
changeTimer = setTimeout(() => saveField(currentField, "key"), 600);
|
|
}
|
|
|
|
function clearChangeTimer(): void {
|
|
if (changeTimer) {
|
|
clearTimeout(changeTimer);
|
|
changeTimer = null;
|
|
}
|
|
}
|
|
|
|
export function saveField(currentField: EditingArea, type: "blur" | "key"): void {
|
|
clearChangeTimer();
|
|
const command = `${type}:${currentField.ord}:${getNoteId()}:${currentField.fieldHTML}`
|
|
bridgeCommand(command);
|
|
}
|
|
|
|
export function saveNow(keepFocus: boolean): void {
|
|
const currentField = getCurrentField();
|
|
|
|
if (!currentField) {
|
|
return;
|
|
}
|
|
|
|
clearChangeTimer();
|
|
|
|
if (keepFocus) {
|
|
saveField(currentField, "key");
|
|
} else {
|
|
// triggers onBlur, which saves
|
|
currentField.blur();
|
|
}
|
|
}
|