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

Update heading block on ENTER key to create empty paragraph instead of heading #10382

Conversation

andrewserong
Copy link
Contributor

@andrewserong andrewserong commented Oct 7, 2018

Resolves #10326

Description

This PR tweaks the behaviour for the heading block when a user presses ENTER at the beginning of a heading (as mentioned in #10326).

This allows the ENTER key pressed at the beginning of a heading to create an empty paragraph above it. Also, when the ENTER key is pressed at the end of a heading, an empty paragraph block is created below, instead of another heading block.

I realise this could be a matter of personal preference, but to me this feels like a more natural behaviour when writing long-form prose, where the default block would seem to be the paragraph, and I assume a user creating headings would want to switch back to a paragraph block instead of creating multiple heading blocks.

How has this been tested?

Unit tests and linting is passing locally. Tested in Chrome 69 and Safari 11.1.1 on macOS 10.13.5.

Screenshots

gutenberg-heading-enter

Types of changes

  • Extending the logic of the onSplit method in the heading component.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@chrisvanpatten chrisvanpatten added [Feature] Blocks Overall functionality of blocks [Feature] Writing Flow Block selection, navigation, splitting, merging, deletion... labels Oct 8, 2018
@gziolo gziolo requested review from mcsf and ellatrix February 1, 2019 09:22
@gziolo gziolo added this to the 5.1 (Gutenberg) milestone Feb 7, 2019
@ellatrix
Copy link
Member

ellatrix commented Feb 7, 2019

Worth noting that this bug is also addressed in #11005, but it's cool if I can be done without it.

@gziolo
Copy link
Member

gziolo commented Feb 8, 2019

@andrewserong do you plan to continue working on this PR? It looks like the changes which @iseulde referenced are much broader but you would have to validate yourself how much overlap there is.

@andrewserong
Copy link
Contributor Author

@gziolo apologies for the late reply — I'll take a look over this PR again later on today. I've just rebased master into the branch (since the PR is pretty old now), and will compare against the PR @iseulde mentioned.

@andrewserong
Copy link
Contributor Author

@gziolo and @iseulde — I've fixed up this PR, updating the order of onReplace and insertBlocksAfter (looks like the behaviour of onReplace might have changed slightly since I initially submitted the PR).

Looks like the tests are passing now, but I'm also aware that I haven't added e2e tests for this specific behaviour. It looks like particular blocks aren't targeted in the existing e2e tests, so I wasn't sure if behaviour specific to the heading block should be covered or not?

The other consideration is that this PR works with the current unstableOnSplit method, but will be superseded by the changes @iseulde has made to onSplit. If you'd like a quick fix for this bug, I'm happy to do any further changes you might like on this PR, otherwise, also happy for you to close it and wait for the revised onSplit prop in #11005.

@ellatrix
Copy link
Member

I think it's ok to add the fix, but we should definitely add e2e tests. :)

… instead of heading (WordPress#10326)

The onSplit method for the heading block will now replace an empty heading block with an empty paragraph block for the 'before' content.

This allows the ENTER key pressed at the beginning of a heading to create an empty paragraph above it. Also, when the ENTER key is pressed at the end of a heading, an empty paragraph block is created below, instead of another heading block. The RichText component now also calls event.stopImmediatePropagation() before splitContent() to prevent TinyMCE from throwing an error. This avoids a similar error as in WordPress#4580.
Since my last commit, the behaviour of onReplace seems to have changed a little bit. This commit ensures that an empty paragraph is added before a heading if a heading is 'split' with no before text, but with some after text (e.g. if the carat is at the beginning of a heading and the user presses ENTER)

Also removed unnecessary `event.stopImmediatePropagation()`.
@andrewserong andrewserong force-pushed the update/heading-block-switch-to-paragraph-on-enter branch from e512f16 to ca48320 Compare February 13, 2019 12:57
@andrewserong
Copy link
Contributor Author

Thanks @iseulde! — I've added e2e tests now to cover the initial bug report in #10326, so the issue should be adequately covered when this fix is superseded by your revised onSplit prop. cc: @gziolo

This ensures that if a user types a heading, then moves the caret to the beginning of the line and presses ENTER, that a paragraph block is created above. Also ensures that when a user presses ENTER at the end of a heading, a paragraph block is created below.
@andrewserong andrewserong force-pushed the update/heading-block-switch-to-paragraph-on-enter branch from ca48320 to 69fc7cc Compare February 13, 2019 13:36
@gziolo
Copy link
Member

gziolo commented Feb 13, 2019

Nice addition with tests. Let’s merge it as is after some testing in that case 👍

@andrewserong andrewserong force-pushed the update/heading-block-switch-to-paragraph-on-enter branch from 8cc648a to 1c398b6 Compare February 13, 2019 21:30
@andrewserong
Copy link
Contributor Author

Thanks for the feedback @iseulde! I've moved the tests up to the heading block tests, and removed the extra active class check.

@ellatrix
Copy link
Member

Something strange happens when merging back:

  • Put the caret in front of the heading text.
  • Press enter. An empty paragraph is inserted before the heading block. The caret stays in place. All good.
  • Now press backspace to merge the blocks.
    • Expected: remove the empty paragraph block and the caret should stay in place.
    • Actual: removes the paragraph block, but also transforms the heading block into a paragraph block and the caret is put at the end...

@ellatrix
Copy link
Member

I could live with it being transformed to a paragraph block, but the caret moving to the end is very unexpected. This doesn't happen in master.

@@ -5,6 +5,8 @@ import {
clickBlockAppender,
getEditedPostContent,
createNewPost,
insertBlock,
pressKeyTimes,
} from '@wordpress/e2e-test-utils';

describe( 'Separator', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Not introduced in this PR, but this title is not correct. 😅

@andrewserong
Copy link
Contributor Author

I could live with it being transformed to a paragraph block, but the caret moving to the end is very unexpected. This doesn't happen in master.

Oh, thanks for spotting that! Yeah, that's not meant to happen. I'll take another look and see if I can work out what's going on...

@andrewserong
Copy link
Contributor Author

I'm a little bit stumped by this issue — it feels to me like it's got to do with how onReplace behaves. I've run out of time for working on it tonight, but I'll take another look over the weekend.

@andrewserong
Copy link
Contributor Author

I've done a bit more investigating, and I think there might be an issue that exists on Master that affects the behaviour here. If we have an empty paragraph block that already exists, and press backspace at the beginning of a heading block, the paragraph block above is removed, and the caret sits at the beginning of the heading as we would expect. It looks like this issue only occurs on paragraph blocks that have been created above by the heading block when it's split. So it looks like there's something different about the block that was 'created' by the heading block onSplit than a standard paragraph block — even though the code editor view of the block remains the same.

The issue that I noticed on Master occurs if you create a heading, then move the caret to the beginning of the line, and then press ENTER. The heading moves down to a paragraph block as we would expect, but the empty heading block now has two <h2> tags in it, which isn't usually noticeable, but if you're using the 2019 theme, you can see two horizontal lines in the Heading block where there should be just one:

image

My hunch at the moment is that whatever it is about this block that gets created from the Heading block when a user hits ENTER is causing it to not do what we expect it to when we want to merge those blocks again. If that makes sense!

Since this issue is affecting this PR, I'm happy to keep investigating when I have time, but I don't want to hold anything up if there's a desire to get this one merged in. Let me know, and I'm happy to open a new issue for this.

@ellatrix
Copy link
Member

@andrewserong Thank you for the work you've done here! I've refactored selection state in #14640 and now #14765 will revise the onSpit prop entirely and fix this issue as well, so I'm going to close this PR.

@ellatrix ellatrix closed this Apr 19, 2019
@andrewserong
Copy link
Contributor Author

@ellatrix That's great to hear, thanks for letting me know!

@andrewserong andrewserong deleted the update/heading-block-switch-to-paragraph-on-enter branch April 20, 2019 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Blocks Overall functionality of blocks [Feature] Writing Flow Block selection, navigation, splitting, merging, deletion...
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pressing enter at the start of a heading block should push it down and create a paragraph above.
5 participants