Skip to content

Commit

Permalink
Fix the editor save keyboard shortcut not working in code editor view (
Browse files Browse the repository at this point in the history
…#13159)

* Fix the editor save keyboard shortcut not working in code editor view

* Refactor based on code review feedback

- Introduce new TextEditorGlobalShortcuts component that implements the save shortcut
- Rename EditorGlobalShortcuts to VisualEditorGlobalShortcuts
- Add a deprecated version of EditorGlobalShortcuts
- Update CHANGELOG
  • Loading branch information
talldan authored Feb 1, 2019
1 parent 62f6163 commit 21c4aac
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 36 deletions.
3 changes: 3 additions & 0 deletions packages/edit-post/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

* Expose the `className` property to style the `PluginSidebar` component.

### Bug Fixes
- Fix 'save' keyboard shortcut not functioning in the Code Editor.

## 3.1.7 (2019-01-03)

## 3.1.6 (2018-12-18)
Expand Down
7 changes: 6 additions & 1 deletion packages/edit-post/src/components/text-editor/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/**
* WordPress dependencies
*/
import { PostTextEditor, PostTitle } from '@wordpress/editor';
import {
PostTextEditor,
PostTitle,
TextEditorGlobalKeyboardShortcuts,
} from '@wordpress/editor';
import { IconButton } from '@wordpress/components';
import { withDispatch, withSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
Expand All @@ -21,6 +25,7 @@ function TextEditor( { onExit, isRichEditingEnabled } ) {
>
{ __( 'Exit Code Editor' ) }
</IconButton>
<TextEditorGlobalKeyboardShortcuts />
</div>
) }
<div className="edit-post-text-editor__body">
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
PostTitle,
WritingFlow,
ObserveTyping,
EditorGlobalKeyboardShortcuts,
VisualEditorGlobalKeyboardShortcuts,
BlockSelectionClearer,
MultiSelectScrollIntoView,
_BlockSettingsMenuFirstItem,
Expand All @@ -23,7 +23,7 @@ import PluginBlockSettingsMenuGroup from '../block-settings-menu/plugin-block-se
function VisualEditor() {
return (
<BlockSelectionClearer className="edit-post-visual-editor editor-styles-wrapper">
<EditorGlobalKeyboardShortcuts />
<VisualEditorGlobalKeyboardShortcuts />
<CopyHandler />
<MultiSelectScrollIntoView />
<WritingFlow>
Expand Down
7 changes: 6 additions & 1 deletion packages/editor/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
## 9.1.0 (Unreleased)

### New Feature
### New Features

- Added `createCustomColorsHOC` for creating a higher order `withCustomColors` component.
- Added a new `TextEditorGlobalKeyboardShortcuts` component.

### Deprecations

- `EditorGlobalKeyboardShortcuts` has been deprecated in favor of `VisualEditorGlobalKeyboardShortcuts`.

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { withDispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { shortcuts } from '../editor-global-keyboard-shortcuts';
import { shortcuts } from '../global-keyboard-shortcuts/visual-editor-shortcuts';
import BlockActions from '../block-actions';
import BlockModeToggle from './block-mode-toggle';
import ReusableBlockConvertButton from './reusable-block-convert-button';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* WordPress dependencies
*/
import { KeyboardShortcuts } from '@wordpress/components';
import { rawShortcut } from '@wordpress/keycodes';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';

export function SaveShortcut( { onSave } ) {
return (
<KeyboardShortcuts
bindGlobal
shortcuts={ {
[ rawShortcut.primary( 's' ) ]: ( event ) => {
event.preventDefault();
onSave();
},
} }
/>
);
}

export default compose( [
withSelect( ( select ) => {
const { isEditedPostDirty } = select( 'core/editor' );

return {
isDirty: isEditedPostDirty(),
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => {
const {
savePost,
} = dispatch( 'core/editor' );

return {
onSave() {
// TODO: This should be handled in the `savePost` effect in
// considering `isSaveable`. See note on `isEditedPostSaveable`
// selector about dirtiness and meta-boxes.
//
// See: `isEditedPostSaveable`
const { isEditedPostDirty } = select( 'core/editor' );
if ( ! isEditedPostDirty() ) {
return;
}

savePost();
},
};
} ),
] )( SaveShortcut );
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Internal dependencies
*/
import SaveShortcut from './save-shortcut';

export default function TextEditorGlobalKeyboardShortcuts() {
return (
<SaveShortcut />
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { KeyboardShortcuts } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { rawShortcut, displayShortcut } from '@wordpress/keycodes';
import { compose } from '@wordpress/compose';
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
*/
import BlockActions from '../block-actions';
import SaveShortcut from './save-shortcut';

const preventDefault = ( event ) => {
event.preventDefault();
Expand All @@ -41,13 +43,12 @@ export const shortcuts = {
},
};

class EditorGlobalKeyboardShortcuts extends Component {
class VisualEditorGlobalKeyboardShortcuts extends Component {
constructor() {
super( ...arguments );

this.selectAll = this.selectAll.bind( this );
this.undoOrRedo = this.undoOrRedo.bind( this );
this.save = this.save.bind( this );
this.deleteSelectedBlocks = this.deleteSelectedBlocks.bind( this );
this.clearMultiSelection = this.clearMultiSelection.bind( this );
}
Expand All @@ -70,11 +71,6 @@ class EditorGlobalKeyboardShortcuts extends Component {
event.preventDefault();
}

save( event ) {
event.preventDefault();
this.props.onSave();
}

deleteSelectedBlocks( event ) {
const { selectedBlockClientIds, hasMultiSelection, onRemove, isLocked } = this.props;
if ( hasMultiSelection ) {
Expand Down Expand Up @@ -110,12 +106,7 @@ class EditorGlobalKeyboardShortcuts extends Component {
escape: this.clearMultiSelection,
} }
/>
<KeyboardShortcuts
bindGlobal
shortcuts={ {
[ rawShortcut.primary( 's' ) ]: this.save,
} }
/>
<SaveShortcut />
{ selectedBlockClientIds.length > 0 && (
<BlockActions clientIds={ selectedBlockClientIds }>
{ ( { onDuplicate, onRemove, onInsertAfter, onInsertBefore } ) => (
Expand Down Expand Up @@ -146,7 +137,7 @@ class EditorGlobalKeyboardShortcuts extends Component {
}
}

export default compose( [
const EnhancedVisualEditorGlobalKeyboardShortcuts = compose( [
withSelect( ( select ) => {
const {
getBlockOrder,
Expand All @@ -169,35 +160,32 @@ export default compose( [
selectedBlockClientIds,
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => {
withDispatch( ( dispatch ) => {
const {
clearSelectedBlock,
multiSelect,
redo,
undo,
removeBlocks,
savePost,
} = dispatch( 'core/editor' );

return {
onSave() {
// TODO: This should be handled in the `savePost` effect in
// considering `isSaveable`. See note on `isEditedPostSaveable`
// selector about dirtiness and meta-boxes.
//
// See: `isEditedPostSaveable`
const { isEditedPostDirty } = select( 'core/editor' );
if ( ! isEditedPostDirty() ) {
return;
}

savePost();
},
clearSelectedBlock,
onMultiSelect: multiSelect,
onRedo: redo,
onUndo: undo,
onRemove: removeBlocks,
};
} ),
] )( EditorGlobalKeyboardShortcuts );
] )( VisualEditorGlobalKeyboardShortcuts );

export default EnhancedVisualEditorGlobalKeyboardShortcuts;

export function EditorGlobalKeyboardShortcuts() {
deprecated( 'EditorGlobalKeyboardShortcuts', {
alternative: 'VisualEditorGlobalKeyboardShortcuts',
plugin: 'Gutenberg',
} );

return <EnhancedVisualEditorGlobalKeyboardShortcuts />;
}
6 changes: 5 additions & 1 deletion packages/editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export { default as URLPopover } from './url-popover';
export { default as AutosaveMonitor } from './autosave-monitor';
export { default as DocumentOutline } from './document-outline';
export { default as DocumentOutlineCheck } from './document-outline/check';
export { default as EditorGlobalKeyboardShortcuts } from './editor-global-keyboard-shortcuts';
export {
default as VisualEditorGlobalKeyboardShortcuts,
EditorGlobalKeyboardShortcuts,
} from './global-keyboard-shortcuts/visual-editor-shortcuts';
export { default as TextEditorGlobalKeyboardShortcuts } from './global-keyboard-shortcuts/text-editor-shortcuts';
export { default as EditorHistoryRedo } from './editor-history/redo';
export { default as EditorHistoryUndo } from './editor-history/undo';
export { default as EditorNotices } from './editor-notices';
Expand Down

0 comments on commit 21c4aac

Please sign in to comment.