import { KeyCodes } from 'office-ui-fabric-react/lib/Utilities' ; import { Browser } from 'roosterjs-editor-dom' ; import { PluginDomEvent , PluginEvent , PluginEventType } from 'roosterjs-editor-types' ; export const enum RoosterShortcutCommands { None = 'None' , Bold = 'Bold' , Italic = 'Italic' , Underline = 'Underline' , Undo = 'Undo' , Redo = 'Redo' , Bullet = 'Bullet' , Numbering = 'Numbering' , InsertLink = 'InsertLink' , ClearFormat = 'ClearFormat' } interface ShortcutCommand { metaKey : boolean ; ctrlKey : boolean ; shiftKey : boolean ; which : number ; command : RoosterShortcutCommands ; } const macCommands = [ { metaKey : true , ctrlKey : false , shiftKey : false , which : KeyCodes . b , command : RoosterShortcutCommands . Bold } , { metaKey : true , ctrlKey : false , shiftKey : false , which : KeyCodes . i , command : RoosterShortcutCommands . Italic } , { metaKey : true , ctrlKey : false , shiftKey : false , which : KeyCodes . u , command : RoosterShortcutCommands . Underline } , { metaKey : true , ctrlKey : false , shiftKey : false , which : KeyCodes . z , command : RoosterShortcutCommands . Undo } , { metaKey : true , ctrlKey : false , shiftKey : true , which : KeyCodes . z , command : RoosterShortcutCommands . Redo } , { metaKey : true , ctrlKey : false , shiftKey : false , which : KeyCodes . period , command : RoosterShortcutCommands . Bullet } , { metaKey : true , ctrlKey : false , shiftKey : false , which : KeyCodes . forwardSlash , command : RoosterShortcutCommands . Numbering } , { metaKey : true , ctrlKey : false , shiftKey : false , which : KeyCodes . k , command : RoosterShortcutCommands . InsertLink } , { metaKey : true , ctrlKey : false , shiftKey : false , which : KeyCodes . space , command : RoosterShortcutCommands . ClearFormat } ] ; const winCommands = [ { metaKey : false , ctrlKey : true , shiftKey : false , which : KeyCodes . b , command : RoosterShortcutCommands . Bold } , { metaKey : false , ctrlKey : true , shiftKey : false , which : KeyCodes . i , command : RoosterShortcutCommands . Italic } , { metaKey : false , ctrlKey : true , shiftKey : false , which : KeyCodes . u , command : RoosterShortcutCommands . Underline } , { metaKey : false , ctrlKey : true , shiftKey : false , which : KeyCodes . z , command : RoosterShortcutCommands . Undo } , { metaKey : false , ctrlKey : true , shiftKey : false , which : KeyCodes . y , command : RoosterShortcutCommands . Redo } , { metaKey : false , ctrlKey : true , shiftKey : false , which : KeyCodes . period , command : RoosterShortcutCommands . Bullet } , { metaKey : false , ctrlKey : true , shiftKey : false , which : KeyCodes . forwardSlash , command : RoosterShortcutCommands . Numbering } , { metaKey : false , ctrlKey : true , shiftKey : false , which : KeyCodes . k , command : RoosterShortcutCommands . InsertLink } , { metaKey : false , ctrlKey : true , shiftKey : false , which : KeyCodes . space , command : RoosterShortcutCommands . ClearFormat } ] ; export function getCommandFromEvent ( event ) { if ( event . eventType !== PluginEventType . KeyDown ) { return RoosterShortcutCommands . None ; } const commands = Browser . isMac ? macCommands : winCommands ; const keyboardEvent = ( event as PluginDomEvent ) . rawEvent as KeyboardEvent ; for ( const cmd of commands ) { if ( ! keyboardEvent . altKey && cmd . ctrlKey === keyboardEvent . ctrlKey && cmd . metaKey === keyboardEvent . metaKey && cmd . shiftKey === keyboardEvent . shiftKey && cmd . which === keyboardEvent . which ) { return cmd . command ; } } return RoosterShortcutCommands . None ; }