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