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 aacdc9b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 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
42 changes: 27 additions & 15 deletions packages/core/src/tools/tool_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EventEmitter, noop } from '@suika/common';
import { type IPoint } from '@suika/geo';

import { type SuikaEditor } from '../editor';
import { type IKey } from '../key_binding_manager';
import { DragCanvasTool } from './tool_drag_canvas';
import { DrawEllipseTool } from './tool_draw_ellipse';
import { DrawLineTool } from './tool_draw_line';
Expand Down Expand Up @@ -93,27 +94,38 @@ export class ToolManager {

// select and pathSelect tool has same hotkey
const hotkey = toolCtor.hotkey;
let keyCode = '';
let hotkeyObj: IKey;

if (!hotkey) {
console.log(`${type} has no hotkey`);
return;
} else if (typeof hotkey === 'string') {
keyCode = `Key${hotkey.toUpperCase()}`;
hotkeyObj = { keyCode: keyCode };
} else {
if (this.hotkeySet.has(hotkey)) {
console.log(`register same hotkey: "${hotkey}"`);
}
this.hotkeySet.add(hotkey);
// support complex hotkey
keyCode = `${hotkey.altKey ? 'alt+' : ''}${
hotkey.ctrlKey ? 'ctrl+' : ''
}${hotkey.shiftKey ? 'shift+' : ''}${hotkey.metaKey ? 'meta+' : ''}${
hotkey.keyCode
}`;
hotkeyObj = hotkey;
}

const keyCode = `Key${toolCtor.hotkey.toUpperCase()}`;
// TODO: support complex hotkey
const token = this.editor.keybindingManager.register({
key: { keyCode: keyCode },
actionName: type,
when: () => this.enableToolTypes.includes(type),
action: () => {
this.setActiveTool(type);
},
});
this.keyBindingToken.push(token);
if (this.hotkeySet.has(keyCode)) {
console.log(`register same hotkey: "${keyCode}"`);
}
this.hotkeySet.add(keyCode);
const token = this.editor.keybindingManager.register({
key: hotkeyObj,
actionName: type,
when: () => this.enableToolTypes.includes(type),
action: () => {
this.setActiveTool(type);
},
});
this.keyBindingToken.push(token);
}
getActiveToolName() {
return this.currentTool?.type;
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 aacdc9b

Please sign in to comment.