mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
Generalize ChangeTimer and use it in Mathjax editor
This commit is contained in:
parent
cb762b880e
commit
b06b5e9151
6 changed files with 57 additions and 44 deletions
|
@ -4,6 +4,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|||
-->
|
||||
<script lang="typescript">
|
||||
import { onMount, createEventDispatcher } from "svelte";
|
||||
import { ChangeTimer } from "./change-timer";
|
||||
|
||||
import * as CodeMirror from "codemirror/lib/codemirror";
|
||||
import "codemirror/mode/stex/stex";
|
||||
|
@ -32,9 +33,13 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|||
};
|
||||
|
||||
let codeMirror: CodeMirror;
|
||||
const changeTimer = new ChangeTimer();
|
||||
|
||||
function onInput() {
|
||||
dispatch("update", { mathjax: codeMirror.getValue() });
|
||||
changeTimer.schedule(
|
||||
() => dispatch("update", { mathjax: codeMirror.getValue() }),
|
||||
400
|
||||
);
|
||||
}
|
||||
|
||||
function openCodemirror(textarea: HTMLTextAreaElement): void {
|
||||
|
|
|
@ -1,47 +1,18 @@
|
|||
// 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";
|
||||
export class ChangeTimer {
|
||||
private value: number | null = null;
|
||||
|
||||
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;
|
||||
schedule(action: () => void, delay: number) {
|
||||
this.clear();
|
||||
this.value = setTimeout(action, delay);
|
||||
}
|
||||
|
||||
clearChangeTimer();
|
||||
|
||||
if (keepFocus) {
|
||||
saveField(currentField, "key");
|
||||
} else {
|
||||
// triggers onBlur, which saves
|
||||
currentField.blur();
|
||||
clear() {
|
||||
if (this.value) {
|
||||
clearTimeout(this.value);
|
||||
this.value = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
import { fieldFocused } from "./toolbar";
|
||||
import type { EditingArea } from "./editing-area";
|
||||
|
||||
import { saveField } from "./change-timer";
|
||||
import { saveField } from "./saving";
|
||||
import { bridgeCommand } from "./lib";
|
||||
import { getCurrentField } from "./helpers";
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ import { isApplePlatform } from "lib/platform";
|
|||
import { registerShortcut } from "lib/shortcuts";
|
||||
import { bridgeCommand } from "lib/bridgecommand";
|
||||
import { updateActiveButtons } from "./toolbar";
|
||||
import { saveField } from "./change-timer";
|
||||
import { saveField } from "./saving";
|
||||
|
||||
import "./fields.css";
|
||||
|
||||
|
@ -36,7 +36,7 @@ import { initTagEditor } from "./tag-editor";
|
|||
import { getCurrentField } from "./helpers";
|
||||
|
||||
export { setNoteId, getNoteId } from "./note-id";
|
||||
export { saveNow } from "./change-timer";
|
||||
export { saveNow } from "./saving";
|
||||
export { wrap, wrapIntoText } from "./wrap";
|
||||
export { editorToolbar } from "./toolbar";
|
||||
export { activateStickyShortcuts } from "./label-container";
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
import { nodeIsElement } from "lib/dom";
|
||||
import { updateActiveButtons } from "./toolbar";
|
||||
import { EditingArea } from "./editing-area";
|
||||
import { triggerChangeTimer } from "./change-timer";
|
||||
import { triggerChangeTimer } from "./saving";
|
||||
import { registerShortcut } from "lib/shortcuts";
|
||||
|
||||
export function onInput(event: Event): void {
|
||||
|
|
37
ts/editor/saving.ts
Normal file
37
ts/editor/saving.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import type { EditingArea } from "./editing-area";
|
||||
|
||||
import { ChangeTimer } from "./change-timer";
|
||||
import { getCurrentField } from "./helpers";
|
||||
import { bridgeCommand } from "./lib";
|
||||
import { getNoteId } from "./note-id";
|
||||
|
||||
const saveFieldTimer = new ChangeTimer();
|
||||
|
||||
export function triggerChangeTimer(currentField: EditingArea): void {
|
||||
saveFieldTimer.schedule(() => saveField(currentField, "key"), 600);
|
||||
}
|
||||
|
||||
export function saveField(currentField: EditingArea, type: "blur" | "key"): void {
|
||||
saveFieldTimer.clear();
|
||||
const command = `${type}:${currentField.ord}:${getNoteId()}:${
|
||||
currentField.fieldHTML
|
||||
}`;
|
||||
bridgeCommand(command);
|
||||
}
|
||||
|
||||
export function saveNow(keepFocus: boolean): void {
|
||||
const currentField = getCurrentField();
|
||||
|
||||
if (!currentField) {
|
||||
return;
|
||||
}
|
||||
|
||||
saveFieldTimer.clear();
|
||||
|
||||
if (keepFocus) {
|
||||
saveField(currentField, "key");
|
||||
} else {
|
||||
// triggers onBlur, which saves
|
||||
currentField.blur();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue