autosave.js 21 KB

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