Keep the cursor position in the Classic Block#12393
Conversation
- On blur store the cursor position and the "raw" content of the Classic Block. - Use the "raw" content when refreshing the editor content on `componentDidUpdate()`. - Restore the cursor position on focus and before inserting content.
|
|
||
| const { clientId, attributes } = this.props; | ||
| const editor = window.tinymce.get( `editor-${ clientId }` ); | ||
| let { contentRaw } = attributes; |
There was a problem hiding this comment.
contentRaw is not persisted, we should probably use "local state" for it.
There was a problem hiding this comment.
Ah, sure, that'd work even better :)
youknowriad
left a comment
There was a problem hiding this comment.
We could use some e2e test for the classic block :)
Needed when the first thing the user does after adding a Classic Block is to open a modal that inserts something, like the media modal.
|
|
||
| // Keep the "raw" content as it contains the caret position bookmark. | ||
| setAttributes( { | ||
| contentRaw: editor.getContent( { format: 'raw' } ), |
There was a problem hiding this comment.
Storing the raw content without any processing seems like a bad idea? Why should this be different from the classic editor?
| editor.on( 'init', () => { | ||
| // Store the initial "raw" content. | ||
| setAttributes( { | ||
| contentRaw: editor.getContent( { format: 'raw' } ), |
There was a problem hiding this comment.
I wouldn't expect merely initialising the block to change the content. The user didn't do anything.
| 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.
What kind of whitespace is different?
There was a problem hiding this comment.
The only differences I'm seeing are in some <p>\u00a0</p> and some ; missing/being added to ends of inline style attributes.
|
Looking more at this, still can't figure out what the refresh code in Seems at some point it was possible that Even if this is still needed, don't think that looking at @iseulde @aduth any chance you could remember what was the purpose of the above code? I know, it's been there for over a year... |
|
To me, this is an interesting thing that happens in master:
This means that clicking the image specifically is causing the focus loss? |
I think it is still needed for undo/redo. It needs to be possible to set the content from outside the classic block.
Sounds good to me. |
Yes, clicking anywhere outside the editor triggers the That is easily fixed by doing the |
I'm probably missing something but can't trigger that... Disabling the revert of the content doesn't seem to break undo/redo. |
|
Another interesting thing: even if I remove |
Right. It still needs |
Why does this only happen for selecting an image though? What's special about this image? |
|
Here it happens for any click that focuses something outside the editor body. For example opening the menu, etc. |
|
Found it :) The check in componentDidUpdate() is needed only when the global undo/redo buttons are clicked. Not needed/not running for Ctrl/Cmd + z though... |
|
Closing this in favor of #12415. |
Fixes #10509.
componentDidUpdate(). It contains the cursor position "bookmark".