Skip to content

Commit

Permalink
feat: support complex hotkey
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodyfunk committed Jul 17, 2024
1 parent eda1f2a commit e7b63f3
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*


.history
2 changes: 1 addition & 1 deletion packages/core/src/key_binding_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isWindows } from '@suika/common';

import { type SuikaEditor } from './editor';

interface IKey {
export interface IKey {
ctrlKey?: boolean;
shiftKey?: boolean;
altKey?: boolean;
Expand Down
25 changes: 22 additions & 3 deletions packages/core/src/tools/tool_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,13 @@ export class ToolManager {

if (!hotkey) {
console.log(`${type} has no hotkey`);
} else {
} else if (typeof hotkey === 'string') {
if (this.hotkeySet.has(hotkey)) {
console.log(`register same hotkey: "${hotkey}"`);
}
this.hotkeySet.add(hotkey);

const keyCode = `Key${toolCtor.hotkey.toUpperCase()}`;
// TODO: support complex hotkey
const keyCode = `Key${toolCtor.hotkey}`;
const token = this.editor.keybindingManager.register({
key: { keyCode: keyCode },
actionName: type,
Expand All @@ -113,6 +112,26 @@ export class ToolManager {
},
});
this.keyBindingToken.push(token);
} else {
// support complex hotkey
const hotkeyStr = `${hotkey.altKey ? 'alt+' : ''}${
hotkey.ctrlKey ? 'ctrl+' : ''
}${hotkey.shiftKey ? 'shift+' : ''}${hotkey.metaKey ? 'meta+' : ''}${
hotkey.keyCode
}`;
if (this.hotkeySet.has(hotkeyStr)) {
console.log(`register same hotkey: "${hotkey}"`);
}
this.hotkeySet.add(hotkeyStr);
const token = this.editor.keybindingManager.register({
key: hotkey,
actionName: type,
when: () => this.enableToolTypes.includes(type),
action: () => {
this.setActiveTool(type);
},
});
this.keyBindingToken.push(token);
}
}
getActiveToolName() {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/tools/tool_pencil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SuikaPath } from '../graphs';
import { type ITool } from './type';

const TYPE = 'pencil';
const HOTKEY = '';
const HOTKEY = { shiftKey: true, keyCode: 'KeyP' };

export class PencilTool implements ITool {
static readonly type = TYPE;
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/tools/type.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { type ICursor } from '../cursor_manager';
import { type SuikaEditor } from '../editor';
import { type IKey } from '../key_binding_manager';

export interface ITool extends IBaseTool {
hotkey: string;
hotkey: string | IKey;
type: string;
cursor: ICursor;
onMoveExcludeDrag: (event: PointerEvent, isOutsideCanvas: boolean) => void;
Expand Down Expand Up @@ -40,5 +41,5 @@ export interface IBaseTool {
export interface IToolClassConstructor {
new (editor: SuikaEditor): ITool;
type: string;
hotkey: string;
hotkey: string | IKey;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './Toolbar.scss';

import { isWindows } from '@suika/common';
import { Button } from '@suika/components';
import {
EllipseOutlined,
Expand Down Expand Up @@ -92,7 +93,7 @@ export const ToolBar = () => {
},
pencil: {
name: 'pencil',
hotkey: '',
hotkey: `${isWindows ? 'Shift+' : '⇧'}P`,
intlId: 'tool.pencil',
icon: <PencilOutlined />,
},
Expand Down
1 change: 1 addition & 0 deletions packages/suika/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineConfig({
base: './',
server: {
port: 6167,
host: true,
},
build: {
outDir: 'build',
Expand Down

0 comments on commit e7b63f3

Please sign in to comment.