Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/e2e-test-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ _Returns_

- `Promise`: Promise resolving with a string containing the block title.

<a name="getCurrentPostContent" href="#getCurrentPostContent">#</a> **getCurrentPostContent**

Returns a promise which resolves with the current post content (HTML string).

_Returns_

- `Promise`: Promise resolving with current post content markup.

<a name="getEditedPostContent" href="#getEditedPostContent">#</a> **getEditedPostContent**

Returns a promise which resolves with the edited post content (HTML string).
Expand Down
14 changes: 14 additions & 0 deletions packages/e2e-test-utils/src/get-current-post-content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Internal dependencies
*/
import { wpDataSelect } from './wp-data-select';

/**
* Returns a promise which resolves with the current post content (HTML string).
*
* @return {Promise} Promise resolving with current post content markup.
*/
export async function getCurrentPostContent() {
const post = await wpDataSelect( 'core/editor', 'getCurrentPost' );
return post.content;
}
1 change: 1 addition & 0 deletions packages/e2e-test-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export { getAllBlocks } from './get-all-blocks';
export { getAvailableBlockTransforms } from './get-available-block-transforms';
export { getBlockSetting } from './get-block-setting';
export { getEditedPostContent } from './get-edited-post-content';
export { getCurrentPostContent } from './get-current-post-content';
export { hasBlockSwitcher } from './has-block-switcher';
export { getPageError } from './get-page-error';
export {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Editing modes (visual/HTML) saves content when using the shortcut in the Code Editor 1`] = `
"<!-- wp:paragraph -->
<p>Hi world!</p>
<!-- /wp:paragraph -->"
`;
28 changes: 28 additions & 0 deletions packages/e2e-tests/specs/editor/various/editor-modes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
clickBlockAppender,
clickBlockToolbarButton,
createNewPost,
getCurrentPostContent,
switchEditorModeTo,
pressKeyTimes,
pressKeyWithModifier,
} from '@wordpress/e2e-test-utils';

describe( 'Editing modes (visual/HTML)', () => {
Expand Down Expand Up @@ -134,4 +137,29 @@ describe( 'Editing modes (visual/HTML)', () => {
);
expect( disabledInserter ).not.toBeNull();
} );

// Test for regressions of https://github.com/WordPress/gutenberg/issues/24054.
it( 'saves content when using the shortcut in the Code Editor', async () => {
await switchEditorModeTo( 'Code' );

const textContent = await page.evaluate(
() => document.querySelector( '.editor-post-text-editor' ).value
);
const editPosition = textContent.indexOf( 'Hello' );

// Replace the word 'Hello' with 'Hi'.
await page.click( '.editor-post-title__input' );
await page.keyboard.press( 'Tab' );
await pressKeyTimes( 'ArrowRight', editPosition );
await pressKeyTimes( 'Delete', 5 );
await page.keyboard.type( 'Hi' );

// Save the post using the shortcut.
await pressKeyWithModifier( 'primary', 's' );
await page.waitForSelector( '.editor-post-saved-state.is-saved' );

await switchEditorModeTo( 'Visual' );

expect( await getCurrentPostContent() ).toMatchSnapshot();
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
*/
import { useShortcut } from '@wordpress/keyboard-shortcuts';
import { useDispatch, useSelect } from '@wordpress/data';
import { parse } from '@wordpress/blocks';

function SaveShortcut() {
const { savePost } = useDispatch( 'core/editor' );
const isEditedPostDirty = useSelect(
( select ) => select( 'core/editor' ).isEditedPostDirty,
[]
);
function SaveShortcut( { resetBlocksOnSave } ) {
const { resetEditorBlocks, savePost } = useDispatch( 'core/editor' );
const { isEditedPostDirty, getPostEdits } = useSelect( ( select ) => {
const {
isEditedPostDirty: _isEditedPostDirty,
getPostEdits: _getPostEdits,
} = select( 'core/editor' );

return {
isEditedPostDirty: _isEditedPostDirty,
getPostEdits: _getPostEdits,
};
}, [] );

useShortcut(
'core/editor/save',
Expand All @@ -25,6 +33,21 @@ function SaveShortcut() {
return;
}

// The text editor requires that editor blocks are updated for a
// save to work correctly. Usually this happens when the textarea
// for the code editors blurs, but the shortcut can be used without
// blurring the textarea.
if ( resetBlocksOnSave ) {
const postEdits = getPostEdits();
if (
postEdits.content &&
typeof postEdits.content === 'string'
) {
const blocks = parse( postEdits.content );
resetEditorBlocks( blocks );
}
}

savePost();
},
{ bindGlobal: true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import SaveShortcut from './save-shortcut';

export default function TextEditorGlobalKeyboardShortcuts() {
return <SaveShortcut />;
return <SaveShortcut resetBlocksOnSave />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { useShortcut } from '@wordpress/keyboard-shortcuts';
import { useDispatch, useSelect } from '@wordpress/data';
import { useDispatch } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import { BlockEditorKeyboardShortcuts } from '@wordpress/block-editor';

Expand All @@ -12,11 +12,7 @@ import { BlockEditorKeyboardShortcuts } from '@wordpress/block-editor';
import SaveShortcut from './save-shortcut';

function VisualEditorGlobalKeyboardShortcuts() {
const { redo, undo, savePost } = useDispatch( 'core/editor' );
const isEditedPostDirty = useSelect(
( select ) => select( 'core/editor' ).isEditedPostDirty,
[]
);
const { redo, undo } = useDispatch( 'core/editor' );

useShortcut(
'core/editor/undo',
Expand All @@ -36,25 +32,6 @@ function VisualEditorGlobalKeyboardShortcuts() {
{ bindGlobal: true }
);

useShortcut(
'core/editor/save',
( event ) => {
event.preventDefault();

// TODO: This should be handled in the `savePost` effect in
// considering `isSaveable`. See note on `isEditedPostSaveable`
// selector about dirtiness and meta-boxes.
//
// See: `isEditedPostSaveable`
if ( ! isEditedPostDirty() ) {
return;
}

savePost();
},
{ bindGlobal: true }
);

return (
<>
<BlockEditorKeyboardShortcuts />
Expand Down