import { ExtensionContext , TreeItemCollapsibleState , Command } from 'vscode' ; import { EditorPanel } from '../../webview/panel' ; import { BaseTreeItem } from './Base' ; import { decorateLocale , NodeHelper , resolveFlattenRootKeypath , ROOT_KEY , resolveFlattenRoot } from '~/utils' ; import i18n from '~/i18n' ; import { Node , Translator , CurrentFile , Config } from '~/core' ; import { Commands } from '~/commands' ; export class LocaleTreeItem extends BaseTreeItem { public readonly node : Node ; constructor ( ctx , node , public flatten = false , public readonly displayLocale ? , public readonly listedLocales ? ) { super ( ctx ) ; if ( node . type !== 'record' ) this . node = resolveFlattenRoot ( node ) ; else this . node = node ; } get tooltip ( ) : string { return resolveFlattenRootKeypath ( this . node . keypath ) ; } getLabel ( ) { if ( this . node . type === 'record' ) { return decorateLocale ( this . node . locale ) ; } else { return this . flatten ? resolveFlattenRootKeypath ( this . node . keypath ) : this . node . keyname === ROOT_KEY ? '<root>' : this . node . keyname ; } } get collapsibleState ( ) { if ( this . node . type === 'record' || this . editorMode ) return TreeItemCollapsibleState . None ; else return TreeItemCollapsibleState . Collapsed ; } set collapsibleState ( _ ) { } get description ( ) : string { if ( this . node . type === 'node' ) return this . node . getValue ( this . displayLocale , true ) ; if ( this . node . type === 'record' ) return this . node . value ; return '' ; } get iconPath ( ) { if ( Translator . isTranslating ( this . node ) ) return this . getIcon ( 'loading' ) ; if ( this . node . type === 'record' ) { return this . getFlagIcon ( this . node . locale ) ; } else if ( this . node . shadow ) { return this . getIcon ( 'icon-unknown' ) ; } else if ( this . node . type === 'tree' ) { if ( this . node . isCollection ) return this . getIcon ( 'collection' ) ; else return this . getIcon ( 'namespace' ) ; } else if ( this . node . type === 'node' ) { if ( this . description ) return this . getIcon ( 'string' ) ; else return this . getIcon ( 'string-missing' ) ; } } get contextValue ( ) { const values = [ this . node . type ] ; if ( this . node . readonly ) values . push ( 'readonly' ) ; else values . push ( 'writable' ) ; if ( NodeHelper . isOpenable ( this . node ) ) values . push ( 'openable' ) ; if ( ! this . editorMode ) { if ( NodeHelper . isTranslatable ( this . node ) ) values . push ( 'translatable' ) ; if ( NodeHelper . isEditable ( this . node ) ) values . push ( 'editable' ) ; if ( this . node . type !== 'tree' ) values . push ( 'open-in-editor' ) ; } return values . join ( '-' ) ; } get editorMode ( ) { return this . node . type === 'node' && ( Config . preferEditor || ! ! EditorPanel . currentPanel ) ; } async getChildren ( filter = ( ) => true ) { if ( this . editorMode ) return [ ] ; let nodes = [ ] ; if ( this . node . type === 'tree' ) nodes = Object . values ( this . node . children ) ; else if ( this . node . type === 'node' ) nodes = Object . values ( CurrentFile . loader . getShadowLocales ( this . node , this . listedLocales ) ) ; const items = nodes . filter ( filter ) . map ( node => new LocaleTreeItem ( this . ctx , node , false ) ) ; return items ; } get command ( ) : Command | undefined { if ( this . editorMode ) { return { command : Commands . open_in_editor , title : i18n . t ( 'command.open_in_editor' ) , arguments : [ this ] , } ; } return undefined ; } }