@@ -7,10 +7,11 @@ import { CaretAdapter, FormattingAdapter } from '@editorjs/dom-adapters';
77import type { CoreConfigValidated } from './entities/Config.js' ;
88import type { CoreConfig } from '@editorjs/sdk' ;
99import { BlocksManager } from './components/BlockManager.js' ;
10- import { EditorUI } from './ui/Editor/index.js' ;
11- import { ToolboxUI } from './ui/Toolbox/index.js' ;
12- import { InlineToolbarUI } from './ui/InlineToolbar/index.js' ;
1310import { SelectionManager } from './components/SelectionManager.js' ;
11+ import { EventBus } from './components/EventBus/index.js' ;
12+ import type { EditorjsPluginConstructor } from './entities/EditorjsPlugin.js' ;
13+ import { EditorAPI } from './api/index.js' ;
14+ import { UiComponentType } from './entities/Ui.js' ;
1415
1516/**
1617 * If no holder is provided via config, the editor will be appended to the element with this id
@@ -23,7 +24,6 @@ const DEFAULT_HOLDER_ID = 'editorjs';
2324 * - subscribes to model updates
2425 * - creates Adapters for Tools
2526 * - creates Tools
26- * - adds Blocks accodring to model updates
2727 */
2828export default class Core {
2929 /**
@@ -71,10 +71,7 @@ export default class Core {
7171
7272 this . #iocContainer. set ( 'EditorConfig' , this . #config) ;
7373
74- const { blocks } = composeDataFromVersion2 ( config . data ?? { blocks : [ ] } ) ;
75-
7674 this . #model = new EditorJSModel ( ) ;
77-
7875 this . #iocContainer. set ( EditorJSModel , this . #model) ;
7976
8077 this . #toolsManager = this . #iocContainer. get ( ToolsManager ) ;
@@ -92,8 +89,27 @@ export default class Core {
9289 config . onModelUpdate ?.( this . #model) ;
9390 } ) ;
9491 }
92+ }
93+
94+ /**
95+ * Initialize and injects Plugin into the container
96+ * @param plugin - allows to pass any implementation of editor plugins
97+ */
98+ public use ( plugin : EditorjsPluginConstructor ) : Core {
99+ const pluginType = plugin . type ;
100+
101+ this . #iocContainer. set ( pluginType , plugin ) ;
102+
103+ return this ;
104+ }
105+
106+ /**
107+ * Initializes the core
108+ */
109+ public initialize ( ) : void {
110+ const { blocks } = composeDataFromVersion2 ( this . #config. data ?? { blocks : [ ] } ) ;
95111
96- this . #prepareUI ( ) ;
112+ this . initializePlugins ( ) ;
97113
98114 this . #toolsManager. prepareTools ( )
99115 . then ( ( ) => {
@@ -105,15 +121,36 @@ export default class Core {
105121 }
106122
107123 /**
108- * Renders Editor`s UI
124+ * Initialize all registered UI plugins
109125 */
110- #prepareUI( ) : void {
111- const editorUI = this . #iocContainer. get ( EditorUI ) ;
126+ private initializePlugins ( ) : void {
127+ /**
128+ * Get all registered plugin types from the container
129+ */
130+ const pluginTypes = Object . values ( UiComponentType ) as string [ ] ;
131+
132+ for ( const pluginType of pluginTypes ) {
133+ const plugin = this . #iocContainer. get < EditorjsPluginConstructor > ( pluginType ) ;
112134
113- this . #iocContainer. get ( ToolboxUI ) ;
114- this . #iocContainer. get ( InlineToolbarUI ) ;
135+ if ( plugin !== undefined && typeof plugin === 'function' ) {
136+ this . initializePlugin ( plugin ) ;
137+ }
138+ }
139+ }
115140
116- editorUI . render ( ) ;
141+ /**
142+ * Create instance of plugin
143+ * @param plugin - Plugin constructor to initialize
144+ */
145+ private initializePlugin ( plugin : EditorjsPluginConstructor ) : void {
146+ const eventBus = this . #iocContainer. get ( EventBus ) ;
147+ const api = this . #iocContainer. get ( EditorAPI ) ;
148+
149+ new plugin ( {
150+ config : this . #config,
151+ api,
152+ eventBus,
153+ } ) ;
117154 }
118155
119156 /**
@@ -147,4 +184,10 @@ export default class Core {
147184 }
148185}
149186
187+ /**
188+ * @todo move to "sdk" package
189+ */
150190export * from './entities/index.js' ;
191+ export * from './components/EventBus/index.js' ;
192+ export * from './api/index.js' ;
193+ export * from './tools/facades/index.js' ;
0 commit comments