Skip to content

Add basic undo/redo controls in the toolbar #100

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

Merged
merged 1 commit into from
Oct 17, 2017
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ onSave: () => {},
enableHorizontalRule: false,
// Enable the use of line breaks in the editor.
enableLineBreak: false,
// Show undo/redo controls in the toolbar.
showUndoRedoControls: false,
// Disable copy/paste of rich text in the editor.
stripPastedStyles: true,
// List of the available block types.
Expand Down
1 change: 1 addition & 0 deletions examples/intro.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const editor = (
placeholder="Write here…"
enableHorizontalRule={true}
enableLineBreak={true}
showUndoRedoControls={true}
stripPastedStyles={false}
entityTypes={[
{
Expand Down
1 change: 1 addition & 0 deletions examples/wagtail.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const editor = (
placeholder="Write here…"
enableHorizontalRule={true}
enableLineBreak={true}
showUndoRedoControls={true}
stripPastedStyles={false}
entityTypes={[
{
Expand Down
118 changes: 67 additions & 51 deletions lib/api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export const CUSTOM_STYLE_MAP = {
};

export const BR_TYPE = 'BR';
export const UNDO_TYPE = 'undo';
export const REDO_TYPE = 'redo';

// Originally from https://github.com/facebook/draft-js/blob/master/src/component/utils/getDefaultKeyBinding.js.
export const KEY_CODES = {
Expand All @@ -127,57 +129,71 @@ export const KEY_CODES = {
8: 56,
};

export const KEYBOARD_SHORTCUTS = {};
KEYBOARD_SHORTCUTS[BLOCK_TYPE.UNSTYLED] = {
other: 'Ctrl+Alt+0',
macOS: '⌘+Option+0',
};
KEYBOARD_SHORTCUTS[BLOCK_TYPE.HEADER_ONE] = {
other: 'Ctrl+Alt+1',
macOS: '⌘+Option+1',
};
KEYBOARD_SHORTCUTS[BLOCK_TYPE.HEADER_TWO] = {
other: 'Ctrl+Alt+2',
macOS: '⌘+Option+2',
};
KEYBOARD_SHORTCUTS[BLOCK_TYPE.HEADER_THREE] = {
other: 'Ctrl+Alt+3',
macOS: '⌘+Option+3',
};
KEYBOARD_SHORTCUTS[BLOCK_TYPE.HEADER_FOUR] = {
other: 'Ctrl+Alt+4',
macOS: '⌘+Option+4',
};
KEYBOARD_SHORTCUTS[BLOCK_TYPE.HEADER_FIVE] = {
other: 'Ctrl+Alt+5',
macOS: '⌘+Option+5',
};
KEYBOARD_SHORTCUTS[BLOCK_TYPE.HEADER_SIX] = {
other: 'Ctrl+Alt+6',
macOS: '⌘+Option+6',
};
KEYBOARD_SHORTCUTS[BLOCK_TYPE.UNORDERED_LIST_ITEM] = {
other: 'Ctrl+Shift+8',
macOS: '⌘+Shift+8',
};
KEYBOARD_SHORTCUTS[BLOCK_TYPE.ORDERED_LIST_ITEM] = {
other: 'Ctrl+Shift+7',
macOS: '⌘+Shift+7',
};
KEYBOARD_SHORTCUTS[ENTITY_TYPE.LINK] = { other: 'Ctrl+K', macOS: '⌘+K' };
KEYBOARD_SHORTCUTS[BR_TYPE] = {
other: 'Shift+Enter',
macOS: 'Shift+Enter',
};
KEYBOARD_SHORTCUTS[INLINE_STYLE.BOLD] = { other: 'Ctrl+B', macOS: '⌘+B' };
KEYBOARD_SHORTCUTS[INLINE_STYLE.ITALIC] = { other: 'Ctrl+I', macOS: '⌘+I' };
KEYBOARD_SHORTCUTS[INLINE_STYLE.UNDERLINE] = {
other: 'Ctrl+U',
macOS: '⌘+U',
};
KEYBOARD_SHORTCUTS[INLINE_STYLE.STRIKETHROUGH] = {
other: 'Alt+Shift+5',
macOS: 'Option+Shift+5',
export const KEYBOARD_SHORTCUTS = {
[BLOCK_TYPE.UNSTYLED]: {
other: 'Ctrl+Alt+0',
macOS: '⌘+Option+0',
},
[BLOCK_TYPE.HEADER_ONE]: {
other: 'Ctrl+Alt+1',
macOS: '⌘+Option+1',
},
[BLOCK_TYPE.HEADER_TWO]: {
other: 'Ctrl+Alt+2',
macOS: '⌘+Option+2',
},
[BLOCK_TYPE.HEADER_THREE]: {
other: 'Ctrl+Alt+3',
macOS: '⌘+Option+3',
},
[BLOCK_TYPE.HEADER_FOUR]: {
other: 'Ctrl+Alt+4',
macOS: '⌘+Option+4',
},
[BLOCK_TYPE.HEADER_FIVE]: {
other: 'Ctrl+Alt+5',
macOS: '⌘+Option+5',
},
[BLOCK_TYPE.HEADER_SIX]: {
other: 'Ctrl+Alt+6',
macOS: '⌘+Option+6',
},
[BLOCK_TYPE.UNORDERED_LIST_ITEM]: {
other: 'Ctrl+Shift+8',
macOS: '⌘+Shift+8',
},
[BLOCK_TYPE.ORDERED_LIST_ITEM]: {
other: 'Ctrl+Shift+7',
macOS: '⌘+Shift+7',
},

[INLINE_STYLE.BOLD]: { other: 'Ctrl+B', macOS: '⌘+B' },
[INLINE_STYLE.ITALIC]: { other: 'Ctrl+I', macOS: '⌘+I' },
[INLINE_STYLE.UNDERLINE]: {
other: 'Ctrl+U',
macOS: '⌘+U',
},
[INLINE_STYLE.STRIKETHROUGH]: {
other: 'Alt+Shift+5',
macOS: 'Option+Shift+5',
},

[ENTITY_TYPE.LINK]: { other: 'Ctrl+K', macOS: '⌘+K' },

[BR_TYPE]: {
other: 'Shift+Enter',
macOS: 'Shift+Enter',
},

[UNDO_TYPE]: {
other: 'Ctrl+Z',
macOS: '⌘+Z',
},

[REDO_TYPE]: {
other: 'Ctrl+Shift+Z',
macOS: 'Ctrl+Shift+Z',
},
};

export const NBSP = '\u00a0';
Expand Down
24 changes: 23 additions & 1 deletion lib/components/DraftailEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
INLINE_STYLE,
HANDLED,
NOT_HANDLED,
UNDO_TYPE,
REDO_TYPE,
} from '../api/constants';

import DraftUtils from '../api/DraftUtils';
Expand Down Expand Up @@ -38,6 +40,8 @@ const defaultProps = {
enableHorizontalRule: false,
// Enable the use of line breaks in the editor.
enableLineBreak: false,
// Show undo/redo controls in the toolbar.
showUndoRedoControls: false,
// Disable copy/paste of rich text in the editor.
// TODO Make this false by default once copy/paste is better supported.
stripPastedStyles: true,
Expand All @@ -59,6 +63,7 @@ const propTypes = {
placeholder: PropTypes.string,
enableHorizontalRule: PropTypes.bool,
enableLineBreak: PropTypes.bool,
showUndoRedoControls: PropTypes.bool,
stripPastedStyles: PropTypes.bool,
blockTypes: PropTypes.arrayOf(
PropTypes.shape({
Expand Down Expand Up @@ -190,6 +195,7 @@ class DraftailEditor extends Component {

this.addHR = this.addHR.bind(this);
this.addBR = this.addBR.bind(this);
this.onUndoRedo = this.onUndoRedo.bind(this);

this.blockRenderer = this.blockRenderer.bind(this);
this.onRequestDialog = this.onRequestDialog.bind(this);
Expand Down Expand Up @@ -447,6 +453,19 @@ class DraftailEditor extends Component {
this.onChange(DraftUtils.addLineBreakRemovingSelection(editorState));
}

onUndoRedo(type) {
const { editorState } = this.state;
let newEditorState = editorState;

if (type === UNDO_TYPE) {
newEditorState = EditorState.undo(editorState);
} else if (type === REDO_TYPE) {
newEditorState = EditorState.redo(editorState);
}

this.onChange(newEditorState);
}

blockRenderer(block) {
const { entityTypes } = this.props;
const { editorState } = this.state;
Expand Down Expand Up @@ -576,9 +595,10 @@ class DraftailEditor extends Component {

render() {
const {
placeholder,
enableHorizontalRule,
enableLineBreak,
placeholder,
showUndoRedoControls,
stripPastedStyles,
blockTypes,
inlineStyles,
Expand All @@ -603,6 +623,7 @@ class DraftailEditor extends Component {
editorState={editorState}
enableHorizontalRule={enableHorizontalRule}
enableLineBreak={enableLineBreak}
showUndoRedoControls={showUndoRedoControls}
blockTypes={blockTypes}
inlineStyles={inlineStyles}
entityTypes={entityTypes}
Expand All @@ -611,6 +632,7 @@ class DraftailEditor extends Component {
toggleInlineStyle={this.toggleInlineStyle}
addHR={this.addHR}
addBR={this.addBR}
onUndoRedo={this.onUndoRedo}
onRequestDialog={this.onRequestDialog}
/>

Expand Down
26 changes: 25 additions & 1 deletion lib/components/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import DraftUtils from '../api/DraftUtils';
import Button from '../components/Button';
import ToolbarGroup from '../components/ToolbarGroup';

import { BR_TYPE, ENTITY_TYPE } from '../api/constants';
import { BR_TYPE, ENTITY_TYPE, UNDO_TYPE, REDO_TYPE } from '../api/constants';
import behavior from '../api/behavior';

const getButtonTitle = (description, name) => {
Expand All @@ -27,11 +27,13 @@ const Toolbar = ({
inlineStyles,
enableHorizontalRule,
enableLineBreak,
showUndoRedoControls,
entityTypes,
toggleBlockType,
toggleInlineStyle,
addHR,
addBR,
onUndoRedo,
onRequestDialog,
}) => (
<div className="editor__toolbar" role="toolbar">
Expand Down Expand Up @@ -101,20 +103,42 @@ const Toolbar = ({
/>
))}
</ToolbarGroup>

<ToolbarGroup>

Choose a reason for hiding this comment

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

@thibaudcolas Shouldn't the whole ToolbarGroup be wrapped into the showUndoRedoControls check? Does it displays fine if the group is empty?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, the ToolbarGroup contains some magic so it doesn't render anything when it has no children:

const ToolbarGroup = ({ children }) => {
const hasChildren = React.Children.toArray(children).some(c => c !== null);
return hasChildren ? <div className="toolbar-group">{children}</div> : null;
};

{showUndoRedoControls ? (
<Button
name={UNDO_TYPE}
onClick={onUndoRedo.bind(null, UNDO_TYPE)}
label="↺"
title={getButtonTitle('Undo', UNDO_TYPE)}
/>
) : null}

{showUndoRedoControls ? (
<Button
name={REDO_TYPE}
onClick={onUndoRedo.bind(null, REDO_TYPE)}
label="↻"
title={getButtonTitle('Redo', REDO_TYPE)}
/>
) : null}
</ToolbarGroup>
</div>
);

Toolbar.propTypes = {
editorState: PropTypes.object.isRequired,
enableHorizontalRule: PropTypes.bool.isRequired,
enableLineBreak: PropTypes.bool.isRequired,
showUndoRedoControls: PropTypes.bool.isRequired,
entityTypes: PropTypes.array.isRequired,
blockTypes: PropTypes.array.isRequired,
inlineStyles: PropTypes.array.isRequired,
toggleBlockType: PropTypes.func.isRequired,
toggleInlineStyle: PropTypes.func.isRequired,
addHR: PropTypes.func.isRequired,
addBR: PropTypes.func.isRequired,
onUndoRedo: PropTypes.func.isRequired,
onRequestDialog: PropTypes.func.isRequired,
};

Expand Down
8 changes: 8 additions & 0 deletions lib/components/Toolbar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ const mockProps = {
editorState: EditorState.createEmpty(),
enableHorizontalRule: false,
enableLineBreak: false,
showUndoRedoControls: false,
entityTypes: [],
blockTypes: [],
inlineStyles: [],
toggleBlockType: () => {},
toggleInlineStyle: () => {},
addHR: () => {},
addBR: () => {},
onUndoRedo: () => {},
onRequestDialog: () => {},
};

Expand All @@ -39,6 +41,12 @@ describe('Toolbar', () => {
).toMatchSnapshot();
});

it('#showUndoRedoControls', () => {
expect(
shallow(<Toolbar {...mockProps} showUndoRedoControls={true} />),
).toMatchSnapshot();
});

it('#entityTypes', () => {
expect(
shallow(
Expand Down
Loading