author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
9 | 1 |
/** |
2 |
* @output wp-includes/js/autosave.js |
|
3 |
*/ |
|
4 |
||
5 | 5 |
/* global tinymce, wpCookies, autosaveL10n, switchEditors */ |
16 | 6 |
// Back-compat. |
5 | 7 |
window.autosave = function() { |
8 |
return true; |
|
9 |
}; |
|
0 | 10 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
/** |
9 | 12 |
* Adds autosave to the window object on dom ready. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
* @param {jQuery} $ jQuery object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* @param {window} The window object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
*/ |
5 | 20 |
( function( $, window ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
/** |
9 | 22 |
* Auto saves the post. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
24 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
* |
16 | 26 |
* @return {Object} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
* {{ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
* getPostData: getPostData, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
* getCompareString: getCompareString, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
* disableButtons: disableButtons, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
* enableButtons: enableButtons, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
* local: ({hasStorage, getSavedPostData, save, suspend, resume}|*), |
18 | 33 |
* server: ({tempBlockSave, triggerSave, postChanged, suspend, resume}|*) |
34 |
* }} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
* The object with all functions for autosave. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
*/ |
5 | 37 |
function autosave() { |
38 |
var initialCompareString, |
|
18 | 39 |
initialCompareData = {}, |
40 |
lastTriggerSave = 0, |
|
41 |
$document = $( document ); |
|
42 |
||
43 |
/** |
|
44 |
* Sets the initial compare data. |
|
45 |
* |
|
46 |
* @since 5.6.1 |
|
47 |
*/ |
|
48 |
function setInitialCompare() { |
|
49 |
initialCompareData = { |
|
50 |
post_title: $( '#title' ).val() || '', |
|
51 |
content: $( '#content' ).val() || '', |
|
52 |
excerpt: $( '#excerpt' ).val() || '' |
|
53 |
}; |
|
54 |
||
55 |
initialCompareString = getCompareString( initialCompareData ); |
|
56 |
} |
|
0 | 57 |
|
5 | 58 |
/** |
9 | 59 |
* Returns the data saved in both local and remote autosave. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
* @since 3.9.0 |
5 | 62 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
* @param {string} type The type of autosave either local or remote. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
* |
16 | 65 |
* @return {Object} Object containing the post data. |
5 | 66 |
*/ |
67 |
function getPostData( type ) { |
|
68 |
var post_name, parent_id, data, |
|
69 |
time = ( new Date() ).getTime(), |
|
70 |
cats = [], |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
editor = getEditor(); |
0 | 72 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
// Don't run editor.save() more often than every 3 seconds. |
5 | 74 |
// It is resource intensive and might slow down typing in long posts on slow devices. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
if ( editor && editor.isDirty() && ! editor.isHidden() && time - 3000 > lastTriggerSave ) { |
5 | 76 |
editor.save(); |
77 |
lastTriggerSave = time; |
|
78 |
} |
|
0 | 79 |
|
5 | 80 |
data = { |
81 |
post_id: $( '#post_ID' ).val() || 0, |
|
82 |
post_type: $( '#post_type' ).val() || '', |
|
83 |
post_author: $( '#post_author' ).val() || '', |
|
84 |
post_title: $( '#title' ).val() || '', |
|
85 |
content: $( '#content' ).val() || '', |
|
86 |
excerpt: $( '#excerpt' ).val() || '' |
|
87 |
}; |
|
88 |
||
89 |
if ( type === 'local' ) { |
|
90 |
return data; |
|
0 | 91 |
} |
92 |
||
5 | 93 |
$( 'input[id^="in-category-"]:checked' ).each( function() { |
94 |
cats.push( this.value ); |
|
95 |
}); |
|
96 |
data.catslist = cats.join(','); |
|
0 | 97 |
|
5 | 98 |
if ( post_name = $( '#post_name' ).val() ) { |
99 |
data.post_name = post_name; |
|
100 |
} |
|
0 | 101 |
|
5 | 102 |
if ( parent_id = $( '#parent_id' ).val() ) { |
103 |
data.parent_id = parent_id; |
|
104 |
} |
|
0 | 105 |
|
5 | 106 |
if ( $( '#comment_status' ).prop( 'checked' ) ) { |
107 |
data.comment_status = 'open'; |
|
108 |
} |
|
109 |
||
110 |
if ( $( '#ping_status' ).prop( 'checked' ) ) { |
|
111 |
data.ping_status = 'open'; |
|
0 | 112 |
} |
113 |
||
5 | 114 |
if ( $( '#auto_draft' ).val() === '1' ) { |
115 |
data.auto_draft = '1'; |
|
116 |
} |
|
117 |
||
118 |
return data; |
|
0 | 119 |
} |
120 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
/** |
9 | 122 |
* Concatenates the title, content and excerpt. This is used to track changes |
123 |
* when auto-saving. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
* @param {Object} postData The object containing the post data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
* |
16 | 129 |
* @return {string} A concatenated string with title, content and excerpt. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
*/ |
5 | 131 |
function getCompareString( postData ) { |
132 |
if ( typeof postData === 'object' ) { |
|
133 |
return ( postData.post_title || '' ) + '::' + ( postData.content || '' ) + '::' + ( postData.excerpt || '' ); |
|
134 |
} |
|
135 |
||
136 |
return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' ); |
|
137 |
} |
|
0 | 138 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
/** |
9 | 140 |
* Disables save buttons. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
143 |
* |
16 | 144 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
*/ |
5 | 146 |
function disableButtons() { |
147 |
$document.trigger('autosave-disable-buttons'); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
|
5 | 149 |
// Re-enable 5 sec later. Just gives autosave a head start to avoid collisions. |
150 |
setTimeout( enableButtons, 5000 ); |
|
151 |
} |
|
152 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
/** |
9 | 154 |
* Enables save buttons. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
* |
16 | 158 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
*/ |
5 | 160 |
function enableButtons() { |
161 |
$document.trigger( 'autosave-enable-buttons' ); |
|
0 | 162 |
} |
163 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
/** |
9 | 165 |
* Gets the content editor. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
168 |
* |
16 | 169 |
* @return {boolean|*} Returns either false if the editor is undefined, |
170 |
* or the instance of the content editor. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
172 |
function getEditor() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
return typeof tinymce !== 'undefined' && tinymce.get('content'); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
/** |
9 | 177 |
* Autosave in localStorage. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
180 |
* |
16 | 181 |
* @return { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
* { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
* hasStorage: *, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
184 |
* getSavedPostData: getSavedPostData, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
185 |
* save: save, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
* suspend: suspend, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
187 |
* resume: resume |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
188 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
* The object with all functions for local storage autosave. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
*/ |
5 | 192 |
function autosaveLocal() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
var blog_id, post_id, hasStorage, intervalTimer, |
5 | 194 |
lastCompareString, |
195 |
isSuspended = false; |
|
196 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
/** |
9 | 198 |
* Checks if the browser supports sessionStorage and it's not disabled. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
199 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
* |
16 | 202 |
* @return {boolean} True if the sessionStorage is supported and enabled. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
*/ |
5 | 204 |
function checkStorage() { |
205 |
var test = Math.random().toString(), |
|
206 |
result = false; |
|
0 | 207 |
|
5 | 208 |
try { |
209 |
window.sessionStorage.setItem( 'wp-test', test ); |
|
210 |
result = window.sessionStorage.getItem( 'wp-test' ) === test; |
|
211 |
window.sessionStorage.removeItem( 'wp-test' ); |
|
212 |
} catch(e) {} |
|
213 |
||
214 |
hasStorage = result; |
|
215 |
return result; |
|
216 |
} |
|
0 | 217 |
|
5 | 218 |
/** |
9 | 219 |
* Initializes the local storage. |
5 | 220 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
* |
16 | 223 |
* @return {boolean|Object} False if no sessionStorage in the browser or an Object |
224 |
* containing all postData for this blog. |
|
5 | 225 |
*/ |
226 |
function getStorage() { |
|
227 |
var stored_obj = false; |
|
16 | 228 |
// Separate local storage containers for each blog_id. |
5 | 229 |
if ( hasStorage && blog_id ) { |
230 |
stored_obj = sessionStorage.getItem( 'wp-autosave-' + blog_id ); |
|
0 | 231 |
|
5 | 232 |
if ( stored_obj ) { |
233 |
stored_obj = JSON.parse( stored_obj ); |
|
234 |
} else { |
|
235 |
stored_obj = {}; |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
return stored_obj; |
|
240 |
} |
|
241 |
||
242 |
/** |
|
9 | 243 |
* Sets the storage for this blog. Confirms that the data was saved |
244 |
* successfully. |
|
5 | 245 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
* |
16 | 248 |
* @return {boolean} True if the data was saved successfully, false if it wasn't saved. |
5 | 249 |
*/ |
250 |
function setStorage( stored_obj ) { |
|
251 |
var key; |
|
0 | 252 |
|
5 | 253 |
if ( hasStorage && blog_id ) { |
254 |
key = 'wp-autosave-' + blog_id; |
|
255 |
sessionStorage.setItem( key, JSON.stringify( stored_obj ) ); |
|
256 |
return sessionStorage.getItem( key ) !== null; |
|
257 |
} |
|
258 |
||
259 |
return false; |
|
260 |
} |
|
261 |
||
262 |
/** |
|
9 | 263 |
* Gets the saved post data for the current post. |
5 | 264 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
* |
16 | 267 |
* @return {boolean|Object} False if no storage or no data or the postData as an Object. |
5 | 268 |
*/ |
269 |
function getSavedPostData() { |
|
270 |
var stored = getStorage(); |
|
271 |
||
272 |
if ( ! stored || ! post_id ) { |
|
273 |
return false; |
|
274 |
} |
|
275 |
||
276 |
return stored[ 'post_' + post_id ] || false; |
|
0 | 277 |
} |
278 |
||
5 | 279 |
/** |
9 | 280 |
* Sets (save or delete) post data in the storage. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
281 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
282 |
* If stored_data evaluates to 'false' the storage key for the current post will be removed. |
5 | 283 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
284 |
* @since 3.9.0 |
5 | 285 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
* @param {Object|boolean|null} stored_data The post data to store or null/false/empty to delete the key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
287 |
* |
16 | 288 |
* @return {boolean} True if data is stored, false if data was removed. |
5 | 289 |
*/ |
290 |
function setData( stored_data ) { |
|
291 |
var stored = getStorage(); |
|
0 | 292 |
|
5 | 293 |
if ( ! stored || ! post_id ) { |
294 |
return false; |
|
295 |
} |
|
0 | 296 |
|
5 | 297 |
if ( stored_data ) { |
298 |
stored[ 'post_' + post_id ] = stored_data; |
|
299 |
} else if ( stored.hasOwnProperty( 'post_' + post_id ) ) { |
|
300 |
delete stored[ 'post_' + post_id ]; |
|
301 |
} else { |
|
302 |
return false; |
|
303 |
} |
|
304 |
||
305 |
return setStorage( stored ); |
|
306 |
} |
|
307 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
/** |
9 | 309 |
* Sets isSuspended to true. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
312 |
* |
16 | 313 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
*/ |
5 | 315 |
function suspend() { |
316 |
isSuspended = true; |
|
0 | 317 |
} |
318 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
/** |
9 | 320 |
* Sets isSuspended to false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
* |
16 | 324 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
*/ |
5 | 326 |
function resume() { |
327 |
isSuspended = false; |
|
328 |
} |
|
0 | 329 |
|
5 | 330 |
/** |
9 | 331 |
* Saves post data for the current post. |
5 | 332 |
* |
16 | 333 |
* Runs on a 15 seconds interval, saves when there are differences in the post title or content. |
5 | 334 |
* When the optional data is provided, updates the last saved post data. |
335 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
338 |
* @param {Object} data The post data for saving, minimum 'post_title' and 'content'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
* |
16 | 340 |
* @return {boolean} Returns true when data has been saved, otherwise it returns false. |
5 | 341 |
*/ |
342 |
function save( data ) { |
|
343 |
var postData, compareString, |
|
344 |
result = false; |
|
0 | 345 |
|
5 | 346 |
if ( isSuspended || ! hasStorage ) { |
347 |
return false; |
|
348 |
} |
|
0 | 349 |
|
5 | 350 |
if ( data ) { |
351 |
postData = getSavedPostData() || {}; |
|
352 |
$.extend( postData, data ); |
|
353 |
} else { |
|
354 |
postData = getPostData('local'); |
|
355 |
} |
|
356 |
||
357 |
compareString = getCompareString( postData ); |
|
0 | 358 |
|
5 | 359 |
if ( typeof lastCompareString === 'undefined' ) { |
360 |
lastCompareString = initialCompareString; |
|
361 |
} |
|
0 | 362 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
// If the content, title and excerpt did not change since the last save, don't save again. |
5 | 364 |
if ( compareString === lastCompareString ) { |
365 |
return false; |
|
366 |
} |
|
0 | 367 |
|
5 | 368 |
postData.save_time = ( new Date() ).getTime(); |
369 |
postData.status = $( '#post_status' ).val() || ''; |
|
370 |
result = setData( postData ); |
|
0 | 371 |
|
5 | 372 |
if ( result ) { |
373 |
lastCompareString = compareString; |
|
374 |
} |
|
0 | 375 |
|
5 | 376 |
return result; |
377 |
} |
|
0 | 378 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
379 |
/** |
9 | 380 |
* Initializes the auto save function. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
381 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
382 |
* Checks whether the editor is active or not to use the editor events |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
* to autosave, or uses the values from the elements to autosave. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
385 |
* Runs on DOM ready. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
386 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
388 |
* |
16 | 389 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
*/ |
5 | 391 |
function run() { |
392 |
post_id = $('#post_ID').val() || 0; |
|
0 | 393 |
|
5 | 394 |
// Check if the local post data is different than the loaded post data. |
395 |
if ( $( '#wp-content-wrap' ).hasClass( 'tmce-active' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
|
16 | 397 |
/* |
398 |
* If TinyMCE loads first, check the post 1.5 seconds after it is ready. |
|
399 |
* By this time the content has been loaded in the editor and 'saved' to the textarea. |
|
400 |
* This prevents false positives. |
|
401 |
*/ |
|
5 | 402 |
$document.on( 'tinymce-editor-init.autosave', function() { |
403 |
window.setTimeout( function() { |
|
404 |
checkPost(); |
|
405 |
}, 1500 ); |
|
406 |
}); |
|
407 |
} else { |
|
408 |
checkPost(); |
|
409 |
} |
|
410 |
||
16 | 411 |
// Save every 15 seconds. |
5 | 412 |
intervalTimer = window.setInterval( save, 15000 ); |
0 | 413 |
|
5 | 414 |
$( 'form#post' ).on( 'submit.autosave-local', function() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
var editor = getEditor(), |
5 | 416 |
post_id = $('#post_ID').val() || 0; |
417 |
||
418 |
if ( editor && ! editor.isHidden() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
|
5 | 420 |
// Last onSubmit event in the editor, needs to run after the content has been moved to the textarea. |
421 |
editor.on( 'submit', function() { |
|
422 |
save({ |
|
423 |
post_title: $( '#title' ).val() || '', |
|
424 |
content: $( '#content' ).val() || '', |
|
425 |
excerpt: $( '#excerpt' ).val() || '' |
|
426 |
}); |
|
427 |
}); |
|
428 |
} else { |
|
429 |
save({ |
|
430 |
post_title: $( '#title' ).val() || '', |
|
431 |
content: $( '#content' ).val() || '', |
|
432 |
excerpt: $( '#excerpt' ).val() || '' |
|
0 | 433 |
}); |
434 |
} |
|
435 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
436 |
var secure = ( 'https:' === window.location.protocol ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
437 |
wpCookies.set( 'wp-saving-post', post_id + '-check', 24 * 60 * 60, false, false, secure ); |
0 | 438 |
}); |
439 |
} |
|
440 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
441 |
/** |
9 | 442 |
* Compares 2 strings. Removes whitespaces in the strings before comparing them. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
* @param {string} str1 The first string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
* @param {string} str2 The second string. |
16 | 448 |
* @return {boolean} True if the strings are the same. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
*/ |
5 | 450 |
function compare( str1, str2 ) { |
451 |
function removeSpaces( string ) { |
|
452 |
return string.toString().replace(/[\x20\t\r\n\f]+/g, ''); |
|
453 |
} |
|
454 |
||
455 |
return ( removeSpaces( str1 || '' ) === removeSpaces( str2 || '' ) ); |
|
456 |
} |
|
0 | 457 |
|
5 | 458 |
/** |
9 | 459 |
* Checks if the saved data for the current post (if any) is different than the |
460 |
* loaded post data on the screen. |
|
5 | 461 |
* |
462 |
* Shows a standard message letting the user restore the post data if different. |
|
463 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
464 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
* |
16 | 466 |
* @return {void} |
5 | 467 |
*/ |
468 |
function checkPost() { |
|
469 |
var content, post_title, excerpt, $notice, |
|
470 |
postData = getSavedPostData(), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
cookie = wpCookies.get( 'wp-saving-post' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
$newerAutosaveNotice = $( '#has-newer-autosave' ).parent( '.notice' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
$headerEnd = $( '.wp-header-end' ); |
5 | 474 |
|
475 |
if ( cookie === post_id + '-saved' ) { |
|
476 |
wpCookies.remove( 'wp-saving-post' ); |
|
16 | 477 |
// The post was saved properly, remove old data and bail. |
5 | 478 |
setData( false ); |
479 |
return; |
|
480 |
} |
|
0 | 481 |
|
5 | 482 |
if ( ! postData ) { |
483 |
return; |
|
484 |
} |
|
485 |
||
486 |
content = $( '#content' ).val() || ''; |
|
487 |
post_title = $( '#title' ).val() || ''; |
|
488 |
excerpt = $( '#excerpt' ).val() || ''; |
|
489 |
||
490 |
if ( compare( content, postData.content ) && compare( post_title, postData.post_title ) && |
|
491 |
compare( excerpt, postData.excerpt ) ) { |
|
0 | 492 |
|
5 | 493 |
return; |
494 |
} |
|
495 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
496 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
497 |
* If '.wp-header-end' is found, append the notices after it otherwise |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
498 |
* after the first h1 or h2 heading found within the main content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
500 |
if ( ! $headerEnd.length ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
501 |
$headerEnd = $( '.wrap h1, .wrap h2' ).first(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
502 |
} |
5 | 503 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
504 |
$notice = $( '#local-storage-notice' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
505 |
.insertAfter( $headerEnd ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
506 |
.addClass( 'notice-warning' ); |
5 | 507 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
508 |
if ( $newerAutosaveNotice.length ) { |
0 | 509 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
510 |
// If there is a "server" autosave notice, hide it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
511 |
// The data in the session storage is either the same or newer. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
512 |
$newerAutosaveNotice.slideUp( 150, function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
513 |
$notice.slideDown( 150 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
514 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
515 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
516 |
$notice.slideDown( 200 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
517 |
} |
0 | 518 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
$notice.find( '.restore-backup' ).on( 'click.autosave-local', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
520 |
restorePost( postData ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
$notice.fadeTo( 250, 0, function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
522 |
$notice.slideUp( 150 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
}); |
5 | 524 |
}); |
0 | 525 |
} |
5 | 526 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
/** |
9 | 528 |
* Restores the current title, content and excerpt from postData. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
529 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
531 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
532 |
* @param {Object} postData The object containing all post data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
533 |
* |
16 | 534 |
* @return {boolean} True if the post is restored. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
535 |
*/ |
5 | 536 |
function restorePost( postData ) { |
537 |
var editor; |
|
538 |
||
539 |
if ( postData ) { |
|
16 | 540 |
// Set the last saved data. |
5 | 541 |
lastCompareString = getCompareString( postData ); |
542 |
||
543 |
if ( $( '#title' ).val() !== postData.post_title ) { |
|
18 | 544 |
$( '#title' ).trigger( 'focus' ).val( postData.post_title || '' ); |
5 | 545 |
} |
546 |
||
547 |
$( '#excerpt' ).val( postData.excerpt || '' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
548 |
editor = getEditor(); |
0 | 549 |
|
5 | 550 |
if ( editor && ! editor.isHidden() && typeof switchEditors !== 'undefined' ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
551 |
if ( editor.settings.wpautop && postData.content ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
552 |
postData.content = switchEditors.wpautop( postData.content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
553 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
554 |
|
16 | 555 |
// Make sure there's an undo level in the editor. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
editor.undoManager.transact( function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
editor.setContent( postData.content || '' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
editor.nodeChanged(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
559 |
}); |
5 | 560 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
|
16 | 562 |
// Make sure the Text editor is selected. |
18 | 563 |
$( '#content-html' ).trigger( 'click' ); |
564 |
$( '#content' ).trigger( 'focus' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
566 |
// Using document.execCommand() will let the user undo. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
567 |
document.execCommand( 'selectAll' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
568 |
document.execCommand( 'insertText', false, postData.content || '' ); |
5 | 569 |
} |
570 |
||
571 |
return true; |
|
572 |
} |
|
573 |
||
574 |
return false; |
|
575 |
} |
|
0 | 576 |
|
5 | 577 |
blog_id = typeof window.autosaveL10n !== 'undefined' && window.autosaveL10n.blog_id; |
0 | 578 |
|
16 | 579 |
/* |
580 |
* Check if the browser supports sessionStorage and it's not disabled, |
|
581 |
* then initialize and run checkPost(). |
|
582 |
* Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'. |
|
583 |
*/ |
|
5 | 584 |
if ( checkStorage() && blog_id && ( $('#content').length || $('#excerpt').length ) ) { |
18 | 585 |
$( run ); |
5 | 586 |
} |
0 | 587 |
|
5 | 588 |
return { |
589 |
hasStorage: hasStorage, |
|
590 |
getSavedPostData: getSavedPostData, |
|
591 |
save: save, |
|
592 |
suspend: suspend, |
|
593 |
resume: resume |
|
594 |
}; |
|
0 | 595 |
} |
596 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
597 |
/** |
9 | 598 |
* Auto saves the post on the server. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
599 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
600 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
601 |
* |
16 | 602 |
* @return {Object} { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
603 |
* { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
604 |
* tempBlockSave: tempBlockSave, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
605 |
* triggerSave: triggerSave, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
606 |
* postChanged: postChanged, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
607 |
* suspend: suspend, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
608 |
* resume: resume |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
609 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
610 |
* } The object all functions for autosave. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
611 |
*/ |
5 | 612 |
function autosaveServer() { |
613 |
var _blockSave, _blockSaveTimer, previousCompareString, lastCompareString, |
|
614 |
nextRun = 0, |
|
615 |
isSuspended = false; |
|
616 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
617 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
618 |
/** |
9 | 619 |
* Blocks saving for the next 10 seconds. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
620 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
621 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
622 |
* |
16 | 623 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
624 |
*/ |
5 | 625 |
function tempBlockSave() { |
626 |
_blockSave = true; |
|
627 |
window.clearTimeout( _blockSaveTimer ); |
|
0 | 628 |
|
5 | 629 |
_blockSaveTimer = window.setTimeout( function() { |
630 |
_blockSave = false; |
|
631 |
}, 10000 ); |
|
632 |
} |
|
633 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
634 |
/** |
9 | 635 |
* Sets isSuspended to true. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
636 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
637 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
638 |
* |
16 | 639 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
640 |
*/ |
5 | 641 |
function suspend() { |
642 |
isSuspended = true; |
|
643 |
} |
|
644 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
645 |
/** |
9 | 646 |
* Sets isSuspended to false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
647 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
648 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
649 |
* |
16 | 650 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
*/ |
5 | 652 |
function resume() { |
653 |
isSuspended = false; |
|
654 |
} |
|
655 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
656 |
/** |
9 | 657 |
* Triggers the autosave with the post data. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
658 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
660 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
* @param {Object} data The post data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
* |
16 | 663 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
664 |
*/ |
5 | 665 |
function response( data ) { |
666 |
_schedule(); |
|
667 |
_blockSave = false; |
|
668 |
lastCompareString = previousCompareString; |
|
669 |
previousCompareString = ''; |
|
0 | 670 |
|
5 | 671 |
$document.trigger( 'after-autosave', [data] ); |
672 |
enableButtons(); |
|
673 |
||
674 |
if ( data.success ) { |
|
16 | 675 |
// No longer an auto-draft. |
5 | 676 |
$( '#auto_draft' ).val(''); |
677 |
} |
|
678 |
} |
|
0 | 679 |
|
5 | 680 |
/** |
9 | 681 |
* Saves immediately. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
682 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
683 |
* Resets the timing and tells heartbeat to connect now. |
5 | 684 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
685 |
* @since 3.9.0 |
5 | 686 |
* |
16 | 687 |
* @return {void} |
5 | 688 |
*/ |
689 |
function triggerSave() { |
|
690 |
nextRun = 0; |
|
691 |
wp.heartbeat.connectNow(); |
|
692 |
} |
|
693 |
||
694 |
/** |
|
9 | 695 |
* Checks if the post content in the textarea has changed since page load. |
5 | 696 |
* |
697 |
* This also happens when TinyMCE is active and editor.save() is triggered by |
|
698 |
* wp.autosave.getPostData(). |
|
699 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
700 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
701 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
702 |
* @return {boolean} True if the post has been changed. |
5 | 703 |
*/ |
704 |
function postChanged() { |
|
18 | 705 |
var changed = false; |
706 |
||
707 |
// If there are TinyMCE instances, loop through them. |
|
708 |
if ( window.tinymce ) { |
|
709 |
window.tinymce.each( [ 'content', 'excerpt' ], function( field ) { |
|
710 |
var editor = window.tinymce.get( field ); |
|
711 |
||
712 |
if ( ! editor || editor.isHidden() ) { |
|
713 |
if ( ( $( '#' + field ).val() || '' ) !== initialCompareData[ field ] ) { |
|
714 |
changed = true; |
|
715 |
// Break. |
|
716 |
return false; |
|
717 |
} |
|
718 |
} else if ( editor.isDirty() ) { |
|
719 |
changed = true; |
|
720 |
return false; |
|
721 |
} |
|
722 |
} ); |
|
723 |
||
724 |
if ( ( $( '#title' ).val() || '' ) !== initialCompareData.post_title ) { |
|
725 |
changed = true; |
|
726 |
} |
|
727 |
||
728 |
return changed; |
|
729 |
} |
|
730 |
||
5 | 731 |
return getCompareString() !== initialCompareString; |
0 | 732 |
} |
733 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
734 |
/** |
9 | 735 |
* Checks if the post can be saved or not. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
736 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
737 |
* If the post hasn't changed or it cannot be updated, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
738 |
* because the autosave is blocked or suspended, the function returns false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
739 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
740 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
741 |
* |
16 | 742 |
* @return {Object} Returns the post data. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
743 |
*/ |
5 | 744 |
function save() { |
745 |
var postData, compareString; |
|
0 | 746 |
|
16 | 747 |
// window.autosave() used for back-compat. |
5 | 748 |
if ( isSuspended || _blockSave || ! window.autosave() ) { |
749 |
return false; |
|
750 |
} |
|
0 | 751 |
|
5 | 752 |
if ( ( new Date() ).getTime() < nextRun ) { |
753 |
return false; |
|
754 |
} |
|
755 |
||
756 |
postData = getPostData(); |
|
757 |
compareString = getCompareString( postData ); |
|
0 | 758 |
|
16 | 759 |
// First check. |
5 | 760 |
if ( typeof lastCompareString === 'undefined' ) { |
761 |
lastCompareString = initialCompareString; |
|
762 |
} |
|
0 | 763 |
|
16 | 764 |
// No change. |
5 | 765 |
if ( compareString === lastCompareString ) { |
766 |
return false; |
|
767 |
} |
|
0 | 768 |
|
5 | 769 |
previousCompareString = compareString; |
770 |
tempBlockSave(); |
|
771 |
disableButtons(); |
|
772 |
||
773 |
$document.trigger( 'wpcountwords', [ postData.content ] ) |
|
774 |
.trigger( 'before-autosave', [ postData ] ); |
|
775 |
||
776 |
postData._wpnonce = $( '#_wpnonce' ).val() || ''; |
|
777 |
||
778 |
return postData; |
|
779 |
} |
|
780 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
781 |
/** |
9 | 782 |
* Sets the next run, based on the autosave interval. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
783 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
784 |
* @private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
785 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
786 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
787 |
* |
16 | 788 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
789 |
*/ |
5 | 790 |
function _schedule() { |
791 |
nextRun = ( new Date() ).getTime() + ( autosaveL10n.autosaveInterval * 1000 ) || 60000; |
|
0 | 792 |
} |
793 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
794 |
/** |
9 | 795 |
* Sets the autosaveData on the autosave heartbeat. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
796 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
797 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
798 |
* |
16 | 799 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
800 |
*/ |
18 | 801 |
$( function() { |
802 |
_schedule(); |
|
803 |
}).on( 'heartbeat-send.autosave', function( event, data ) { |
|
5 | 804 |
var autosaveData = save(); |
805 |
||
806 |
if ( autosaveData ) { |
|
807 |
data.wp_autosave = autosaveData; |
|
808 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
809 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
810 |
/** |
9 | 811 |
* Triggers the autosave of the post with the autosave data on the autosave |
812 |
* heartbeat. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
813 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
815 |
* |
16 | 816 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
817 |
*/ |
5 | 818 |
}).on( 'heartbeat-tick.autosave', function( event, data ) { |
819 |
if ( data.wp_autosave ) { |
|
820 |
response( data.wp_autosave ); |
|
821 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
822 |
/** |
9 | 823 |
* Disables buttons and throws a notice when the connection is lost. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
824 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
826 |
* |
16 | 827 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
*/ |
5 | 829 |
}).on( 'heartbeat-connection-lost.autosave', function( event, error, status ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
830 |
|
5 | 831 |
// When connection is lost, keep user from submitting changes. |
832 |
if ( 'timeout' === error || 603 === status ) { |
|
833 |
var $notice = $('#lost-connection-notice'); |
|
834 |
||
835 |
if ( ! wp.autosave.local.hasStorage ) { |
|
836 |
$notice.find('.hide-if-no-sessionstorage').hide(); |
|
837 |
} |
|
838 |
||
839 |
$notice.show(); |
|
840 |
disableButtons(); |
|
841 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
842 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
843 |
/** |
9 | 844 |
* Enables buttons when the connection is restored. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
845 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
846 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
847 |
* |
16 | 848 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
849 |
*/ |
5 | 850 |
}).on( 'heartbeat-connection-restored.autosave', function() { |
851 |
$('#lost-connection-notice').hide(); |
|
852 |
enableButtons(); |
|
853 |
}); |
|
854 |
||
855 |
return { |
|
856 |
tempBlockSave: tempBlockSave, |
|
857 |
triggerSave: triggerSave, |
|
858 |
postChanged: postChanged, |
|
859 |
suspend: suspend, |
|
860 |
resume: resume |
|
861 |
}; |
|
0 | 862 |
} |
863 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
864 |
/** |
9 | 865 |
* Sets the autosave time out. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
866 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
867 |
* Wait for TinyMCE to initialize plus 1 second. for any external css to finish loading, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
* then save to the textarea before setting initialCompareString. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
* This avoids any insignificant differences between the initial textarea content and the content |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
* extracted from the editor. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
871 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
872 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
873 |
* |
16 | 874 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
875 |
*/ |
18 | 876 |
$( function() { |
877 |
// Set the initial compare string in case TinyMCE is not used or not loaded first. |
|
878 |
setInitialCompare(); |
|
879 |
}).on( 'tinymce-editor-init.autosave', function( event, editor ) { |
|
880 |
// Reset the initialCompare data after the TinyMCE instances have been initialized. |
|
881 |
if ( 'content' === editor.id || 'excerpt' === editor.id ) { |
|
5 | 882 |
window.setTimeout( function() { |
883 |
editor.save(); |
|
18 | 884 |
setInitialCompare(); |
5 | 885 |
}, 1000 ); |
886 |
} |
|
887 |
}); |
|
0 | 888 |
|
5 | 889 |
return { |
890 |
getPostData: getPostData, |
|
891 |
getCompareString: getCompareString, |
|
892 |
disableButtons: disableButtons, |
|
893 |
enableButtons: enableButtons, |
|
894 |
local: autosaveLocal(), |
|
895 |
server: autosaveServer() |
|
896 |
}; |
|
897 |
} |
|
0 | 898 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
899 |
/** @namespace wp */ |
5 | 900 |
window.wp = window.wp || {}; |
901 |
window.wp.autosave = autosave(); |
|
902 |
||
903 |
}( jQuery, window )); |