Skip to content

Add: Keyboard shortcut for Edit as HTML - #45671

Open
sdnunca wants to merge 1 commit into
WordPress:trunkfrom
sdnunca:feature/add-keyboard-shortcut-switch-to-html-block-level
Open

sdnunca wants to merge 1 commit into
WordPress:trunkfrom
sdnunca:feature/add-keyboard-shortcut-switch-to-html-block-level

Conversation

@sdnunca

@sdnunca sdnunca commented Nov 9, 2022

Copy link
Copy Markdown
Contributor

What?

Add a keyboard shortcut to switch to HTML editing on a block level.

I've tried to integrate feedback from previous attempts into this PR. The shortcut shows in the help modal:
image
with the text previously agreed upon. The shortcut is also triggered only in context where the Edit as HTML option would show (when having exactly 1 block selected, for blocks that support the option).

Why?

To solve #9723

How?

Testing Instructions

  1. Open a Post or Page.
  2. Insert 2 blocks.
  3. Select one of the blocks and press Shift + Option + Command + H
  4. Confirm that the block switches from visual mode to HTML mode.
  5. Press Shift + Option + Command + H
  6. Confirm that the block switches from HTML mode to visual mode.
  7. Select the 2 blocks, press Shift + Option + Command + H and confirm that nothing happens.
  8. Select a block that does not have the Edit as HTML option available, press Shift + Option + Command + H and confirm that nothing happens.

@sdnunca
sdnunca requested a review from ellatrix as a code owner November 9, 2022 22:02
@codesandbox

codesandbox Bot commented Nov 9, 2022

Copy link
Copy Markdown

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@github-actions

github-actions Bot commented Nov 9, 2022

Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @sdnunca! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 9, 2022
@sdnunca
sdnunca force-pushed the feature/add-keyboard-shortcut-switch-to-html-block-level branch from 8a43327 to 545fd11 Compare November 9, 2022 22:38
@noisysocks noisysocks added [Type] Enhancement A suggestion for improvement. [a11y] Keyboard & Focus labels Nov 9, 2022
@talldan

talldan commented Nov 9, 2022

Copy link
Copy Markdown
Contributor

I see a bug happening when testing:

  1. Add a paragraph with some text
  2. Use the shortcut to switch to HTML mode
  3. Edit the HTML (e.g. update the paragraph content)
  4. Use the shortcut to switch back to visual mode

Expected: Visual mode shows the changes made in HTML mode
Actual: The changes are not reflected in visual mode.

It might be related to the block only being updated onBlur or something (I seem to remember similar issues around that with the main editor's code editing mode). I'll have a look to see if I can find the responsible code.

@talldan

talldan commented Nov 9, 2022

Copy link
Copy Markdown
Contributor

Yep - it's here, as suspected it only updates on blur:

A simple fix might be to also make it update on unmount using a useEffect.

@sdnunca
sdnunca force-pushed the feature/add-keyboard-shortcut-switch-to-html-block-level branch from 545fd11 to c2351f3 Compare November 16, 2022 19:33
@sdnunca

sdnunca commented Nov 16, 2022

Copy link
Copy Markdown
Contributor Author

@talldan I've updated the PR to use the approach from https://github.com/WordPress/gutenberg/blob/trunk/packages/edit-site/src/components/code-editor/code-editor-text-area.js

Calling onChange on component unmount should fix the problem you mentioned.

Thank you so much for all your help.

@noisysocks noisysocks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Works well in my testing! Nice one. Just a couple of minor comments. Would be good to test the extra logic we've added to the selector.

It's interesting that BlockHTML has so many similarities to CodeEditorTextArea in @wordpress/edit-site (and assumedly the code editor in @wordpress/edit-post, too?). Would be nice to create a component for all three code editors to use that contains all of this finicky state wrangling.

I suppose that's out of scope for this PR though 😀

if ( ! clientId ) {
return;
}
const block = select.getBlocksByClientId( clientId )[ 0 ];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Minor nitpick to get rid of those unsightly [ 0 ]s 😛

Suggested change
const block = select.getBlocksByClientId( clientId )[ 0 ];
const [ block ] = select.getBlocksByClientId( clientId );

return;
}
const block = select.getBlocksByClientId( clientId )[ 0 ];
if ( ! hasBlockSupport( block.name, 'html', true ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it possible that block could be null here?

@@ -770,7 +770,21 @@ describe( 'actions', () => {
describe( 'toggleBlockMode', () => {
it( 'should return TOGGLE_BLOCK_MODE action', () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we add tests for the cases where either codeEditingEnabled or hasBlockSupport( ... ) is falsey?

select.getSettings().codeEditingEnabled ?? true;
if ( ! isCodeEditingEnabled ) {
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just noting that this logic will also prevent a block that doesn't support html etc. from being switched to visual mode. That is probably fine since assumedly the block supports and editor settings won't change after the app is initialised.

@priethor priethor added [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). and removed [a11y] Keyboard & Focus labels Jul 24, 2023

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants