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

feat: support complex hotkey #168

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
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
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
44 changes: 29 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,40 @@ 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;
}

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
Loading