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

Editor: Prevent switching editor tabs from marking content as dirty #6880

Closed
wants to merge 1 commit into from

Conversation

nylen
Copy link
Contributor

@nylen nylen commented Jul 18, 2016

Fixes #6869 where switching between the Visual and HTML tabs would cause the post/page being edited to be marked as dirty.

This is because our wpautop filter and TinyMCE apply changes to empty content that makes it look like this: <p><br data-mce-bogus="1"></p>

We have an isContentEmpty function that accounts for this specific string, and we should use it to make sure that truly empty content is treated the same as the special empty content string above.

This works well when the editor content is actually empty. Typing any text into the editor then switching tabs may still cause the post to be marked as dirty, but the fix for this situation is more involved because we will need to judiciously apply wpautop (right now we are not consistent about how this is done).

The debug statement added here will come in handy for future related changes.

To test

  1. Start a new post or page
  2. Do not add any content, but switch between the Visual and HTML editor
  3. Refresh the page and verify that you are prompted to save non-existent changes in master but not in this branch

/cc @aduth - I think you had a different idea about how to fix this, but I feel more comfortable with this simple approach, though it only works well with empty post content.

Test live: https://calypso.live/?branch=fix/editor/dirty-on-switch-tabs

@nylen nylen added [Feature] Post/Page Editor The editor for editing posts and pages. [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Jul 18, 2016
@nylen nylen force-pushed the fix/editor/dirty-on-switch-tabs branch from 5ab21c8 to 46adb12 Compare July 18, 2016 23:52
@nylen nylen force-pushed the fix/editor/dirty-on-switch-tabs branch from 46adb12 to 6c1fe8c Compare July 18, 2016 23:54
}
if ( _rawContent !== _initialRawContent ) {
return (
! isContentEmpty( _rawContent ) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

My thinking was to prevent raw content from being set as empty before allowing it to get to this point. Especially since isDirty is run alot.

diff --git a/client/lib/posts/post-edit-store.js b/client/lib/posts/post-edit-store.js
index a8c695e..4777058 100644
--- a/client/lib/posts/post-edit-store.js
+++ b/client/lib/posts/post-edit-store.js
@@ -237,6 +237,8 @@ function normalize( post ) {
 function setRawContent( content ) {
        var isDirty, hasContent;

+       content = content.replace( REGEXP_EMPTY_CONTENT, '' );
+
        if ( null === _initialRawContent ) {
                debug( 'Set initial raw content to: %s', content );
                _initialRawContent = content;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried this out, but it appears that TinyMCE is fighting with this content replacement. When switching between Visual and HTML tabs, I see the content ending up as <br />, which is not currently considered "empty", so the post is still marked dirty. Also, the replacement unexpectedly clears the post content when you enter a blank line as the only content.

Copy link
Contributor

Choose a reason for hiding this comment

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

I have a feeling there may be some related issues around this line:

https://github.com/Automattic/wp-calypso/blob/76a4b7a/client/post-editor/post-editor.jsx#L753

Specifically, I was observing that when switching from HTML to Visual editors, it was setting the raw content without <p> tags. I'm not sure this is the same root cause as what you were observing, but I have a feeling that the solution here won't cover all cases (e.g. when switching from HTML mode to Visual mode where multiple paragraphs exist, does it show as dirty?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, this PR does not address switching between HTML and Visual with content entered. I tried a couple of things without luck.

@lancewillett
Copy link
Contributor

Closing as older and possibly abandoned. Feel free to re-open if still valid and in-progress.

@lancewillett lancewillett deleted the fix/editor/dirty-on-switch-tabs branch October 4, 2016 18:05
@lancewillett lancewillett removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Oct 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Post/Page Editor The editor for editing posts and pages.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Editor: Switching between Visual/HTML editors marks page as dirty, gives double prompt
3 participants