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

Adds a new option to core/editor to disable code editing #14932

Merged
merged 12 commits into from
May 21, 2019
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
22 changes: 21 additions & 1 deletion docs/designers-developers/developers/filters/editor-filters.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Editor Filters (Experimental)

To modify the behavior of the editor experience, the following Filters are exposed:

### `editor.PostFeaturedImage.imageSize`
Expand Down Expand Up @@ -30,3 +29,24 @@ var customPreviewMessage = function() {
wp.hooks.addFilter( 'editor.PostPreview.interstitialMarkup', 'my-plugin/custom-preview-message', customPreviewMessage );
```

## Editor settings

### `block_editor_settings`
This is a PHP filter which is applied before sending settings to the WordPress block editor.

You may find details about this filter [on its WordPress Code Reference page](https://developer.wordpress.org/reference/hooks/block_editor_settings/).

The filter will send any setting to the initialized Editor, which means any editor setting that is used to configure the editor at initialisation can be filtered by a PHP WordPress plugin before being sent.

### Available default editor settings

#### `richEditingEnabled`
If it is `true` the user can edit the content using the Visual Editor.

It is set by default to the return value of the [`user_can_richedit`](https://developer.wordpress.org/reference/functions/user_can_richedit/) function. It checks if the user can access the Visual Editor and whether it’s supported by the user’s browser.


#### `codeEditingEnabled`
Default `true`. Indicates whether the user can access the Code Editor **in addition** to the Visual Editor.

If set to false the user will not be able to switch between Visual and Code editor. The option in the settings menu will not be available and the keyboard shortcut for switching editor types will not fire.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { getBlockType, hasBlockSupport } from '@wordpress/blocks';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';

export function BlockModeToggle( { blockType, mode, onToggleMode, small = false } ) {
if ( ! hasBlockSupport( blockType, 'html', true ) ) {
export function BlockModeToggle( { blockType, mode, onToggleMode, small = false, isCodeEditingEnabled = true } ) {
if ( ! hasBlockSupport( blockType, 'html', true ) || ! isCodeEditingEnabled ) {
return null;
}

Expand All @@ -36,10 +36,12 @@ export default compose( [
withSelect( ( select, { clientId } ) => {
const { getBlock, getBlockMode } = select( 'core/block-editor' );
const block = getBlock( clientId );
const isCodeEditingEnabled = select( 'core/editor' ).getEditorSettings().codeEditingEnabled;
draganescu marked this conversation as resolved.
Show resolved Hide resolved

return {
mode: getBlockMode( clientId ),
blockType: block ? getBlockType( block.name ) : null,
isCodeEditingEnabled,
};
} ),
withDispatch( ( dispatch, { onToggle = noop, clientId } ) => ( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ describe( 'BlockModeToggle', () => {

expect( text ).toEqual( 'Edit visually' );
} );

it( 'should not render the Visual mode button if code editing is disabled', () => {
const wrapper = getShallowRenderOutput(
<BlockModeToggle
blockType={ { supports: { html: true } } }
mode="html"
isCodeEditingEnabled={ false }
/>
);

expect( wrapper ).toBe( null );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ function ModeSwitcher( { onSwitch, mode } ) {
export default compose( [
withSelect( ( select ) => ( {
isRichEditingEnabled: select( 'core/editor' ).getEditorSettings().richEditingEnabled,
isCodeEditingEnabled: select( 'core/editor' ).getEditorSettings().codeEditingEnabled,
mode: select( 'core/edit-post' ).getEditorMode(),
} ) ),
ifCondition( ( { isRichEditingEnabled } ) => isRichEditingEnabled ),
ifCondition( ( { isRichEditingEnabled, isCodeEditingEnabled } ) => isRichEditingEnabled && isCodeEditingEnabled ),
withDispatch( ( dispatch ) => ( {
onSwitch( mode ) {
dispatch( 'core/edit-post' ).switchEditorMode( mode );
Expand Down
18 changes: 11 additions & 7 deletions packages/edit-post/src/components/keyboard-shortcuts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class EditorModeKeyboardShortcuts extends Component {
}

toggleMode() {
const { mode, switchMode, isRichEditingEnabled } = this.props;
if ( ! isRichEditingEnabled ) {
const { mode, switchMode, isModeSwitchEnabled } = this.props;
if ( ! isModeSwitchEnabled ) {
return;
}
switchMode( mode === 'visual' ? 'text' : 'visual' );
Expand Down Expand Up @@ -54,11 +54,15 @@ class EditorModeKeyboardShortcuts extends Component {
}

export default compose( [
withSelect( ( select ) => ( {
isRichEditingEnabled: select( 'core/editor' ).getEditorSettings().richEditingEnabled,
mode: select( 'core/edit-post' ).getEditorMode(),
isEditorSidebarOpen: select( 'core/edit-post' ).isEditorSidebarOpened(),
} ) ),
withSelect( ( select ) => {
const { richEditingEnabled, codeEditingEnabled } = select( 'core/editor' ).getEditorSettings();

return {
isModeSwitchEnabled: richEditingEnabled && codeEditingEnabled,
mode: select( 'core/edit-post' ).getEditorMode(),
isEditorSidebarOpen: select( 'core/edit-post' ).isEditorSidebarOpened(),
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => ( {
switchMode( mode ) {
dispatch( 'core/edit-post' ).switchEditorMode( mode );
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const INITIAL_EDITS_DEFAULTS = {};
*
* allowedBlockTypes boolean|Array Allowed block types
* richEditingEnabled boolean Whether rich editing is enabled or not
* codeEditingEnabled boolean Whether code editing is enabled or not
* enableCustomFields boolean Whether the WordPress custom fields are enabled or not
* autosaveInterval number Autosave Interval
* availableTemplates array? The available post templates
Expand All @@ -31,6 +32,7 @@ export const EDITOR_SETTINGS_DEFAULTS = {
...SETTINGS_DEFAULTS,

richEditingEnabled: true,
codeEditingEnabled: true,
enableCustomFields: false,
};