Skip to content

Commit

Permalink
tools are initialized inside of the inline toolbar initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
e11sy committed Aug 29, 2024
1 parent e39d5bc commit 7f66686
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 51 deletions.
21 changes: 7 additions & 14 deletions packages/core/src/entities/InlineTool.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { TextRange, InlineFragment, FormattingAction, IntersectType } from '@editorjs/model';
import type { InlineTool as InlineToolVersion2 } from '@editorjs/editorjs';
import type { TextRange, InlineFragment, FormattingAction, IntersectType, InlineToolName } from '@editorjs/model';
import type { InlineToolConstructorOptions as InlineToolConstructorOptionsVersion2 } from '@editorjs/editorjs';

/**
* Extended InlineToolConstructorOptions interface for version 3.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface InlineToolConstructorOptions extends InlineToolVersion2 {}
export interface InlineToolConstructorOptions extends InlineToolConstructorOptionsVersion2 {}

/**
* Object represents formatting action with text range to be applied on
Expand Down Expand Up @@ -56,19 +57,11 @@ export interface InlineTool extends Omit<InlineToolVersion2, 'save' | 'checkStat
/**
* Interface, that represents inline tool with configured name
*/
export interface InlineToolWithName {
/**
* Name of the inline tool got from editor config
*/
name: InlineToolName;

/**
* Inline tool instanse
*/
tool: InlineTool;
}
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface InlineToolConfig extends Record<string, InlineToolConstructor> {};

/**
* @todo support options: InlineToolConstructableOptions
* Inline Tool constructor class
*/
export type InlineToolConstructor = new (options: InlineToolConstructorOptions) => InlineTool;
export type InlineToolConstructor = new () => InlineTool;
16 changes: 6 additions & 10 deletions packages/core/src/tools/ToolsManager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { InlineToolWithName } from '../entities/InlineTool.js';
import type { InlineToolConfig } from '../entities/InlineTool.js';
import type { BlockToolConstructor } from '../entities/BlockTool.js';
import { Paragraph } from './internal/block-tools/paragraph/index.js';
import type { EditorConfig } from '@editorjs/editorjs';
import BoldInlineTool from './internal/inline-tools/bold/index.js';
import ItalicInlineTool from './internal/inline-tools/italic/index.js';
import { createInlineToolName } from '@editorjs/model';

/**
* Works with tools
Expand Down Expand Up @@ -35,13 +34,10 @@ export default class ToolsManager {
/**
* Returns inline tools got from the EditorConfig tools
*/
public getInlineTools(): InlineToolWithName[] {
return [{
name: createInlineToolName('Bold'),
tool: new BoldInlineTool(),
}, {
name: createInlineToolName('Italic'),
tool: new ItalicInlineTool(),
}];
public getInlineTools(): InlineToolConfig {
return {
bold: BoldInlineTool,
italic: ItalicInlineTool,
};
};
}
8 changes: 1 addition & 7 deletions packages/core/src/tools/internal/inline-tools/bold/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FormattingActionWithRange, InlineTool } from '../../../../entities/InlineTool.js';
import type { InlineFragment, TextRange } from '@editorjs/model';
import { FormattingAction } from '@editorjs/model';
import { createInlineToolName, IntersectType, type InlineToolName } from '@editorjs/model';
import { IntersectType } from '@editorjs/model';
import { make } from '@editorjs/dom';

/**
Expand All @@ -18,12 +18,6 @@ export default class BoldInlineTool implements InlineTool {
*/
public static isInline = true;

/**
* @todo - get rid of tool name, since name could be user-spesific
* It is a temporary solution, because tools inline tools are now formed not from config
*/
public name: InlineToolName = createInlineToolName('Bold');

/**
* Type of behaviour of the tool if new selection range intersect with existing fragment
* If two fragment intersect, they should be merged
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FormattingActionWithRange, InlineTool } from '../../../../entities/InlineTool.js';
import type { InlineFragment, TextRange } from '@editorjs/model';
import { FormattingAction } from '@editorjs/model';
import { createInlineToolName, IntersectType, type InlineToolName } from '@editorjs/model';
import { IntersectType } from '@editorjs/model';
import { make } from '@editorjs/dom';

/**
Expand All @@ -18,12 +18,6 @@ export default class ItalicInlineTool implements InlineTool {
*/
public static isInline = true;

/**
* @todo - get rid of tool name, since name could be user-spesific
* It is a temporary solution, because tools inline tools are now formed not from config
*/
public name: InlineToolName = createInlineToolName('Iatlic');

/**
* Type of behaviour of the tool if new selection range intersect with existing fragment
* If two fragment intersect, they should be merged
Expand Down
32 changes: 20 additions & 12 deletions packages/core/src/ui/InlineToolbar/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { InlineToolsAdapter } from '@editorjs/dom-adapters';
import type { InlineToolWithName } from '../../entities/InlineTool.js';
import { type EditorJSModel, type TextRange, Index } from '@editorjs/model';
import type { InlineTool, InlineToolConfig } from '../../entities/InlineTool.js';
import type { InlineToolName } from '@editorjs/model';
import { type EditorJSModel, type TextRange, createInlineToolName, Index } from '@editorjs/model';
import { EventType } from '@editorjs/model';
import type { Nominal } from '@editorjs/model/dist/utils/Nominal';
import { make } from '@editorjs/dom';
Expand Down Expand Up @@ -31,7 +32,7 @@ export class InlineToolbar {
/**
* Tools that would be attached to the adapter
*/
#tools: InlineToolWithName[];
#tools: Map<InlineToolName, InlineTool>;

/**
* Toolbar html element related to the editor
Expand All @@ -46,11 +47,18 @@ export class InlineToolbar {
* @param tools - tools, that should be attached to adapter
* @param holder - editor holder element
*/
constructor(model: EditorJSModel, inlineToolAdapter: InlineToolsAdapter, tools: InlineToolWithName[], holder: HTMLElement) {
constructor(model: EditorJSModel, inlineToolAdapter: InlineToolsAdapter, tools: InlineToolConfig, holder: HTMLElement) {
this.#model = model;
this.#inlineToolAdapter = inlineToolAdapter;
this.#tools = tools;
this.#holder = holder;
this.#tools = new Map<InlineToolName, InlineTool>();

Object.entries(tools).forEach(([toolName, toolConstructable]) => {
/**
* @todo - support inline tool options
*/
this.#tools.set(createInlineToolName(toolName), new toolConstructable());
});

this.#attachTools();

Expand Down Expand Up @@ -123,13 +131,13 @@ export class InlineToolbar {

this.#toolbar = make('div');

this.#tools.forEach((tool) => {
this.#tools.forEach((_tool, toolName) => {
const inlineElementButton = make('button');

inlineElementButton.innerHTML = tool.name;
inlineElementButton.innerHTML = toolName;

inlineElementButton.addEventListener('click', (_) => {
this.apply(tool);
inlineElementButton.addEventListener('click', (_event) => {
this.apply(toolName);
});
if (this.#toolbar !== undefined) {
this.#toolbar.appendChild(inlineElementButton);
Expand All @@ -148,12 +156,12 @@ export class InlineToolbar {

/**
* Apply format with data formed in toolbar
* @param tool - tool, that was triggered
* @param toolName - name of the inline tool, whose format would be applied
*/
public apply(tool: InlineToolWithName): void {
public apply(toolName: InlineToolName): void {
/**
* @todo pass to applyFormat inline tool data formed in toolbar
*/
this.#inlineToolAdapter.applyFormat(tool.name, {} as Nominal<Record<string, unknown>, 'InlineToolData'>);
this.#inlineToolAdapter.applyFormat(toolName, {} as Nominal<Record<string, unknown>, 'InlineToolData'>);
};
}
2 changes: 1 addition & 1 deletion packages/playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"baseUrl": ".",
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", "src/components/Toolbar/Toolbarvue"],
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [
{
"path": "./tsconfig.node.json"
Expand Down

0 comments on commit 7f66686

Please sign in to comment.