Skip to content

Commit 587a124

Browse files
committed
chore: simplify key down handling
1 parent daa823a commit 587a124

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/extension/app/aem-sidekick.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ export class AEMSidekick extends LitElement {
3535

3636
@property({
3737
type: Boolean,
38-
reflect: true,
3938
converter: {
4039
fromAttribute: (value) => (value === 'true'),
41-
toAttribute: (value) => (value ? 'true' : 'false'),
4240
},
4341
})
4442
accessor open = false;
@@ -54,23 +52,29 @@ export class AEMSidekick extends LitElement {
5452
this.loadContext(config);
5553
}
5654

55+
#boundHandleKeyDown = (e) => {
56+
this.#handleKeyDown(e);
57+
};
58+
5759
async #handleKeyDown(e) {
58-
// only handle Cmd/Ctrl+R
59-
if (!(e.metaKey || e.ctrlKey) || e.key !== 'r') {
60+
// only handle Cmd/Ctrl+R if sidekick is open
61+
if (!this.open || !(e.metaKey || e.ctrlKey) || e.key !== 'r') {
6062
return;
6163
}
6264
e.preventDefault();
6365
e.stopPropagation();
6466
try {
6567
await chrome.runtime.sendMessage({ action: 'bustCache' });
6668
} finally {
67-
window.location.reload();
69+
this.appStore.reloadPage();
6870
}
6971
}
7072

7173
async connectedCallback() {
7274
super.connectedCallback();
7375

76+
window.addEventListener('keydown', this.#boundHandleKeyDown, true);
77+
7478
reaction(
7579
() => this.appStore.theme,
7680
() => {
@@ -80,21 +84,10 @@ export class AEMSidekick extends LitElement {
8084
}
8185

8286
disconnectedCallback() {
83-
window.removeEventListener('keydown', this.#handleKeyDown, true);
87+
window.removeEventListener('keydown', this.#boundHandleKeyDown, true);
8488
super.disconnectedCallback?.();
8589
}
8690

87-
updated(changedProperties) {
88-
super.updated(changedProperties);
89-
if (changedProperties.has('open')) {
90-
if (this.open) {
91-
window.addEventListener('keydown', this.#handleKeyDown, true);
92-
} else {
93-
window.removeEventListener('keydown', this.#handleKeyDown, true);
94-
}
95-
}
96-
}
97-
9891
async loadContext(config) {
9992
await this.appStore.loadContext(this, config);
10093

0 commit comments

Comments
 (0)