-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into inline-tool-adapter
- Loading branch information
Showing
10 changed files
with
357 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import 'reflect-metadata'; | ||
import { Inject, Service } from 'typedi'; | ||
import { BlocksManager } from '../components/BlockManager.js'; | ||
import { BlockToolData, ToolConfig } from '@editorjs/editorjs'; | ||
import { CoreConfigValidated } from '../entities/index.js'; | ||
|
||
/** | ||
* Blocks API | ||
* - provides methods to work with blocks | ||
*/ | ||
@Service() | ||
export class BlocksAPI { | ||
/** | ||
* BlocksManager instance to work with blocks | ||
*/ | ||
#blocksManager: BlocksManager; | ||
|
||
/** | ||
* EditorJS configuration | ||
*/ | ||
#config: CoreConfigValidated; | ||
|
||
/** | ||
* BlocksAPI class constructor | ||
* @param blocksManager - BlocksManager instance to work with blocks | ||
* @param config - EditorJS configuration | ||
*/ | ||
constructor( | ||
blocksManager: BlocksManager, | ||
@Inject('EditorConfig') config: CoreConfigValidated | ||
) { | ||
this.#blocksManager = blocksManager; | ||
this.#config = config; | ||
} | ||
|
||
/** | ||
* Inserts a new block to the editor | ||
* @param type - Block tool name to insert | ||
* @param data - Block's initial data | ||
* @param _config - not used but left for compatibility | ||
* @param index - index to insert block at | ||
* @param needToFocus - flag indicates if new block should be focused @todo implement | ||
* @param replace - flag indicates if block at index should be replaced @todo implement | ||
* @param id - id of the inserted block @todo implement | ||
*/ | ||
public insert( | ||
type: string = this.#config.defaultBlock, | ||
data: BlockToolData = {}, | ||
/** | ||
* Not used but left for compatibility | ||
*/ | ||
_config: ToolConfig = {}, | ||
index?: number, | ||
needToFocus?: boolean, | ||
replace?: boolean, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
id?: string | ||
): void { | ||
this.#blocksManager.insert({ | ||
type, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
data, | ||
index, | ||
replace, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'reflect-metadata'; | ||
import { Service } from 'typedi'; | ||
import ToolsManager from '../tools/ToolsManager.js'; | ||
import { ToolboxUI } from '../ui/Toolbox/index.js'; | ||
|
||
/** | ||
* Class responsible for Toolbox business logic | ||
* - calls ToolboxUI to render the tools buttons in the toolbox | ||
*/ | ||
@Service() | ||
export class Toolbox { | ||
/** | ||
* Toolbox class constructor, all parameters are injected through the IoC container | ||
* @param toolsManager - ToolsManager instance to get block tools | ||
* @param toolboxUI - ToolboxUI instance to render tools buttons in the toolbox | ||
*/ | ||
constructor( | ||
toolsManager: ToolsManager, | ||
toolboxUI: ToolboxUI | ||
) { | ||
const blockTools = toolsManager.blockTools; | ||
|
||
blockTools.forEach((blockTool) => { | ||
toolboxUI.addTool(blockTool); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'reflect-metadata'; | ||
import { Service } from 'typedi'; | ||
import { BlockToolFacade } from '../../tools/facades/index.js'; | ||
import { make } from '@editorjs/dom'; | ||
import { BlocksAPI } from '../../api/BlocksAPI.js'; | ||
|
||
/** | ||
* UI module responsible for rendering the toolbox | ||
* - renders tool buttons in the toolbox | ||
* - listens to the click event on the tool buttons to insert blocks | ||
*/ | ||
@Service() | ||
export class ToolboxUI { | ||
/** | ||
* BlocksAPI instance to insert blocks | ||
* @todo replace with the full Editor API | ||
*/ | ||
#blocksAPI: BlocksAPI; | ||
|
||
/** | ||
* Object with Toolbox HTML nodes | ||
*/ | ||
#nodes: Record<string, HTMLElement> = {}; | ||
|
||
/** | ||
* ToolboxUI class constructor | ||
* @param blocksAPI - BlocksAPI instance to insert blocks | ||
*/ | ||
constructor(blocksAPI: BlocksAPI) { | ||
this.#blocksAPI = blocksAPI; | ||
} | ||
|
||
/** | ||
* Renders Toolbox UI | ||
* @returns Toolbox HTML element | ||
*/ | ||
public render(): HTMLElement { | ||
this.#nodes.holder = make('div'); | ||
|
||
this.#nodes.holder.style.display = 'flex'; | ||
|
||
return this.#nodes.holder; | ||
} | ||
|
||
/** | ||
* Renders tool button in the toolbox | ||
* @param tool - Block tool to add to the toolbox | ||
*/ | ||
public addTool(tool: BlockToolFacade): void { | ||
const toolButton = make('button'); | ||
|
||
toolButton.textContent = tool.name; | ||
|
||
toolButton.addEventListener('click', () => { | ||
void this.#blocksAPI.insert(tool.name); | ||
}); | ||
|
||
this.#nodes.holder.appendChild(toolButton); | ||
} | ||
} |
Oops, something went wrong.