mirror of
https://github.com/codex-team/editor.js
synced 2024-06-03 06:22:39 +02:00
rename toolbox types, simplify hasTools method
This commit is contained in:
parent
6b60cb110e
commit
359aead3e9
|
@ -5,7 +5,7 @@ import {
|
|||
BlockTune as IBlockTune,
|
||||
SanitizerConfig,
|
||||
ToolConfig,
|
||||
ToolboxConfig
|
||||
ToolboxConfigEntry
|
||||
} from '../../../types';
|
||||
|
||||
import { SavedData } from '../../../types/data-formats';
|
||||
|
@ -739,14 +739,14 @@ export default class Block extends EventsDispatcher<BlockEvents> {
|
|||
* Tool could specify several entries to be displayed at the Toolbox (for example, "Heading 1", "Heading 2", "Heading 3")
|
||||
* This method returns the entry that is related to the Block (depended on the Block data)
|
||||
*/
|
||||
public async getActiveToolboxEntry(): Promise<ToolboxConfig | undefined> {
|
||||
public async getActiveToolboxEntry(): Promise<ToolboxConfigEntry | undefined> {
|
||||
const toolboxSettings = this.tool.toolbox;
|
||||
|
||||
/**
|
||||
* If Tool specifies just the single entry, treat it like an active
|
||||
*/
|
||||
if (Array.isArray(toolboxSettings) === false) {
|
||||
return Promise.resolve(this.tool.toolbox as ToolboxConfig);
|
||||
return Promise.resolve(this.tool.toolbox as ToolboxConfigEntry);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -767,7 +767,7 @@ export default class Block extends EventsDispatcher<BlockEvents> {
|
|||
* that means that for the current block, the second toolbox item (matched by "{level: 2}") is active
|
||||
*/
|
||||
const blockData = await this.data;
|
||||
const toolboxItems = toolboxSettings as ToolboxConfig[];
|
||||
const toolboxItems = toolboxSettings as ToolboxConfigEntry[];
|
||||
|
||||
return toolboxItems.find((item) => {
|
||||
return Object.entries(item.data)
|
||||
|
|
|
@ -11,7 +11,7 @@ import Module from '../__module';
|
|||
import $ from '../dom';
|
||||
import * as _ from '../utils';
|
||||
import Blocks from '../blocks';
|
||||
import { BlockToolData, PasteEvent, ToolConfig } from '../../../types';
|
||||
import { BlockToolData, PasteEvent } from '../../../types';
|
||||
import { BlockTuneData } from '../../../types/block-tunes/block-tune-data';
|
||||
import BlockAPI from '../block/api';
|
||||
import { BlockMutationType } from '../../../types/events/block/mutation-type';
|
||||
|
@ -237,10 +237,9 @@ export default class BlockManager extends Module {
|
|||
data = {},
|
||||
id = undefined,
|
||||
tunes: tunesData = {},
|
||||
}: {tool: string; id?: string; data?: BlockToolData; tunes?: {[name: string]: BlockTuneData}; config?: ToolConfig}): Block {
|
||||
}: {tool: string; id?: string; data?: BlockToolData; tunes?: {[name: string]: BlockTuneData}}): Block {
|
||||
const readOnly = this.Editor.ReadOnly.isEnabled;
|
||||
const tool = this.Editor.Tools.blockTools.get(name);
|
||||
|
||||
const block = new Block({
|
||||
id,
|
||||
data,
|
||||
|
|
|
@ -6,7 +6,7 @@ import Flipper from '../../flipper';
|
|||
import I18n from '../../i18n';
|
||||
import { I18nInternalNS } from '../../i18n/namespace-internal';
|
||||
import { clean } from '../../utils/sanitizer';
|
||||
import { ToolboxConfig, BlockToolData } from '../../../../types';
|
||||
import { ToolboxConfigEntry, BlockToolData } from '../../../../types';
|
||||
|
||||
/**
|
||||
* HTML Elements used for ConversionToolbar
|
||||
|
@ -50,7 +50,7 @@ export default class ConversionToolbar extends Module<ConversionToolbarNodes> {
|
|||
/**
|
||||
* Available tools data
|
||||
*/
|
||||
private tools: {name: string; toolboxItem: ToolboxConfig; button: HTMLElement}[] = []
|
||||
private tools: {name: string; toolboxItem: ToolboxConfigEntry; button: HTMLElement}[] = []
|
||||
|
||||
/**
|
||||
* Instance of class that responses for leafing buttons by arrows/tab
|
||||
|
@ -167,7 +167,11 @@ export default class ConversionToolbar extends Module<ConversionToolbarNodes> {
|
|||
* Returns true if it has more than one tool available for convert in
|
||||
*/
|
||||
public hasTools(): boolean {
|
||||
return !(this.tools.length === 1 && this.tools[0].name === this.config.defaultBlock);
|
||||
if (this.tools.length === 1) {
|
||||
return this.tools[0].name !== this.config.defaultBlock;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -299,7 +303,7 @@ export default class ConversionToolbar extends Module<ConversionToolbarNodes> {
|
|||
* @param name - tool's name
|
||||
* @param toolboxSettings - tool's single toolbox setting
|
||||
*/
|
||||
private addToolIfValid(name: string, toolboxSettings: ToolboxConfig): void {
|
||||
private addToolIfValid(name: string, toolboxSettings: ToolboxConfigEntry): void {
|
||||
/**
|
||||
* Skip tools that don't pass 'toolbox' property
|
||||
*/
|
||||
|
@ -316,7 +320,7 @@ export default class ConversionToolbar extends Module<ConversionToolbarNodes> {
|
|||
* @param toolName - name of Tool to add
|
||||
* @param toolboxItem - tool's toolbox item data
|
||||
*/
|
||||
private addTool(toolName: string, toolboxItem: ToolboxConfig): void {
|
||||
private addTool(toolName: string, toolboxItem: ToolboxConfigEntry): void {
|
||||
const tool = $.make('div', [ ConversionToolbar.CSS.conversionTool ]);
|
||||
const icon = $.make('div', [ ConversionToolbar.CSS.conversionToolIcon ]);
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@ import {
|
|||
BlockToolConstructable,
|
||||
BlockToolData,
|
||||
ConversionConfig,
|
||||
PasteConfig, SanitizerConfig,
|
||||
ToolboxConfig
|
||||
PasteConfig, SanitizerConfig, ToolboxConfig,
|
||||
ToolboxConfigEntry
|
||||
} from '../../../types';
|
||||
import * as _ from '../utils';
|
||||
import InlineTool from './inline';
|
||||
|
@ -72,7 +72,7 @@ export default class BlockTool extends BaseTool<IBlockTool> {
|
|||
/**
|
||||
* Returns Tool toolbox configuration (internal or user-specified)
|
||||
*/
|
||||
public get toolbox(): ToolboxConfig | ToolboxConfig[] {
|
||||
public get toolbox(): ToolboxConfig {
|
||||
const toolToolboxSettings = this.constructable[InternalBlockToolSettings.Toolbox] as ToolboxConfig;
|
||||
|
||||
if (Array.isArray(toolToolboxSettings)) {
|
||||
|
@ -167,7 +167,7 @@ export default class BlockTool extends BaseTool<IBlockTool> {
|
|||
*
|
||||
* @param toolboxItemSettings - toolbox item settings to merge
|
||||
*/
|
||||
private getActualToolboxSettings(toolboxItemSettings: ToolboxConfig): ToolboxConfig {
|
||||
private getActualToolboxSettings(toolboxItemSettings: ToolboxConfigEntry): ToolboxConfigEntry {
|
||||
const userToolboxSettings = this.config[UserSettings.Toolbox];
|
||||
|
||||
if (_.isEmpty(toolboxItemSettings)) {
|
||||
|
|
|
@ -3,7 +3,7 @@ import { BlockToolAPI } from '../block';
|
|||
import Shortcuts from '../utils/shortcuts';
|
||||
import BlockTool from '../tools/block';
|
||||
import ToolsCollection from '../tools/collection';
|
||||
import { API, BlockToolData, ToolboxConfig } from '../../../types';
|
||||
import { API, BlockToolData, ToolboxConfigEntry } from '../../../types';
|
||||
import EventsDispatcher from '../utils/events';
|
||||
import Popover, { PopoverEvent, PopoverItem } from '../utils/popover';
|
||||
import I18n from '../i18n';
|
||||
|
@ -283,7 +283,7 @@ export default class Toolbox extends EventsDispatcher<ToolboxEvent> {
|
|||
/**
|
||||
* Maps tool data to popover item structure
|
||||
*/
|
||||
const toPopoverItem = (toolboxItem: ToolboxConfig, tool: BlockTool): PopoverItem => {
|
||||
const toPopoverItem = (toolboxItem: ToolboxConfigEntry, tool: BlockTool): PopoverItem => {
|
||||
return {
|
||||
icon: toolboxItem.icon,
|
||||
label: I18n.t(I18nInternalNS.toolNames, toolboxItem.title || _.capitalize(tool.name)),
|
||||
|
@ -315,7 +315,7 @@ export default class Toolbox extends EventsDispatcher<ToolboxEvent> {
|
|||
* @param toolToolboxSettings - item to validate
|
||||
* @param toolName - name of the tool used in console warning if item is not valid
|
||||
*/
|
||||
private areToolboxSettingsValid(toolToolboxSettings: ToolboxConfig, toolName: string): boolean {
|
||||
private areToolboxSettingsValid(toolToolboxSettings: ToolboxConfigEntry, toolName: string): boolean {
|
||||
/**
|
||||
* Skip tools that don't pass 'toolbox' property
|
||||
*/
|
||||
|
|
1
types/index.d.ts
vendored
1
types/index.d.ts
vendored
|
@ -48,6 +48,7 @@ export {
|
|||
Tool,
|
||||
ToolConstructable,
|
||||
ToolboxConfig,
|
||||
ToolboxConfigEntry,
|
||||
ToolSettings,
|
||||
ToolConfig,
|
||||
PasteEvent,
|
||||
|
|
24
types/tools/block-tool.d.ts
vendored
24
types/tools/block-tool.d.ts
vendored
|
@ -1,11 +1,10 @@
|
|||
import { ConversionConfig, PasteConfig, SanitizerConfig } from '../configs';
|
||||
import { BlockToolData } from './block-tool-data';
|
||||
import {BaseTool, BaseToolConstructable} from './tool';
|
||||
import { BaseTool, BaseToolConstructable } from './tool';
|
||||
import { ToolConfig } from './tool-config';
|
||||
import {API, BlockAPI} from '../index';
|
||||
import { API, BlockAPI, ToolboxConfig } from '../index';
|
||||
import { PasteEvent } from './paste-events';
|
||||
import { MoveEvent } from './hook-events';
|
||||
import { ToolboxConfig } from './tool-settings';
|
||||
|
||||
/**
|
||||
* Describe Block Tool object
|
||||
|
@ -17,13 +16,6 @@ export interface BlockTool extends BaseTool {
|
|||
*/
|
||||
sanitize?: SanitizerConfig;
|
||||
|
||||
|
||||
/**
|
||||
* Current active toolbox entry
|
||||
*/
|
||||
activeToolboxEntry?: ToolboxConfig
|
||||
|
||||
|
||||
/**
|
||||
* Process Tool's element in DOM and return raw data
|
||||
* @param {HTMLElement} block - element created by {@link BlockTool#render} function
|
||||
|
@ -103,17 +95,7 @@ export interface BlockToolConstructable extends BaseToolConstructable {
|
|||
/**
|
||||
* Tool's Toolbox settings
|
||||
*/
|
||||
toolbox?: {
|
||||
/**
|
||||
* HTML string with an icon for Toolbox
|
||||
*/
|
||||
icon: string;
|
||||
|
||||
/**
|
||||
* Tool title for Toolbox
|
||||
*/
|
||||
title?: string;
|
||||
};
|
||||
toolbox?: ToolboxConfig;
|
||||
|
||||
/**
|
||||
* Paste substitutions configuration
|
||||
|
|
12
types/tools/tool-settings.d.ts
vendored
12
types/tools/tool-settings.d.ts
vendored
|
@ -1,10 +1,16 @@
|
|||
import {ToolConfig} from './tool-config';
|
||||
import {ToolConstructable, BlockToolData} from './index';
|
||||
import { ToolConfig } from './tool-config';
|
||||
import { ToolConstructable, BlockToolData } from './index';
|
||||
|
||||
/**
|
||||
* Tool may specify its toolbox configuration
|
||||
* It may include several entries as well
|
||||
*/
|
||||
export type ToolboxConfig = ToolboxConfigEntry | ToolboxConfigEntry[];
|
||||
|
||||
/**
|
||||
* Tool's Toolbox settings
|
||||
*/
|
||||
export interface ToolboxConfig {
|
||||
export interface ToolboxConfigEntry {
|
||||
/**
|
||||
* Tool title for Toolbox
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue