wp/wp-includes/js/autosave.js
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 22 8c2e4d02f4ef
--- a/wp/wp-includes/js/autosave.js	Tue Dec 15 15:52:01 2020 +0100
+++ b/wp/wp-includes/js/autosave.js	Wed Sep 21 18:19:35 2022 +0200
@@ -30,14 +30,30 @@
 	 * 		disableButtons: disableButtons,
 	 * 		enableButtons: enableButtons,
 	 * 		local: ({hasStorage, getSavedPostData, save, suspend, resume}|*),
-	 * 		server: ({tempBlockSave, triggerSave, postChanged, suspend, resume}|*)}
-	 * 	}
+	 * 		server: ({tempBlockSave, triggerSave, postChanged, suspend, resume}|*)
+	 * 	}}
 	 * 	The object with all functions for autosave.
 	 */
 	function autosave() {
 		var initialCompareString,
-			lastTriggerSave = 0,
-			$document = $(document);
+			initialCompareData = {},
+			lastTriggerSave    = 0,
+			$document          = $( document );
+
+		/**
+		 * Sets the initial compare data.
+		 *
+		 * @since 5.6.1
+		 */
+		function setInitialCompare() {
+			initialCompareData = {
+				post_title: $( '#title' ).val() || '',
+				content: $( '#content' ).val() || '',
+				excerpt: $( '#excerpt' ).val() || ''
+			};
+
+			initialCompareString = getCompareString( initialCompareData );
+		}
 
 		/**
 		 * Returns the data saved in both local and remote autosave.
@@ -525,7 +541,7 @@
 					lastCompareString = getCompareString( postData );
 
 					if ( $( '#title' ).val() !== postData.post_title ) {
-						$( '#title' ).focus().val( postData.post_title || '' );
+						$( '#title' ).trigger( 'focus' ).val( postData.post_title || '' );
 					}
 
 					$( '#excerpt' ).val( postData.excerpt || '' );
@@ -544,8 +560,8 @@
 					} else {
 
 						// Make sure the Text editor is selected.
-						$( '#content-html' ).click();
-						$( '#content' ).focus();
+						$( '#content-html' ).trigger( 'click' );
+						$( '#content' ).trigger( 'focus' );
 
 						// Using document.execCommand() will let the user undo.
 						document.execCommand( 'selectAll' );
@@ -566,7 +582,7 @@
 			 * Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'.
 			 */
 			if ( checkStorage() && blog_id && ( $('#content').length || $('#excerpt').length ) ) {
-				$document.ready( run );
+				$( run );
 			}
 
 			return {
@@ -686,6 +702,32 @@
 			 * @return {boolean} True if the post has been changed.
 			 */
 			function postChanged() {
+				var changed = false;
+
+				// If there are TinyMCE instances, loop through them.
+				if ( window.tinymce ) {
+					window.tinymce.each( [ 'content', 'excerpt' ], function( field ) {
+						var editor = window.tinymce.get( field );
+
+						if ( ! editor || editor.isHidden() ) {
+							if ( ( $( '#' + field ).val() || '' ) !== initialCompareData[ field ] ) {
+								changed = true;
+								// Break.
+								return false;
+							}
+						} else if ( editor.isDirty() ) {
+							changed = true;
+							return false;
+						}
+					} );
+
+					if ( ( $( '#title' ).val() || '' ) !== initialCompareData.post_title ) {
+						changed = true;
+					}
+
+					return changed;
+				}
+
 				return getCompareString() !== initialCompareString;
 			}
 
@@ -756,7 +798,9 @@
 			 *
 			 * @return {void}
 			 */
-			$document.on( 'heartbeat-send.autosave', function( event, data ) {
+			$( function() {
+				_schedule();
+			}).on( 'heartbeat-send.autosave', function( event, data ) {
 				var autosaveData = save();
 
 				if ( autosaveData ) {
@@ -806,8 +850,6 @@
 			}).on( 'heartbeat-connection-restored.autosave', function() {
 				$('#lost-connection-notice').hide();
 				enableButtons();
-			}).ready( function() {
-				_schedule();
 			});
 
 			return {
@@ -831,17 +873,17 @@
 		 *
 		 * @return {void}
 		 */
-		$document.on( 'tinymce-editor-init.autosave', function( event, editor ) {
-			if ( editor.id === 'content' ) {
+		$( function() {
+			// Set the initial compare string in case TinyMCE is not used or not loaded first.
+			setInitialCompare();
+		}).on( 'tinymce-editor-init.autosave', function( event, editor ) {
+			// Reset the initialCompare data after the TinyMCE instances have been initialized.
+			if ( 'content' === editor.id || 'excerpt' === editor.id ) {
 				window.setTimeout( function() {
 					editor.save();
-					initialCompareString = getCompareString();
+					setInitialCompare();
 				}, 1000 );
 			}
-		}).ready( function() {
-
-			// Set the initial compare string in case TinyMCE is not used or not loaded first.
-			initialCompareString = getCompareString();
 		});
 
 		return {