Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keyboard controller should attach event on document #8801

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/framework/block-std/src/gfx/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,35 @@ export class KeyboardController {

private _init() {
this._disposable.add(
this.std.event.add('keyDown', evt => {
const state = evt.get('keyboardState');

this.shiftKey$.value = state.raw.shiftKey && state.raw.key === 'Shift';
this.spaceKey$.value = state.raw.code === 'Space';
this._listenKeyboard('keydown', evt => {
this.shiftKey$.value = evt.shiftKey && evt.key === 'Shift';
this.spaceKey$.value = evt.code === 'Space';
})
);

this._disposable.add(
this.std.event.add('keyUp', evt => {
const state = evt.get('keyboardState');

this.shiftKey$.value = state.raw.shiftKey && state.raw.key === 'Shift';
this._listenKeyboard('keyup', evt => {
this.shiftKey$.value =
evt.shiftKey && evt.key === 'Shift' ? true : false;

if (state.raw.code === 'Space') {
if (evt.code === 'Space') {
this.spaceKey$.value = false;
}
})
);
}

private _listenKeyboard(
event: 'keydown' | 'keyup',
callback: (keyboardEvt: KeyboardEvent) => void
) {
document.addEventListener(event, callback, false);

return () => {
document.removeEventListener(event, callback, false);
};
}

Comment on lines +37 to +47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does listening on the document affect user interactions on the AFFiNE side?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. It should be fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested the situation where multiple editors exist at the same time?

dispose() {
this._disposable.dispose();
}
Expand Down
Loading