From 3b6541de2f78766d73fb303cfab08e817d79ff50 Mon Sep 17 00:00:00 2001 From: F-star Date: Sat, 7 Sep 2024 17:04:37 +0800 Subject: [PATCH] feat: add align shortcuts --- .../host_event_manager/command_key_binding.ts | 58 ++++++++++++++++++- packages/core/src/service/align_and_record.ts | 2 + 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/core/src/host_event_manager/command_key_binding.ts b/packages/core/src/host_event_manager/command_key_binding.ts index 3aea2117..a09d6378 100644 --- a/packages/core/src/host_event_manager/command_key_binding.ts +++ b/packages/core/src/host_event_manager/command_key_binding.ts @@ -6,6 +6,7 @@ import { SuikaText, } from '../graphs'; import { + alignAndRecord, arrangeAndRecord, flipHorizontalAndRecord, flipVerticalAndRecord, @@ -13,7 +14,7 @@ import { ungroupAndRecord, } from '../service'; import { groupAndRecord } from '../service/group_and_record'; -import { ArrangeType } from '../type'; +import { AlignType, ArrangeType } from '../type'; export class CommandKeyBinding { private isBound = false; @@ -248,6 +249,61 @@ export class CommandKeyBinding { action: backwardAction, }); + /*************** align **************/ + editor.keybindingManager.register({ + key: { altKey: true, keyCode: 'KeyA' }, + when: (ctx) => !ctx.isToolDragging, + actionName: 'AlignLeft', + action: () => { + alignAndRecord(editor, AlignType.Left); + }, + }); + + editor.keybindingManager.register({ + key: { altKey: true, keyCode: 'KeyH' }, + when: (ctx) => !ctx.isToolDragging, + actionName: 'AlignHorizontalCenters', + action: () => { + alignAndRecord(editor, AlignType.HCenter); + }, + }); + + editor.keybindingManager.register({ + key: { altKey: true, keyCode: 'KeyD' }, + when: (ctx) => !ctx.isToolDragging, + actionName: 'AlignRight', + action: () => { + alignAndRecord(editor, AlignType.Right); + }, + }); + + editor.keybindingManager.register({ + key: { altKey: true, keyCode: 'KeyW' }, + when: (ctx) => !ctx.isToolDragging, + actionName: 'AlignTop', + action: () => { + alignAndRecord(editor, AlignType.Top); + }, + }); + + editor.keybindingManager.register({ + key: { altKey: true, keyCode: 'KeyV' }, + when: (ctx) => !ctx.isToolDragging, + actionName: 'AlignVerticalCenters', + action: () => { + alignAndRecord(editor, AlignType.VCenter); + }, + }); + + editor.keybindingManager.register({ + key: { altKey: true, keyCode: 'KeyS' }, + when: (ctx) => !ctx.isToolDragging, + actionName: 'AlignBottom', + action: () => { + alignAndRecord(editor, AlignType.Bottom); + }, + }); + /*************** group **************/ // group const groupAction = () => { diff --git a/packages/core/src/service/align_and_record.ts b/packages/core/src/service/align_and_record.ts index 6110c122..d557d7f0 100644 --- a/packages/core/src/service/align_and_record.ts +++ b/packages/core/src/service/align_and_record.ts @@ -22,6 +22,8 @@ export const alignAndRecord = (editor: SuikaEditor, type: AlignType) => { const worldTfs = graphicsArr.map((item) => item.getWorldTransform()); const mixedBBox = mergeBoxes(bboxes); + // TODO: check whether had align + const transaction = new Transaction(editor); const updateGraphicsPosition = (