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

Rich Text: Remove updateContent function #7620

Merged
merged 3 commits into from
Jul 3, 2018
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
29 changes: 15 additions & 14 deletions editor/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,21 +695,22 @@ export class RichText extends Component {
}
}

updateContent() {
// Do not trigger a change event coming from the TinyMCE undo manager.
// Our global state is already up-to-date.
this.editor.undoManager.ignore( () => {
const bookmark = this.editor.selection.getBookmark( 2, true );

this.savedContent = this.props.value;
this.setContent( this.savedContent );
this.editor.selection.moveToBookmark( bookmark );
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Dropping the focus management breaks the "merge". The focus is not restored at the right position when merging two paragraphs.

  • I believe the undo behavior is also necessary in this case or in cases the value of a RichText is updated programmatically (the change is not triggered by the RichText element itself)

Copy link
Member Author

Choose a reason for hiding this comment

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

But all tests have passed successfully!

😉

Copy link
Contributor

@youknowriad youknowriad Jun 29, 2018

Choose a reason for hiding this comment

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

hahaha :) we definitely need a test for that. I guess we only test the content and not the cursor position.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a failing test in dd0e347.

} );
}

setContent( content ) {
const { format } = this.props;

// If editor has focus while content is being set, save the selection
// and restore caret position after content is set.
let bookmark;
if ( this.editor.hasFocus() ) {
bookmark = this.editor.selection.getBookmark( 2, true );
}

this.savedContent = content;
this.editor.setContent( valueToString( content, format ) );

if ( bookmark ) {
this.editor.selection.moveToBookmark( bookmark );
}
}

getContent() {
Expand Down Expand Up @@ -739,7 +740,7 @@ export class RichText extends Component {
! isEqual( this.props.value, prevProps.value ) &&
! isEqual( this.props.value, this.savedContent )
) {
this.updateContent();
this.setContent( this.props.value );
}

if ( 'development' === process.env.NODE_ENV ) {
Expand Down Expand Up @@ -829,7 +830,7 @@ export class RichText extends Component {
* @param {?Array} blocks blocks to insert at the split position
*/
restoreContentAndSplit( before, after, blocks = [] ) {
this.updateContent();
this.setContent( this.props.value );
this.props.onSplit( before, after, ...blocks );
}

Expand Down
12 changes: 11 additions & 1 deletion test/e2e/specs/__snapshots__/splitting-merging.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ exports[`splitting and merging blocks Should split and merge paragraph blocks us

exports[`splitting and merging blocks Should split and merge paragraph blocks using Enter and Backspace 2`] = `
"<!-- wp:paragraph -->
<p>FirstSecond</p>
<p>FirstBetweenSecond</p>
<!-- /wp:paragraph -->"
`;

exports[`splitting and merging blocks Should split and merge paragraph blocks using Enter and Backspace 3`] = `
"<!-- wp:paragraph -->
<p><strong>First</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>BeforeSecond:Second</p>
<!-- /wp:paragraph -->"
`;
64 changes: 37 additions & 27 deletions test/e2e/specs/splitting-merging.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
* Internal dependencies
*/
import '../support/bootstrap';
import { newPost, newDesktopBrowserPage, insertBlock } from '../support/utils';
import {
newPost,
newDesktopBrowserPage,
insertBlock,
getHTMLFromCodeEditor,
pressTimes,
pressWithModifier,
} from '../support/utils';

describe( 'splitting and merging blocks', () => {
beforeAll( async () => {
Expand All @@ -11,42 +18,45 @@ describe( 'splitting and merging blocks', () => {
} );

it( 'Should split and merge paragraph blocks using Enter and Backspace', async () => {
//Use regular inserter to add paragraph block and text
// Use regular inserter to add paragraph block and text
await insertBlock( 'Paragraph' );
await page.keyboard.type( 'FirstSecond' );

//Move caret between 'First' and 'Second' and press Enter to split paragraph blocks
for ( let i = 0; i < 6; i++ ) {
await page.keyboard.press( 'ArrowLeft' );
}
// Move caret between 'First' and 'Second' and press Enter to split
// paragraph blocks
await pressTimes( 'ArrowLeft', 6 );
await page.keyboard.press( 'Enter' );

//Switch to Code Editor to check HTML output
await page.click( '.edit-post-more-menu [aria-label="More"]' );
let codeEditorButton = ( await page.$x( '//button[contains(text(), \'Code Editor\')]' ) )[ 0 ];
await codeEditorButton.click( 'button' );
// Assert that there are now two paragraph blocks with correct content
expect( await getHTMLFromCodeEditor() ).toMatchSnapshot();

//Assert that there are now two paragraph blocks with correct content
let textEditorContent = await page.$eval( '.editor-post-text-editor', ( element ) => element.value );
expect( textEditorContent ).toMatchSnapshot();

//Switch to Visual Editor to continue testing
await page.click( '.edit-post-more-menu [aria-label="More"]' );
const visualEditorButton = ( await page.$x( '//button[contains(text(), \'Visual Editor\')]' ) )[ 0 ];
await visualEditorButton.click( 'button' );

//Press Backspace to merge paragraph blocks
// Press Backspace to merge paragraph blocks
await page.click( '.is-selected' );
await page.keyboard.press( 'Home' );
await page.keyboard.press( 'Backspace' );

//Switch to Code Editor to check HTML output
await page.click( '.edit-post-more-menu [aria-label="More"]' );
codeEditorButton = ( await page.$x( '//button[contains(text(), \'Code Editor\')]' ) )[ 0 ];
await codeEditorButton.click( 'button' );
// Ensure that caret position is correctly placed at the between point.
await page.keyboard.type( 'Between' );
expect( await getHTMLFromCodeEditor() ).toMatchSnapshot();
// Workaround: When transitioning back from Code to Visual, the caret
// is placed at the beginning of the selected paragraph. Ideally this
// should persist selection between modes.
await pressTimes( 'ArrowRight', 5 ); // After "First"
await pressTimes( 'Delete', 7 ); // Delete "Between"

// Edge case: Without ensuring that the editor still has focus when
// restoring a bookmark, the caret may be inadvertently moved back to
// an inline boundary after a split occurs.
await page.keyboard.press( 'Home' );
await page.keyboard.down( 'Shift' );
await pressTimes( 'ArrowRight', 5 );
await page.keyboard.up( 'Shift' );
await pressWithModifier( 'mod', 'b' );
// Collapse selection, still within inline boundary.
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'BeforeSecond:' );

//Assert that there is now one paragraph with correct content
textEditorContent = await page.$eval( '.editor-post-text-editor', ( element ) => element.value );
expect( textEditorContent ).toMatchSnapshot();
expect( await getHTMLFromCodeEditor() ).toMatchSnapshot();
} );
} );
32 changes: 32 additions & 0 deletions test/e2e/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import { join } from 'path';
import { URL } from 'url';

/**
* External dependencies
*/
import { times } from 'lodash';

const {
WP_BASE_URL = 'http://localhost:8888',
WP_USERNAME = 'admin',
Expand All @@ -26,6 +31,21 @@ const MOD_KEY = process.platform === 'darwin' ? 'Meta' : 'Control';
*/
const REGEXP_ZWSP = /[\u200B\u200C\u200D\uFEFF]/;

/**
* Given an array of functions, each returning a promise, performs all
* promises in sequence (waterfall) order.
*
* @param {Function[]} sequence Array of promise creators.
*
* @return {Promise} Promise resolving once all in the sequence complete.
*/
async function promiseSequence( sequence ) {
return sequence.reduce(
( current, next ) => current.then( next ),
Promise.resolve()
);
}

export function getUrl( WPPath, query = '' ) {
const url = new URL( WP_BASE_URL );

Expand Down Expand Up @@ -232,6 +252,18 @@ export async function openDocumentSettingsSidebar() {
}
}

/**
* Presses the given keyboard key a number of times in sequence.
*
* @param {string} key Key to press.
* @param {number} count Number of times to press.
*
* @return {Promise} Promise resolving when key presses complete.
*/
export async function pressTimes( key, count ) {
return promiseSequence( times( count, () => () => page.keyboard.press( key ) ) );
}

export async function clearLocalStorage() {
await page.evaluate( () => window.localStorage.clear() );
}