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: Update behavior of notice and preview action #13001

Merged
merged 1 commit into from
Jun 21, 2017

Conversation

jeremeylduvall
Copy link
Contributor

This is a first attempt at fixing #12829. It feels a bit hacky tbh. Right now, the "Preview" link in the Editor and the "View" link have the same behavior. This works in most cases, but in specific instances, it produces unexpected behavior. For example:

I have a published post. I make changes to the post, but I haven't updated the post yet. I would expect the "View" link in the notice to show me the saved version of the post (without my new changes). I would expect the "Preview" link to show me the new changes as well. Right now, you would see the new changes in both instances.

To decipher when we should show a preview versus the published post, I introduced a new state item previewAction that changes according to which action is taken by the user.

While testing this out, I also noticed that when we modify the ExternalUrl prop in editor-preview/index.jsx, we're stripping out the preview component. This leads to some counterintuitive behavior. For example, try the following:

  1. Start a new post and enter some content. Publish the post.
  2. Enter some new content in the post, but do not update the post.
  3. Click the "Preview" link at the top of the Editor. The iframe will show your changes as expected.
  4. Click the external icon on the iframe. The resulting window will not show your changes.

I would expect the iframe and the external icon URL to show the same content. If we leave in the preview piece to the URL, this behavior is fixed. cc @ehg as it looks like you removed it in #5550. I'm not quite clear on the ramifications of leaving preview. I didn't see any adverse side effects.

To test

  1. Create a new post.
  2. Type in some content and use the Preview link at the top of the editor. Verify that you see the same content as currently in the editor.
  3. Publish the post. The View Post link should show the same content.
  4. Leave the notice in place. Add some new content to the editor, but don't update it.
  5. Click on View Post. You should not see the new content that you haven't updated/saved yet. Click on Preview at the top of the editor. You should see the new content since it's a post preview.

GIF

Here's a GIF of the various flows.

editoraction

@jeremeylduvall jeremeylduvall 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 Apr 11, 2017
@jeremeylduvall jeremeylduvall self-assigned this Apr 11, 2017
@jeremeylduvall jeremeylduvall requested review from ehg and aduth April 11, 2017 15:04
@matticbot
Copy link
Contributor

@matticbot matticbot added the [Size] M Medium sized issue label Apr 11, 2017
@aduth
Copy link
Contributor

aduth commented Apr 24, 2017

Rather than applying as state, we could pass the intended after-save action as an argument to onPreview. This way we don't need to manage the state lifecycle, while still being certain that the value is preserved after the save callback completes (idea of closure).

@ehg
Copy link
Member

ehg commented Apr 25, 2017

I'm not quite clear on the ramifications of leaving preview. I didn't see any adverse side effects.

This was an attempt to stop the pasting of links with generally noisy qargs by users. I guess the expected behaviour here would be to only include the preview qarg if the editor is in a 'dirty state'?

@jeremeylduvall jeremeylduvall force-pushed the fix/12829-preview-link-editor branch from cc7f8d0 to c63f6a0 Compare April 27, 2017 15:41
@matticbot matticbot added [Size] S Small sized issue and removed [Size] M Medium sized issue labels Apr 27, 2017
@jeremeylduvall jeremeylduvall force-pushed the fix/12829-preview-link-editor branch from c63f6a0 to 5c80afa Compare April 27, 2017 15:42
@jeremeylduvall
Copy link
Contributor Author

@aduth thanks for the feedback! What do you think about this new approach? It no longer uses state. Instead, we're passing an argument to onPreview that will indicate which approach we should take.

@aduth
Copy link
Contributor

aduth commented May 2, 2017

I may have made an error in suggesting against state. Specifically, I think I overlooked how we're passing URLs to the EditorPreview in the render. In these cases, we'd need to guarantee that a rerender occurs when calling onPreview. Setting an instance property alone is not sufficient for this. Instead, we'd need to affect some change to the component's state, or incoming properties (or elsewhere that would incur this as a side effect, like changes to the post store). It may work as-is, but only because some other change is occurring that's causing a rerender, but relying on this could be prone to bugs in the future.

I don't recall exactly what your original implementation looked like, but I'm guessing assigning and reading from previewAction as state with setState and this.state would be preferable. That or trying to consolidate this.state.previewUrl to automatically reflect whether we're viewing or previewing the post. I don't know that the latter would be very easy to achieve, so it might be fine to have two separate properties in state (previewAction, previewUrl).

Sorry to flip-flop on my suggestions, and for my delays in revisiting this.

@jeremeylduvall
Copy link
Contributor Author

Will take a look at this tomorrow. Setting the URL is indeed a bit of a wrinkle. I remember originally looking at:

That or trying to consolidate this.state.previewUrl to automatically reflect whether we're viewing or previewing the post

I forget exactly why I didn't go with that originally.

@jeremeylduvall jeremeylduvall added [Status] In Progress and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels May 4, 2017
@jeremeylduvall jeremeylduvall force-pushed the fix/12829-preview-link-editor branch from 5c80afa to d008cf7 Compare May 12, 2017 12:43
@matticbot matticbot added [Size] M Medium sized issue and removed [Size] S Small sized issue labels May 12, 2017
@jeremeylduvall
Copy link
Contributor Author

I opted to re-add previewAction to state and modify the preview/view behavior using this.state.

Manipulating this.state.previewUrl could get confusing since taking this approach means that we would need to change previewUrl to the post URL at some points. I thought it made more sense to always have previewUrl reflect the preview and use this.state.post.URL when necessary.

This should work as originally described. I'll go ahead and flip it back to "Needs Review."

@jeremeylduvall jeremeylduvall added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] In Progress labels May 12, 2017
this.setState( {
previewAction: action
} );
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Quick note: This comparison is helpful if the user does the following:

  1. Cmd + Click on Preview to open a new tab.
  2. Come back to the editor and click the Preview link like normal while the other tab is still open.

In that scenario, this.state.previewAction would still be set to preview so we're avoiding an extra setState call.

Copy link
Contributor

Choose a reason for hiding this comment

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

In that scenario, this.state.previewAction would still be set to preview so we're avoiding an extra setState call.

Hmm, kinda goes to highlight that letting the state be long-lived beyond the initial view/preview intent is perhaps a bit fragile. I don't really have a good alternative to suggest, but a little unnerving.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right - we could use a setTimeout to eventually revert the state back to null, but that still feels a bit hacky. Outside of closing the preview window, there isn't a great cue to reset the this.state.previewAction back to null :(

Copy link
Contributor

@aduth aduth left a comment

Choose a reason for hiding this comment

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

This seems to work well in my testing. Let me know what you think of my remarks, but I'm not opposed to moving forward with merging.

this._previewWindow.focus();
} else {
this._previewWindow = window.open( this.state.previewUrl, 'WordPress.com Post Preview' );
this._previewWindow = window.open(
( this.state.previewAction === 'view' ? this.state.post.URL : this.state.previewUrl ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this ternary expression occurs four separate times, maybe it'd be worth moving to a helper function on the component?

getPreviewUrl() {
	const { previewAction, previewUrl, post } = this.state;
	if ( 'view' === previewAction ) {
		return post.URL;
	}

	return previewUrl;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. Ternary converted to helper function. getPreviewUrl introduces a tiny bit of shadowing since PostEditStore has a method with the same name. This naming makes the most sense and hopefully it's still straightforward enough.

this.setState( {
previewAction: action
} );
}
Copy link
Contributor

Choose a reason for hiding this comment

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

In that scenario, this.state.previewAction would still be set to preview so we're avoiding an extra setState call.

Hmm, kinda goes to highlight that letting the state be long-lived beyond the initial view/preview intent is perhaps a bit fragile. I don't really have a good alternative to suggest, but a little unnerving.

@supernovia
Copy link
Contributor

Just noting I ran into issues with this today. It's a bit awkward to have to tell folks they need to save before they can preview, so I'd love to see this fixed.

@jeremeylduvall jeremeylduvall force-pushed the fix/12829-preview-link-editor branch from d008cf7 to dbb6e0f Compare May 23, 2017 12:48
@matticbot
Copy link
Contributor

@jeremeylduvall This PR needs a rebase

Copy link
Contributor

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Looks good 👍

If a post is published with new changes in the editor, the "View" link should
still open the published page. The "Preview" link should open the post with the
new changes.

Resolves: #12829
@jeremeylduvall jeremeylduvall force-pushed the fix/12829-preview-link-editor branch from dbb6e0f to e7e3b0d Compare June 21, 2017 13:20
@jeremeylduvall jeremeylduvall merged commit 788e4cb into master Jun 21, 2017
@jeremeylduvall jeremeylduvall deleted the fix/12829-preview-link-editor branch June 21, 2017 18:02
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jun 21, 2017
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. [Size] M Medium sized issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants