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

[Refactor] Separate internal and external settings #845

Merged
merged 2 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions dist/editor.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### 2.16

- `Refactoring` — Constants of tools settings separated by internal and external to correspond API

### 2.15

- `New` — New [`blocks.insert()`](api.md) API method [#715](https://github.com/codex-team/editor.js/issues/715).
Expand Down
4 changes: 2 additions & 2 deletions src/components/modules/blockEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export default class BlockEvents extends Module {
* Uses for Tools like <code> where line breaks should be handled by default behaviour.
*/
if (tool
&& tool[Tools.apiSettings.IS_ENABLED_LINE_BREAKS]
&& tool[Tools.INTERNAL_SETTINGS.IS_ENABLED_LINE_BREAKS]
&& !BlockSettings.opened
&& !InlineToolbar.opened
&& !ConversionToolbar.opened) {
Expand Down Expand Up @@ -434,7 +434,7 @@ export default class BlockEvents extends Module {
*
* But if caret is at start of the block, we allow to remove it by backspaces
*/
if (tool && tool[this.Editor.Tools.apiSettings.IS_ENABLED_LINE_BREAKS] && !Caret.isAtStart) {
if (tool && tool[this.Editor.Tools.INTERNAL_SETTINGS.IS_ENABLED_LINE_BREAKS] && !Caret.isAtStart) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/modules/sanitizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export default class Sanitizer extends Module {
return this.configCache[toolName];
}

const sanitizeGetter = this.Editor.Tools.apiSettings.SANITIZE_CONFIG;
const sanitizeGetter = this.Editor.Tools.INTERNAL_SETTINGS.SANITIZE_CONFIG;
const toolClass = this.Editor.Tools.available[toolName];
const baseConfig = this.getInlineToolsConfig(toolName);

Expand Down Expand Up @@ -199,7 +199,7 @@ export default class Sanitizer extends Module {
(enableInlineTools as string[]).map( (inlineToolName) => {
config = Object.assign(
config,
Tools.inline[inlineToolName][Tools.apiSettings.SANITIZE_CONFIG],
Tools.inline[inlineToolName][Tools.INTERNAL_SETTINGS.SANITIZE_CONFIG],
) as SanitizerConfig;
});
}
Expand All @@ -221,7 +221,7 @@ export default class Sanitizer extends Module {

Object.entries(Tools.inline)
.forEach( ([name, inlineTool]: [string, InlineToolConstructable]) => {
Object.assign(config, inlineTool[Tools.apiSettings.SANITIZE_CONFIG]);
Object.assign(config, inlineTool[Tools.INTERNAL_SETTINGS.SANITIZE_CONFIG]);
});

this.inlineToolsConfigCache = config;
Expand Down
2 changes: 0 additions & 2 deletions src/components/modules/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ export default class Shortcuts extends Module {
* @param {ShortcutData} shortcut
*/
public add(shortcut: ShortcutData): void {
const { UI } = this.Editor;

const newShortcut = new Shortcut({
name: shortcut.name,
on: document, // UI.nodes.redactor
Expand Down
6 changes: 3 additions & 3 deletions src/components/modules/toolbar/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ export default class ConversionToolbar extends Module {
continue;
}

const api = this.Editor.Tools.apiSettings;
const internalSettings = this.Editor.Tools.INTERNAL_SETTINGS;
const toolClass = tools[toolName] as BlockToolConstructable;
const toolToolboxSettings = toolClass[api.TOOLBOX];
const conversionConfig = toolClass[api.CONVERSION_CONFIG];
const toolToolboxSettings = toolClass[internalSettings.TOOLBOX];
const conversionConfig = toolClass[internalSettings.CONVERSION_CONFIG];

/**
* Skip tools that don't pass 'toolbox' property
Expand Down
16 changes: 8 additions & 8 deletions src/components/modules/toolbar/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export default class InlineToolbar extends Module {

const toolSettings = this.Editor.Tools.getToolSettings(currentBlock.name);

return toolSettings && toolSettings[this.Editor.Tools.apiSettings.IS_ENABLED_INLINE_TOOLBAR];
return toolSettings && toolSettings[this.Editor.Tools.USER_SETTINGS.ENABLED_INLINE_TOOLS];
}

/**
Expand All @@ -363,7 +363,7 @@ export default class InlineToolbar extends Module {
currentBlock = this.Editor.BlockManager.getBlock(currentSelection.anchorNode as HTMLElement);

const toolSettings = this.Editor.Tools.getToolSettings(currentBlock.name),
inlineToolbarSettings = toolSettings && toolSettings[this.Editor.Tools.apiSettings.IS_ENABLED_INLINE_TOOLBAR];
inlineToolbarSettings = toolSettings && toolSettings[this.Editor.Tools.USER_SETTINGS.ENABLED_INLINE_TOOLS];

/**
* All Inline Toolbar buttons
Expand Down Expand Up @@ -469,10 +469,10 @@ export default class InlineToolbar extends Module {
.entries(Tools.internalTools)
.filter(([name, toolClass]: [string, ToolConstructable | ToolSettings]) => {
if (_.isFunction(toolClass)) {
return toolClass[Tools.apiSettings.IS_INLINE];
return toolClass[Tools.INTERNAL_SETTINGS.IS_INLINE];
}

return (toolClass as ToolSettings).class[Tools.apiSettings.IS_INLINE];
return (toolClass as ToolSettings).class[Tools.INTERNAL_SETTINGS.IS_INLINE];
})
.map(([name]: [string, InlineToolConstructable | ToolSettings]) => name);

Expand All @@ -481,9 +481,9 @@ export default class InlineToolbar extends Module {
* 2) For external tools, check tool's settings
*/
if (internalTools.includes(toolName)) {
shortcut = this.inlineTools[toolName].shortcut;
} else if (toolSettings && toolSettings[Tools.apiSettings.SHORTCUT]) {
shortcut = toolSettings[Tools.apiSettings.SHORTCUT];
shortcut = this.inlineTools[toolName][Tools.INTERNAL_SETTINGS.SHORTCUT];
} else if (toolSettings && toolSettings[Tools.USER_SETTINGS.SHORTCUT]) {
shortcut = toolSettings[Tools.USER_SETTINGS.SHORTCUT];
}

if (shortcut) {
Expand Down Expand Up @@ -518,7 +518,7 @@ export default class InlineToolbar extends Module {

const toolSettings = this.Editor.Tools.getToolSettings(currentBlock.name);

if (!toolSettings || !toolSettings[this.Editor.Tools.apiSettings.IS_ENABLED_INLINE_TOOLBAR]) {
if (!toolSettings || !toolSettings[this.Editor.Tools.USER_SETTINGS.ENABLED_INLINE_TOOLS]) {
return;
}

Expand Down
17 changes: 9 additions & 8 deletions src/components/modules/toolbar/toolbox.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Module from '../../__module';
import $ from '../../dom';
import _ from '../../utils';
import {BlockToolConstructable, ToolboxConfig} from '../../../../types';
import {BlockToolConstructable} from '../../../../types';

/**
* @class Toolbox
Expand Down Expand Up @@ -203,9 +203,10 @@ export default class Toolbox extends Module {
* @param {BlockToolConstructable} tool - tool class
*/
private addTool(toolName: string, tool: BlockToolConstructable): void {
const api = this.Editor.Tools.apiSettings;
const internalSettings = this.Editor.Tools.INTERNAL_SETTINGS;
const userSettings = this.Editor.Tools.USER_SETTINGS;

const toolToolboxSettings = tool[api.TOOLBOX];
const toolToolboxSettings = tool[internalSettings.TOOLBOX];

/**
* Skip tools that don't pass 'toolbox' property
Expand All @@ -227,7 +228,7 @@ export default class Toolbox extends Module {
// return;
// }

const {toolbox: userToolboxSettings = {} as ToolboxConfig} = this.Editor.Tools.getToolSettings(toolName);
const userToolboxSettings = this.Editor.Tools.getToolSettings(toolName)[userSettings.TOOLBOX] || {};

const button = $.make('li', [ this.CSS.toolboxButton ]);

Expand Down Expand Up @@ -262,8 +263,8 @@ export default class Toolbox extends Module {
*/
const toolSettings = this.Editor.Tools.getToolSettings(toolName);

if (toolSettings && toolSettings[this.Editor.Tools.apiSettings.SHORTCUT]) {
this.enableShortcut(tool, toolName, toolSettings[this.Editor.Tools.apiSettings.SHORTCUT]);
if (toolSettings && toolSettings[this.Editor.Tools.USER_SETTINGS.SHORTCUT]) {
this.enableShortcut(tool, toolName, toolSettings[this.Editor.Tools.USER_SETTINGS.SHORTCUT]);
}

/** Increment Tools count */
Expand All @@ -288,11 +289,11 @@ export default class Toolbox extends Module {
*/
private showTooltip(button: HTMLElement, toolName: string): void {
const toolSettings = this.Editor.Tools.getToolSettings(toolName);
const toolboxSettings = this.Editor.Tools.available[toolName][this.Editor.Tools.apiSettings.TOOLBOX] || {};
const toolboxSettings = this.Editor.Tools.available[toolName][this.Editor.Tools.INTERNAL_SETTINGS.TOOLBOX] || {};
const userToolboxSettings = toolSettings.toolbox || {};
const name = userToolboxSettings.title || toolboxSettings.title || toolName;

let shortcut = toolSettings[this.Editor.Tools.apiSettings.SHORTCUT];
let shortcut = toolSettings[this.Editor.Tools.USER_SETTINGS.SHORTCUT];

const fragment = document.createDocumentFragment();
const hint = document.createTextNode(_.capitalize(name));
Expand Down
33 changes: 22 additions & 11 deletions src/components/modules/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class Tools extends Module {
}

const tools = Object.entries(this.available).filter( ([name, tool]) => {
if (!tool[this.apiSettings.IS_INLINE]) {
if (!tool[this.INTERNAL_SETTINGS.IS_INLINE]) {
return false;
}

Expand Down Expand Up @@ -108,7 +108,7 @@ export default class Tools extends Module {
public get blockTools(): {[name: string]: BlockToolConstructable} {
// eslint-disable-next-line no-unused-vars
const tools = Object.entries(this.available).filter( ([name, tool]) => {
return !tool[this.apiSettings.IS_INLINE];
return !tool[this.INTERNAL_SETTINGS.IS_INLINE];
});

/**
Expand All @@ -122,24 +122,35 @@ export default class Tools extends Module {
}

/**
* Constant for available Tools Settings
* @todo separate internal and external options
* Constant for available Tools internal settings provided by Tool developer
*
* @return {object}
*/
public get apiSettings() {
public get INTERNAL_SETTINGS() {
return {
CONFIG: 'config',
IS_ENABLED_INLINE_TOOLBAR: 'inlineToolbar',
IS_ENABLED_LINE_BREAKS: 'enableLineBreaks',
IS_INLINE: 'isInline',
IS_PASTE_DISALLOWED: 'disallowPaste',
SHORTCUT: 'shortcut',
TOOLBOX: 'toolbox',
SANITIZE_CONFIG: 'sanitize',
CONVERSION_CONFIG: 'conversionConfig',
};
}

/**
* Constant for available Tools settings provided by user
*
* return {object}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* return {object}
* @return {object}

*/
public get USER_SETTINGS() {
return {
SHORTCUT: 'shortcut',
TOOLBOX: 'toolbox',
ENABLED_INLINE_TOOLS: 'inlineToolbar',
CONFIG: 'config',
};
}

/**
* Map {name: Class, ...} where:
* name — block type name in JSON. Got from EditorConfig.tools keys
Expand Down Expand Up @@ -304,7 +315,7 @@ export default class Tools extends Module {
/**
* Configuration to be passed to the Tool's constructor
*/
const config = this.toolsSettings[tool][this.apiSettings.CONFIG] || {};
const config = this.toolsSettings[tool][this.USER_SETTINGS.CONFIG] || {};

// Pass placeholder to initial Block config
if (tool === this.config.initialBlock && !config.placeholder) {
Expand Down Expand Up @@ -336,7 +347,7 @@ export default class Tools extends Module {
*/
const constructorOptions = {
api: this.Editor.API.methods,
config: toolSettings[this.apiSettings.CONFIG] || {},
config: (toolSettings[this.USER_SETTINGS.CONFIG] || {}) as ToolSettings,
};

return new tool(constructorOptions) as InlineTool;
Expand Down Expand Up @@ -382,7 +393,7 @@ export default class Tools extends Module {
function: toolClass.prepare,
data: {
toolName,
config: this.toolsSettings[toolName][this.apiSettings.CONFIG],
config: this.toolsSettings[toolName][this.USER_SETTINGS.CONFIG],
},
});
} else {
Expand Down