28 * getPostData: getPostData, |
28 * getPostData: getPostData, |
29 * getCompareString: getCompareString, |
29 * getCompareString: getCompareString, |
30 * disableButtons: disableButtons, |
30 * disableButtons: disableButtons, |
31 * enableButtons: enableButtons, |
31 * enableButtons: enableButtons, |
32 * local: ({hasStorage, getSavedPostData, save, suspend, resume}|*), |
32 * local: ({hasStorage, getSavedPostData, save, suspend, resume}|*), |
33 * server: ({tempBlockSave, triggerSave, postChanged, suspend, resume}|*)} |
33 * server: ({tempBlockSave, triggerSave, postChanged, suspend, resume}|*) |
34 * } |
34 * }} |
35 * The object with all functions for autosave. |
35 * The object with all functions for autosave. |
36 */ |
36 */ |
37 function autosave() { |
37 function autosave() { |
38 var initialCompareString, |
38 var initialCompareString, |
39 lastTriggerSave = 0, |
39 initialCompareData = {}, |
40 $document = $(document); |
40 lastTriggerSave = 0, |
|
41 $document = $( document ); |
|
42 |
|
43 /** |
|
44 * Sets the initial compare data. |
|
45 * |
|
46 * @since 5.6.1 |
|
47 */ |
|
48 function setInitialCompare() { |
|
49 initialCompareData = { |
|
50 post_title: $( '#title' ).val() || '', |
|
51 content: $( '#content' ).val() || '', |
|
52 excerpt: $( '#excerpt' ).val() || '' |
|
53 }; |
|
54 |
|
55 initialCompareString = getCompareString( initialCompareData ); |
|
56 } |
41 |
57 |
42 /** |
58 /** |
43 * Returns the data saved in both local and remote autosave. |
59 * Returns the data saved in both local and remote autosave. |
44 * |
60 * |
45 * @since 3.9.0 |
61 * @since 3.9.0 |
523 if ( postData ) { |
539 if ( postData ) { |
524 // Set the last saved data. |
540 // Set the last saved data. |
525 lastCompareString = getCompareString( postData ); |
541 lastCompareString = getCompareString( postData ); |
526 |
542 |
527 if ( $( '#title' ).val() !== postData.post_title ) { |
543 if ( $( '#title' ).val() !== postData.post_title ) { |
528 $( '#title' ).focus().val( postData.post_title || '' ); |
544 $( '#title' ).trigger( 'focus' ).val( postData.post_title || '' ); |
529 } |
545 } |
530 |
546 |
531 $( '#excerpt' ).val( postData.excerpt || '' ); |
547 $( '#excerpt' ).val( postData.excerpt || '' ); |
532 editor = getEditor(); |
548 editor = getEditor(); |
533 |
549 |
542 editor.nodeChanged(); |
558 editor.nodeChanged(); |
543 }); |
559 }); |
544 } else { |
560 } else { |
545 |
561 |
546 // Make sure the Text editor is selected. |
562 // Make sure the Text editor is selected. |
547 $( '#content-html' ).click(); |
563 $( '#content-html' ).trigger( 'click' ); |
548 $( '#content' ).focus(); |
564 $( '#content' ).trigger( 'focus' ); |
549 |
565 |
550 // Using document.execCommand() will let the user undo. |
566 // Using document.execCommand() will let the user undo. |
551 document.execCommand( 'selectAll' ); |
567 document.execCommand( 'selectAll' ); |
552 document.execCommand( 'insertText', false, postData.content || '' ); |
568 document.execCommand( 'insertText', false, postData.content || '' ); |
553 } |
569 } |
564 * Check if the browser supports sessionStorage and it's not disabled, |
580 * Check if the browser supports sessionStorage and it's not disabled, |
565 * then initialize and run checkPost(). |
581 * then initialize and run checkPost(). |
566 * Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'. |
582 * Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'. |
567 */ |
583 */ |
568 if ( checkStorage() && blog_id && ( $('#content').length || $('#excerpt').length ) ) { |
584 if ( checkStorage() && blog_id && ( $('#content').length || $('#excerpt').length ) ) { |
569 $document.ready( run ); |
585 $( run ); |
570 } |
586 } |
571 |
587 |
572 return { |
588 return { |
573 hasStorage: hasStorage, |
589 hasStorage: hasStorage, |
574 getSavedPostData: getSavedPostData, |
590 getSavedPostData: getSavedPostData, |
684 * @since 3.9.0 |
700 * @since 3.9.0 |
685 * |
701 * |
686 * @return {boolean} True if the post has been changed. |
702 * @return {boolean} True if the post has been changed. |
687 */ |
703 */ |
688 function postChanged() { |
704 function postChanged() { |
|
705 var changed = false; |
|
706 |
|
707 // If there are TinyMCE instances, loop through them. |
|
708 if ( window.tinymce ) { |
|
709 window.tinymce.each( [ 'content', 'excerpt' ], function( field ) { |
|
710 var editor = window.tinymce.get( field ); |
|
711 |
|
712 if ( ! editor || editor.isHidden() ) { |
|
713 if ( ( $( '#' + field ).val() || '' ) !== initialCompareData[ field ] ) { |
|
714 changed = true; |
|
715 // Break. |
|
716 return false; |
|
717 } |
|
718 } else if ( editor.isDirty() ) { |
|
719 changed = true; |
|
720 return false; |
|
721 } |
|
722 } ); |
|
723 |
|
724 if ( ( $( '#title' ).val() || '' ) !== initialCompareData.post_title ) { |
|
725 changed = true; |
|
726 } |
|
727 |
|
728 return changed; |
|
729 } |
|
730 |
689 return getCompareString() !== initialCompareString; |
731 return getCompareString() !== initialCompareString; |
690 } |
732 } |
691 |
733 |
692 /** |
734 /** |
693 * Checks if the post can be saved or not. |
735 * Checks if the post can be saved or not. |
804 * @return {void} |
848 * @return {void} |
805 */ |
849 */ |
806 }).on( 'heartbeat-connection-restored.autosave', function() { |
850 }).on( 'heartbeat-connection-restored.autosave', function() { |
807 $('#lost-connection-notice').hide(); |
851 $('#lost-connection-notice').hide(); |
808 enableButtons(); |
852 enableButtons(); |
809 }).ready( function() { |
|
810 _schedule(); |
|
811 }); |
853 }); |
812 |
854 |
813 return { |
855 return { |
814 tempBlockSave: tempBlockSave, |
856 tempBlockSave: tempBlockSave, |
815 triggerSave: triggerSave, |
857 triggerSave: triggerSave, |
829 * |
871 * |
830 * @since 3.9.0 |
872 * @since 3.9.0 |
831 * |
873 * |
832 * @return {void} |
874 * @return {void} |
833 */ |
875 */ |
834 $document.on( 'tinymce-editor-init.autosave', function( event, editor ) { |
876 $( function() { |
835 if ( editor.id === 'content' ) { |
877 // Set the initial compare string in case TinyMCE is not used or not loaded first. |
|
878 setInitialCompare(); |
|
879 }).on( 'tinymce-editor-init.autosave', function( event, editor ) { |
|
880 // Reset the initialCompare data after the TinyMCE instances have been initialized. |
|
881 if ( 'content' === editor.id || 'excerpt' === editor.id ) { |
836 window.setTimeout( function() { |
882 window.setTimeout( function() { |
837 editor.save(); |
883 editor.save(); |
838 initialCompareString = getCompareString(); |
884 setInitialCompare(); |
839 }, 1000 ); |
885 }, 1000 ); |
840 } |
886 } |
841 }).ready( function() { |
|
842 |
|
843 // Set the initial compare string in case TinyMCE is not used or not loaded first. |
|
844 initialCompareString = getCompareString(); |
|
845 }); |
887 }); |
846 |
888 |
847 return { |
889 return { |
848 getPostData: getPostData, |
890 getPostData: getPostData, |
849 getCompareString: getCompareString, |
891 getCompareString: getCompareString, |