@@ -9,9 +9,10 @@ import * as fs from 'fs';
9
9
import { BrowserWindow , shell , session , app } from 'electron' ;
10
10
import log from 'electron-log' ;
11
11
import Store from 'electron-store' ;
12
+ import axios from 'axios' ;
13
+ import WebContainer from './webContainer' ;
12
14
13
15
// import DB from './db';
14
- import axios from 'axios' ;
15
16
import {
16
17
deleteFolder ,
17
18
getAppDir ,
@@ -33,10 +34,14 @@ class PluginManager {
33
34
34
35
private configDir : string = path . join ( getAppDir ( ) , 'config' ) ;
35
36
37
+ private container : WebContainer = new WebContainer ( ) ;
38
+
36
39
private sortSettingId = 'sortSettingId' ;
37
40
38
41
public allPlugins : any [ ] = [ ] ;
39
42
43
+ private webContainers : Map < string , string > = new Map ( ) ;
44
+
40
45
// 创建一个新的存储实例
41
46
public store = new Store ( ) ;
42
47
@@ -93,8 +98,8 @@ class PluginManager {
93
98
const packageJsonPath = path . join ( pluginPath , 'package.json' ) ;
94
99
const packagePath = path . join ( pluginPath , 'plugin.json' ) ;
95
100
if ( fs . existsSync ( packagePath ) ) {
96
- const packageObj = readJsonObjFromFile ( packageJsonPath ) ;
97
- const pluginObj = readJsonObjFromFile ( packagePath ) ;
101
+ const packageObj : any = readJsonObjFromFile ( packageJsonPath ) ;
102
+ const pluginObj : any = readJsonObjFromFile ( packagePath ) ;
98
103
if ( packageObj ) {
99
104
pluginObj . name = packageObj . name ;
100
105
pluginObj . version = packageObj . version ;
@@ -116,7 +121,7 @@ class PluginManager {
116
121
117
122
const { sort } = this . setting . getSetting ( ) ;
118
123
if ( sort ) {
119
- let sortData = this . store . get ( this . sortSettingId , { } ) ;
124
+ let sortData : any = this . store . get ( this . sortSettingId , { } ) ;
120
125
sortData = new Map ( Object . entries ( sortData ) ) ;
121
126
if ( sortData . size > 0 ) {
122
127
pluginList . sort ( ( a , b ) => {
@@ -153,7 +158,10 @@ class PluginManager {
153
158
return this . allPlugins . find ( ( plugin ) => name === plugin . name ) ;
154
159
}
155
160
156
- public openPlugin ( name : string , pluginViewPool : Map < string , BrowserWindow > ) {
161
+ public async openPlugin (
162
+ name : string ,
163
+ pluginViewPool : Map < string , BrowserWindow > ,
164
+ ) {
157
165
const pluginObj = this . getPlugin ( name ) ;
158
166
const storeId = `${ name } -windowSize` ;
159
167
const savedSize = this . store . get ( storeId , {
@@ -169,7 +177,7 @@ class PluginManager {
169
177
const { sort } = this . setting . getSetting ( ) ;
170
178
171
179
if ( sort ) {
172
- let sortData = this . store . get ( this . sortSettingId , { } ) ;
180
+ let sortData : any = this . store . get ( this . sortSettingId , { } ) ;
173
181
sortData = new Map ( Object . entries ( sortData ) ) ;
174
182
if ( sortData . has ( name ) ) {
175
183
const click = sortData . get ( name ) ;
@@ -201,10 +209,6 @@ class PluginManager {
201
209
spellcheck : false ,
202
210
} ,
203
211
} ) ;
204
- // pluginWin.setPosition(
205
- // pluginWin.getPosition()[0],
206
- // pluginWin.getPosition()[1],
207
- // );
208
212
pluginWin . on ( 'resize' , ( ) => {
209
213
const [ width , height ] = pluginWin ?. getSize ( ) || [
210
214
DEFAULT_WINDOW_WIDTH ,
@@ -213,7 +217,21 @@ class PluginManager {
213
217
214
218
this . store . set ( storeId , { width, height } ) ;
215
219
} ) ;
216
- if ( pluginObj . entry && pluginObj . entry . startsWith ( 'http' ) ) {
220
+ if ( pluginObj . webContainer ) {
221
+ let url : string ;
222
+ if ( ! this . webContainers . has ( name ) ) {
223
+ const port = await this . container . listenPlugin (
224
+ name ,
225
+ pluginObj . pluginPath ,
226
+ ) ;
227
+ url = path . join ( `http://127.0.0.1:${ port } ` , pluginObj . entry ) ;
228
+ } else {
229
+ url = this . webContainers . get ( name ) as string ;
230
+ }
231
+
232
+ pluginWin . loadURL ( url ) ;
233
+ this . webContainers . set ( name , url ) ;
234
+ } else if ( pluginObj . entry && pluginObj . entry . startsWith ( 'http' ) ) {
217
235
pluginWin . loadURL ( pluginObj . entry ) ;
218
236
} else {
219
237
// pluginWin.loadURL(resolveHtmlPath('plugin.html'));
@@ -242,6 +260,10 @@ class PluginManager {
242
260
pluginWin . show ( ) ;
243
261
} ) ;
244
262
pluginWin . on ( 'closed' , async ( ) => {
263
+ if ( pluginObj . webContainer ) {
264
+ this . webContainers . delete ( name ) ;
265
+ this . container . closePlugin ( name ) ;
266
+ }
245
267
if ( pluginViewPool ) {
246
268
pluginViewPool . delete ( name ) ;
247
269
}
0 commit comments