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

Try: Add toggle editor mode shortcut #3755

Merged

Conversation

vladanost
Copy link
Contributor

Description

I find switching often from Visual to Code editor mode tiring process so I wanted to add a shortcut for that.

All editor related keyboard shortcuts are grouped under EditorGlobalKeyboardShortcuts which is a great move but for this case it causes problems. EditorGlobalKeyboardShortcuts is rendered from VisualEditor block and if I add the shortcut there, once it switches to TextEditor, shortcuts are no longer available.

My temp approach is to add another component EditorModeKeyboardShortcuts and render it from Layout component.
For the moment it works but I'm wondering:

  1. is this shortcut really needed for everyone or I'm the only one that find it very useful when developing blocks?
  2. Is this the right way to include such a functionality?
  3. I chose the shortcut to be mod+shift+alt+m, maybe better solution can be found.
  4. The name of the component EditorModeKeyboardShortcuts is making a confusion?

All suggestions are welcome. Thanks.

How Has This Been Tested?

npm run test-unit
Manually tested in the browser.

Types of changes

New feature (non-breaking change which adds functionality)

@codecov
Copy link

codecov bot commented Dec 1, 2017

Codecov Report

Merging #3755 into master will decrease coverage by 0.11%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3755      +/-   ##
==========================================
- Coverage   39.08%   38.97%   -0.12%     
==========================================
  Files         290      292       +2     
  Lines        6967     6987      +20     
  Branches     1273     1280       +7     
==========================================
  Hits         2723     2723              
- Misses       3570     3583      +13     
- Partials      674      681       +7
Impacted Files Coverage Δ
components/menu-items/menu-items-group.js 0% <ø> (ø) ⬆️
components/menu-items/menu-items-toggle.js 0% <ø> (ø) ⬆️
components/menu-items/menu-items-shortcut.js 0% <0%> (ø)
editor/edit-post/header/mode-switcher/index.js 0% <0%> (ø) ⬆️
editor/edit-post/layout/index.js 0% <0%> (ø) ⬆️
editor/edit-post/modes/keyboard-shortcuts/index.js 0% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c97ebf8...915d1ec. Read the comment docs.

@youknowriad
Copy link
Contributor

Thanks for the PR, it's a good one, I can't speak for the chosen shortcut but it makes sense to me. Also, I recall mockups showing these shortcuts next to the button in the ellipsis menu for discoverability (cc @jasmussen).

@@ -0,0 +1,54 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

The editor/components folder is only for reusable editor components without "layout" information. I think the "mode" is a layout information because it only indicates which mode is "visible". Can we move this component to the editor/edit-post folder instead?

@vladanost
Copy link
Contributor Author

@youknowriad Thanks. I moved the code to edit-post/modes/keyboard-shortcuts, will that work?
As for the shortcuts in the ellipsis menu I'm not sure where to put it since this shortcut toggles Visual/Code editor, it seems awkward to print it twice next to both names?

@afercia
Copy link
Contributor

afercia commented Dec 18, 2017

I'm not sure where to put it since this shortcut toggles Visual/Code editor, it seems awkward to print it twice next to both names?

I'd agree it wouldn't make much sense since tehre are 2 separate controls:

screen shot 2017-12-18 at 10 54 25

Hm the only way I can think of is merging the two controls in just one "dynamic" control:

  • in Visual mode the label would say "Switch to Code Editor"
  • in Code mode the label would say "Switch to Visual Editor"

However, I've heard some discussions about allowing plugins to add additional "modes" so I'm not sure the current UI would be so ideal in the first place.

@youknowriad
Copy link
Contributor

youknowriad commented Dec 18, 2017

@afercia @vladanost @jasmussen We could show it next to the "Editor" label, can't we?

@jasmussen
Copy link
Contributor

It's true that the editor mode switch was designed to be able to scale to more types, but probably this shouldn't keep us from making improvements right now.

It seems the feedback centers around having a single keyboard shortcut that would cycle between modes. So you'd invoke the shortcut to go to HTML mode, and the same shorcut would take you back.

Combined with #2980, we'd want to show that keyboard shortcut in the menu, right aligned.

Given the ellipsis menu for selecting the editing mode right now behaves like a radio group, it feels weird to show the same shortcut next to both items, even one that is active and unselectable. So can we just show the shortcut next to the item that's not selected? I.e. this:

✓ Visual Editor
  Code Editor   [⌘+Shift+Alt+M]

and subsequently ...

  Visual Editor [⌘+Shift+Alt+M]
✓ Code Editor

Would that work? It seems like the obvious thing to try.

The next step would be to make the item dynamic, like Andrea suggests.

@vladanost
Copy link
Contributor Author

@youknowriad @jasmussen @afercia I tried to display the shortcuts by adding MenuItemsShortcut component.
Also added a separate map for shortcuts.
This is what I have at this stage:
mode-switcher

@jasmussen
Copy link
Contributor

Really nice work. That looks good.

I'd remove the [] brackets, those were just for the ASCII mockup ;) — so long as the shortcut is right aligned. In the future we might need to either change the font size and/or dropdown width, to make room for long shortcuts. But nothing to worry about in this PR. Very nice work.

@afercia
Copy link
Contributor

afercia commented Dec 22, 2017

Really nice. A few questions/considerations:

  • will work on Windows/Linux and other OSes?
  • contrast is too low, especially against the gray background:

screen shot 2017-12-22 at 10 36 22

On a white background, the lightest gray that can be used is $dark-gray-300: #6c7781;. On the gray background, it should be a bit darker to have a contrast ratio of at least 4.5:1

  • can we avoid opacity? it makes calculating the actual contrast a bit hard. If necessary, I guess we can introduce a new gray in the color palette /cc @jasmussen

I chose the shortcut to be mod+shift+alt+m, maybe better solution can be found.

Yep, I think at some point all the shortcuts would need a final review to check for potential conflicts, I guess we can keep it as is for now!

@afercia
Copy link
Contributor

afercia commented Dec 22, 2017

Wondering if the + is really necessary? In macOS, the keys are separated with a small space. It's not just a visual thing: VoiceOver announces the plus and I guess other screen readers do the same (can't test on Windows right now). Removing it could help making things a bit more clear.

screen shot 2017-12-22 at 10 54 07

@@ -0,0 +1,3 @@
export default {
toggle_editor_mode: '⌘+Shift+Alt+M',
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it a good idea to keep "editor"'s specific shortcuts in a generic components module? why can't we avoid this map entirely and just pass the shortcut?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I can change it to use the shortcut directly. My thinking was to have a separate list in case it should be used elsewhere (like complete list of shortcuts).

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough! let's move the list to the editor/edit-post folder then and retrieve passing the mapped value directly to the generic component.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this map could also hold the actual value used to trigger the shortcut mod+shift+alt+m. So we'll have a value and a label per shortcut.

@@ -31,10 +31,17 @@ const MODES = [
];

function ModeSwitcher( { onSwitch, mode } ) {
const filteredModes = MODES.map( MODE => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shoud we use mode instead of MODE? It's not a module's constant.

Copy link
Contributor Author

@vladanost vladanost Dec 22, 2017

Choose a reason for hiding this comment

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

Right. I will change it to lower case. I used uppercase to differentiate it from mode that is passed as argument.

Copy link
Contributor

Choose a reason for hiding this comment

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

mmm right, maybe choice or something else :)

@@ -0,0 +1,6 @@
export default {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the changes 👍

If this map is supposed to grow over time and contain all edit-post shortcuts, should we move it to editor/edito-post/keyboard-shortcuts.js instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, probably :) I was confused, since I already have modes/keyboard-shortcuts folder and didn't want to create clutter. I will move them there.

@@ -0,0 +1,6 @@
export default {
toggle_editor_mode: {
Copy link
Contributor

Choose a reason for hiding this comment

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

What if instead of exporting an object, we export several constants. This one could be

const TOGGLE_EDITOR_MODE = { label, value } ?

Alternatively, we should use camelCase for keys to be able to reference them like this shortcuts.toggleEditorMode.label.

render() {
return (
<KeyboardShortcuts shortcuts={ {
'mod+shift+alt+m': this.toggleMode,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we use the newly introduce shortcut value here

export default {
toggleEditorMode: {
value: 'mod+shift+alt+m',
label: '⌘+Shift+Alt+M',
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: I wonder if we should detect the platform and replace with ctrl if not on MacOS.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it reliable to use navigator.platform? I can't find any example in the Gutenberg code.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is but that's the first time we need it :)

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

LGTM 👍 Can you rebase your PR?

return {
switchMode: ( mode ) => {
dispatch( {
type: 'SWITCH_MODE', mode: mode,
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: We could extract the SWITCH_MODE action into an action creator in editor/store/actions.js to factorize its usage.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, you can replace mode: mode here by just mode

@youknowriad
Copy link
Contributor

Also, I'm not able to merge can you rebase? git fetch origin && git rebase origin/master?

@youknowriad youknowriad force-pushed the try/toggle-editor-mode-shortcut branch from a5a1d98 to 79ebbfd Compare December 25, 2017 12:26
@@ -0,0 +1,54 @@
/**
Copy link
Member

Choose a reason for hiding this comment

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

While I'd like for us to flatten and eliminate the modes directory altogether, I don't particularly like the creation of a new folder here, because as it was prior to these changes, the modes directory contains exclusively the supported editing modes for the editor: visual and text. The shortcuts are not in themselves a mode.

@ellatrix
Copy link
Member

Hi @vladanost @youknowriad

I was wondering if we could change this shortcut to fit in with the rest of WP and this editor. We have chosen Ctrl+Alt (Mac) and Shift+Alt (Windows) in the past because it can be entirely used by us without running into issues with browsers and operating systems. Also known as the access combination (we even got TinyMCE to move to that). Could we figure out a good combination for the mode switcher in this space? Any reason cmd/ctrl+shift+alt+m was chosen?

I think we should also build the API around that, so plugins can also only extend in this combination space.

Additionally, I can't figure out how to get the combination to work in master (Mac).

@ellatrix ellatrix mentioned this pull request Jan 17, 2018
3 tasks
@vladanost
Copy link
Contributor Author

Hi @iseulde
I'm currently on Linux and the shortcut combination works in master 2.0.
No particular reason for that combination, it should be revised.

@ellatrix
Copy link
Member

@vladanost It only seems to work if the pop up is open. If it's closed, it doesn't.

@vladanost
Copy link
Contributor Author

@iseulde On Linux, it works regardless of the popup being open. I will make a test later today on Mac. (I remember it worked there too)

@vladanost
Copy link
Contributor Author

@iseulde

I think we should also build the API around that, so plugins can also only extend in this combination space.

Sound good.

@vladanost
Copy link
Contributor Author

@iseulde It works for me on Mac too.

@ellatrix ellatrix mentioned this pull request May 2, 2018
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants