11import { createElement } from 'react' ;
2- import { render } from 'react-dom' ;
2+ import { render , unmountComponentAtNode } from 'react-dom' ;
33import { globalContext , Editor , engineConfig , EngineOptions } from '@alilc/lowcode-editor-core' ;
44import {
55 Designer ,
@@ -16,7 +16,7 @@ import {
1616
1717import Outline , { OutlineBackupPane , getTreeMaster } from '@alilc/lowcode-plugin-outline-pane' ;
1818import DesignerPlugin from '@alilc/lowcode-plugin-designer' ;
19- import { Hotkey , Project , Skeleton , Setters , Material , Event } from '@alilc/lowcode-shell' ;
19+ import { Hotkey , Project , Skeleton , Setters , Material , Event , DocumentModel } from '@alilc/lowcode-shell' ;
2020import { getLogger , isPlainObject } from '@alilc/lowcode-utils' ;
2121import './modules/live-editing' ;
2222import utils from './modules/utils' ;
@@ -184,7 +184,8 @@ engineConfig.set('isOpenSource', isOpenSource);
184184 await plugins . register ( defaultPanelRegistry ) ;
185185} ) ( ) ;
186186
187- let engineInited = false ;
187+ // container which will host LowCodeEngine DOM
188+ let engineContainer : HTMLElement ;
188189// @ts -ignore webpack Define variable
189190export const version = VERSION_PLACEHOLDER ;
190191engineConfig . set ( 'ENGINE_VERSION' , version ) ;
@@ -193,23 +194,22 @@ export async function init(
193194 options ?: EngineOptions ,
194195 pluginPreference ?: PluginPreference ,
195196 ) {
196- if ( engineInited ) return ;
197- engineInited = true ;
197+ await destroy ( ) ;
198198 let engineOptions = null ;
199- let engineContainer = null ;
200199 if ( isPlainObject ( container ) ) {
201200 engineOptions = container ;
202201 engineContainer = document . createElement ( 'div' ) ;
202+ engineContainer . id = 'engine' ;
203203 document . body . appendChild ( engineContainer ) ;
204204 } else {
205205 engineOptions = options ;
206206 engineContainer = container ;
207207 if ( ! container ) {
208208 engineContainer = document . createElement ( 'div' ) ;
209+ engineContainer . id = 'engine' ;
209210 document . body . appendChild ( engineContainer ) ;
210211 }
211212 }
212- engineContainer . id = 'engine' ;
213213 engineConfig . setEngineOptions ( engineOptions as any ) ;
214214
215215 await plugins . init ( pluginPreference as any ) ;
@@ -222,3 +222,17 @@ export async function init(
222222 engineContainer ,
223223 ) ;
224224}
225+
226+ export async function destroy ( ) {
227+ // remove all documents
228+ const { documents } = project ;
229+ if ( Array . isArray ( documents ) && documents . length > 0 ) {
230+ documents . forEach ( ( ( doc : DocumentModel ) => project . removeDocument ( doc ) ) ) ;
231+ }
232+
233+ // TODO: delete plugins except for core plugins
234+
235+ // unmount DOM container, this will trigger React componentWillUnmount lifeCycle,
236+ // so necessary cleanups will be done.
237+ engineContainer && unmountComponentAtNode ( engineContainer ) ;
238+ }
0 commit comments