Opened 7 months ago
Closed 7 months ago
#22491 closed defect (bug) (fixed)
Autosave / AYS on the attachment edit screen
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | 3.5 |
| Component: | Media | Version: | 3.5 |
| Severity: | normal | Keywords: | has-patch |
| Cc: |
Description
Autosave/AYS you want to leave this page behavior is based on an input with the ID of content; specifically, the JS is looking for #post #content. The attachment edit screen uses attachment_content for the textarea's ID instead (#content is also used for some JS dealing with the editor size). Thus, if there is a description/attachment page content for the attachment, you'll get an AYS when trying to leave the page, even if you haven't changed anything, and if autosave has fired (which you can trigger by running autosave(); in the console instead of waiting), there will always be a red warning at the top saying that there is a newer autosave, which has empty content.
Attachments (7)
Change History (36)
comment:2
nacin
— 7 months ago
1 seems good at this stage. Attachments didn't have autosave in 3.4, so this is not a regression.
comment:4
follow-up:
↓ 5
helenyhou
— 7 months ago
- Keywords has-patch added
22491.diff checks the post type before enqueueing the script on both post-new.php and post.php.
comment:5
in reply to:
↑ 4
azaozz
— 7 months ago
Replying to helenyhou:
Is there a case where attachments are loaded as new posts (with post-new.php)? As far as I can see attachments are always edited, they cannot be created separately from the uploaded file.
comment:6
follow-up:
↓ 7
helenyhou
— 7 months ago
I don't think there's a link to it anywhere, no. Technically you can navigate to /wp-admin/post-new.php?post_type=attachment though. Seems like we should probably prevent/redirect that as well.
comment:7
in reply to:
↑ 6
nacin
— 7 months ago
Replying to helenyhou:
I don't think there's a link to it anywhere, no. Technically you can navigate to /wp-admin/post-new.php?post_type=attachment though. Seems like we should probably prevent/redirect that as well.
Yeah, getting to that today.
comment:8
azaozz
— 7 months ago
22491-2.patch includes 22491.diff and adds AYS on page unload for title, caption, alt, content and slug fields.
comment:9
azaozz
— 7 months ago
- Resolution set to fixed
- Status changed from assigned to closed
In 22725:
comment:10
nacin
— 7 months ago
- Resolution fixed deleted
- Status changed from closed to reopened
I don't mean to be a pain, but because getFieldsContent() doesn't use a delimiter, moving an entire field's content from title to caption, or caption to alt, etc., would result in no AYS. I'm fine with simple, but moving content from field to field isn't exactly an uncommon use case.
comment:11
follow-up:
↓ 15
SergeyBiryukov
— 7 months ago
22491.3.diff is an attempt to use .map().
SergeyBiryukov
— 7 months ago
comment:12
follow-up:
↓ 14
nacin
— 7 months ago
My patch avoids delimiters by comparing two arrays.
Should 'autosave' simply not be printed when the post type doesn't support 'editor'?
comment:13
nacin
— 7 months ago
In 22777:
comment:14
in reply to:
↑ 12
helenyhou
— 7 months ago
Replying to nacin:
Should 'autosave' simply not be printed when the post type doesn't support 'editor'?
Yeah, actually. That would be the sensible way. :)
comment:15
in reply to:
↑ 11
azaozz
— 7 months ago
Replying to nacin:
Sure, comparing an array of strings would make it more precise.
Replying to SergeyBiryukov:
We could use few different jQuery functions but thinking building the array by hand is the fastest and clearest way :)
comment:16
azaozz
— 7 months ago
22491-4.patch is essentially the same as 22491.2.diff but uses jQuery.each() to loop through the array and compare the values (makes it a little shorter).
comment:17
nacin
— 7 months ago
I count 6 lines of code versus 8 lines of code. If by mean shorter you mean 25% longer and using a function instead of a language construct, then sure. :-)
comment:18
nacin
— 7 months ago
The issue here simply appears to be that when you do title + content, and one of them is undefined, autosaveLast sometimes gets 'undefined' stuffed into the string — but only in some situations. It is set properly during autosave, and improperly on pageload. So when autosave fires, it screws things up.
Can someone confirm that, after reverting [22725], 22491.4.diff solves the reported issues?
comment:19
azaozz
— 7 months ago
It does, although I'm a bit concerned changing autosave right before RC :)
On the other hand the change looks good and fixes a bug there, so lets go with it.
comment:20
follow-up:
↓ 21
azaozz
— 7 months ago
Also: fixing and loading autosave here would run it and catch changes to the title only. If the title is edited, it will create a revision of the attachment post and show AYS on unload.
We can avoid creating a revision by changing the id of the title field (not the name, so $_POST won't change).
comment:21
in reply to:
↑ 20
nacin
— 7 months ago
Replying to azaozz:
Also: fixing and loading autosave here would run it and catch changes to the title only. If the title is edited, it will create a revision of the attachment post and show AYS on unload.
We can avoid creating a revision/showing AYS by changing the id of the title field (not the name, so $_POST won't change).
The chance of the title field changing but not being saved is fairly slim. We can improve autosave in a future release.
I'm also okay with keeping [22725] as long as we also apply 22491-4.patch or 22491.2.diff for 3.5. But we should also apply 22491.4.diff for the sake of plugins.
comment:22
azaozz
— 7 months ago
Okay, lets do that. Having autosaves/revisions only for the titles would be strange.
22491.2.diff looks good, native JS would always be faster than jQuery :)
comment:23
nacin
— 7 months ago
In 22794:
comment:24
azaozz
— 7 months ago
- Resolution set to fixed
- Status changed from reopened to closed
In 22795:
comment:25
johnbillion
— 7 months ago
- Keywords needs-patch added; has-patch removed
- Resolution fixed deleted
- Status changed from closed to reopened
I'm getting an AYS when I do update the content on this screen now.
To reproduce: edit any of the fields and hit the Update button.
johnbillion
— 7 months ago
comment:26
johnbillion
— 7 months ago
- Keywords has-patch added; needs-patch removed
22491.5.diff removes the onbeforeunload handler when the form is submitted or the delete link is clicked. It also disables the submit button and displays the spinner so it's consistent with the main post editing screen.
We have a bad case of D.R.Y. on this screen. This code is mostly copied from autosave.js.
comment:27
nacin
— 7 months ago
I'd be inclined to just restore the original autosave.js here. It'd only autosave the title, but I think that is fine.
If changing the ID from attachment_content to content provides autosaving for the attachment page content without breaking anything, I'm fine with that oo.
comment:28
helenyhou
— 7 months ago
Can we just go back to the original plan of not having autosave on the attachment edit screen (or if the post type doesn't support editor)? The original autosave.js has problems because it's specifically looking for #post #content to match the post content, which in this case, it won't if there's anything in the post content (#post #content being empty here). It may autosave the title, but you'll still get the AYS as well as an autosave with empty content that will give you the newer autosave admin warning when the content wasn't originally empty.
Using the ID of content creates issues with styling, since it's not a TinyMCE instance but there's a bunch of JS that looks for #content and does special things to it that are specific to matching the textarea to TinyMCE and the editor size. Seems like that should be dealt with in a better way (like only doing that special stuff if #content is associated with a TinyMCE instance), but I don't think that's a wise thing to pursue in the RC tage.
comment:29
nacin
— 7 months ago
- Resolution set to fixed
- Status changed from reopened to closed
In 22858:
Here are options I see:
The first option is probably the least disruptive, and is where both azaozz and I am leaning.