@@ -18,9 +18,16 @@ import { ViewBasedJSONModel } from './core/viewbasedjsonmodel';
1818import { KeyHandler } from './keyhandler' ;
1919import { MouseHandler as FeatherGridMouseHandler } from './mousehandler' ;
2020import { Theme } from './utils' ;
21+ import { MODULE_NAME , MODULE_VERSION } from './version' ;
2122
2223import { DataGridModel as BackBoneModel } from './datagrid' ;
2324
25+ import {
26+ DOMWidgetModel ,
27+ ISerializers ,
28+ unpack_models ,
29+ } from '@jupyter-widgets/base' ;
30+
2431import '@lumino/default-theme/style/datagrid.css' ;
2532import '../style/feathergrid.css' ;
2633
@@ -107,6 +114,11 @@ const themeVariables: Map<string, string[]> = new Map([
107114] ) ;
108115
109116export class FeatherGrid extends Widget {
117+ static serializers : ISerializers = {
118+ ...DOMWidgetModel . serializers ,
119+ renderers : { deserialize : unpack_models as any } ,
120+ } ;
121+
110122 constructor ( options : DataGrid . IOptions = { } ) {
111123 super ( ) ;
112124 this . addClass ( 'ipydatagrid-widget' ) ;
@@ -871,6 +883,55 @@ export class FeatherGrid extends Widget {
871883 } ) ;
872884 }
873885
886+ private async _createTextRendererWidget ( ) {
887+ const model = await this . backboneModel . widget_manager . new_widget ( {
888+ model_name : 'TextRendererModel' ,
889+ model_module : MODULE_NAME ,
890+ model_module_version : MODULE_VERSION ,
891+ view_name : 'TextRendererView' ,
892+ view_module : MODULE_NAME ,
893+ view_module_version : MODULE_VERSION ,
894+ } ) ;
895+ return model ;
896+ }
897+
898+ private async _updateTextAlignment (
899+ columnName : string ,
900+ alignment : 'left' | 'center' | 'right' ,
901+ ) {
902+ const currentRenderers = this . backboneModel . get ( 'renderers' ) ;
903+ const defaultRenderer = this . backboneModel . get ( 'default_renderer' ) ;
904+ const currentRendererForColumn = currentRenderers [ columnName ] ;
905+
906+ // If there is a renderer for which we can set the alignment, set it
907+ if (
908+ currentRendererForColumn !== undefined &&
909+ 'horizontal_alignment' in currentRendererForColumn . attributes
910+ ) {
911+ currentRendererForColumn . set ( 'horizontal_alignment' , alignment ) ;
912+ currentRendererForColumn . save_changes ( ) ;
913+ return ;
914+ }
915+
916+ // Assuming it's using the default renderer, we create a new renderer and copy its attributes
917+ // TODO create a renderer of the same type as the default renderer
918+ const model = await this . _createTextRendererWidget ( ) ;
919+ for ( const attr in model . attributes ) {
920+ if ( attr in defaultRenderer . attributes ) {
921+ model . set ( attr , defaultRenderer . get ( attr ) ) ;
922+ }
923+ }
924+ model . set ( 'horizontal_alignment' , alignment ) ;
925+ model . save_changes ( ) ;
926+
927+ const updatedRenderers = { ...currentRenderers } ;
928+ updatedRenderers [ columnName ] = model ;
929+
930+ // TODO Find why this is not propagated to Python correctly
931+ this . backboneModel . set ( 'renderers' , updatedRenderers ) ;
932+ this . backboneModel . save_changes ( ) ;
933+ }
934+
874935 private _createCommandRegistry ( ) : CommandRegistry {
875936 const commands = new CommandRegistry ( ) ;
876937 commands . addCommand ( FeatherGridContextMenu . CommandID . SortAscending , {
@@ -1034,6 +1095,48 @@ export class FeatherGrid extends Widget {
10341095 this . grid . selectionModel ?. clear ( ) ;
10351096 } ,
10361097 } ) ;
1098+ commands . addCommand ( FeatherGridContextMenu . CommandID . AlignLeft , {
1099+ label : 'Align Left' ,
1100+ mnemonic : - 1 ,
1101+ execute : async ( args ) => {
1102+ const commandArgs = < FeatherGridContextMenu . CommandArgs > args ;
1103+ const columnName : string = this . dataModel . metadata (
1104+ commandArgs . region ,
1105+ commandArgs . rowIndex ,
1106+ commandArgs . columnIndex ,
1107+ ) [ 'name' ] ;
1108+
1109+ await this . _updateTextAlignment ( columnName , 'left' ) ;
1110+ } ,
1111+ } ) ;
1112+ commands . addCommand ( FeatherGridContextMenu . CommandID . AlignCenter , {
1113+ label : 'Align Center' ,
1114+ mnemonic : - 1 ,
1115+ execute : async ( args ) => {
1116+ const commandArgs = < FeatherGridContextMenu . CommandArgs > args ;
1117+ const columnName : string = this . dataModel . metadata (
1118+ commandArgs . region ,
1119+ commandArgs . rowIndex ,
1120+ commandArgs . columnIndex ,
1121+ ) [ 'name' ] ;
1122+
1123+ await this . _updateTextAlignment ( columnName , 'center' ) ;
1124+ } ,
1125+ } ) ;
1126+ commands . addCommand ( FeatherGridContextMenu . CommandID . AlignRight , {
1127+ label : 'Align Right' ,
1128+ mnemonic : - 1 ,
1129+ execute : async ( args ) => {
1130+ const commandArgs = < FeatherGridContextMenu . CommandArgs > args ;
1131+ const columnName : string = this . dataModel . metadata (
1132+ commandArgs . region ,
1133+ commandArgs . rowIndex ,
1134+ commandArgs . columnIndex ,
1135+ ) [ 'name' ] ;
1136+
1137+ await this . _updateTextAlignment ( columnName , 'right' ) ;
1138+ } ,
1139+ } ) ;
10371140
10381141 return commands ;
10391142 }
0 commit comments