TinyMCE: fix adding of too many undo levels for wpviews. The HTML changes several times when a wpview is added. We only want one undo level. Also fixes cases when the cursor is next to an embeddable URL in the Text tab and the user switches to the Visual tab.

See #45307.
Built from https://develop.svn.wordpress.org/trunk@45631


git-svn-id: http://core.svn.wordpress.org/trunk@45442 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2019-07-12 03:25:55 +00:00
parent 4d3a84aa9f
commit ee8813606d
6 changed files with 38 additions and 13 deletions

View File

@ -444,12 +444,16 @@
'<div class="wpview wpview-wrap" data-wpview-text="' + this.encodedText + '" data-wpview-type="' + this.type + '" contenteditable="false"></div>'
);
editor.$( node ).replaceWith( $viewNode );
editor.undoManager.ignore( function() {
editor.$( node ).replaceWith( $viewNode );
} );
if ( selected ) {
setTimeout( function() {
editor.selection.select( $viewNode[0] );
editor.selection.collapse();
editor.undoManager.ignore( function() {
editor.selection.select( $viewNode[0] );
editor.selection.collapse();
} );
} );
}
} );
@ -961,8 +965,9 @@
views.register( 'embedURL', _.extend( {}, embed, {
match: function( content ) {
var re = /(^|<p>)(https?:\/\/[^\s"]+?)(<\/p>\s*|$)/gi,
match = re.exec( content );
// There may be a "bookmark" node next to the URL...
var re = /(^|<p>(?:<span data-mce-type="bookmark"[^>]+>\s*<\/span>)?)(https?:\/\/[^\s"]+?)((?:<span data-mce-type="bookmark"[^>]+>\s*<\/span>)?<\/p>\s*|$)/gi;
var match = re.exec( content );
if ( match ) {
return {

File diff suppressed because one or more lines are too long

View File

@ -25,7 +25,7 @@
return '<p>' + window.decodeURIComponent( $1 ) + '</p>';
}
if ( ! content ) {
if ( ! content || content.indexOf( ' data-wpview-' ) === -1 ) {
return content;
}
@ -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.

View File

@ -1 +1 @@
!function(a){a.PluginManager.add("wpview",function(b){function c(){}function d(a){return b.dom.hasClass(a,"wpview")}function e(a){function b(a,b){return"<p>"+window.decodeURIComponent(b)+"</p>"}return a?a.replace(/<div[^>]+data-wpview-text="([^"]+)"[^>]*>(?:\.|[\s\S]+?wpview-end[^>]+>\s*<\/span>\s*)?<\/div>/g,b).replace(/<p[^>]+data-wpview-marker="([^"]+)"[^>]*>[\s\S]*?<\/p>/g,b):a}var f=window.wp;return f&&f.mce&&f.mce.views?(b.on("init",function(){var a=window.MutationObserver||window.WebKitMutationObserver;a&&new a(function(){b.fire("wp-body-class-change")}).observe(b.getBody(),{attributes:!0,attributeFilter:["class"]}),b.on("wp-body-class-change",function(){var a=b.getBody().className;b.$('iframe[class="wpview-sandbox"]').each(function(b,c){if(!c.src||'javascript:""'===c.src)try{c.contentWindow.document.body.className=a}catch(d){}})})}),b.on("beforesetcontent",function(a){var c;if(a.selection||f.mce.views.unbind(),a.content){if(!a.load&&(c=b.selection.getNode(),c&&c!==b.getBody()&&/^\s*https?:\/\/\S+\s*$/i.test(a.content))){if(c=b.dom.getParent(c,"p"),!c||!/^[\s\uFEFF\u00A0]*$/.test(b.$(c).text()||""))return;c.innerHTML=""}a.content=f.mce.views.setMarkers(a.content,b)}}),b.on("setcontent",function(){f.mce.views.render()}),b.on("preprocess hide",function(a){b.$("div[data-wpview-text], p[data-wpview-marker]",a.node).each(function(a,b){b.innerHTML="."})},!0),b.on("postprocess",function(a){a.content=e(a.content)}),b.on("beforeaddundo",function(a){a.level.content=e(a.level.content)}),b.on("drop objectselected",function(a){d(a.targetClone)&&(a.targetClone=b.getDoc().createTextNode(window.decodeURIComponent(b.dom.getAttrib(a.targetClone,"data-wpview-text"))))}),b.on("pastepreprocess",function(b){var c=b.content;c&&(c=a.trim(c.replace(/<[^>]+>/g,"")),/^https?:\/\/\S+$/i.test(c)&&(b.content=c))}),b.on("resolvename",function(a){d(a.target)&&(a.name=b.dom.getAttrib(a.target,"data-wpview-type")||"object")}),b.on("click keyup",function(){var a=b.selection.getNode();d(a)&&b.dom.getAttrib(a,"data-mce-selected")&&a.setAttribute("data-mce-selected","2")}),b.addButton("wp_view_edit",{tooltip:"Edit|button",icon:"dashicon dashicons-edit",onclick:function(){var a=b.selection.getNode();d(a)&&f.mce.views.edit(b,a)}}),b.addButton("wp_view_remove",{tooltip:"Remove",icon:"dashicon dashicons-no",onclick:function(){b.fire("cut")}}),b.once("preinit",function(){var a;b.wp&&b.wp._createToolbar&&(a=b.wp._createToolbar(["wp_view_edit","wp_view_remove"]),b.on("wptoolbar",function(b){!b.collapsed&&d(b.element)&&(b.toolbar=a)}))}),b.wp=b.wp||{},b.wp.getView=c,b.wp.setViewCursor=c,{getView:c}):{getView:c}})}(window.tinymce);
!function(a){a.PluginManager.add("wpview",function(b){function c(){}function d(a){return b.dom.hasClass(a,"wpview")}function e(a){function b(a,b){return"<p>"+window.decodeURIComponent(b)+"</p>"}return a&&a.indexOf(" data-wpview-")!==-1?a.replace(/<div[^>]+data-wpview-text="([^"]+)"[^>]*>(?:\.|[\s\S]+?wpview-end[^>]+>\s*<\/span>\s*)?<\/div>/g,b).replace(/<p[^>]+data-wpview-marker="([^"]+)"[^>]*>[\s\S]*?<\/p>/g,b):a}var f=window.wp;return f&&f.mce&&f.mce.views?(b.on("init",function(){var a=window.MutationObserver||window.WebKitMutationObserver;a&&new a(function(){b.fire("wp-body-class-change")}).observe(b.getBody(),{attributes:!0,attributeFilter:["class"]}),b.on("wp-body-class-change",function(){var a=b.getBody().className;b.$('iframe[class="wpview-sandbox"]').each(function(b,c){if(!c.src||'javascript:""'===c.src)try{c.contentWindow.document.body.className=a}catch(d){}})})}),b.on("beforesetcontent",function(a){var c;if(a.selection||f.mce.views.unbind(),a.content){if(!a.load&&(c=b.selection.getNode(),c&&c!==b.getBody()&&/^\s*https?:\/\/\S+\s*$/i.test(a.content))){if(c=b.dom.getParent(c,"p"),!c||!/^[\s\uFEFF\u00A0]*$/.test(b.$(c).text()||""))return;c.innerHTML=""}a.content=f.mce.views.setMarkers(a.content,b)}}),b.on("setcontent",function(){f.mce.views.render()}),b.on("preprocess hide",function(a){b.$("div[data-wpview-text], p[data-wpview-marker]",a.node).each(function(a,b){b.innerHTML="."})},!0),b.on("postprocess",function(a){a.content=e(a.content)}),b.on("beforeaddundo",function(a){var c,d=a.level.content||a.level.fragments&&a.level.fragments.join("");c=a.lastLevel?a.lastLevel.content||a.lastLevel.fragments&&a.lastLevel.fragments.join(""):b.startContent,d&&c&&d.indexOf(" data-wpview-")!==-1&&c.indexOf(" data-wpview-")!==-1&&e(c)===e(d)&&a.preventDefault()}),b.on("drop objectselected",function(a){d(a.targetClone)&&(a.targetClone=b.getDoc().createTextNode(window.decodeURIComponent(b.dom.getAttrib(a.targetClone,"data-wpview-text"))))}),b.on("pastepreprocess",function(b){var c=b.content;c&&(c=a.trim(c.replace(/<[^>]+>/g,"")),/^https?:\/\/\S+$/i.test(c)&&(b.content=c))}),b.on("resolvename",function(a){d(a.target)&&(a.name=b.dom.getAttrib(a.target,"data-wpview-type")||"object")}),b.on("click keyup",function(){var a=b.selection.getNode();d(a)&&b.dom.getAttrib(a,"data-mce-selected")&&a.setAttribute("data-mce-selected","2")}),b.addButton("wp_view_edit",{tooltip:"Edit|button",icon:"dashicon dashicons-edit",onclick:function(){var a=b.selection.getNode();d(a)&&f.mce.views.edit(b,a)}}),b.addButton("wp_view_remove",{tooltip:"Remove",icon:"dashicon dashicons-no",onclick:function(){b.fire("cut")}}),b.once("preinit",function(){var a;b.wp&&b.wp._createToolbar&&(a=b.wp._createToolbar(["wp_view_edit","wp_view_remove"]),b.on("wptoolbar",function(b){!b.collapsed&&d(b.element)&&(b.toolbar=a)}))}),b.wp=b.wp||{},b.wp.getView=c,b.wp.setViewCursor=c,{getView:c}):{getView:c}})}(window.tinymce);

View File

@ -46,4 +46,4 @@ tinymce.PluginManager.add("wpgallery",function(a){function b(a){return a.replace
// Source: wp-includes/js/tinymce/plugins/wptextpattern/plugin.min.js
!function(a,b){function c(a){return a.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}a.Env.ie&&a.Env.ie<9||a.PluginManager.add("wptextpattern",function(d){function e(){var e,f,g,h,i,k=d.selection.getRng(),l=k.startContainer,m=k.startOffset;if(l&&3===l.nodeType&&l.data.length&&m){var n=l.data.slice(0,m),p=l.data.charAt(m-1);a.each(o,function(a){if(p===a.delimiter.slice(-1)){var b=c(a.delimiter),d=a.delimiter.charAt(0),h=new RegExp("(.*)"+b+".+"+b+"$"),i=n.match(h);if(i){e=i[1].length,f=m-a.delimiter.length;var j=n.charAt(e-1),k=n.charAt(e+a.delimiter.length);if(!(e&&/\S/.test(j)&&(/\s/.test(k)||j===d)||new RegExp("^[\\s"+c(d)+"]+$").test(n.slice(e,f))))return g=a,!1}}}),g&&(h=d.formatter.get(g.format),h&&h[0].inline&&(d.undoManager.add(),d.undoManager.transact(function(){l.insertData(m,"\ufeff"),l=l.splitText(e),i=l.splitText(m-e),l.deleteData(0,g.delimiter.length),l.deleteData(l.data.length-g.delimiter.length,g.delimiter.length),d.formatter.apply(g.format,{},l),d.selection.setCursorLocation(i,1)}),b(function(){j="space",d.once("selectionchange",function(){var a;i&&(a=i.data.indexOf("\ufeff"),a!==-1&&i.deleteData(a,a+1))})})))}}function f(a){var b,c=d.dom.getParent(a,"p");if(c){for(;(b=c.firstChild)&&3!==b.nodeType;)c=b;if(b)return b.data||(b=b.nextSibling&&3===b.nextSibling.nodeType?b.nextSibling:null),b}}function g(){var c,e,g=d.selection.getRng(),h=g.startContainer;h&&f(h)===h&&(c=h.parentNode,e=h.data,a.each(m,function(a){var f=e.match(a.regExp);if(f&&g.startOffset===f[0].length)return d.undoManager.add(),d.undoManager.transact(function(){h.deleteData(0,f[0].length),c.innerHTML||c.appendChild(document.createElement("br")),d.selection.setCursorLocation(c),d.execCommand(a.cmd)}),b(function(){j="space"}),!1}))}function h(){var c,e,g,h=d.selection.getRng(),k=h.startContainer,l=f(k),m=n.length;if(l){for(c=l.data;m--;)if(n[m].start){if(0===c.indexOf(n[m].start)){e=n[m];break}}else if(n[m].regExp&&n[m].regExp.test(c)){e=n[m];break}e&&(l===k&&a.trim(c)===e.start||d.once("keyup",function(){d.undoManager.add(),d.undoManager.transact(function(){e.format?(d.formatter.apply(e.format,{},l),l.replaceData(0,l.data.length,i(l.data.slice(e.start.length)))):e.element&&(g=l.parentNode&&l.parentNode.parentNode,g&&g.replaceChild(document.createElement(e.element),l.parentNode))}),b(function(){j="enter"})}))}}function i(a){return a?a.replace(/^\s+/,""):""}var j,k=a.util.VK,l=d.settings.wptextpattern||{},m=l.space||[{regExp:/^[*-]\s/,cmd:"InsertUnorderedList"},{regExp:/^1[.)]\s/,cmd:"InsertOrderedList"}],n=l.enter||[{start:"##",format:"h2"},{start:"###",format:"h3"},{start:"####",format:"h4"},{start:"#####",format:"h5"},{start:"######",format:"h6"},{start:">",format:"blockquote"},{regExp:/^(-){3,}$/,element:"hr"}],o=l.inline||[{delimiter:"`",format:"code"}];d.on("selectionchange",function(){j=null}),d.on("keydown",function(a){(j&&27===a.keyCode||"space"===j&&a.keyCode===k.BACKSPACE)&&(d.undoManager.undo(),a.preventDefault(),a.stopImmediatePropagation()),k.metaKeyPressed(a)||(a.keyCode===k.ENTER?h():a.keyCode===k.SPACEBAR?b(g):a.keyCode>47&&!(a.keyCode>=91&&a.keyCode<=93)&&b(e))},!0)})}(window.tinymce,window.setTimeout);
// Source: wp-includes/js/tinymce/plugins/wpview/plugin.min.js
!function(a){a.PluginManager.add("wpview",function(b){function c(){}function d(a){return b.dom.hasClass(a,"wpview")}function e(a){function b(a,b){return"<p>"+window.decodeURIComponent(b)+"</p>"}return a?a.replace(/<div[^>]+data-wpview-text="([^"]+)"[^>]*>(?:\.|[\s\S]+?wpview-end[^>]+>\s*<\/span>\s*)?<\/div>/g,b).replace(/<p[^>]+data-wpview-marker="([^"]+)"[^>]*>[\s\S]*?<\/p>/g,b):a}var f=window.wp;return f&&f.mce&&f.mce.views?(b.on("init",function(){var a=window.MutationObserver||window.WebKitMutationObserver;a&&new a(function(){b.fire("wp-body-class-change")}).observe(b.getBody(),{attributes:!0,attributeFilter:["class"]}),b.on("wp-body-class-change",function(){var a=b.getBody().className;b.$('iframe[class="wpview-sandbox"]').each(function(b,c){if(!c.src||'javascript:""'===c.src)try{c.contentWindow.document.body.className=a}catch(d){}})})}),b.on("beforesetcontent",function(a){var c;if(a.selection||f.mce.views.unbind(),a.content){if(!a.load&&(c=b.selection.getNode(),c&&c!==b.getBody()&&/^\s*https?:\/\/\S+\s*$/i.test(a.content))){if(c=b.dom.getParent(c,"p"),!c||!/^[\s\uFEFF\u00A0]*$/.test(b.$(c).text()||""))return;c.innerHTML=""}a.content=f.mce.views.setMarkers(a.content,b)}}),b.on("setcontent",function(){f.mce.views.render()}),b.on("preprocess hide",function(a){b.$("div[data-wpview-text], p[data-wpview-marker]",a.node).each(function(a,b){b.innerHTML="."})},!0),b.on("postprocess",function(a){a.content=e(a.content)}),b.on("beforeaddundo",function(a){a.level.content=e(a.level.content)}),b.on("drop objectselected",function(a){d(a.targetClone)&&(a.targetClone=b.getDoc().createTextNode(window.decodeURIComponent(b.dom.getAttrib(a.targetClone,"data-wpview-text"))))}),b.on("pastepreprocess",function(b){var c=b.content;c&&(c=a.trim(c.replace(/<[^>]+>/g,"")),/^https?:\/\/\S+$/i.test(c)&&(b.content=c))}),b.on("resolvename",function(a){d(a.target)&&(a.name=b.dom.getAttrib(a.target,"data-wpview-type")||"object")}),b.on("click keyup",function(){var a=b.selection.getNode();d(a)&&b.dom.getAttrib(a,"data-mce-selected")&&a.setAttribute("data-mce-selected","2")}),b.addButton("wp_view_edit",{tooltip:"Edit|button",icon:"dashicon dashicons-edit",onclick:function(){var a=b.selection.getNode();d(a)&&f.mce.views.edit(b,a)}}),b.addButton("wp_view_remove",{tooltip:"Remove",icon:"dashicon dashicons-no",onclick:function(){b.fire("cut")}}),b.once("preinit",function(){var a;b.wp&&b.wp._createToolbar&&(a=b.wp._createToolbar(["wp_view_edit","wp_view_remove"]),b.on("wptoolbar",function(b){!b.collapsed&&d(b.element)&&(b.toolbar=a)}))}),b.wp=b.wp||{},b.wp.getView=c,b.wp.setViewCursor=c,{getView:c}):{getView:c}})}(window.tinymce);
!function(a){a.PluginManager.add("wpview",function(b){function c(){}function d(a){return b.dom.hasClass(a,"wpview")}function e(a){function b(a,b){return"<p>"+window.decodeURIComponent(b)+"</p>"}return a&&a.indexOf(" data-wpview-")!==-1?a.replace(/<div[^>]+data-wpview-text="([^"]+)"[^>]*>(?:\.|[\s\S]+?wpview-end[^>]+>\s*<\/span>\s*)?<\/div>/g,b).replace(/<p[^>]+data-wpview-marker="([^"]+)"[^>]*>[\s\S]*?<\/p>/g,b):a}var f=window.wp;return f&&f.mce&&f.mce.views?(b.on("init",function(){var a=window.MutationObserver||window.WebKitMutationObserver;a&&new a(function(){b.fire("wp-body-class-change")}).observe(b.getBody(),{attributes:!0,attributeFilter:["class"]}),b.on("wp-body-class-change",function(){var a=b.getBody().className;b.$('iframe[class="wpview-sandbox"]').each(function(b,c){if(!c.src||'javascript:""'===c.src)try{c.contentWindow.document.body.className=a}catch(d){}})})}),b.on("beforesetcontent",function(a){var c;if(a.selection||f.mce.views.unbind(),a.content){if(!a.load&&(c=b.selection.getNode(),c&&c!==b.getBody()&&/^\s*https?:\/\/\S+\s*$/i.test(a.content))){if(c=b.dom.getParent(c,"p"),!c||!/^[\s\uFEFF\u00A0]*$/.test(b.$(c).text()||""))return;c.innerHTML=""}a.content=f.mce.views.setMarkers(a.content,b)}}),b.on("setcontent",function(){f.mce.views.render()}),b.on("preprocess hide",function(a){b.$("div[data-wpview-text], p[data-wpview-marker]",a.node).each(function(a,b){b.innerHTML="."})},!0),b.on("postprocess",function(a){a.content=e(a.content)}),b.on("beforeaddundo",function(a){var c,d=a.level.content||a.level.fragments&&a.level.fragments.join("");c=a.lastLevel?a.lastLevel.content||a.lastLevel.fragments&&a.lastLevel.fragments.join(""):b.startContent,d&&c&&d.indexOf(" data-wpview-")!==-1&&c.indexOf(" data-wpview-")!==-1&&e(c)===e(d)&&a.preventDefault()}),b.on("drop objectselected",function(a){d(a.targetClone)&&(a.targetClone=b.getDoc().createTextNode(window.decodeURIComponent(b.dom.getAttrib(a.targetClone,"data-wpview-text"))))}),b.on("pastepreprocess",function(b){var c=b.content;c&&(c=a.trim(c.replace(/<[^>]+>/g,"")),/^https?:\/\/\S+$/i.test(c)&&(b.content=c))}),b.on("resolvename",function(a){d(a.target)&&(a.name=b.dom.getAttrib(a.target,"data-wpview-type")||"object")}),b.on("click keyup",function(){var a=b.selection.getNode();d(a)&&b.dom.getAttrib(a,"data-mce-selected")&&a.setAttribute("data-mce-selected","2")}),b.addButton("wp_view_edit",{tooltip:"Edit|button",icon:"dashicon dashicons-edit",onclick:function(){var a=b.selection.getNode();d(a)&&f.mce.views.edit(b,a)}}),b.addButton("wp_view_remove",{tooltip:"Remove",icon:"dashicon dashicons-no",onclick:function(){b.fire("cut")}}),b.once("preinit",function(){var a;b.wp&&b.wp._createToolbar&&(a=b.wp._createToolbar(["wp_view_edit","wp_view_remove"]),b.on("wptoolbar",function(b){!b.collapsed&&d(b.element)&&(b.toolbar=a)}))}),b.wp=b.wp||{},b.wp.getView=c,b.wp.setViewCursor=c,{getView:c}):{getView:c}})}(window.tinymce);

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.3-alpha-45630';
$wp_version = '5.3-alpha-45631';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.