mirror of
https://github.com/codex-team/editor.js
synced 2024-06-27 01:40:22 +02:00
2d89105670
* Add internal wrappers for tools classes * FIx lint * Change tools collections to map * Apply some more refactoring * Make tool instance private field * Add some docs * Fix eslint * Basic implementation for Block Tunes * Small fix for demo * Review changes * Fix * Add common tunes and ToolsCollection class * Fixes after review * Rename tools collections * Readonly fix * Some fixes after review * Apply suggestions from code review Co-authored-by: Peter Savchenko <specc.dev@gmail.com> * Fixes after review * Add docs and changelog * Update docs/block-tunes.md Co-authored-by: Peter Savchenko <specc.dev@gmail.com> * Apply suggestions from code review Co-authored-by: Peter Savchenko <specc.dev@gmail.com> * Update src/components/block/index.ts Co-authored-by: Murod Khaydarov <murod.haydarov@gmail.com> * [Dev] Tools utils tests (#1602) * Add tests for tools utils and coverage report * Fix eslint * Adjust test * Add more tests * Update after code review * Fix test & bump version Co-authored-by: Peter Savchenko <specc.dev@gmail.com> Co-authored-by: Murod Khaydarov <murod.haydarov@gmail.com>
84 lines
2.1 KiB
TypeScript
84 lines
2.1 KiB
TypeScript
import { ToolConstructable, ToolSettings } from '../../../types/tools';
|
|
import { InternalInlineToolSettings, InternalTuneSettings } from './base';
|
|
import InlineTool from './inline';
|
|
import BlockTune from './tune';
|
|
import BlockTool from './block';
|
|
import API from '../modules/api';
|
|
import { EditorConfig } from '../../../types/configs';
|
|
|
|
type ToolConstructor = typeof InlineTool | typeof BlockTool | typeof BlockTune;
|
|
|
|
/**
|
|
* Factory to construct classes to work with tools
|
|
*/
|
|
export default class ToolsFactory {
|
|
/**
|
|
* Tools configuration specified by user
|
|
*/
|
|
private config: {[name: string]: ToolSettings & { isInternal?: boolean }};
|
|
|
|
/**
|
|
* EditorJS API Module
|
|
*/
|
|
private api: API;
|
|
|
|
/**
|
|
* EditorJS configuration
|
|
*/
|
|
private editorConfig: EditorConfig;
|
|
|
|
/**
|
|
* @class
|
|
*
|
|
* @param config - tools config
|
|
* @param editorConfig - EditorJS config
|
|
* @param api - EditorJS API module
|
|
*/
|
|
constructor(
|
|
config: {[name: string]: ToolSettings & { isInternal?: boolean }},
|
|
editorConfig: EditorConfig,
|
|
api: API
|
|
) {
|
|
this.api = api;
|
|
this.config = config;
|
|
this.editorConfig = editorConfig;
|
|
}
|
|
|
|
/**
|
|
* Returns Tool object based on it's type
|
|
*
|
|
* @param name - tool name
|
|
*/
|
|
public get(name: string): InlineTool | BlockTool | BlockTune {
|
|
const { class: constructable, isInternal = false, ...config } = this.config[name];
|
|
|
|
const Constructor = this.getConstructor(constructable);
|
|
|
|
return new Constructor({
|
|
name,
|
|
constructable,
|
|
config,
|
|
api: this.api,
|
|
isDefault: name === this.editorConfig.defaultBlock,
|
|
defaultPlaceholder: this.editorConfig.placeholder,
|
|
isInternal,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Find appropriate Tool object constructor for Tool constructable
|
|
*
|
|
* @param constructable - Tools constructable
|
|
*/
|
|
private getConstructor(constructable: ToolConstructable): ToolConstructor {
|
|
switch (true) {
|
|
case constructable[InternalInlineToolSettings.IsInline]:
|
|
return InlineTool;
|
|
case constructable[InternalTuneSettings.IsTune]:
|
|
return BlockTune;
|
|
default:
|
|
return BlockTool;
|
|
}
|
|
}
|
|
}
|