anki/ts/editor/helpers.ts
Hikaru Y 9123821131
Refactor plain/rich text input toggling code; fix focus loss (#2479)
* Refactor plain/rich text input toggling code; fix focus loss

Fix:
- Issue where field loses focus when plain/rich text input is closed

Refactoring:
- Call refocus() inside the reactive statement in
  Plain/RichTextInput.svelte to eliminate the need for polling
  with requestAnimationFrame
- Introduce 'Flag' class
- Move 'on:toggle' handlers from inline to functions defined in
  the <script> section for better readability

* Improve code clarity based on feedback from code review

- Rename method and add comment to it
- Add 'private' access modifier to property
2023-04-22 16:08:25 +10:00

41 lines
893 B
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
function isFontElement(element: Element): element is HTMLFontElement {
return element.tagName === "FONT";
}
/**
* Avoid both HTMLFontElement and .color, as they are both deprecated
*/
export function withFontColor(
element: Element,
callback: (color: string) => void,
): boolean {
if (isFontElement(element)) {
callback(element.color);
return true;
}
return false;
}
export class Flag {
private flag: boolean;
constructor() {
this.flag = false;
}
setFlag(on: boolean): void {
this.flag = on;
}
/** Resets the flag to false and returns the previous value. */
checkAndReset(): boolean {
const val = this.flag;
this.flag = false;
return val;
}
}