-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
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 |
---|---|---|
|
@@ -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; | ||
|
||
// Strip white space before comparing to avoid resetting the editor content when not necessary; | ||
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. What kind of whitespace is different? 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. The only differences I'm seeing are in some |
||
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' } ); | ||
} | ||
} | ||
|
||
|
@@ -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' } ), | ||
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. 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' } ), | ||
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 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 | ||
|
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.
contentRaw is not persisted, we should probably use "local state" for it.
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.
Ah, sure, that'd work even better :)