Skip to content

Commit

Permalink
Rich Text: Remove updateContent function (#7620)
Browse files Browse the repository at this point in the history
* Rich Text: Remove updateContent function

* Testing: Update splitting test to ensure caret merge positioning

* Rich Text: Restore selection after setting content
  • Loading branch information
aduth authored Jul 3, 2018
1 parent b88141a commit 0b4eaa8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
29 changes: 15 additions & 14 deletions editor/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,21 +777,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 );
} );
}

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 @@ -821,7 +822,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 @@ -911,7 +912,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();
} );
} );

0 comments on commit 0b4eaa8

Please sign in to comment.