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

Keyboard Shortcuts: Bind Cmd/Ctrl+S as save #2863

Merged
merged 1 commit into from
Feb 16, 2018

Conversation

aduth
Copy link
Member

@aduth aduth commented Oct 3, 2017

Related: #2179
Related: #71

In Progress: While this works, it could lead to unexpected results when adding a new paragraph with text and using the keyboard shortcut, since until #2758 is implemented the value of an Editable field is not synced with the post state until the field is blurred.

This pull request seeks to add support for saving the post via Ctrl/Cmd+S .

Open questions:

Currently, this uses the same behavior as autosave, which is to save a draft if the post is a draft. There is no autosave behavior yet for published posts (#1295, #1773), so Ctrl/Cmd+S will do nothing in these cases. We could bind Cmd+S to update the published post, but the implementation here follows that of the current post editor.

Testing instructions:

Verify that Ctrl/Cmd+S saves the current post if editing the current editor.

  1. Navigate to Gutenberg > New Post
  2. Insert a new paragraph
  3. Enter some text
  4. Press Ctrl/Cmd+S
  5. Note that the post is saved

@aduth aduth added the [Status] In Progress Tracking issues with work in progress label Oct 3, 2017
@codecov
Copy link

codecov bot commented Oct 3, 2017

Codecov Report

Merging #2863 into master will decrease coverage by 0.27%.
The diff coverage is 63.63%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2863      +/-   ##
==========================================
- Coverage   34.15%   33.88%   -0.28%     
==========================================
  Files         340      191     -149     
  Lines        9400     5704    -3696     
  Branches     1606      999     -607     
==========================================
- Hits         3211     1933    -1278     
+ Misses       5292     3191    -2101     
+ Partials      897      580     -317
Impacted Files Coverage Δ
editor/modes/visual-editor/index.js 0% <0%> (ø)
components/keyboard-shortcuts/index.js 90.9% <100%> (+5.19%) ⬆️
blocks/url-input/button.js 0% <0%> (-100%) ⬇️
components/panel/row.js 0% <0%> (-100%) ⬇️
blocks/block-controls/index.js 0% <0%> (-100%) ⬇️
blocks/color-palette/index.js 0% <0%> (-90.91%) ⬇️
blocks/alignment-toolbar/index.js 16.66% <0%> (-83.34%) ⬇️
blocks/library/more/index.js 30% <0%> (-60%) ⬇️
blocks/block-alignment-toolbar/index.js 33.33% <0%> (-55.56%) ⬇️
components/drop-zone/index.js 5.88% <0%> (-49.12%) ⬇️
... and 419 more

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 08cba69...155ac8f. Read the comment docs.

@@ -73,6 +81,11 @@ class VisualEditor extends Component {
event.preventDefault();
}

save( event ) {
Copy link
Member

Choose a reason for hiding this comment

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

They use a new pattern in Calypso to avoid binding in the constructor:

save = ( event ) => {
    ...
}

I think this is also how the official React codemod migrates React.createClass to class component.

Copy link
Member Author

Choose a reason for hiding this comment

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

The feature you're referring to (public class fields property initializers) is actually not part of the language specification, but rather a stage 2 proposal. We've tended to avoid the most language proposal features out of fear for churn, and the syntax will fail to build. With class fields specifically there's been some recent revisions to combine proposals for public and private fields and unified class features with decorators that gives me cause for concern (introducing uncertainty to their stability).

Copy link
Member

Choose a reason for hiding this comment

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

That makes a lot of sense in the context of WordPress development. I totally support this choice 👍 Thanks for taking your time to explain the rationale behind it :)

} } />
);

keyPress( 68, document );
Copy link
Member

Choose a reason for hiding this comment

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

It took me a few seconds to connect d char with 68 :)
Can we hide 68 behind the constant?

*/
import KeyboardShortcuts from '../';

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

Choose a reason for hiding this comment

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

I like this tests a lot. It helped me better understand changes introduced 👍

keyPress( 68, document );

expect( spy ).toHaveBeenCalled();
} );
Copy link
Member

@gziolo gziolo Oct 3, 2017

Choose a reason for hiding this comment

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

Does it mean that if non-global keypress is intercepted by a node, then we could have the following test pass:

it( 'should not capture local key events globally', () => {
 	const spy = jest.fn();
 	const attachNode = document.createElement( 'div' );
 	document.body.appendChild( attachNode );
  	const wrapper = mount(
 		<div>
 			<KeyboardShortcuts
 				shortcuts={ {
 					d: spy,
 				} } />
 			<textarea></textarea>
 		</div>,
 		{ attachTo: attachNode }
 	);
  	keyPress( 68, wrapper.find( 'textarea' ).getDOMNode() );
  	expect( spy ).not.toHaveBeenCalled();
 } );

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.

It works as described. It doesn't save changes until I change focus to a different block. Otherwise, it works perfectly fine. Should we merge it as it is and fix other issues as part of other related PRs?

@aduth
Copy link
Member Author

aduth commented Oct 4, 2017

This is a nice usability improvement, but I think I'd want to wait until after we resolve editor content not being kept in sync before moving to merge this, since as you note it can be confusing that Save is not available until moving focus.

@gziolo
Copy link
Member

gziolo commented Oct 5, 2017

Fair enough, it can wait until Save is not available until moving focus is fixed. Is there an open PR that tries to tackle it? It seems to be also very important in the context of Collaborative Editing which I'm exploring lately. At the moment content changes are synced after a user moves focus to another block, which isn't ideal.

@aduth
Copy link
Member Author

aduth commented Oct 5, 2017

Is there an open PR that tries to tackle it?

Issue tracked at #2758, with an in-progress effort at #2418 which includes throttling (I think we may want to avoid). There's some extra considerations noted in the issue, including "Undo" behaviors and whether we'd want to refactor shape of editable value before incurring change events so frequently.

@gziolo
Copy link
Member

gziolo commented Oct 6, 2017

Thanks @aduth for the update. I'd be interested to help you with the cross-linked issue.

@aduth
Copy link
Member Author

aduth commented Oct 6, 2017

@gziolo: @iseulde also expressed interest, if you'd want to collaborate on a solution.

@aduth aduth force-pushed the add/keyboard-shortcuts-save branch from 1db2e49 to 155ac8f Compare February 16, 2018 02:14
@aduth aduth added [Feature] Writing Flow Block selection, navigation, splitting, merging, deletion... and removed [Status] In Progress Tracking issues with work in progress labels Feb 16, 2018
@aduth
Copy link
Member Author

aduth commented Feb 16, 2018

This one has sat stale for a while, but now that #4955 is finally merged, I did a rebase to bring this back up to date. Feels nice with the immediate change-ing paragraph blocks!

cc @youknowriad

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.

This feels really nice

@ellatrix
Copy link
Member

Yaaay we have always-synced RichText instances!

@aduth aduth merged commit b7ecf95 into master Feb 16, 2018
@aduth aduth deleted the add/keyboard-shortcuts-save branch February 16, 2018 13:12
@mtias
Copy link
Member

mtias commented Feb 16, 2018

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Writing Flow Block selection, navigation, splitting, merging, deletion...
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants