[Refactor] Separate internal and external settings (#845)

This commit is contained in:
George Berezhnoy 2019-07-10 20:23:26 +03:00 committed by GitHub
commit fdeae3f1a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 54 additions and 40 deletions

6
dist/editor.js vendored

File diff suppressed because one or more lines are too long

View file

@ -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).

View file

@ -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) {
@ -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;
}

View file

@ -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);
@ -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;
});
}
@ -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;

View file

@ -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

View file

@ -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

View file

@ -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];
}
/**
@ -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
@ -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);
@ -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) {
@ -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;
}

View file

@ -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
@ -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
@ -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 ]);
@ -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 */
@ -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));

View file

@ -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;
}
@ -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];
});
/**
@ -122,17 +122,14 @@ 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',
@ -140,6 +137,20 @@ export default class Tools extends Module {
};
}
/**
* Constant for available Tools settings provided by user
*
* 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
@ -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) {
@ -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;
@ -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 {