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

Keep the cursor position in the Classic Block #12393

Closed
wants to merge 2 commits into from
Closed
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
33 changes: 29 additions & 4 deletions packages/block-library/src/classic/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,20 @@ export default class ClassicEdit extends Component {
}

componentDidUpdate( prevProps ) {
const { clientId, attributes: { content } } = this.props;

const { clientId, attributes } = this.props;
const editor = window.tinymce.get( `editor-${ clientId }` );
let { contentRaw } = attributes;
Copy link
Contributor

Choose a reason for hiding this comment

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

contentRaw is not persisted, we should probably use "local state" for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, sure, that'd work even better :)


// Strip white space before comparing to avoid resetting the editor content when not necessary;
Copy link
Member

Choose a reason for hiding this comment

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

What kind of whitespace is different?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only differences I'm seeing are in some <p>\u00a0</p> and some ; missing/being added to ends of inline style attributes.

contentRaw = contentRaw || '';
const newContent = contentRaw.replace( /\s+/g, '' );

let oldContent = prevProps.attributes.contentRaw || '';
oldContent = oldContent.replace( /\s+/g, '' );

if ( prevProps.attributes.content !== content ) {
editor.setContent( content || '' );
if ( oldContent !== newContent ) {
// Use the "raw" editor content as it contains the caret position bookmark.
editor.setContent( contentRaw, { format: 'raw' } );
}
}

Expand Down Expand Up @@ -84,12 +92,29 @@ export default class ClassicEdit extends Component {
}

editor.on( 'blur', () => {
editor.wpClassicBlockBookmark = editor.selection.getBookmark();

// Keep the "raw" content as it contains the caret position bookmark.
setAttributes( {
contentRaw: editor.getContent( { format: 'raw' } ),
Copy link
Member

Choose a reason for hiding this comment

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

Storing the raw content without any processing seems like a bad idea? Why should this be different from the classic editor?

content: editor.getContent(),
} );
return false;
} );

editor.on( 'init', () => {
// Store the initial "raw" content.
setAttributes( {
contentRaw: editor.getContent( { format: 'raw' } ),
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't expect merely initialising the block to change the content. The user didn't do anything.

} );
} );

editor.on( 'beforeSetContent focus', () => {
if ( editor.wpClassicBlockBookmark ) {
editor.selection.moveToBookmark( editor.wpClassicBlockBookmark );
}
} );

editor.on( 'keydown', ( event ) => {
if ( ( event.keyCode === BACKSPACE || event.keyCode === DELETE ) && isTmceEmpty( editor ) ) {
// delete the block
Expand Down