[editor] Initialize KeyboardManager-instances lazily

As far as I can tell there's no particular reason for initializing `KeyboardManager`-instances eagerly, since the user may never use editing, and we can easily do this lazily instead by utilizing shadowed getters.
This commit is contained in:
Jonas Jenwald 2023-06-02 15:49:34 +02:00
parent 0023c4a511
commit ea93c507f5
2 changed files with 49 additions and 30 deletions

View File

@ -21,6 +21,7 @@ import {
AnnotationEditorType, AnnotationEditorType,
assert, assert,
LINE_FACTOR, LINE_FACTOR,
shadow,
Util, Util,
} from "../../shared/util.js"; } from "../../shared/util.js";
import { bindEvents, KeyboardManager } from "./tools.js"; import { bindEvents, KeyboardManager } from "./tools.js";
@ -58,12 +59,18 @@ class FreeTextEditor extends AnnotationEditor {
static _defaultFontSize = 10; static _defaultFontSize = 10;
static _keyboardManager = new KeyboardManager([ static get _keyboardManager() {
return shadow(
this,
"_keyboardManager",
new KeyboardManager([
[ [
["ctrl+Enter", "mac+meta+Enter", "Escape", "mac+Escape"], ["ctrl+Enter", "mac+meta+Enter", "Escape", "mac+Escape"],
FreeTextEditor.prototype.commitOrRemove, FreeTextEditor.prototype.commitOrRemove,
], ],
]); ])
);
}
static _type = "freetext"; static _type = "freetext";

View File

@ -396,8 +396,15 @@ class AnnotationEditorUIManager {
#container = null; #container = null;
static _keyboardManager = new KeyboardManager([ static get _keyboardManager() {
[["ctrl+a", "mac+meta+a"], AnnotationEditorUIManager.prototype.selectAll], return shadow(
this,
"_keyboardManager",
new KeyboardManager([
[
["ctrl+a", "mac+meta+a"],
AnnotationEditorUIManager.prototype.selectAll,
],
[["ctrl+z", "mac+meta+z"], AnnotationEditorUIManager.prototype.undo], [["ctrl+z", "mac+meta+z"], AnnotationEditorUIManager.prototype.undo],
[ [
["ctrl+y", "ctrl+shift+Z", "mac+meta+shift+Z"], ["ctrl+y", "ctrl+shift+Z", "mac+meta+shift+Z"],
@ -418,8 +425,13 @@ class AnnotationEditorUIManager {
], ],
AnnotationEditorUIManager.prototype.delete, AnnotationEditorUIManager.prototype.delete,
], ],
[["Escape", "mac+Escape"], AnnotationEditorUIManager.prototype.unselectAll], [
]); ["Escape", "mac+Escape"],
AnnotationEditorUIManager.prototype.unselectAll,
],
])
);
}
constructor(container, eventBus, annotationStorage) { constructor(container, eventBus, annotationStorage) {
this.#container = container; this.#container = container;