Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Block Tunes API #1596

Merged
merged 29 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
24dc069
Add internal wrappers for tools classes
gohabereg Mar 14, 2021
224dd3b
FIx lint
gohabereg Mar 14, 2021
83e17bb
Change tools collections to map
gohabereg Mar 15, 2021
1ffcbed
Apply some more refactoring
gohabereg Mar 15, 2021
3d743f3
Make tool instance private field
gohabereg Mar 15, 2021
e56579c
Add some docs
gohabereg Mar 15, 2021
0069a2a
Fix eslint
gohabereg Mar 15, 2021
d407e32
Basic implementation for Block Tunes
gohabereg Mar 15, 2021
a0160ef
Small fix for demo
gohabereg Mar 15, 2021
dde3bd4
Review changes
gohabereg Mar 16, 2021
28bfacf
Merge branch 'next' of github.com:codex-team/editor.js into reafctori…
gohabereg Mar 16, 2021
ca1aef3
Fix
gohabereg Mar 16, 2021
a459ab5
Merge branch 'reafctoring/tools' of github.com:codex-team/editor.js i…
gohabereg Mar 16, 2021
6ecc3ac
Add common tunes and ToolsCollection class
gohabereg Mar 17, 2021
1f9c5f1
Fixes after review
gohabereg Mar 18, 2021
c81b9a0
Merge branch 'reafctoring/tools' of github.com:codex-team/editor.js i…
gohabereg Mar 18, 2021
b8ba38c
Rename tools collections
gohabereg Mar 18, 2021
aea3158
Readonly fix
gohabereg Mar 18, 2021
82f3bfa
Merge branch 'reafctoring/tools' of github.com:codex-team/editor.js i…
gohabereg Mar 18, 2021
a3bb039
Some fixes after review
gohabereg Mar 27, 2021
41274fb
Merge branch 'next' of github.com:codex-team/editor.js into feature/b…
gohabereg Mar 31, 2021
13b64eb
Apply suggestions from code review
gohabereg Apr 2, 2021
4e9a098
Fixes after review
gohabereg Apr 2, 2021
ebc8b3f
Add docs and changelog
gohabereg Apr 2, 2021
cad9691
Update docs/block-tunes.md
gohabereg Apr 2, 2021
66feab7
Apply suggestions from code review
gohabereg Apr 2, 2021
8ce77a8
Update src/components/block/index.ts
gohabereg Apr 4, 2021
94bfd10
[Dev] Tools utils tests (#1602)
gohabereg Apr 4, 2021
5fe699c
Fix test & bump version
gohabereg Apr 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixes after review
  • Loading branch information
gohabereg committed Mar 18, 2021
commit 1f9c5f1e37bebfde4d91a57bc72e83b2cc31890f
2 changes: 1 addition & 1 deletion src/components/modules/blockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export default class BlockManager extends Module {
*/
public composeBlock({ tool: name, data = {} }: {tool: string; data?: BlockToolData}): Block {
const readOnly = this.Editor.ReadOnly.isEnabled;
const tool = this.Editor.Tools.block.get(name);
const tool = this.Editor.Tools.blockTools.get(name);
const block = new Block({
name,
data,
Expand Down
12 changes: 2 additions & 10 deletions src/components/modules/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import $ from '../dom';
import * as _ from '../utils';
import {
BlockAPI,
PasteConfig,
PasteEvent,
PasteEventDetail
} from '../../../types';
Expand All @@ -18,7 +17,6 @@ interface TagSubstitute {
/**
* Name of related Tool
*
* @type {string}
*/
tool: BlockTool;
}
Expand All @@ -29,22 +27,16 @@ interface TagSubstitute {
interface PatternSubstitute {
/**
* Pattern`s key
*
* @type {string}
*/
key: string;

/**
* Pattern regexp
*
* @type {RegExp}
*/
pattern: RegExp;

/**
* Name of related Tool
*
* @type {string}
*/
tool: BlockTool;
}
Expand Down Expand Up @@ -277,7 +269,7 @@ export default class Paste extends Module {
* Get and process tool`s paste configs
*/
private processTools(): void {
const tools = this.Editor.Tools.block;
const tools = this.Editor.Tools.blockTools;

Array
.from(tools.values())
Expand Down Expand Up @@ -648,7 +640,7 @@ export default class Paste extends Module {
* @param {PasteData} dataToInsert - data of Block to insert
*/
private async processInlinePaste(dataToInsert: PasteData): Promise<void> {
const { BlockManager, Caret, Sanitizer, Tools } = this.Editor;
const { BlockManager, Caret, Sanitizer } = this.Editor;
const { content } = dataToInsert;

const currentBlockIsDefault = BlockManager.currentBlock && BlockManager.currentBlock.tool.isDefault;
Expand Down
4 changes: 2 additions & 2 deletions src/components/modules/readonly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export default class ReadOnly extends Module {
*/
public async prepare(): Promise<void> {
const { Tools } = this.Editor;
const { block } = Tools;
const { blockTools } = Tools;
const toolsDontSupportReadOnly: string[] = [];

Array
.from(block.entries())
.from(blockTools.entries())
.forEach(([name, tool]) => {
if (tool.isReadOnlySupported) {
toolsDontSupportReadOnly.push(name);
Expand Down
4 changes: 2 additions & 2 deletions src/components/modules/sanitizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export default class Sanitizer extends Module {
(enableInlineTools as string[]).map((inlineToolName) => {
config = Object.assign(
config,
Tools.inline.get(inlineToolName).sanitizeConfig
Tools.inlineTools.get(inlineToolName).sanitizeConfig
) as SanitizerConfig;
});
}
Expand All @@ -233,7 +233,7 @@ export default class Sanitizer extends Module {

const config: SanitizerConfig = {} as SanitizerConfig;

Object.entries(Tools.inline)
Object.entries(Tools.inlineTools)
.forEach(([, inlineTool]: [string, InlineTool]) => {
Object.assign(config, inlineTool.sanitizeConfig);
});
Expand Down
4 changes: 2 additions & 2 deletions src/components/modules/toolbar/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default class ConversionToolbar extends Module<ConversionToolbarNodes> {
*
* @type {BlockToolConstructable}
*/
const replacingTool = this.Editor.Tools.block.get(replacingToolName);
const replacingTool = this.Editor.Tools.blockTools.get(replacingToolName);

/**
* Export property can be:
Expand Down Expand Up @@ -269,7 +269,7 @@ export default class ConversionToolbar extends Module<ConversionToolbarNodes> {
* if tools have ability to import
*/
private addTools(): void {
const tools = this.Editor.Tools.block;
const tools = this.Editor.Tools.blockTools;

Array
.from(tools.entries())
Expand Down
8 changes: 4 additions & 4 deletions src/components/modules/toolbar/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
* If common settings is 'true' or not specified (will be set as true at core.ts), get the default order
*/
if (commonInlineToolbarSettings === true) {
return Array.from(this.Editor.Tools.inline.keys());
return Array.from(this.Editor.Tools.inlineTools.keys());
}

return false;
Expand Down Expand Up @@ -573,7 +573,7 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
const inlineToolbarOrder = this.getInlineToolbarSettings(currentBlock.tool) as string[];

inlineToolbarOrder.forEach((toolName) => {
const tool = this.Editor.Tools.inline.get(toolName);
const tool = this.Editor.Tools.inlineTools.get(toolName);

this.addTool(tool);
});
Expand Down Expand Up @@ -663,7 +663,7 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
* Enable shortcuts
* Ignore tool that doesn't have shortcut or empty string
*/
const tool = Tools.inline.get(toolName);
const tool = Tools.inlineTools.get(toolName);

/**
* 1) For internal tools, check public getter 'shortcut'
Expand Down Expand Up @@ -745,7 +745,7 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
const result = {};

Array
.from(this.Editor.Tools.inline.entries())
.from(this.Editor.Tools.inlineTools.entries())
.forEach(([name, tool]) => {
result[name] = tool.instance();
});
Expand Down
4 changes: 2 additions & 2 deletions src/components/modules/toolbar/toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export default class Toolbox extends Module<ToolboxNodes> {
* Iterates available tools and appends them to the Toolbox
*/
private addTools(): void {
const tools = this.Editor.Tools.block;
const tools = this.Editor.Tools.blockTools;

Array
.from(tools.values())
Expand Down Expand Up @@ -284,7 +284,7 @@ export default class Toolbox extends Module<ToolboxNodes> {
* Fired when the Read-Only mode is activated
*/
private removeAllShortcuts(): void {
const tools = this.Editor.Tools.block;
const tools = this.Editor.Tools.blockTools;

Array
.from(tools.values())
Expand Down
18 changes: 9 additions & 9 deletions src/components/modules/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class Tools extends Module {
*
* @returns {object} - object of Inline Tool's classes
*/
public get inline(): Map<string, InlineTool> {
public get inlineTools(): Map<string, InlineTool> {
if (this._inlineTools) {
return this._inlineTools;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ export default class Tools extends Module {
/**
* Return editor block tools
*/
public get block(): Map<string, BlockTool> {
public get blockTools(): Map<string, BlockTool> {
if (this._blockTools) {
return this._blockTools;
}
Expand All @@ -128,7 +128,7 @@ export default class Tools extends Module {
* Returns default Tool object
*/
public get defaultTool(): BlockTool {
return this.block.get(this.config.defaultBlock);
return this.blockTools.get(this.config.defaultBlock);
}

/**
Expand Down Expand Up @@ -215,9 +215,9 @@ export default class Tools extends Module {
* to see how it works {@link '../utils.ts#sequence'}
*/
return _.sequence(sequenceData, (data: { toolName: string }) => {
this.success(data);
this.toolPrepareMethodSuccess(data);
}, (data: { toolName: string }) => {
this.fallback(data);
this.toolPrepareMethodFallback(data);
});
}

Expand Down Expand Up @@ -263,20 +263,20 @@ export default class Tools extends Module {
}

/**
* Success callback
* Tool prepare method success callback
*
* @param {object} data - append tool to available list
*/
private success(data: { toolName: string }): void {
private toolPrepareMethodSuccess(data: { toolName: string }): void {
this.toolsAvailable.set(data.toolName, this.factory.get(data.toolName));
}

/**
* Fail callback
* Tool prepare method fail callback
*
* @param {object} data - append tool to unavailable list
*/
private fallback(data: { toolName: string }): void {
private toolPrepareMethodFallback(data: { toolName: string }): void {
this.toolsUnavailable.set(data.toolName, this.factory.get(data.toolName));
}

Expand Down
57 changes: 57 additions & 0 deletions src/components/tools/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,90 @@ import { Tool, ToolConstructable, ToolSettings } from '../../../types/tools';
import { API, SanitizerConfig } from '../../../types';
import * as _ from '../utils';

/**
* Enum of Tool options provided by user
*/
export enum UserSettings {
/**
* Shortcut for Tool
*/
Shortcut = 'shortcut',
/**
* Toolbox config for Tool
*/
Toolbox = 'toolbox',
/**
* Enabled Inline Tools for Block Tool
*/
EnabledInlineTools = 'inlineToolbar',
/**
* Tool configuration
*/
Config = 'config',
}

/**
* Enum of Tool options provided by Tool
*/
export enum CommonInternalSettings {
/**
* Shortcut for Tool
*/
Shortcut = 'shortcut',
/**
* Sanitize configuration for Tool
*/
SanitizeConfig = 'sanitize',

}

/**
* Enum of Tool optoins provided by Block Tool
*/
export enum InternalBlockToolSettings {
/**
* Is linebreaks enabled for Tool
*/
IsEnabledLineBreaks = 'enableLineBreaks',
/**
* Tool Toolbox config
*/
Toolbox = 'toolbox',
/**
* Tool conversion config
*/
ConversionConfig = 'conversionConfig',
/**
* Is readonly mode supported for Tool
*/
IsReadOnlySupported = 'isReadOnlySupported',
/**
* Tool paste config
*/
PasteConfig = 'pasteConfig'
}

/**
* Enum of Tool options provided by Inline Tool
*/
export enum InternalInlineToolSettings {
/**
* Flag specifies Tool is inline
*/
IsInline = 'isInline',
/**
* Inline Tool title for toolbar
*/
Title = 'title', // for Inline Tools. Block Tools can pass title along with icon through the 'toolbox' static prop.
}

/**
* Enum of Tool options provided by Block Tune
*/
export enum InternalTuneSettings {
/**
* Flag specifies Tool is Block Tune
*/
IsTune = 'isTune',
}

Expand Down