mirror of
https://github.com/ankitects/anki.git
synced 2025-09-23 16:26:40 -04:00
add more specific function type annotation
This commit is contained in:
parent
449367dfe2
commit
b0848717b4
3 changed files with 21 additions and 13 deletions
|
@ -1 +1 @@
|
||||||
Subproject commit b90ef6f03c251eb336029ac7c5f551200d41273f
|
Subproject commit 3d04bcbf7fefca0007bc9db307409d88210995d8
|
|
@ -1 +1 @@
|
||||||
Subproject commit 9aa63c335c61b30421d39cf43fd8e3975179059c
|
Subproject commit c65a9587b1f18931986bdf145872e8e4c44c5c82
|
|
@ -9,8 +9,8 @@ type State = {
|
||||||
export class UndoManager {
|
export class UndoManager {
|
||||||
private undoStack: State[] = [];
|
private undoStack: State[] = [];
|
||||||
private redoStack: State[] = [];
|
private redoStack: State[] = [];
|
||||||
private isUpdating: boolean = false;
|
private isUpdating = false;
|
||||||
private transactionStart: number = 0;
|
private transactionStart = 0;
|
||||||
public register = this.debounce(this.pushToUndo, 500, (position: number) => this.transactionStart = position);
|
public register = this.debounce(this.pushToUndo, 500, (position: number) => this.transactionStart = position);
|
||||||
|
|
||||||
public clearRedoStack() {
|
public clearRedoStack() {
|
||||||
|
@ -44,13 +44,13 @@ export class UndoManager {
|
||||||
element.innerHTML = last.content;
|
element.innerHTML = last.content;
|
||||||
|
|
||||||
const selection = getSelection(element)!;
|
const selection = getSelection(element)!;
|
||||||
let range = getRange(selection);
|
const range = getRange(selection);
|
||||||
|
|
||||||
let counter = this.transactionStart;
|
let counter = this.transactionStart;
|
||||||
let nodeFound: Node | null = null;
|
let nodeFound: Node | null = null;
|
||||||
let nodeOffset = 0;
|
let nodeOffset = 0;
|
||||||
for (const node of element.childNodes) {
|
for (const node of element.childNodes) {
|
||||||
let nodeLength = node.textContent?.length || 0;
|
const nodeLength = node.textContent?.length || 0;
|
||||||
if (counter <= nodeLength) {
|
if (counter <= nodeLength) {
|
||||||
nodeFound = node;
|
nodeFound = node;
|
||||||
nodeOffset = counter;
|
nodeOffset = counter;
|
||||||
|
@ -94,13 +94,13 @@ export class UndoManager {
|
||||||
element.innerHTML = redoedState.content;
|
element.innerHTML = redoedState.content;
|
||||||
|
|
||||||
const selection = getSelection(element)!;
|
const selection = getSelection(element)!;
|
||||||
let range = getRange(selection);
|
const range = getRange(selection);
|
||||||
|
|
||||||
let counter = this.transactionStart;
|
let counter = this.transactionStart;
|
||||||
let nodeFound: Node | null = null;
|
let nodeFound: Node | null = null;
|
||||||
let nodeOffset = 0;
|
let nodeOffset = 0;
|
||||||
for (const node of element.childNodes) {
|
for (const node of element.childNodes) {
|
||||||
let nodeLength = node.textContent?.length || 0;
|
const nodeLength = node.textContent?.length || 0;
|
||||||
if (counter <= nodeLength) {
|
if (counter <= nodeLength) {
|
||||||
nodeFound = node;
|
nodeFound = node;
|
||||||
nodeOffset = counter;
|
nodeOffset = counter;
|
||||||
|
@ -134,15 +134,23 @@ export class UndoManager {
|
||||||
this.isUpdating = false;
|
this.isUpdating = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private debounce(func: Function, delay: number, onTransactionStart: Function): Function {
|
private debounce<A, B>(
|
||||||
let timeout;
|
func: (arg: A) => void,
|
||||||
return (...args) => {
|
delay: number,
|
||||||
|
onTransactionStart: (arg: B) => void,
|
||||||
|
): (mainArg: A, startArg: B) => void {
|
||||||
|
let timeout: ReturnType<typeof setTimeout> | undefined;
|
||||||
|
|
||||||
|
return (mainArg: A, startArg: B) => {
|
||||||
const isNewTransaction = timeout === undefined;
|
const isNewTransaction = timeout === undefined;
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
if (isNewTransaction) { onTransactionStart.call(this, args[1]); }
|
|
||||||
|
if (isNewTransaction) {
|
||||||
|
onTransactionStart.call(this, startArg);
|
||||||
|
}
|
||||||
|
|
||||||
timeout = setTimeout(() => {
|
timeout = setTimeout(() => {
|
||||||
func.call(this, args[0]);
|
func.call(this, mainArg);
|
||||||
timeout = undefined;
|
timeout = undefined;
|
||||||
}, delay);
|
}, delay);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue