1+ import { IPublicModelPluginContext , IPublicEnumPluginRegisterLevel } from '@alilc/lowcode-types' ;
2+ import { getInjectedResource , injectAssets } from './utils' ;
3+ import Icon from './icon' ;
4+ import { Pane } from './pane' ;
5+ import React from 'react' ;
6+ import { InjectConfig } from './controller' ;
7+
8+ const injectConfig = new InjectConfig ( ) ;
9+
10+ let injectedPluginConfigMap = null ;
11+ let injectedPlugins = [ ] ;
12+
13+ export async function getInjectedPlugin ( name : string , ctx : IPublicModelPluginContext ) {
14+ if ( ! injectedPluginConfigMap ) {
15+ injectedPluginConfigMap = { } ;
16+ injectedPlugins = await getInjectedResource ( 'plugin' ) ;
17+ if ( injectedPlugins && injectedPlugins . length > 0 ) {
18+ injectedPlugins . forEach ( ( item : any ) => {
19+ const config = item . module ( ctx ) ;
20+ injectedPluginConfigMap [ config . name || item . module . pluginName ] = item . module ;
21+ } ) ;
22+ }
23+ }
24+ return injectedPluginConfigMap [ name ] ;
25+ }
26+
27+ export function AppInject ( ctx : IPublicModelPluginContext , options : any ) {
28+
29+ const { workspace } = ctx ;
30+
31+ const getInjectPlugin = async ( plugin : any , pluginOptions : any , ctx ?: IPublicModelPluginContext ) => {
32+ let pluginName = plugin . pluginName ;
33+ if ( ! pluginName ) {
34+ const pluginConfig = plugin ( ctx , pluginOptions ) ;
35+ pluginName = pluginConfig . name ;
36+ }
37+
38+ const injectedSameNamePlugin = await getInjectedPlugin ( pluginName , ctx ) ;
39+ const resourceName = ctx . editorWindow ?. resource ?. name ;
40+ const viewName = ( ctx ?. editorWindow ?. currentEditorView as any ) ?. viewName ;
41+ const isGlobal = ctx ?. registerLevel === IPublicEnumPluginRegisterLevel . Workspace ;
42+ if ( ! injectedSameNamePlugin ) {
43+ return plugin ;
44+ }
45+
46+ if ( isGlobal ) {
47+ if ( injectConfig . has ( pluginName , 'global' ) ) {
48+ return injectConfig . get ( pluginName , 'global' ) ? injectedSameNamePlugin : plugin ;
49+ }
50+
51+ injectConfig . set ( pluginName , 'global' , undefined , true )
52+ return injectedSameNamePlugin ;
53+ }
54+
55+ if ( ! viewName || ! resourceName ) {
56+ return injectedSameNamePlugin ;
57+ }
58+
59+ if ( injectConfig . has ( pluginName , resourceName , viewName ) ) {
60+ return injectConfig . get ( pluginName , resourceName , viewName ) ? injectedSameNamePlugin : plugin ;
61+ }
62+
63+ injectConfig . set ( pluginName , resourceName , viewName , true ) ;
64+
65+ return injectedSameNamePlugin ;
66+ }
67+
68+ ctx . config . set ( 'customPluginTransducer' , async ( originPlugin : any , ctx : IPublicModelPluginContext , options ) => {
69+ const injectedSameNamePlugin = await getInjectPlugin ( originPlugin , options , ctx ) ;
70+ return injectedSameNamePlugin ;
71+ } ) ;
72+
73+ return {
74+ // 插件名,注册环境下唯一
75+ name : 'LowcodePluginInjectAlt' ,
76+ // 依赖的插件(插件名数组)
77+ dep : [ ] ,
78+
79+ async init ( ) {
80+ const subPluginName = '___injectPlugins___' ;
81+
82+ const subPlugin = ( ctx : IPublicModelPluginContext ) => {
83+ injectAssets ( ctx )
84+ return {
85+ async init ( ) {
86+ }
87+ }
88+ }
89+
90+ subPlugin . pluginName = subPluginName ;
91+ subPlugin . meta = {
92+ dependencies : [ ] ,
93+ engines : {
94+ lowcodeEngine : '^1.0.0' , // 插件需要配合 ^1.0.0 的引擎才可运行
95+ } ,
96+ } ;
97+
98+ workspace . onChangeActiveWindow ( async ( ) => {
99+ for ( const pluginName in injectedPluginConfigMap ) {
100+ const injectedSameNamePlugin = await getInjectedPlugin ( pluginName , ctx ) ;
101+ if ( ! injectedSameNamePlugin ) {
102+ continue ;
103+ }
104+
105+ const resourceName = workspace . window ?. resource ?. name ;
106+ const currentEditorView = workspace . window ?. currentEditorView ;
107+ const viewName = ( currentEditorView as any ) ?. viewName ;
108+ if ( injectConfig . get ( pluginName , resourceName , viewName ) && ! currentEditorView ?. plugins . has ( pluginName ) ) {
109+ await currentEditorView ?. plugins . register ( injectedPluginConfigMap [ pluginName ] , {
110+ autoInit : true ,
111+ } ) ;
112+ }
113+
114+ if ( injectConfig . get ( pluginName , 'global' ) && ! workspace . plugins . has ( pluginName ) ) {
115+ await workspace . plugins . register ( injectedPluginConfigMap [ pluginName ] , {
116+ autoInit : true ,
117+ } ) ;
118+ }
119+ }
120+
121+ if ( workspace . window ?. currentEditorView && ! workspace . window ?. currentEditorView . plugins ?. has ( subPlugin . pluginName ) ) {
122+ await workspace . window ?. currentEditorView . plugins . register ( subPlugin )
123+ }
124+ } ) ;
125+
126+ await getInjectedPlugin ( undefined , ctx ) ;
127+
128+ ctx . skeleton . add ( {
129+ area : 'leftArea' ,
130+ name : 'inject-pane' ,
131+ type : 'PanelDock' ,
132+ props : {
133+ icon : < Icon /> ,
134+ description : '调试' ,
135+ className : 'inject-pane-icon' ,
136+ } ,
137+ index : 2 ,
138+ panelProps : {
139+ width : '600px' ,
140+ canSetFixed : false ,
141+ } ,
142+ content : ( props ) => {
143+ return < Pane { ...props } injectConfig = { injectConfig } />
144+ } ,
145+ contentProps : {
146+ pluginContext : ctx ,
147+ injectConfig,
148+ injectedPluginConfigMap,
149+ getInjectInfo : async ( ) => {
150+ const injectedSetters = await getInjectedResource ( 'vs' ) ;
151+ return {
152+ injectedSetters,
153+ }
154+ } ,
155+ updateInjectConfig : ( pluginName : string , resourceName : string , viewName : string , check : boolean ) => {
156+ injectConfig . set ( pluginName , resourceName , viewName , check )
157+ injectConfig . save ( )
158+ }
159+ } ,
160+ } ) ;
161+ }
162+ }
163+
164+ }
0 commit comments