author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:28:13 +0200 | |
changeset 9 | 177826044cd9 |
parent 7 | cf61fcea0001 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
9 | 1 |
/** |
2 |
* @output wp-includes/js/autosave.js |
|
3 |
*/ |
|
4 |
||
5 | 5 |
/* global tinymce, wpCookies, autosaveL10n, switchEditors */ |
6 |
// Back-compat |
|
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
* @returns {Object} |
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
* @returns {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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
113 |
* @returns {string} A concatenated string with title, content and excerpt. |
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
* @returns {void} |
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
* @returns {void} |
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
* @returns {boolean|*} Returns either false if the editor is undefined, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
154 |
* or the instance of the content editor. |
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
* @returns { |
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
* @returns {boolean} True if the sessionStorage is supported and enabled. |
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
207 |
* @returns {boolean|Object} False if no sessionStorage in the browser or an Object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
* containing all postData for this blog. |
5 | 209 |
*/ |
210 |
function getStorage() { |
|
211 |
var stored_obj = false; |
|
212 |
// Separate local storage containers for each blog_id |
|
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
* @returns {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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
* @returns {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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
272 |
* @returns {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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
297 |
* @returns {void} |
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
* @returns {void} |
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 |
* |
317 |
* Runs on a 15 sec. interval, saves when there are differences in the post title or content. |
|
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
* @returns {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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
* @returns {void} |
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 |
|
5 | 381 |
// If TinyMCE loads first, check the post 1.5 sec. after it is ready. |
382 |
// By this time the content has been loaded in the editor and 'saved' to the textarea. |
|
383 |
// This prevents false positives. |
|
384 |
$document.on( 'tinymce-editor-init.autosave', function() { |
|
385 |
window.setTimeout( function() { |
|
386 |
checkPost(); |
|
387 |
}, 1500 ); |
|
388 |
}); |
|
389 |
} else { |
|
390 |
checkPost(); |
|
391 |
} |
|
392 |
||
393 |
// Save every 15 sec. |
|
394 |
intervalTimer = window.setInterval( save, 15000 ); |
|
0 | 395 |
|
5 | 396 |
$( 'form#post' ).on( 'submit.autosave-local', function() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
var editor = getEditor(), |
5 | 398 |
post_id = $('#post_ID').val() || 0; |
399 |
||
400 |
if ( editor && ! editor.isHidden() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
|
5 | 402 |
// Last onSubmit event in the editor, needs to run after the content has been moved to the textarea. |
403 |
editor.on( 'submit', function() { |
|
404 |
save({ |
|
405 |
post_title: $( '#title' ).val() || '', |
|
406 |
content: $( '#content' ).val() || '', |
|
407 |
excerpt: $( '#excerpt' ).val() || '' |
|
408 |
}); |
|
409 |
}); |
|
410 |
} else { |
|
411 |
save({ |
|
412 |
post_title: $( '#title' ).val() || '', |
|
413 |
content: $( '#content' ).val() || '', |
|
414 |
excerpt: $( '#excerpt' ).val() || '' |
|
0 | 415 |
}); |
416 |
} |
|
417 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
418 |
var secure = ( 'https:' === window.location.protocol ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
wpCookies.set( 'wp-saving-post', post_id + '-check', 24 * 60 * 60, false, false, secure ); |
0 | 420 |
}); |
421 |
} |
|
422 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
/** |
9 | 424 |
* 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
|
425 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
* @since 3.9.0 |
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 |
* @param {string} str1 The first string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
* @param {string} str2 The second string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
430 |
* @returns {boolean} True if the strings are the same. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
*/ |
5 | 432 |
function compare( str1, str2 ) { |
433 |
function removeSpaces( string ) { |
|
434 |
return string.toString().replace(/[\x20\t\r\n\f]+/g, ''); |
|
435 |
} |
|
436 |
||
437 |
return ( removeSpaces( str1 || '' ) === removeSpaces( str2 || '' ) ); |
|
438 |
} |
|
0 | 439 |
|
5 | 440 |
/** |
9 | 441 |
* Checks if the saved data for the current post (if any) is different than the |
442 |
* loaded post data on the screen. |
|
5 | 443 |
* |
444 |
* Shows a standard message letting the user restore the post data if different. |
|
445 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
* @returns {void} |
5 | 449 |
*/ |
450 |
function checkPost() { |
|
451 |
var content, post_title, excerpt, $notice, |
|
452 |
postData = getSavedPostData(), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
453 |
cookie = wpCookies.get( 'wp-saving-post' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
$newerAutosaveNotice = $( '#has-newer-autosave' ).parent( '.notice' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
$headerEnd = $( '.wp-header-end' ); |
5 | 456 |
|
457 |
if ( cookie === post_id + '-saved' ) { |
|
458 |
wpCookies.remove( 'wp-saving-post' ); |
|
459 |
// The post was saved properly, remove old data and bail |
|
460 |
setData( false ); |
|
461 |
return; |
|
462 |
} |
|
0 | 463 |
|
5 | 464 |
if ( ! postData ) { |
465 |
return; |
|
466 |
} |
|
467 |
||
468 |
content = $( '#content' ).val() || ''; |
|
469 |
post_title = $( '#title' ).val() || ''; |
|
470 |
excerpt = $( '#excerpt' ).val() || ''; |
|
471 |
||
472 |
if ( compare( content, postData.content ) && compare( post_title, postData.post_title ) && |
|
473 |
compare( excerpt, postData.excerpt ) ) { |
|
0 | 474 |
|
5 | 475 |
return; |
476 |
} |
|
477 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
478 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
479 |
* 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
|
480 |
* 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
|
481 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
if ( ! $headerEnd.length ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
$headerEnd = $( '.wrap h1, .wrap h2' ).first(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
} |
5 | 485 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
486 |
$notice = $( '#local-storage-notice' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
487 |
.insertAfter( $headerEnd ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
488 |
.addClass( 'notice-warning' ); |
5 | 489 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
490 |
if ( $newerAutosaveNotice.length ) { |
0 | 491 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
// If there is a "server" autosave notice, hide it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
493 |
// 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
|
494 |
$newerAutosaveNotice.slideUp( 150, function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
495 |
$notice.slideDown( 150 ); |
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 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
498 |
$notice.slideDown( 200 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
} |
0 | 500 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
501 |
$notice.find( '.restore-backup' ).on( 'click.autosave-local', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
502 |
restorePost( postData ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
503 |
$notice.fadeTo( 250, 0, function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
504 |
$notice.slideUp( 150 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
505 |
}); |
5 | 506 |
}); |
0 | 507 |
} |
5 | 508 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
509 |
/** |
9 | 510 |
* 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
|
511 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
512 |
* @since 3.9.0 |
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 |
* @param {Object} postData The object containing all post data. |
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 |
* @returns {boolean} True if the post is restored. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
517 |
*/ |
5 | 518 |
function restorePost( postData ) { |
519 |
var editor; |
|
520 |
||
521 |
if ( postData ) { |
|
522 |
// Set the last saved data |
|
523 |
lastCompareString = getCompareString( postData ); |
|
524 |
||
525 |
if ( $( '#title' ).val() !== postData.post_title ) { |
|
526 |
$( '#title' ).focus().val( postData.post_title || '' ); |
|
527 |
} |
|
528 |
||
529 |
$( '#excerpt' ).val( postData.excerpt || '' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
editor = getEditor(); |
0 | 531 |
|
5 | 532 |
if ( editor && ! editor.isHidden() && typeof switchEditors !== 'undefined' ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
533 |
if ( editor.settings.wpautop && postData.content ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
534 |
postData.content = switchEditors.wpautop( postData.content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
535 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
536 |
|
5 | 537 |
// 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
|
538 |
editor.undoManager.transact( function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
editor.setContent( postData.content || '' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
540 |
editor.nodeChanged(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
}); |
5 | 542 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
543 |
|
5 | 544 |
// Make sure the Text editor is selected |
545 |
$( '#content-html' ).click(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
546 |
$( '#content' ).focus(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
547 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
548 |
// Using document.execCommand() will let the user undo. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
549 |
document.execCommand( 'selectAll' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
550 |
document.execCommand( 'insertText', false, postData.content || '' ); |
5 | 551 |
} |
552 |
||
553 |
return true; |
|
554 |
} |
|
555 |
||
556 |
return false; |
|
557 |
} |
|
0 | 558 |
|
5 | 559 |
blog_id = typeof window.autosaveL10n !== 'undefined' && window.autosaveL10n.blog_id; |
0 | 560 |
|
5 | 561 |
// Check if the browser supports sessionStorage and it's not disabled, |
562 |
// then initialize and run checkPost(). |
|
563 |
// Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'. |
|
564 |
if ( checkStorage() && blog_id && ( $('#content').length || $('#excerpt').length ) ) { |
|
565 |
$document.ready( run ); |
|
566 |
} |
|
0 | 567 |
|
5 | 568 |
return { |
569 |
hasStorage: hasStorage, |
|
570 |
getSavedPostData: getSavedPostData, |
|
571 |
save: save, |
|
572 |
suspend: suspend, |
|
573 |
resume: resume |
|
574 |
}; |
|
0 | 575 |
} |
576 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
577 |
/** |
9 | 578 |
* Auto saves the post on the server. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
579 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
580 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
581 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
582 |
* @returns {Object} { |
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 |
* tempBlockSave: tempBlockSave, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
* triggerSave: triggerSave, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
586 |
* postChanged: postChanged, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
587 |
* suspend: suspend, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
588 |
* resume: resume |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
589 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
590 |
* } The object all functions for autosave. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
591 |
*/ |
5 | 592 |
function autosaveServer() { |
593 |
var _blockSave, _blockSaveTimer, previousCompareString, lastCompareString, |
|
594 |
nextRun = 0, |
|
595 |
isSuspended = false; |
|
596 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
597 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
598 |
/** |
9 | 599 |
* Blocks saving for the next 10 seconds. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
600 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
601 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
602 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
603 |
* @returns {void} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
604 |
*/ |
5 | 605 |
function tempBlockSave() { |
606 |
_blockSave = true; |
|
607 |
window.clearTimeout( _blockSaveTimer ); |
|
0 | 608 |
|
5 | 609 |
_blockSaveTimer = window.setTimeout( function() { |
610 |
_blockSave = false; |
|
611 |
}, 10000 ); |
|
612 |
} |
|
613 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
614 |
/** |
9 | 615 |
* Sets isSuspended to true. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
616 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
617 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
618 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
619 |
* @returns {void} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
620 |
*/ |
5 | 621 |
function suspend() { |
622 |
isSuspended = true; |
|
623 |
} |
|
624 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
/** |
9 | 626 |
* Sets isSuspended to false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
627 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
628 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
629 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
* @returns {void} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
631 |
*/ |
5 | 632 |
function resume() { |
633 |
isSuspended = false; |
|
634 |
} |
|
635 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
636 |
/** |
9 | 637 |
* Triggers the autosave with the post data. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
638 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
640 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
641 |
* @param {Object} data The post data. |
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 |
* @returns {void} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
644 |
*/ |
5 | 645 |
function response( data ) { |
646 |
_schedule(); |
|
647 |
_blockSave = false; |
|
648 |
lastCompareString = previousCompareString; |
|
649 |
previousCompareString = ''; |
|
0 | 650 |
|
5 | 651 |
$document.trigger( 'after-autosave', [data] ); |
652 |
enableButtons(); |
|
653 |
||
654 |
if ( data.success ) { |
|
655 |
// No longer an auto-draft |
|
656 |
$( '#auto_draft' ).val(''); |
|
657 |
} |
|
658 |
} |
|
0 | 659 |
|
5 | 660 |
/** |
9 | 661 |
* Saves immediately. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
* Resets the timing and tells heartbeat to connect now. |
5 | 664 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
665 |
* @since 3.9.0 |
5 | 666 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
667 |
* @returns {void} |
5 | 668 |
*/ |
669 |
function triggerSave() { |
|
670 |
nextRun = 0; |
|
671 |
wp.heartbeat.connectNow(); |
|
672 |
} |
|
673 |
||
674 |
/** |
|
9 | 675 |
* Checks if the post content in the textarea has changed since page load. |
5 | 676 |
* |
677 |
* This also happens when TinyMCE is active and editor.save() is triggered by |
|
678 |
* wp.autosave.getPostData(). |
|
679 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
680 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
681 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
682 |
* @return {boolean} True if the post has been changed. |
5 | 683 |
*/ |
684 |
function postChanged() { |
|
685 |
return getCompareString() !== initialCompareString; |
|
0 | 686 |
} |
687 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
688 |
/** |
9 | 689 |
* 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
|
690 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
691 |
* 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
|
692 |
* 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
|
693 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
694 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
695 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
696 |
* @returns {Object} Returns the post data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
697 |
*/ |
5 | 698 |
function save() { |
699 |
var postData, compareString; |
|
0 | 700 |
|
5 | 701 |
// window.autosave() used for back-compat |
702 |
if ( isSuspended || _blockSave || ! window.autosave() ) { |
|
703 |
return false; |
|
704 |
} |
|
0 | 705 |
|
5 | 706 |
if ( ( new Date() ).getTime() < nextRun ) { |
707 |
return false; |
|
708 |
} |
|
709 |
||
710 |
postData = getPostData(); |
|
711 |
compareString = getCompareString( postData ); |
|
0 | 712 |
|
5 | 713 |
// First check |
714 |
if ( typeof lastCompareString === 'undefined' ) { |
|
715 |
lastCompareString = initialCompareString; |
|
716 |
} |
|
0 | 717 |
|
5 | 718 |
// No change |
719 |
if ( compareString === lastCompareString ) { |
|
720 |
return false; |
|
721 |
} |
|
0 | 722 |
|
5 | 723 |
previousCompareString = compareString; |
724 |
tempBlockSave(); |
|
725 |
disableButtons(); |
|
726 |
||
727 |
$document.trigger( 'wpcountwords', [ postData.content ] ) |
|
728 |
.trigger( 'before-autosave', [ postData ] ); |
|
729 |
||
730 |
postData._wpnonce = $( '#_wpnonce' ).val() || ''; |
|
731 |
||
732 |
return postData; |
|
733 |
} |
|
734 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
735 |
/** |
9 | 736 |
* 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
|
737 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
738 |
* @private |
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
742 |
* @returns {void} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
743 |
*/ |
5 | 744 |
function _schedule() { |
745 |
nextRun = ( new Date() ).getTime() + ( autosaveL10n.autosaveInterval * 1000 ) || 60000; |
|
0 | 746 |
} |
747 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
748 |
/** |
9 | 749 |
* Sets the autosaveData on the autosave heartbeat. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
750 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
751 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
752 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
753 |
* @returns {void} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
754 |
*/ |
5 | 755 |
$document.on( 'heartbeat-send.autosave', function( event, data ) { |
756 |
var autosaveData = save(); |
|
757 |
||
758 |
if ( autosaveData ) { |
|
759 |
data.wp_autosave = autosaveData; |
|
760 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
761 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
762 |
/** |
9 | 763 |
* Triggers the autosave of the post with the autosave data on the autosave |
764 |
* heartbeat. |
|
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 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
767 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
768 |
* @returns {void} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
769 |
*/ |
5 | 770 |
}).on( 'heartbeat-tick.autosave', function( event, data ) { |
771 |
if ( data.wp_autosave ) { |
|
772 |
response( data.wp_autosave ); |
|
773 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
774 |
/** |
9 | 775 |
* 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
|
776 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
777 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
778 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
779 |
* @returns {void} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
780 |
*/ |
5 | 781 |
}).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
|
782 |
|
5 | 783 |
// When connection is lost, keep user from submitting changes. |
784 |
if ( 'timeout' === error || 603 === status ) { |
|
785 |
var $notice = $('#lost-connection-notice'); |
|
786 |
||
787 |
if ( ! wp.autosave.local.hasStorage ) { |
|
788 |
$notice.find('.hide-if-no-sessionstorage').hide(); |
|
789 |
} |
|
790 |
||
791 |
$notice.show(); |
|
792 |
disableButtons(); |
|
793 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
794 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
795 |
/** |
9 | 796 |
* Enables buttons when the connection is restored. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
797 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
798 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
799 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
800 |
* @returns {void} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
801 |
*/ |
5 | 802 |
}).on( 'heartbeat-connection-restored.autosave', function() { |
803 |
$('#lost-connection-notice').hide(); |
|
804 |
enableButtons(); |
|
805 |
}).ready( function() { |
|
806 |
_schedule(); |
|
807 |
}); |
|
808 |
||
809 |
return { |
|
810 |
tempBlockSave: tempBlockSave, |
|
811 |
triggerSave: triggerSave, |
|
812 |
postChanged: postChanged, |
|
813 |
suspend: suspend, |
|
814 |
resume: resume |
|
815 |
}; |
|
0 | 816 |
} |
817 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
818 |
/** |
9 | 819 |
* Sets the autosave time out. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
820 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
821 |
* 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
|
822 |
* then save to the textarea before setting initialCompareString. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
823 |
* 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
|
824 |
* extracted from the editor. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
826 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
827 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
* @returns {void} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
829 |
*/ |
5 | 830 |
$document.on( 'tinymce-editor-init.autosave', function( event, editor ) { |
831 |
if ( editor.id === 'content' ) { |
|
832 |
window.setTimeout( function() { |
|
833 |
editor.save(); |
|
834 |
initialCompareString = getCompareString(); |
|
835 |
}, 1000 ); |
|
836 |
} |
|
837 |
}).ready( function() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
838 |
|
5 | 839 |
// Set the initial compare string in case TinyMCE is not used or not loaded first |
840 |
initialCompareString = getCompareString(); |
|
841 |
}); |
|
0 | 842 |
|
5 | 843 |
return { |
844 |
getPostData: getPostData, |
|
845 |
getCompareString: getCompareString, |
|
846 |
disableButtons: disableButtons, |
|
847 |
enableButtons: enableButtons, |
|
848 |
local: autosaveLocal(), |
|
849 |
server: autosaveServer() |
|
850 |
}; |
|
851 |
} |
|
0 | 852 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
853 |
/** @namespace wp */ |
5 | 854 |
window.wp = window.wp || {}; |
855 |
window.wp.autosave = autosave(); |
|
856 |
||
857 |
}( jQuery, window )); |