11import { isLocationChildrenDetail } from '@alilc/lowcode-utils' ;
2- import { IPublicModelPluginContext , IPublicTypeActiveTarget , IPublicModelNode } from '@alilc/lowcode-types' ;
2+ import { IPublicModelPluginContext , IPublicTypeActiveTarget , IPublicModelNode , IPublicTypeDisposable , IPublicEnumPluginRegisterLevel } from '@alilc/lowcode-types' ;
33import TreeNode from './tree-node' ;
44import { Tree } from './tree' ;
5+ import EventEmitter from 'events' ;
6+ import { enUS , zhCN } from '../locale' ;
7+ import { ReactNode } from 'react' ;
58
69export interface ITreeBoard {
710 readonly at : string | symbol ;
811 scrollToNode ( treeNode : TreeNode , detail ?: any ) : void ;
912}
1013
14+ enum EVENT_NAMES {
15+ pluginContextChanged = 'pluginContextChanged' ,
16+ }
17+
18+ export interface IOutlinePanelPluginContext extends IPublicModelPluginContext {
19+ extraTitle ?: string ;
20+ intlNode ( id : string , params ?: object ) : ReactNode ;
21+ intl ( id : string , params ?: object ) : string ;
22+ getLocale ( ) : string ;
23+ }
24+
1125export class TreeMaster {
12- readonly pluginContext : IPublicModelPluginContext ;
26+ pluginContext : IOutlinePanelPluginContext ;
1327
1428 private boards = new Set < ITreeBoard > ( ) ;
1529
1630 private treeMap = new Map < string , Tree > ( ) ;
1731
18- constructor ( pluginContext : IPublicModelPluginContext ) {
19- this . pluginContext = pluginContext ;
20- let startTime : any ;
21- const { event, project, canvas } = this . pluginContext ;
22- canvas . dragon ?. onDragstart ( ( ) => {
23- startTime = Date . now ( ) / 1000 ;
24- // needs?
25- this . toVision ( ) ;
26- } ) ;
27- canvas . activeTracker ?. onChange ( ( target : IPublicTypeActiveTarget ) => {
28- const { node, detail } = target ;
29- const tree = this . currentTree ;
30- if ( ! tree /* || node.document !== tree.document */ ) {
31- return ;
32- }
33- const treeNode = tree . getTreeNode ( node ) ;
34- if ( detail && isLocationChildrenDetail ( detail ) ) {
35- treeNode . expand ( true ) ;
36- } else {
37- treeNode . expandParents ( ) ;
38- }
39- this . boards . forEach ( ( board ) => {
40- board . scrollToNode ( treeNode , detail ) ;
32+ private disposeEvents : ( IPublicTypeDisposable | undefined ) [ ] = [ ] ;
33+
34+ event = new EventEmitter ( ) ;
35+
36+ constructor ( pluginContext : IPublicModelPluginContext , readonly options : {
37+ extraTitle ?: string ;
38+ } ) {
39+ this . setPluginContext ( pluginContext ) ;
40+ const { workspace } = this . pluginContext ;
41+ this . initEvent ( ) ;
42+ if ( pluginContext . registerLevel === IPublicEnumPluginRegisterLevel . Workspace ) {
43+ workspace . onWindowRendererReady ( ( ) => {
44+ this . setPluginContext ( workspace . window ?. currentEditorView ) ;
45+ let dispose : IPublicTypeDisposable | undefined ;
46+ const windowViewTypeChangeEvent = ( ) => {
47+ dispose = workspace . window ?. onChangeViewType ( ( ) => {
48+ this . setPluginContext ( workspace . window ?. currentEditorView ) ;
49+ } ) ;
50+ } ;
51+
52+ windowViewTypeChangeEvent ( ) ;
53+
54+ workspace . onChangeActiveWindow ( ( ) => {
55+ windowViewTypeChangeEvent ( ) ;
56+ this . setPluginContext ( workspace . window ?. currentEditorView ) ;
57+ dispose && dispose ( ) ;
58+ } ) ;
4159 } ) ;
60+ }
61+ }
62+
63+ private setPluginContext ( pluginContext : IPublicModelPluginContext | undefined ) {
64+ if ( ! pluginContext ) {
65+ return ;
66+ }
67+ const { intl, intlNode, getLocale } = pluginContext . common . utils . createIntl ( {
68+ 'en-US' : enUS ,
69+ 'zh-CN' : zhCN ,
4270 } ) ;
43- canvas . dragon ?. onDragend ( ( ) => {
44- const endTime : any = Date . now ( ) / 1000 ;
45- const nodes = project . currentDocument ?. selection ?. getNodes ( ) ;
46- event . emit ( 'outlinePane.dragend' , {
47- selected : nodes
48- ?. map ( ( n ) => {
49- if ( ! n ) {
50- return ;
51- }
52- const npm = n ?. componentMeta ?. npm ;
53- return (
54- [ npm ?. package , npm ?. componentName ] . filter ( ( item ) => ! ! item ) . join ( '-' ) || n ?. componentMeta ?. componentName
55- ) ;
56- } )
57- . join ( '&' ) ,
58- time : ( endTime - startTime ) . toFixed ( 2 ) ,
59- } ) ;
71+ let _pluginContext : IOutlinePanelPluginContext = Object . assign ( pluginContext , {
72+ intl,
73+ intlNode,
74+ getLocale,
6075 } ) ;
61- project . onRemoveDocument ( ( data : { id : string } ) => {
62- const { id } = data ;
63- this . treeMap . delete ( id ) ;
76+ _pluginContext . extraTitle = this . options && this . options [ 'extraTitle' ] ;
77+ this . pluginContext = _pluginContext ;
78+ this . disposeEvent ( ) ;
79+ this . initEvent ( ) ;
80+ this . emitPluginContextChange ( ) ;
81+ }
82+
83+ private disposeEvent ( ) {
84+ this . disposeEvents . forEach ( d => {
85+ d && d ( ) ;
6486 } ) ;
6587 }
6688
89+ private initEvent ( ) {
90+ let startTime : any ;
91+ const { event, project, canvas } = this . pluginContext ;
92+ this . disposeEvents = [
93+ canvas . dragon ?. onDragstart ( ( ) => {
94+ startTime = Date . now ( ) / 1000 ;
95+ // needs?
96+ this . toVision ( ) ;
97+ } ) ,
98+ canvas . activeTracker ?. onChange ( ( target : IPublicTypeActiveTarget ) => {
99+ const { node, detail } = target ;
100+ const tree = this . currentTree ;
101+ if ( ! tree /* || node.document !== tree.document */ ) {
102+ return ;
103+ }
104+ const treeNode = tree . getTreeNode ( node ) ;
105+ if ( detail && isLocationChildrenDetail ( detail ) ) {
106+ treeNode . expand ( true ) ;
107+ } else {
108+ treeNode . expandParents ( ) ;
109+ }
110+ this . boards . forEach ( ( board ) => {
111+ board . scrollToNode ( treeNode , detail ) ;
112+ } ) ;
113+ } ) ,
114+ canvas . dragon ?. onDragend ( ( ) => {
115+ const endTime : any = Date . now ( ) / 1000 ;
116+ const nodes = project . currentDocument ?. selection ?. getNodes ( ) ;
117+ event . emit ( 'outlinePane.dragend' , {
118+ selected : nodes
119+ ?. map ( ( n ) => {
120+ if ( ! n ) {
121+ return ;
122+ }
123+ const npm = n ?. componentMeta ?. npm ;
124+ return (
125+ [ npm ?. package , npm ?. componentName ] . filter ( ( item ) => ! ! item ) . join ( '-' ) || n ?. componentMeta ?. componentName
126+ ) ;
127+ } )
128+ . join ( '&' ) ,
129+ time : ( endTime - startTime ) . toFixed ( 2 ) ,
130+ } ) ;
131+ } ) ,
132+ project . onRemoveDocument ( ( data : { id : string } ) => {
133+ const { id } = data ;
134+ this . treeMap . delete ( id ) ;
135+ } ) ,
136+ ] ;
137+ }
138+
67139 private toVision ( ) {
68140 const tree = this . currentTree ;
69141 if ( tree ) {
@@ -86,14 +158,22 @@ export class TreeMaster {
86158 // todo others purge
87159 }
88160
161+ onPluginContextChange ( fn : ( ) => void ) {
162+ this . event . on ( EVENT_NAMES . pluginContextChanged , fn ) ;
163+ }
164+
165+ emitPluginContextChange ( ) {
166+ this . event . emit ( EVENT_NAMES . pluginContextChanged ) ;
167+ }
168+
89169 get currentTree ( ) : Tree | null {
90170 const doc = this . pluginContext . project . getCurrentDocument ( ) ;
91171 if ( doc ) {
92172 const { id } = doc ;
93173 if ( this . treeMap . has ( id ) ) {
94174 return this . treeMap . get ( id ) ! ;
95175 }
96- const tree = new Tree ( this . pluginContext ) ;
176+ const tree = new Tree ( this ) ;
97177 this . treeMap . set ( id , tree ) ;
98178 return tree ;
99179 }
0 commit comments