@@ -7,29 +7,33 @@ import {
77 type CommandData ,
88 type CommandKeys ,
99 CommandUtils ,
10- DefaultShortcuts ,
10+ Config ,
1111 I18n ,
1212 type I18nKeys ,
1313 type IConverter ,
1414 Localize ,
1515 Logger ,
1616 PubSub ,
1717 Result ,
18+ ShortcutProfiles ,
1819} from "chili-core" ;
1920import style from "./ribbonButton.module.css" ;
2021
2122export class RibbonButton extends HTMLElement {
22- private observer ?: MutationObserver ;
23+ #shortcut?: string ;
24+ get shortcut ( ) {
25+ return this . #shortcut;
26+ }
2327
2428 constructor (
25- display : I18nKeys ,
29+ readonly commandName : CommandKeys ,
2630 icon : string ,
2731 size : ButtonSize ,
2832 readonly onClick : ( ) => void ,
29- shortcut ?: string ,
33+ display ?: I18nKeys ,
3034 ) {
3135 super ( ) ;
32- this . initHTML ( display , icon , size , shortcut ) ;
36+ this . initHTML ( display ?? `command. ${ commandName } ` , icon , size ) ;
3337 this . addEventListener ( "click" , onClick ) ;
3438 }
3539
@@ -43,26 +47,16 @@ export class RibbonButton extends HTMLElement {
4347 return new RibbonToggleButton ( data , size ) ;
4448 }
4549
46- const shortcutData = DefaultShortcuts [ commandName ] ;
47- const shortcut = Array . isArray ( shortcutData ) ? shortcutData [ 0 ] : shortcutData ;
48-
49- return new RibbonButton (
50- `command.${ data . key } ` ,
51- data . icon ,
52- size ,
53- ( ) => {
54- PubSub . default . pub ( "executeCommand" , commandName ) ;
55- } ,
56- shortcut ,
57- ) ;
50+ return new RibbonButton ( data . key , data . icon , size , ( ) => {
51+ PubSub . default . pub ( "executeCommand" , commandName ) ;
52+ } ) ;
5853 }
5954
6055 dispose ( ) : void {
6156 this . removeEventListener ( "click" , this . onClick ) ;
62- this . observer ?. disconnect ( ) ;
6357 }
6458
65- private initHTML ( display : I18nKeys , icon : string , size : ButtonSize , shortcut ?: string ) {
59+ private initHTML ( display : I18nKeys , icon : string , size : ButtonSize ) {
6660 const image = svg ( { icon } ) ;
6761 this . className = size === ButtonSize . large ? style . normal : style . small ;
6862 image . classList . add ( size === ButtonSize . large ? style . icon : style . smallIcon ) ;
@@ -72,26 +66,26 @@ export class RibbonButton extends HTMLElement {
7266 } ) ;
7367
7468 I18n . set ( this , "title" , display ) ;
69+ this . updateShortcut ( ) ;
70+
71+ this . append ( image , text ) ;
72+ }
73+
74+ updateShortcut ( ) {
75+ const shortcutData = ShortcutProfiles [ Config . instance . navigation3D ] [ this . commandName ] ;
76+ const shortcut = Array . isArray ( shortcutData ) ? shortcutData . join ( "; " ) : shortcutData ;
7577
7678 if ( shortcut ) {
77- const updateTitle = ( ) => {
78- const current = this . getAttribute ( "title" ) ;
79- if ( current && ! current . includes ( `(${ shortcut } )` ) ) {
80- this . setAttribute ( "title" , `${ current } (${ shortcut } )` ) ;
81- }
82- } ;
83- updateTitle ( ) ;
84- this . observer = new MutationObserver ( ( mutations ) => {
85- for ( const m of mutations ) {
86- if ( m . type === "attributes" && m . attributeName === "title" ) {
87- updateTitle ( ) ;
88- }
89- }
90- } ) ;
91- this . observer . observe ( this , { attributes : true } ) ;
79+ if ( this . #shortcut) {
80+ this . title = this . title . replace ( this . #shortcut, shortcut ) ;
81+ } else {
82+ this . title += ` (${ shortcut } )` ;
83+ }
84+ this . #shortcut = shortcut ;
85+ } else if ( this . #shortcut) {
86+ this . title = this . title . replace ( this . #shortcut, "" ) ;
87+ this . #shortcut = undefined ;
9288 }
93-
94- this . append ( image , text ) ;
9589 }
9690}
9791
@@ -109,7 +103,7 @@ class ToggleConverter implements IConverter {
109103
110104export class RibbonToggleButton extends RibbonButton {
111105 constructor ( data : CommandData , size : ButtonSize ) {
112- super ( `command. ${ data . key } ` , data . icon , size , ( ) => {
106+ super ( data . key , data . icon , size , ( ) => {
113107 PubSub . default . pub ( "executeCommand" , data . key ) ;
114108 } ) ;
115109
0 commit comments