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

Issue 354 inline tools filter #376

Merged
merged 4 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
79 changes: 68 additions & 11 deletions build/codex-editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/codex-editor.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/assets/json-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const cPreview = (function (module) {
/** Stylize JSON keys */
string = string.replace( /"(\w+)"\s?:/g, '"<span class=sc_key>$1</span>" :');
/** Stylize tool names */
string = string.replace( /"(text|quote|list|header|link|code|image)"/g, '"<span class=sc_toolname>$1</span>"');
string = string.replace( /"(text|quote|list|header|link|code|image|delimiter)"/g, '"<span class=sc_toolname>$1</span>"');
/** Stylize HTML tags */
string = string.replace( /(&lt;[\/a-z]+(&gt;)?)/gi, '<span class=sc_tag>$1</span>' );
/** Stylize strings */
Expand Down
10 changes: 8 additions & 2 deletions example/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,19 @@
{
type : 'text',
data : {
text : 'В отличие от WYSIWYG-редакторов, CodeX Editor возвращает не сгенерированный HTML-код, включающий и содержание и оформление статьи, а JSON с данными о каждом Блоке.'
text : 'В отличие от WYSIWYG-редакторов, CodeX Editor возвращает не сгенерированный HTML-код, включающий и содержание и оформление статьи, а JSON с данными о каждом Блоке. Пример таких данных находится внизу этой страницы.'
}
},
{
type : 'text',
data : {
text : 'Полученные данные можно использовать как угодно: выводить в вебе, рендерить в нативных мобильных приложениях, передавать в Instant Articles или Google AMP, использовать для генерации аудио-версии и тд. Это очень удобно.'
text : 'Полученные данные можно использовать как угодно: выводить в вебе, рендерить в нативных мобильных приложениях, передавать в Instant Articles или Google AMP, использовать для генерации аудио-версии и тд.'
}
},
{
type : 'text',
data : {
text : 'Помимо этого, данные о Блоках удобно очищать, фильтровать и обрабатывать на бэкенде.'
}
},
{
Expand Down
59 changes: 59 additions & 0 deletions src/components/modules/toolbar-inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export default class InlineToolbar extends Module {
inlineToolbarShowed: 'ce-inline-toolbar--showed',
buttonsWrapper: 'ce-inline-toolbar__buttons',
actionsWrapper: 'ce-inline-toolbar__actions',
inlineToolButton: 'ce-inline-tool',
inlineToolButtonLast: 'ce-inline-tool--last',
};

/**
Expand Down Expand Up @@ -155,7 +157,19 @@ export default class InlineToolbar extends Module {
* Shows Inline Toolbar
*/
private open() {
/**
* Filter inline-tools and show only allowed by Block's Tool
*/
this.filterTools();

/**
* Show Inline Toolbar
*/
this.nodes.wrapper.classList.add(this.CSS.inlineToolbarShowed);

/**
* Call 'clear' method for Inline Tools (for example, 'link' want to clear input)
*/
this.tools.forEach( (toolInstance, toolName) => {
if (typeof toolInstance.clear === 'function') {
toolInstance.clear();
Expand Down Expand Up @@ -215,6 +229,50 @@ export default class InlineToolbar extends Module {
return toolSettings && toolSettings[this.Editor.Tools.apiSettings.IS_ENABLED_INLINE_TOOLBAR];
}

/**
* Show only allowed Tools
*/
private filterTools(): void {
const currentSelection = Selection.get(),
currentBlock = this.Editor.BlockManager.getBlock(currentSelection.anchorNode);

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

/**
* All Inline Toolbar buttons
* @type {HTMLElement[]}
*/
const buttons = Array.from(this.nodes.buttons.querySelectorAll(`.${this.CSS.inlineToolButton}`)) as HTMLElement[];

/**
* Show previously hided
*/
buttons.forEach((button) => {
button.hidden = false;
button.classList.remove(this.CSS.inlineToolButtonLast);
});

/**
* Filter buttons if Block Tool pass config like inlineToolbar=['link']
*/
if (Array.isArray(inlineToolbarSettings)) {
buttons.forEach((button) => {
button.hidden = !inlineToolbarSettings.includes(button.dataset.tool);
});
}

/**
* Tick for removing right-margin from last visible button.
* Current generation of CSS does not allow to filter :visible elements
*/
const lastVisibleButton = buttons.filter((button) => !button.hidden).pop();

if (lastVisibleButton) {
lastVisibleButton.classList.add(this.CSS.inlineToolButtonLast);
}
}

/**
* Working with Tools
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -240,6 +298,7 @@ export default class InlineToolbar extends Module {
return;
}

button.dataset.tool = toolName;
this.nodes.buttons.appendChild(button);

if (typeof tool.renderActions === 'function') {
Expand Down
8 changes: 8 additions & 0 deletions src/styles/inline-toolbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@
&--showed {
display: block;
}

[hidden] {
display: none !important;
}
}

.ce-inline-tool {
@apply --toolbar-button;
line-height: normal;

&--last {
margin-right: 0 !important;
}

&--link {
.icon {
margin-top: -2px;
Expand Down