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

Add markdown editor toolbar customization #1236

Merged
merged 4 commits into from
Apr 16, 2018
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import React from 'react';
import { Editor as Slate } from 'slate-react';
import Plain from 'slate-plain-serializer';
Expand Down Expand Up @@ -50,12 +51,13 @@ export default class RawEditor extends React.Component {
};

render() {
const { className } = this.props;
const { className, field } = this.props;
return (
<div className="nc-rawEditor-rawWrapper">
<div className="nc-visualEditor-editorControlBar">
<Toolbar
onToggleMode={this.handleToggleMode}
buttons={field.get('buttons')}
className="nc-markdownWidget-toolbarRaw"
disabled
rawMode
Expand All @@ -77,4 +79,5 @@ RawEditor.propTypes = {
onMode: PropTypes.func.isRequired,
className: PropTypes.string.isRequired,
value: PropTypes.string,
field: ImmutablePropTypes.map
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class Toolbar extends React.Component {
getAsset: PropTypes.func,
disabled: PropTypes.bool,
className: PropTypes.string,
buttons: ImmutablePropTypes.list
};

constructor(props) {
Expand All @@ -26,6 +27,11 @@ export default class Toolbar extends React.Component {
};
}

isHidden = button => {
const { buttons } = this.props;
return List.isList(buttons) ? !buttons.includes(button) : false;
}

render() {
const {
onMarkClick,
Expand Down Expand Up @@ -65,6 +71,7 @@ export default class Toolbar extends React.Component {
icon="bold"
onClick={onMarkClick}
isActive={selectionHasMark}
isHidden={this.isHidden('bold')}
disabled={disabled}
/>
<ToolbarButton
Expand All @@ -73,6 +80,7 @@ export default class Toolbar extends React.Component {
icon="italic"
onClick={onMarkClick}
isActive={selectionHasMark}
isHidden={this.isHidden('italic')}
disabled={disabled}
/>
<ToolbarButton
Expand All @@ -81,6 +89,7 @@ export default class Toolbar extends React.Component {
icon="code"
onClick={onMarkClick}
isActive={selectionHasMark}
isHidden={this.isHidden('code')}
disabled={disabled}
/>
<ToolbarButton
Expand All @@ -89,6 +98,7 @@ export default class Toolbar extends React.Component {
icon="link"
onClick={onLinkClick}
isActive={selectionHasLink}
isHidden={this.isHidden('link')}
disabled={disabled}
/>
<ToolbarButton
Expand All @@ -97,6 +107,7 @@ export default class Toolbar extends React.Component {
icon="h1"
onClick={onBlockClick}
isActive={selectionHasBlock}
isHidden={this.isHidden('heading-one')}
disabled={disabled}
/>
<ToolbarButton
Expand All @@ -105,6 +116,7 @@ export default class Toolbar extends React.Component {
icon="h2"
onClick={onBlockClick}
isActive={selectionHasBlock}
isHidden={this.isHidden('heading-two')}
disabled={disabled}
/>
<ToolbarButton
Expand All @@ -113,6 +125,7 @@ export default class Toolbar extends React.Component {
icon="quote"
onClick={onBlockClick}
isActive={selectionHasBlock}
isHidden={this.isHidden('quote')}
disabled={disabled}
/>
<ToolbarButton
Expand All @@ -121,6 +134,7 @@ export default class Toolbar extends React.Component {
icon="code-block"
onClick={onBlockClick}
isActive={selectionHasBlock}
isHidden={this.isHidden('code-block')}
disabled={disabled}
/>
<ToolbarButton
Expand All @@ -129,6 +143,7 @@ export default class Toolbar extends React.Component {
icon="list-bulleted"
onClick={onBlockClick}
isActive={selectionHasBlock}
isHidden={this.isHidden('bulleted-list')}
disabled={disabled}
/>
<ToolbarButton
Expand All @@ -137,6 +152,7 @@ export default class Toolbar extends React.Component {
icon="list-numbered"
onClick={onBlockClick}
isActive={selectionHasBlock}
isHidden={this.isHidden('numbered-list')}
disabled={disabled}
/>
<div className="nc-toolbar-dropdown">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import React from 'react';
import c from 'classnames';
import { Icon } from 'UI';

const ToolbarButton = ({ type, label, icon, onClick, isActive, disabled }) => {
const ToolbarButton = ({ type, label, icon, onClick, isActive, isHidden, disabled }) => {
const active = isActive && type && isActive(type);

if (isHidden) {
return null;
}

return (
<button
className={c('nc-toolbarButton-button', { ['nc-toolbarButton-active']: active })}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import React, { Component } from 'react';
import { get, isEmpty, debounce } from 'lodash';
import { Map } from 'immutable';
Expand Down Expand Up @@ -33,6 +34,7 @@ export default class Editor extends Component {
onMode: PropTypes.func.isRequired,
className: PropTypes.string.isRequired,
value: PropTypes.string,
field: ImmutablePropTypes.map
};

constructor(props) {
Expand Down Expand Up @@ -189,7 +191,7 @@ export default class Editor extends Component {
}

render() {
const { onAddAsset, getAsset, className } = this.props;
const { onAddAsset, getAsset, className, field } = this.props;

return (
<div className="nc-visualEditor-wrapper">
Expand All @@ -206,6 +208,7 @@ export default class Editor extends Component {
onSubmit={this.handlePluginAdd}
onAddAsset={onAddAsset}
getAsset={getAsset}
buttons={field.get('buttons')}
/>
</div>
<Slate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class MarkdownControl extends React.Component {
getAsset,
value,
classNameWrapper,
field
} = this.props;

const { mode } = this.state;
Expand All @@ -57,6 +58,7 @@ export default class MarkdownControl extends React.Component {
getAsset={getAsset}
className={classNameWrapper}
value={value}
field={field}
/>
</div>
);
Expand All @@ -69,6 +71,7 @@ export default class MarkdownControl extends React.Component {
getAsset={getAsset}
className={classNameWrapper}
value={value}
field={field}
/>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions website/site/content/docs/widgets/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The markdown widget provides a full fledged text editor - which is based on [sla
- **Data type:** markdown
- **Options:**
- `default`: accepts markdown content
- `buttons`: an array of strings representing the formatting buttons to display, all buttons shown by default. Buttons include: `bold`, `italic`, `code`, `link`, `heading-one`, `heading-two`, `quote`, `code-block`, `bulleted-list`, and `numbered-list`.
- **Example:**

```yaml
Expand Down