@@ -6,6 +6,7 @@ import Showdown from "showdown"
66import iconvLite from "iconv-lite" ;
77import chardet from "chardet" ;
88import { Iconv } from "iconv"
9+ import { isMac , isWin } from "./env" ;
910
1011export const sleep = ( time = 1000 ) => {
1112 return new Promise ( ( resolve ) => {
@@ -103,7 +104,11 @@ export const StrUtil = {
103104 Date . now ( ) ,
104105 ( Math . floor ( Math . random ( ) * 1000000 ) + '' ) . padStart ( 6 , '0' )
105106 ] . join ( '' )
106- }
107+ } ,
108+ ucFirst ( str : string ) {
109+ if ( ! str ) return '' ;
110+ return str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) ;
111+ } ,
107112}
108113
109114export const TimeUtil = {
@@ -469,3 +474,105 @@ export const MarkdownUtil = {
469474 } ,
470475}
471476
477+ type HotkeyModifierType = 'Control' | 'Option' | 'Command' | 'Ctrl' | 'Alt' | 'Win' | 'Meta' | 'Shift' ;
478+ type HotkeyType = { key : string , modifiers : HotkeyModifierType [ ] }
479+
480+ export const HotKeyUtil = {
481+ orderModifiers ( modifiers : HotkeyModifierType [ ] ) {
482+ const order = [ 'Control' , 'Ctrl' , 'Command' , 'Meta' , 'Win' , 'Option' , 'Alt' , 'Shift' ] ;
483+ return modifiers . sort ( ( a , b ) => {
484+ return order . indexOf ( a ) - order . indexOf ( b ) ;
485+ } ) ;
486+ } ,
487+ unifyObject ( hotkey : HotkeyType ) {
488+ return {
489+ key : hotkey . key . toUpperCase ( ) ,
490+ modifiers : this . orderModifiers ( hotkey . modifiers . map ( modifier => StrUtil . ucFirst ( modifier ) ) )
491+ } ;
492+ } ,
493+ unifyString ( hotkey : string ) : HotkeyType {
494+ const parts = hotkey . split ( '+' ) ;
495+ const key = ( parts . pop ( ) || '' ) ;
496+ const modifiers : any [ ] = [ ] ;
497+ parts . forEach ( part => {
498+ modifiers . push ( StrUtil . ucFirst ( part . trim ( ) ) ) ;
499+ } ) ;
500+ return this . unifyObject ( { key, modifiers} ) ;
501+ } ,
502+ unify ( hotkeys : string | string [ ] | HotkeyType | HotkeyType [ ] ) : HotkeyType [ ] {
503+ if ( typeof hotkeys === 'string' ) {
504+ return [ this . unifyString ( hotkeys ) ] ;
505+ } else if ( Array . isArray ( hotkeys ) ) {
506+ return hotkeys . map ( hotkey => {
507+ if ( typeof hotkey === 'string' ) {
508+ return this . unifyString ( hotkey ) ;
509+ } else {
510+ return this . unifyObject ( hotkey ) ;
511+ }
512+ } ) ;
513+ } else {
514+ return [ this . unifyObject ( hotkeys ) ] ;
515+ }
516+ } ,
517+ getFromEvent ( event : any ) : HotkeyType | null {
518+ const valid = [
519+ 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' ,
520+ 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' ,
521+ '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '0' ,
522+ 'Space' ,
523+ ]
524+ const key = ( event . key || '' ) . toUpperCase ( ) ;
525+ if ( ! event || ! event . key || ! valid . includes ( key ) ) {
526+ return null
527+ }
528+ const modifiers : HotkeyModifierType [ ] = [ ] ;
529+ if ( isWin ) {
530+ if ( event . ctrlKey || event . control ) {
531+ modifiers . push ( 'Ctrl' ) ;
532+ }
533+ if ( event . altKey || event . alt ) {
534+ modifiers . push ( 'Alt' ) ;
535+ }
536+ if ( event . metaKey || event . meta ) {
537+ modifiers . push ( 'Win' ) ;
538+ }
539+ } else if ( isMac ) {
540+ if ( event . ctrlKey || event . control ) {
541+ modifiers . push ( 'Control' ) ;
542+ }
543+ if ( event . altKey || event . alt ) {
544+ modifiers . push ( 'Option' ) ;
545+ }
546+ if ( event . metaKey || event . meta ) {
547+ modifiers . push ( 'Command' ) ;
548+ }
549+ } else {
550+ if ( event . ctrlKey || event . control ) {
551+ modifiers . push ( 'Ctrl' ) ;
552+ }
553+ if ( event . altKey || event . alt ) {
554+ modifiers . push ( 'Alt' ) ;
555+ }
556+ if ( event . metaKey || event . meta ) {
557+ modifiers . push ( 'Meta' ) ;
558+ }
559+ }
560+ if ( event . shiftKey || event . shift ) {
561+ modifiers . push ( 'Shift' ) ;
562+ }
563+ return this . unifyObject ( { key, modifiers} ) ;
564+ } ,
565+ match ( hotkeysForMatch : HotkeyType [ ] , hotkey : HotkeyType ) : boolean {
566+ if ( ! hotkeysForMatch || ! hotkey ) {
567+ return false
568+ }
569+ const hotKeyStr = hotkey . modifiers . join ( '+' ) + '+' + hotkey . key ;
570+ for ( const key of hotkeysForMatch ) {
571+ const keyStr = key . modifiers . join ( '+' ) + '+' + key . key ;
572+ if ( keyStr === hotKeyStr ) {
573+ return true
574+ }
575+ }
576+ return false
577+ }
578+ }
0 commit comments