From e612cba1a63b05601fa621a66bdc1c23137e2f26 Mon Sep 17 00:00:00 2001 From: Shravan Kumar Karnati Date: Fri, 22 Dec 2023 16:00:25 -0800 Subject: [PATCH 1/3] update --- src/components/modules/api/index.ts | 1 + src/components/modules/api/tools.ts | 37 ++++++++++++++++++++++++++ src/components/modules/index.ts | 2 ++ src/components/tools/base.ts | 13 ++++++++- src/types-internal/editor-modules.d.ts | 2 ++ types/api/index.d.ts | 1 + types/api/tools.d.ts | 14 ++++++++++ types/index.d.ts | 3 +++ 8 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/components/modules/api/tools.ts create mode 100644 types/api/tools.d.ts diff --git a/src/components/modules/api/index.ts b/src/components/modules/api/index.ts index 0f3562230..16d98cfb0 100644 --- a/src/components/modules/api/index.ts +++ b/src/components/modules/api/index.ts @@ -28,6 +28,7 @@ export default class API extends Module { selection: this.Editor.SelectionAPI.methods, styles: this.Editor.StylesAPI.classes, toolbar: this.Editor.ToolbarAPI.methods, + tools: this.Editor.ToolsAPI.methods, inlineToolbar: this.Editor.InlineToolbarAPI.methods, tooltip: this.Editor.TooltipAPI.methods, i18n: this.Editor.I18nAPI.methods, diff --git a/src/components/modules/api/tools.ts b/src/components/modules/api/tools.ts new file mode 100644 index 000000000..560d03196 --- /dev/null +++ b/src/components/modules/api/tools.ts @@ -0,0 +1,37 @@ +import { ToolConfig } from '../../../../types'; +import { Tools } from '../../../../types/api'; +import * as _ from '../../utils'; +import Module from '../../__module'; + +/** + * @class ToolsAPI + * Provides methods for working with the Tools + */ +export default class ToolsAPI extends Module { + /** + * Available methods + * + * @returns {Tools} + */ + public get methods(): Tools { + return { + updateToolConfig:(toolName:string, config: ToolConfig) => this.updateToolConfig(toolName, config), + }; + } + + /** + * Update Tool's config + * + * @param toolName Name of the tool + * @param config Tools Config + */ + public updateToolConfig(toolName:string, config: ToolConfig): void { + const tool = this.Editor.Tools.available.get(toolName); + + if (tool) { + tool.updateConfig(config); + } else { + _.log(`Incorrect toolName: ${toolName}`); + } + } +} \ No newline at end of file diff --git a/src/components/modules/index.ts b/src/components/modules/index.ts index 17dd56a45..610e83a07 100644 --- a/src/components/modules/index.ts +++ b/src/components/modules/index.ts @@ -14,6 +14,7 @@ import SelectionAPI from './api/selection'; import StylesAPI from './api/styles'; import ToolbarAPI from './api/toolbar'; import TooltipAPI from './api/tooltip'; +import ToolsAPI from './api/tools'; import UiAPI from './api/ui'; /** ./toolbar */ @@ -55,6 +56,7 @@ export default { StylesAPI, ToolbarAPI, TooltipAPI, + ToolsAPI, UiAPI, // Toolbar Modules diff --git a/src/components/tools/base.ts b/src/components/tools/base.ts index f89345a91..ac2f07853 100644 --- a/src/components/tools/base.ts +++ b/src/components/tools/base.ts @@ -1,4 +1,4 @@ -import { Tool, ToolConstructable, ToolSettings } from '../../../types/tools'; +import { Tool, ToolConfig, ToolConstructable, ToolSettings } from '../../../types/tools'; import { SanitizerConfig } from '../../../types'; import * as _ from '../utils'; import type InlineTool from './inline'; @@ -216,6 +216,17 @@ export default abstract class BaseTool { } } + /** + * Update tool's current config + * + * @param {ToolConfig} config - Tool's config + */ + public updateConfig(config: ToolConfig):void { + if (this.config) { + this.config[UserSettings.Config] = config; + } + } + /** * Calls Tool's prepare method */ diff --git a/src/types-internal/editor-modules.d.ts b/src/types-internal/editor-modules.d.ts index 1211247a5..a44fd95b3 100644 --- a/src/types-internal/editor-modules.d.ts +++ b/src/types-internal/editor-modules.d.ts @@ -37,6 +37,7 @@ import Renderer from '../components/modules/renderer'; import Saver from '../components/modules/saver'; import Tools from '../components/modules/tools'; import UI from '../components/modules/ui'; +import ToolsAPI from '../components/modules/api/tools'; export interface EditorModules { // API Modules @@ -55,6 +56,7 @@ export interface EditorModules { StylesAPI: StylesAPI, ToolbarAPI: ToolbarAPI, TooltipAPI: TooltipAPI, + ToolsAPI: ToolsAPI; UiAPI: UiAPI, // Toolbar Modules diff --git a/types/api/index.d.ts b/types/api/index.d.ts index 9df8461b7..7f373361a 100644 --- a/types/api/index.d.ts +++ b/types/api/index.d.ts @@ -14,3 +14,4 @@ export * from './block'; export * from './readonly'; export * from './i18n'; export * from './ui'; +export * from './tools'; \ No newline at end of file diff --git a/types/api/tools.d.ts b/types/api/tools.d.ts new file mode 100644 index 000000000..168b2b2c7 --- /dev/null +++ b/types/api/tools.d.ts @@ -0,0 +1,14 @@ +import { ToolConfig } from '../tools'; + +/** + * Describes Tools API methods + */ +export interface Tools { + /** + * Updates tool's config + * + * @param toolName Name of the tool + * @param type inline or block tools + */ + updateToolConfig(toolName:string, config: ToolConfig): void +} diff --git a/types/index.d.ts b/types/index.d.ts index c26aa2232..2e7d7377e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -26,6 +26,7 @@ import { Styles, Toolbar, Tooltip, + Tools, I18n, Ui, } from './api'; @@ -115,6 +116,7 @@ export interface API { toolbar: Toolbar; inlineToolbar: InlineToolbar; tooltip: Tooltip; + tools: Tools; i18n: I18n; readOnly: ReadOnly; ui: Ui; @@ -135,6 +137,7 @@ declare class EditorJS { public selection: Selection; public styles: Styles; public toolbar: Toolbar; + public tools: Tools; public inlineToolbar: InlineToolbar; public readOnly: ReadOnly; constructor(configuration?: EditorConfig|string); From 545247d56122b62a6950a7e61732fdae0d78e99e Mon Sep 17 00:00:00 2001 From: Shravan Kumar Karnati Date: Tue, 2 Jan 2024 08:54:05 -0800 Subject: [PATCH 2/3] formatting --- src/components/modules/api/tools.ts | 4 ++-- src/components/tools/base.ts | 2 +- types/api/tools.d.ts | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/components/modules/api/tools.ts b/src/components/modules/api/tools.ts index 560d03196..dfebfd5f6 100644 --- a/src/components/modules/api/tools.ts +++ b/src/components/modules/api/tools.ts @@ -15,7 +15,7 @@ export default class ToolsAPI extends Module { */ public get methods(): Tools { return { - updateToolConfig:(toolName:string, config: ToolConfig) => this.updateToolConfig(toolName, config), + updateToolConfig: (toolName: string, config: ToolConfig) => this.updateToolConfig(toolName, config), }; } @@ -25,7 +25,7 @@ export default class ToolsAPI extends Module { * @param toolName Name of the tool * @param config Tools Config */ - public updateToolConfig(toolName:string, config: ToolConfig): void { + public updateToolConfig(toolName: string, config: ToolConfig): void { const tool = this.Editor.Tools.available.get(toolName); if (tool) { diff --git a/src/components/tools/base.ts b/src/components/tools/base.ts index ac2f07853..787bec8c1 100644 --- a/src/components/tools/base.ts +++ b/src/components/tools/base.ts @@ -221,7 +221,7 @@ export default abstract class BaseTool { * * @param {ToolConfig} config - Tool's config */ - public updateConfig(config: ToolConfig):void { + public updateConfig(config: ToolConfig): void { if (this.config) { this.config[UserSettings.Config] = config; } diff --git a/types/api/tools.d.ts b/types/api/tools.d.ts index 168b2b2c7..9de97b2d9 100644 --- a/types/api/tools.d.ts +++ b/types/api/tools.d.ts @@ -4,11 +4,11 @@ import { ToolConfig } from '../tools'; * Describes Tools API methods */ export interface Tools { - /** - * Updates tool's config - * - * @param toolName Name of the tool - * @param type inline or block tools - */ - updateToolConfig(toolName:string, config: ToolConfig): void + /** + * Updates tool's config + * + * @param toolName name of the tool + * @param config config of the tool + */ + updateToolConfig(toolName: string, config: ToolConfig): void } From 2e4c20f3211c28b63e40106b4243529270be1383 Mon Sep 17 00:00:00 2001 From: Shravan Kumar Karnati Date: Tue, 2 Jan 2024 09:07:22 -0800 Subject: [PATCH 3/3] changelog --- docs/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 644369ce0..72ee2694b 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,6 +6,7 @@ - `New` — Toolbox now will be opened by '/' in empty Block instead of Tab - `New` — Block Tunes now will be opened by 'CMD+/' instead of Tab in non-empty block - `New` — Tab now will navigate through Blocks. In last block Tab will navigate to the next input on page. +- `New` - Adds `editor.tools` API, which can be used to update the tools' config without having to destroy & re-initialize the editor. - `Fix` — Passing an empty array via initial data or `blocks.render()` won't break the editor - `Fix` — Layout did not shrink when a large document cleared in Chrome - `Fix` — Multiple Tooltip elements creation fixed