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

Conversation

draganescu
Copy link
Contributor

@draganescu draganescu commented Apr 11, 2019

Description

Closes #14647. This adds a new option codeEditingEnabled which the block_editor_settings filter in WP Core can set to false (default is true), case in which the post editor will have two changes reflected:

  • the more tools and options menu (vertical dots menu top right) will not show the option to switch between code and visual
  • the blocks themselves will not have the option for HTML editing @myleshyson I thought that'd be a good idea, as no code editing means no code editing :)

How has this been tested?

  1. Set the codeEditingEnabled editor setting to false with a plugin (or hack it in edit-form-blocks.php)
  2. Observe the block editor while editing a post, you cannot edit the code anymore.

Screenshots

Screenshot 2019-04-11 at 19 10 23

Types of changes

New feature (non-breaking change which adds functionality)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.

@draganescu draganescu changed the title WIP: Add a new option to core/editor to disable code editing Adds a new option to core/editor to disable code editing Apr 11, 2019
@draganescu
Copy link
Contributor Author

I think this is the first time Travis is right.

@draganescu draganescu changed the title Adds a new option to core/editor to disable code editing WIP: Adds a new option to core/editor to disable code editing Apr 11, 2019
@draganescu draganescu self-assigned this Apr 12, 2019
@gziolo gziolo added [Feature] Extensibility The ability to extend blocks or the editing experience [Type] Task Issues or PRs that have been broken down into an individual action to take [Feature] Code Editor Handling the code view of the editing experience labels Apr 12, 2019
@draganescu draganescu force-pushed the update/disable-code-editor-option branch from 4bbd2c4 to fd6314a Compare April 15, 2019 13:11
@draganescu draganescu changed the title WIP: Adds a new option to core/editor to disable code editing Adds a new option to core/editor to disable code editing Apr 15, 2019
@draganescu
Copy link
Contributor Author

updated the failing tests, should be ok for review @gziolo :)

Copy link
Contributor

@talldan talldan left a comment

Choose a reason for hiding this comment

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

Hey @draganescu - I found one edge case that may or may not need to be addressed.

If I'm one of many users on a site, and I've switched my editor into Code Editor, I believe it's saved as a preference in local storage.

If an admin then disables the Code Editor using this solution as it stands, when I return my editor will still be in Code Editor mode. I can switch back using the 'Exit Code Editor' button, but it still gives me access that to something where the intention was to disable it.

Not sure what the right solution to solve that is. We could override the result of getEditorMode where it's called. That would definitely add some extra cruft to the codebase. Or we could try to dispatch an action to switch the user back.

Alternatively, it may just be something that we live with.

The only other feedback I have is that an e2e test might be nice, but I'm not sure if there's a way to toggle the editor setting before running the assertions.

@gziolo
Copy link
Member

gziolo commented Apr 16, 2019

If I'm one of many users on a site, and I've switched my editor into Code Editor, I believe it's saved as a preference in local storage.

If an admin then disables the Code Editor using this solution as it stands, when I return my editor will still be in Code Editor mode. I can switch back using the 'Exit Code Editor' button, but it still gives me access that to something where the intention was to disable it.

Does it mean that the value saves in local storage takes the precedence over what you set programmatically? The main use case is going to be that someone disables code editor with PHP code.

Edit: I think I now understand what @talldan said. So the issue is when I'm in Code Editor mode and the server gets updated. Next time I visit my editor, Code Editor is disabled but it gets loaded because of the mode stored in the local storage. It definitely should be covered. It seems like a bug.

There are two remaining tasks:

  • there needs to be documentation included so people could discover this feature
  • we should double check if there is any additional work necessary on WordPress core side, it's probably already handled with the following block_editor_settings filter

@draganescu draganescu force-pushed the update/disable-code-editor-option branch from fd6314a to e0f541c Compare April 16, 2019 11:16
@draganescu
Copy link
Contributor Author

draganescu commented Apr 16, 2019

Rebased and updated with code review. Remaining:

  • document the option
  • resolve the loading of the setting from LS

@gziolo I only tested by using the filter block_editor_settings and it works simply by adding a codeEditingEnabled key set to false. There appears nothing is required in WP Core PHP.

@draganescu
Copy link
Contributor Author

Regarding documentation, @gziolo I couldn't find a list of editor settings, it appears that none of these is documented:

	'alignWide'              => $align_wide,
	'availableTemplates'     => $available_templates,
	'allowedBlockTypes'      => $allowed_block_types,
	'disableCustomColors'    => get_theme_support( 'disable-custom-colors' ),
	'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
	'disablePostFormats'     => ! current_theme_supports( 'post-formats' ),
	/** This filter is documented in wp-admin/edit-form-advanced.php */
	'titlePlaceholder'       => apply_filters( 'enter_title_here', __( 'Add title' ), $post ),
	'bodyPlaceholder'        => $body_placeholder,
	'isRTL'                  => is_rtl(),
	'autosaveInterval'       => AUTOSAVE_INTERVAL,
	'maxUploadFileSize'      => $max_upload_size,
	'allowedMimeTypes'       => get_allowed_mime_types(),
	'styles'                 => $styles,
	'imageSizes'             => $available_image_sizes,
	'richEditingEnabled'     => user_can_richedit(),
	'postLock'               => $lock_details,
	'postLockUtils'          => array(
		'nonce'       => wp_create_nonce( 'lock-post_' . $post->ID ),
		'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
		'ajaxUrl'     => admin_url( 'admin-ajax.php' ),
	),

There is this https://developer.wordpress.org/reference/hooks/block_editor_settings/ documentation on the fact that the filter exists :)

@draganescu
Copy link
Contributor Author

@talldan this is my measly solution to the local storage pref. It does the job: if you have text in the preference but someone disables via server side filter the codeEditor then you end up in visual while your preference remains text.

@gziolo
Copy link
Member

gziolo commented Apr 16, 2019

Regarding documentation, @gziolo I couldn't find a list of editor settings, it appears that none of these is documented:

It doesn't sound right. We should include at least this new setting in the docs and start a section for block_editor_settings in Gutenberg docs as well. Other filters are documented here:
https://github.com/WordPress/gutenberg/tree/master/docs/designers-developers/developers/filters
It seems like editor-filters.md would be the best fit.

@draganescu draganescu force-pushed the update/disable-code-editor-option branch 2 times, most recently from b732850 to b523531 Compare May 3, 2019 09:09
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

I'm looking forward to seeing the remaining comments addressed as I'd like to ✅ this PR. It's nearly ready :)

@draganescu
Copy link
Contributor Author

@swissspidy I am back on it starting today!

@draganescu draganescu force-pushed the update/disable-code-editor-option branch from dbd807e to 49fdf62 Compare May 21, 2019 10:15
@draganescu draganescu requested a review from gziolo May 21, 2019 11:02
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

Great work on this PR. Feel free to merge once Travis is green. There was some unrelated failure on one of the jobs so I restarted it.

@gziolo gziolo added this to the 5.8 (Gutenberg) milestone May 21, 2019
@draganescu draganescu merged commit 8960509 into master May 21, 2019
@draganescu draganescu deleted the update/disable-code-editor-option branch May 21, 2019 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Code Editor Handling the code view of the editing experience [Feature] Extensibility The ability to extend blocks or the editing experience [Type] Task Issues or PRs that have been broken down into an individual action to take
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Option to Disable Code Editor
5 participants