文件
mynode/packages/editor/CodeMirror/extensions/keyUpHandlerExtension.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

67 行
2.0 KiB
TypeScript

import { EditorView, ViewPlugin } from '@codemirror/view';
interface OnKeyUpEvent {
domEvent: KeyboardEvent;
view: EditorView;
// true if other keys were pressed (and possibly released) during the time
// that this key was down (and before it was released).
otherKeysWerePressed: boolean;
}
// Should return true if the event was handled
type OnKeyUpHandler = (event: OnKeyUpEvent)=> boolean;
// Should return true if the given event matches the key that should
// be handled by the extension:
type IsTargetKeyCallback = (event: KeyboardEvent)=> boolean;
// CodeMirror's built-in keyboard event handlers trigger on key down, rather than on
// key up. In some cases, it's useful to trigger keyboard shortcuts on key up instead.
// For example, if a shortcut should only activate if it isn't pressed at the same time
// as other keys.
const keyUpHandlerExtension = (isTargetKey: IsTargetKeyCallback, onKeyUp: OnKeyUpHandler) => {
return ViewPlugin.fromClass(class {
private otherKeysWerePressed = false;
private targetKeyDown = false;
public constructor(private view: EditorView) {
view.contentDOM.addEventListener('keydown', this.onKeyDown);
view.contentDOM.addEventListener('keyup', this.onKeyUp);
}
public destroy() {
this.view?.contentDOM?.removeEventListener('keyup', this.onKeyUp);
}
private onKeyDown = (event: KeyboardEvent) => {
if (isTargetKey(event)) {
this.targetKeyDown = true;
} else if (this.targetKeyDown) {
this.otherKeysWerePressed = true;
}
};
private onKeyUp = (event: KeyboardEvent) => {
if (isTargetKey(event)) {
if (this.targetKeyDown && !event.defaultPrevented) {
const handled = onKeyUp({
domEvent: event,
view: this.view,
otherKeysWerePressed: this.otherKeysWerePressed,
});
if (handled) {
event.preventDefault();
}
}
this.targetKeyDown = false;
this.otherKeysWerePressed = false;
} else if (this.targetKeyDown) {
this.otherKeysWerePressed = true;
}
};
});
};
export default keyUpHandlerExtension;