wp/wp-includes/js/tinymce/plugins/wpview/plugin.js
changeset 16 a86126ab1dd4
parent 9 177826044cd9
--- a/wp/wp-includes/js/tinymce/plugins/wpview/plugin.js	Tue Oct 22 16:11:46 2019 +0200
+++ b/wp/wp-includes/js/tinymce/plugins/wpview/plugin.js	Tue Dec 15 13:49:49 2020 +0100
@@ -25,7 +25,7 @@
 				return '<p>' + window.decodeURIComponent( $1 ) + '</p>';
 			}
 
-			if ( ! content ) {
+			if ( ! content || content.indexOf( ' data-wpview-' ) === -1 ) {
 				return content;
 			}
 
@@ -52,7 +52,7 @@
 				var className = editor.getBody().className;
 
 				editor.$( 'iframe[class="wpview-sandbox"]' ).each( function( i, iframe ) {
-					// Make sure it is a local iframe
+					// Make sure it is a local iframe.
 					// jshint scripturl: true
 					if ( ! iframe.src || iframe.src === 'javascript:""' ) {
 						try {
@@ -79,11 +79,11 @@
 				node = editor.selection.getNode();
 
 				if ( node && node !== editor.getBody() && /^\s*https?:\/\/\S+\s*$/i.test( event.content ) ) {
-					// When a url is pasted or inserted, only try to embed it when it is in an empty paragrapgh.
+					// When a url is pasted or inserted, only try to embed it when it is in an empty paragraph.
 					node = editor.dom.getParent( node, 'p' );
 
 					if ( node && /^[\s\uFEFF\u00A0]*$/.test( editor.$( node ).text() || '' ) ) {
-						// Make sure there are no empty inline elements in the <p>
+						// Make sure there are no empty inline elements in the <p>.
 						node.innerHTML = '';
 					} else {
 						return;
@@ -111,10 +111,30 @@
 			event.content = resetViews( event.content );
 		} );
 
-		// Replace views with their text inside undo levels.
-		// This also prevents that new levels are added when there are changes inside the views.
+		// Prevent adding of undo levels when replacing wpview markers
+		// or when there are changes only in the (non-editable) previews.
 		editor.on( 'beforeaddundo', function( event ) {
-			event.level.content = resetViews( event.level.content );
+			var lastContent;
+			var newContent = event.level.content || ( event.level.fragments && event.level.fragments.join( '' ) );
+
+			if ( ! event.lastLevel ) {
+				lastContent = editor.startContent;
+			} else {
+				lastContent = event.lastLevel.content || ( event.lastLevel.fragments && event.lastLevel.fragments.join( '' ) );
+			}
+
+			if (
+				! newContent ||
+				! lastContent ||
+				newContent.indexOf( ' data-wpview-' ) === -1 ||
+				lastContent.indexOf( ' data-wpview-' ) === -1
+			) {
+				return;
+			}
+
+			if ( resetViews( lastContent ) === resetViews( newContent ) ) {
+				event.preventDefault();
+			}
 		} );
 
 		// Make sure views are copied as their text.
@@ -158,7 +178,7 @@
 		} );
 
 		editor.addButton( 'wp_view_edit', {
-			tooltip: 'Edit|button', // '|button' is not displayed, only used for context
+			tooltip: 'Edit|button', // '|button' is not displayed, only used for context.
 			icon: 'dashicon dashicons-edit',
 			onclick: function() {
 				var node = editor.selection.getNode();