-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
RichText: own undo signalling #10650
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { fromPairs } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { rawShortcut } from '@wordpress/keycodes'; | ||
import { KeyboardShortcuts } from '@wordpress/components'; | ||
|
||
/** | ||
* Set of keyboard shortcuts handled internally by RichText. | ||
* | ||
* @type {Array} | ||
*/ | ||
const HANDLED_SHORTCUTS = [ | ||
rawShortcut.primary( 'z' ), | ||
rawShortcut.primaryShift( 'z' ), | ||
rawShortcut.primary( 'y' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary here, but if we're going to support Cmd+Y , we should probably do so consistently between RichText and the top-level handler. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're only removing the browser shortcut here, not adding any support. But maybe we should add support everywhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, in that case, is the line even necessary if we're not handling it anyways? |
||
]; | ||
|
||
/** | ||
* An instance of a KeyboardShortcuts element pre-bound for the handled | ||
* shortcuts. Since shortcuts never change, the element can be considered | ||
* static, and can be skipped in reconciliation. | ||
* | ||
* @type {WPElement} | ||
*/ | ||
const SHORTCUTS_ELEMENT = ( | ||
<KeyboardShortcuts | ||
bindGlobal | ||
shortcuts={ fromPairs( HANDLED_SHORTCUTS.map( ( shortcut ) => { | ||
return [ shortcut, ( event ) => event.preventDefault() ]; | ||
} ) ) } | ||
/> | ||
); | ||
|
||
/** | ||
* Component which registered keyboard event handlers to prevent default | ||
* behaviors for key combinations otherwise handled internally by RichText. | ||
* | ||
* @return {WPElement} WordPress element. | ||
*/ | ||
export const RemoveBrowserShortcuts = () => SHORTCUTS_ELEMENT; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,38 @@ import { | |
} from '../support/utils'; | ||
|
||
describe( 'undo', () => { | ||
beforeAll( async () => { | ||
beforeEach( async () => { | ||
await newPost(); | ||
} ); | ||
|
||
it( 'should undo typing after a pause', async () => { | ||
await clickBlockAppender(); | ||
|
||
await page.keyboard.type( 'before pause' ); | ||
await new Promise( ( resolve ) => setTimeout( resolve, 1000 ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wish we could emulate this without actually slowing our test run by one full second. |
||
await page.keyboard.type( ' after pause' ); | ||
|
||
expect( await getEditedPostContent() ).toMatchSnapshot(); | ||
|
||
await pressWithModifier( META_KEY, 'z' ); | ||
|
||
expect( await getEditedPostContent() ).toMatchSnapshot(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should ideally test caret position here too. Because it resets to the beginning of the field unexpectedly. Apparently this also occurs in the latest plugin release, so not strictly a regression. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's always been like this. We'll probably need to save selection to the editor state. Seems like material for a separate PR. |
||
} ); | ||
|
||
it( 'should undo typing after non input change', async () => { | ||
await clickBlockAppender(); | ||
|
||
await page.keyboard.type( 'before keyboard ' ); | ||
await pressWithModifier( META_KEY, 'b' ); | ||
await page.keyboard.type( 'after keyboard' ); | ||
|
||
expect( await getEditedPostContent() ).toMatchSnapshot(); | ||
|
||
await pressWithModifier( META_KEY, 'z' ); | ||
|
||
expect( await getEditedPostContent() ).toMatchSnapshot(); | ||
} ); | ||
|
||
it( 'Should undo to expected level intervals', async () => { | ||
await clickBlockAppender(); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why this change has been made.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We used to skip applying changes back to the live DOM, and so did we here. Since #11595 we no longer do that, but it hasn't been updated here. I've removed it here while revising the parameters for
this.onChange
.