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

Navigation Link block: add RichText split-at-end/merge/remove behavior #21764

Merged
merged 1 commit into from
May 22, 2020

Conversation

ZebulanStanphill
Copy link
Member

@ZebulanStanphill ZebulanStanphill commented Apr 21, 2020

Description

Closes #21743.
Closes #18208.

This PR adds several RichText behavior enhancements to Navigation Link blocks. It introduces the following behaviors:

  • Removing an empty Navigation Link using Backspace or Delete.
  • Creating a new Navigation Link by pressing Enter at the end of an existing one.
  • Merging two adjacent Navigation Links by pressing Backspace or Delete. The labels are merged; all other attributes are kept as they are on the first block while those on the second are discarded. Children of the first block are kept, but children of the second block are lost. (No idea how to change this; ideally, I would want all the children to be moved under the first block.)

Notably, the ability to split an existing Navigation Link into two has been omitted, since this would cause a loss of all its children. I have no idea how to add this behavior and preserve children; I suspect it may not be possible to fix without improvements to the RichText APIs.

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.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@ZebulanStanphill ZebulanStanphill added [Type] Enhancement A suggestion for improvement. [Package] Block library /packages/block-library [Block] Navigation Affects the Navigation Block labels Apr 21, 2020
@github-actions
Copy link

github-actions bot commented Apr 21, 2020

Size Change: +140 B (0%)

Total Size: 1.12 MB

Filename Size Change
build/block-library/index.js 119 kB +140 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/annotations/index.js 3.62 kB 0 B
build/api-fetch/index.js 3.4 kB 0 B
build/autop/index.js 2.83 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.93 kB 0 B
build/block-directory/style-rtl.css 790 B 0 B
build/block-directory/style.css 791 B 0 B
build/block-editor/index.js 105 kB 0 B
build/block-editor/style-rtl.css 10.9 kB 0 B
build/block-editor/style.css 10.9 kB 0 B
build/block-library/editor-rtl.css 7.17 kB 0 B
build/block-library/editor.css 7.17 kB 0 B
build/block-library/style-rtl.css 7.48 kB 0 B
build/block-library/style.css 7.48 kB 0 B
build/block-library/theme-rtl.css 684 B 0 B
build/block-library/theme.css 686 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.1 kB 0 B
build/components/index.js 190 kB 0 B
build/components/style-rtl.css 17.1 kB 0 B
build/components/style.css 17.1 kB 0 B
build/compose/index.js 9.24 kB 0 B
build/core-data/index.js 11.4 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/data/index.js 8.42 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 771 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.11 kB 0 B
build/edit-navigation/index.js 6.63 kB 0 B
build/edit-navigation/style-rtl.css 857 B 0 B
build/edit-navigation/style.css 856 B 0 B
build/edit-post/index.js 302 kB 0 B
build/edit-post/style-rtl.css 12.2 kB 0 B
build/edit-post/style.css 12.2 kB 0 B
build/edit-site/index.js 13.2 kB 0 B
build/edit-site/style-rtl.css 5.45 kB 0 B
build/edit-site/style.css 5.45 kB 0 B
build/edit-widgets/index.js 7.73 kB 0 B
build/edit-widgets/style-rtl.css 4.59 kB 0 B
build/edit-widgets/style.css 4.59 kB 0 B
build/editor/editor-styles-rtl.css 425 B 0 B
build/editor/editor-styles.css 428 B 0 B
build/editor/index.js 44.6 kB 0 B
build/editor/style-rtl.css 5.06 kB 0 B
build/editor/style.css 5.06 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.64 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 711 B 0 B
build/keyboard-shortcuts/index.js 2.51 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.13 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/media-utils/index.js 5.29 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 14.8 kB 0 B
build/server-side-render/index.js 2.68 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.02 kB 0 B
build/viewport/index.js 1.84 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

@ZebulanStanphill ZebulanStanphill force-pushed the fix/21743 branch 2 times, most recently from 5e17fbc to 9884040 Compare April 21, 2020 18:34
@ZebulanStanphill
Copy link
Member Author

I thought this would be a simple fix, but I've run into an issue I didn't expect. After pressing backspace and removing the block, focus doesn't move to the previous Navigation Link like you would expect. No idea why. 😕

@talldan
Copy link
Contributor

talldan commented May 1, 2020

@ZebulanStanphill From what I can gather, onMerge might have to be specified as well. I had a look at how the replaceBlocks action gets called when deleting a paragraph or heading, and it seems like it's called from a dispatch of MERGE_BLOCKS.

@ZebulanStanphill ZebulanStanphill force-pushed the fix/21743 branch 4 times, most recently from 2002629 to bb599bb Compare May 1, 2020 18:20
@ZebulanStanphill
Copy link
Member Author

@talldan Thanks; I added merge functionality and now the cursor is moving to the previous Navigation Link (and also you can merge the text of two links now).

I'm not done yet, though. Removing/merging causes error in the JS console which reads:

RichText needs an identifier prop that is the block attribute key of the attribute it controls. Its type is expected to be a string, but was number

image

I'm not really sure what this means or how to fix it.

Also, the cursor is indeed moved to the previous block, but it's put at the beginning of the text, rather than the end.

Finally, if a Navigation Link has children, the children are deleted when merging into the previous Navigation Link. Is there any way to transfer them to the other block?

@talldan
Copy link
Contributor

talldan commented May 7, 2020

I'm not done yet, though. Removing/merging causes error in the JS console which reads:

This seems to be an implementation detail of the annotations system, which I'm not sure I've ever really looked at, so I don't have more information unfortunately! Looking at other blocks like heading, the identifier prop seems to be defined as a string (e.g. "content", which is the same as the value attribute name, so maybe try "label".)

Also, the cursor is indeed moved to the previous block, but it's put at the beginning of the text, rather than the end.

Yeah, that's curious. I'll try to have a look into it at some point

Finally, if a Navigation Link has children, the children are deleted when merging into the previous Navigation Link. Is there any way to transfer them to the other block?

I suppose it's a new behavior to have a block that can be deleted in this way that might have inner blocks. I think it's ok to try out like it is initially, but we could maybe flag it for design feedback, either on this PR or as a separate issue.

@ZebulanStanphill ZebulanStanphill added the Needs Design Feedback Needs general design feedback. label May 7, 2020
@ZebulanStanphill
Copy link
Member Author

This seems to be an implementation detail of the annotations system, which I'm not sure I've ever really looked at, so I don't have more information unfortunately! Looking at other blocks like heading, the identifier prop seems to be defined as a string (e.g. "content", which is the same as the value attribute name, so maybe try "label".)

So how do I add the identifier prop? I can't seem to find any documentation about it.

@ZebulanStanphill
Copy link
Member Author

I had hoped that a rebase and adjustment after #22292 would fix some of the issues in this branch, but I'm still running into all of the same issues as before.

@ZebulanStanphill ZebulanStanphill removed the Needs Design Feedback Needs general design feedback. label May 13, 2020
@ZebulanStanphill
Copy link
Member Author

Well, it turns out that all I had to do to fix the JS console errors was literally just add identifier="label" to the RichText. For some reason it never occurred to me that the identifier prop was literally just called "indentifier", and I somehow missed it when looking at other blocks.

Merging/removing is now working as expected.

There is still one minor issue: removing a Navigation Link nested inside another Navigation Link doesn't focus the RichText of the parent like I would expect. Actually, it's not really clear where exactly the focus is. You have to tab or shift tab to get back to the standard tabbable elements. This also happens when backspace-removing a text block in a Group, so the issue isn't isolated to the Navigation block; because of that, I don't think it's a blocker for this issue.

I think this PR is now ready for review.

@ZebulanStanphill ZebulanStanphill marked this pull request as ready for review May 13, 2020 04:28
@ZebulanStanphill ZebulanStanphill force-pushed the fix/21743 branch 2 times, most recently from 9fcd405 to 54f8900 Compare May 21, 2020 00:30
@ZebulanStanphill ZebulanStanphill force-pushed the fix/21743 branch 2 times, most recently from f354ba9 to bface7b Compare May 21, 2020 03:25
@ZebulanStanphill ZebulanStanphill changed the title Enable backspace to remove empty Navigation Links. Navigation Link block: add RichText split/merge/remove behavior May 21, 2020
@ZebulanStanphill ZebulanStanphill added the [Feature] Writing Flow Block selection, navigation, splitting, merging, deletion... label May 21, 2020
@ZebulanStanphill ZebulanStanphill force-pushed the fix/21743 branch 3 times, most recently from 28de2ef to 132d1c2 Compare May 21, 2020 11:48
@ZebulanStanphill ZebulanStanphill changed the title Navigation Link block: add RichText split/merge/remove behavior Navigation Link block: add RichText split-at-end/merge/remove behavior May 21, 2020
@ZebulanStanphill
Copy link
Member Author

I updated this PR, using a similar approach as #22436. It's now working much better. I've updated the PR title and description to better describe all the behavior this PR adds.

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.

Thanks for persisting on this @ZebulanStanphill! Looks like the PR now also fixes #18208, so I've updated the description.

If we can fix the naming I'd be very happy to approve.

Comment on lines 23 to 28
merge( a, { label: bLabel = '' } ) {
return {
...a,
label: a.label + bLabel,
};
},
Copy link
Contributor

Choose a reason for hiding this comment

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

I see the variable names are used in the other PR, but it's against the coding standards and it should probably have been caught during review of that:
https://make.wordpress.org/core/handbook/best-practices/coding-standards/javascript/#naming-conventions

Variable and function names should be full words, using camel case with a lowercase first letter.

I'm guessing here that the object is an attributes object.

I've seen left and right (e.g. leftAttributes, rightAttributes) used to distinguish before with similar operations, like when sorting, which I think makes sense here as there's a left block and a right block being merged.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks; I've revised the variable names.

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.

🎉 Thanks @ZebulanStanphill!

@talldan talldan merged commit 33bace5 into master May 22, 2020
@talldan talldan deleted the fix/21743 branch May 22, 2020 05:59
@github-actions github-actions bot added this to the Gutenberg 8.2 milestone May 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block [Feature] Writing Flow Block selection, navigation, splitting, merging, deletion... [Package] Block library /packages/block-library [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deleting navigation links could be easier Navigation Block: Enter key shouldn't add line break in menu item
2 participants