From b3d84f15e1b673d7ec703ca6260d51e771dc89d3 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 15 Dec 2023 13:37:01 -0500 Subject: [PATCH] Update TinyMCE and CKEditor modules to recognize versions in URLs --- .../plugins/pwimage/plugin.js | 13 ++++++++-- .../plugins/pwimage/plugin.min.js | 2 +- .../InputfieldTinyMCE/InputfieldTinyMCE.js | 6 +++++ .../InputfieldTinyMCE/plugins/pwimage.js | 26 ++++++++++++++++--- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/wire/modules/Inputfield/InputfieldCKEditor/plugins/pwimage/plugin.js b/wire/modules/Inputfield/InputfieldCKEditor/plugins/pwimage/plugin.js index a9807c5e..1483c7ed 100644 --- a/wire/modules/Inputfield/InputfieldCKEditor/plugins/pwimage/plugin.js +++ b/wire/modules/Inputfield/InputfieldCKEditor/plugins/pwimage/plugin.js @@ -180,6 +180,14 @@ var modalUri = ProcessWire.config.urls.admin + 'page/image/'; var queryString = '?id=' + page_id + '&edit_page_id=' + edit_page_id + '&modal=1'; + var version = 0; + + if(typeof ProcessWire.config.PagesVersions !== 'undefined') { + if(ProcessWire.config.PagesVersions.page == page_id) { + version = ProcessWire.config.PagesVersions.version; + queryString += '&version=' + version; + } + } if(file.length) queryString += "&file=" + file; if(imgWidth) queryString += "&width=" + imgWidth; @@ -320,7 +328,8 @@ '&file=' + file + '&width=' + width + '&height=' + height + - '&hidpi=' + hidpi; + '&hidpi=' + hidpi + + '&version=' + version; if(rotate) resizeURL += '&rotate=' + rotate; if($img.hasClass('flip_horizontal')) resizeURL += '&flip=h'; @@ -339,7 +348,7 @@ click: function() { var $i = $iframe.contents(); var page_id = jQuery("#page_id", $i).val(); - $iframe.attr('src', modalUri + '?id=' + page_id + '&modal=1'); + $iframe.attr('src', modalUri + '?id=' + page_id + '&modal=1&version=' + version); $iframe.setButtons({}); } }, { diff --git a/wire/modules/Inputfield/InputfieldCKEditor/plugins/pwimage/plugin.min.js b/wire/modules/Inputfield/InputfieldCKEditor/plugins/pwimage/plugin.min.js index 7cf7d383..1602ebfd 100644 --- a/wire/modules/Inputfield/InputfieldCKEditor/plugins/pwimage/plugin.min.js +++ b/wire/modules/Inputfield/InputfieldCKEditor/plugins/pwimage/plugin.min.js @@ -1 +1 @@ -(function(){CKEDITOR.plugins.add("pwimage",{requires:"dialog",init:function(editor){var pluginName="pwimage";var allowed="img[alt,id,!src,title,width](align_left,align_center,align_right,hidpi,align-left,align-center,align-right);"+"a[!href];"+"figure{width}(align_left,align_center,align_right,hidpi,align-left,align-center,align-right);"+"figcaption;";var required="img[alt,src]";editor.addCommand(pluginName,{allowedContent:allowed,requiredContent:required,exec:loadIframeImagePicker});editor.ui.addButton("PWImage",{label:editor.lang.common.image,command:pluginName,hidpi:true,icon:CKEDITOR.env.hidpi?this.path+"images/hidpi/pwimage.png":this.path+"images/pwimage.png"});editor.on("doubleclick",function(evt){var element=evt.data.element;if(element.is("img")&&!element.data("cke-realelement")&&!element.isReadOnly()){evt.cancel();editor.commands.pwimage.exec()}});if(editor.addMenuItems){editor.addMenuItems({image:{label:editor.lang.image.menu,command:"pwimage",group:"image"}})}if(editor.contextMenu){editor.contextMenu.addListener(function(element,selection){if(getSelectedImage(editor,element))return{image:CKEDITOR.TRISTATE_OFF}})}}});function getSelectedImage(editor,element){if(!element){var sel=editor.getSelection();element=sel.getSelectedElement()}if(element&&element.is("img")&&!element.data("cke-realelement")&&!element.isReadOnly())return element}function loadIframeImagePicker(editor){var $in=jQuery("#Inputfield_id");var page_id;if($in.length){page_id=$in.val()}else{page_id=jQuery("#"+editor.name).closest(".Inputfield").attr("data-pid")}var edit_page_id=page_id;var file="";var imgClass="";var imgWidth=0;var imgHeight=0;var imgDescription="";var imgLink="";var hidpi=false;var selection=editor.getSelection();var se=selection.getSelectedElement();var node=selection.getStartElement();var $node=jQuery(node);var nodeParent=node.getParent();var nodeGrandparent=nodeParent.getParent();var src=$node.attr("src");var $linkWrapper=null;var $figureWrapper=null;var $figureCaption=null;var nodeParentName=nodeParent.$.nodeName.toUpperCase();var nodeGrandparentName=nodeGrandparent?nodeGrandparent.$.nodeName.toUpperCase():"";var figureNodeSafari=null;var $repeaterItem=jQuery("#"+editor.name).closest(".InputfieldRepeaterItem");if(selection.getType()==CKEDITOR.SELECTION_TEXT){if(nodeParentName=="FIGURE"){figureNodeSafari=nodeParent}else if(nodeGrandparentName=="FIGURE"){figureNodeSafari=nodeGrandparent}if(figureNodeSafari){selection.reset();selection.removeAllRanges();figureNodeSafari.$.style.display="block";selection.selectElement(figureNodeSafari)}}if($repeaterItem.length&&$repeaterItem.find(".InputfieldImage").length){var dataPageAttr=$repeaterItem.attr("data-page");if(typeof dataPageAttr!=="undefined")page_id=parseInt(dataPageAttr)}selection.lock();editor.lockSelection();if(nodeGrandparentName=="FIGURE"){$figureWrapper=jQuery(nodeGrandparent.getOuterHtml());$figureCaption=$figureWrapper.find("figcaption");$figureWrapper.find("img").remove()}else if(nodeParentName=="FIGURE"){$figureWrapper=jQuery(nodeParent.getOuterHtml());$figureCaption=$figureWrapper.find("figcaption");$figureWrapper.find("img").remove()}if(nodeParentName==="A"){$linkWrapper=jQuery(nodeParent.getOuterHtml());$linkWrapper.find("img").remove()}if(src){imgClass=$figureWrapper?$figureWrapper.attr("class"):$node.attr("class");hidpi=imgClass&&imgClass.indexOf("hidpi")>-1;imgWidth=$node.attr("width");imgHeight=$node.attr("height");imgDescription=$node.attr("alt");imgLink=nodeParentName==="A"?nodeParent.$.href:"";var parts=src.split("/");file=parts.pop();parts=parts.reverse();page_id="";for(var n=0;n "+ProcessWire.config.InputfieldCKEditor.pwimage.selectLabel,open:function(){if(jQuery(".cke_maximized").length>0){jQuery(".ui-dialog").css("z-index",9999);jQuery(".ui-widget-overlay").css("z-index",9998)}}};var $iframe=pwModalWindow(modalUri+queryString,modalSettings,"large");$iframe.on("load",function(){var $i=$iframe.contents();var buttons;if($i.find("#selected_image").length>0){buttons=[{html:" "+ProcessWire.config.InputfieldCKEditor.pwimage.insertBtn,click:function(){function insertImage(src){var $i=$iframe.contents();var $img=jQuery("#selected_image",$i);var width=$img.attr("width");var height=$img.attr("height");var alt=jQuery("#selected_image_description",$i).val();var caption=jQuery("#selected_image_caption",$i).is(":checked")?true:false;var hidpi=jQuery("#selected_image_hidpi",$i).is(":checked")?true:false;var cls=$img.removeClass("ui-resizable No Alignment resizable_setup").removeClass("rotate90 rotate180 rotate270 rotate-90 rotate-180 rotate-270").removeClass("flip_vertical flip_horizontal").attr("class");var $linkToLarger=jQuery("#selected_image_link",$i);var link=$linkToLarger.is(":checked")?$linkToLarger.val():"";var $insertHTML=jQuery("").attr("src",src).attr("alt",alt);if(hidpi)cls+=(cls.length>0?" ":"")+"hidpi";if(caption===false)$insertHTML.addClass(cls);if(width>0&&$img.attr("data-nosize")!="1")$insertHTML.attr("width",width);if($linkWrapper){if(link&&link.length>0){$linkWrapper.attr("href",link).attr("data-cke-saved-href",link)}else if($linkToLarger.attr("data-was-checked")==1){$linkWrapper=null}if($linkWrapper!==null){$linkWrapper.append($insertHTML);$insertHTML=$linkWrapper}}else if(link&&link.length>0){var $a=jQuery("").attr("href",link).append($insertHTML);$insertHTML=$a}if(caption){var $figure=jQuery("
");if(cls.length)$figure.addClass(cls);if(!$figureCaption){$figureCaption=jQuery("
");if(alt.length>1){$figureCaption.append(alt)}else{$figureCaption.append(ProcessWire.config.InputfieldCKEditor.pwimage.captionLabel)}}if($figureCaption)$figure.append($figureCaption);$figure.prepend($insertHTML);$insertHTML=$figure}if(nodeGrandparentName==="FIGURE"){editor.unlockSelection();selection.unlock();selection.selectElement(nodeGrandparent)}else if(nodeParentName==="A"||nodeParentName=="FIGURE"){editor.unlockSelection();selection.unlock();selection.selectElement(nodeParent)}var html=$insertHTML[0].outerHTML;if(figureNodeSafari)figureNodeSafari.remove();editor.insertHtml(html);editor.fire("change");$iframe.dialog("close")}var $i=$iframe.contents();var $img=jQuery("#selected_image",$i);$iframe.dialog("disable");$iframe.setTitle(ProcessWire.config.InputfieldCKEditor.pwimage.savingNote);$img.removeClass("resized");var width=$img.attr("width");if(!width)width=$img.width();var height=$img.attr("height");if(!height)height=$img.height();var file=$img.attr("src");var page_id=jQuery("#page_id",$i).val();var hidpi=jQuery("#selected_image_hidpi",$i).is(":checked")?1:0;var rotate=parseInt(jQuery("#selected_image_rotate",$i).val());file=file.substring(file.lastIndexOf("/")+1);var resizeURL=modalUri+"resize?id="+page_id+"&file="+file+"&width="+width+"&height="+height+"&hidpi="+hidpi;if(rotate)resizeURL+="&rotate="+rotate;if($img.hasClass("flip_horizontal"))resizeURL+="&flip=h";else if($img.hasClass("flip_vertical"))resizeURL+="&flip=v";jQuery.get(resizeURL,function(data){var $div=jQuery("
").html(data);var src=$div.find("#selected_image").attr("src");insertImage(src)})}},{html:" "+ProcessWire.config.InputfieldCKEditor.pwimage.selectBtn,class:"ui-priority-secondary",click:function(){var $i=$iframe.contents();var page_id=jQuery("#page_id",$i).val();$iframe.attr("src",modalUri+"?id="+page_id+"&modal=1");$iframe.setButtons({})}},{html:" "+ProcessWire.config.InputfieldCKEditor.pwimage.cancelBtn,class:"ui-priority-secondary",click:function(){$iframe.dialog("close")}}];$iframe.setButtons(buttons);$iframe.setTitle($i.find("title").html())}else{buttons=[];jQuery("button.pw-modal-button, button[type=submit]:visible",$i).each(function(){var $button=jQuery(this);var button={html:$button.html(),click:function(){$button.trigger("click")}};buttons.push(button);if(!$button.hasClass("pw-modal-button-visible"))$button.hide()});var cancelButton={html:" "+ProcessWire.config.InputfieldCKEditor.pwimage.cancelBtn,class:"ui-priority-secondary",click:function(){$iframe.dialog("close")}};buttons.push(cancelButton);$iframe.setButtons(buttons)}})}})(); \ No newline at end of file +(function(){CKEDITOR.plugins.add("pwimage",{requires:"dialog",init:function(editor){var pluginName="pwimage";var allowed="img[alt,id,!src,title,width](align_left,align_center,align_right,hidpi,align-left,align-center,align-right);"+"a[!href];"+"figure{width}(align_left,align_center,align_right,hidpi,align-left,align-center,align-right);"+"figcaption;";var required="img[alt,src]";editor.addCommand(pluginName,{allowedContent:allowed,requiredContent:required,exec:loadIframeImagePicker});editor.ui.addButton("PWImage",{label:editor.lang.common.image,command:pluginName,hidpi:true,icon:CKEDITOR.env.hidpi?this.path+"images/hidpi/pwimage.png":this.path+"images/pwimage.png"});editor.on("doubleclick",function(evt){var element=evt.data.element;if(element.is("img")&&!element.data("cke-realelement")&&!element.isReadOnly()){evt.cancel();editor.commands.pwimage.exec()}});if(editor.addMenuItems){editor.addMenuItems({image:{label:editor.lang.image.menu,command:"pwimage",group:"image"}})}if(editor.contextMenu){editor.contextMenu.addListener(function(element,selection){if(getSelectedImage(editor,element))return{image:CKEDITOR.TRISTATE_OFF}})}}});function getSelectedImage(editor,element){if(!element){var sel=editor.getSelection();element=sel.getSelectedElement()}if(element&&element.is("img")&&!element.data("cke-realelement")&&!element.isReadOnly())return element}function loadIframeImagePicker(editor){var $in=jQuery("#Inputfield_id");var page_id;if($in.length){page_id=$in.val()}else{page_id=jQuery("#"+editor.name).closest(".Inputfield").attr("data-pid")}var edit_page_id=page_id;var file="";var imgClass="";var imgWidth=0;var imgHeight=0;var imgDescription="";var imgLink="";var hidpi=false;var selection=editor.getSelection();var se=selection.getSelectedElement();var node=selection.getStartElement();var $node=jQuery(node);var nodeParent=node.getParent();var nodeGrandparent=nodeParent.getParent();var src=$node.attr("src");var $linkWrapper=null;var $figureWrapper=null;var $figureCaption=null;var nodeParentName=nodeParent.$.nodeName.toUpperCase();var nodeGrandparentName=nodeGrandparent?nodeGrandparent.$.nodeName.toUpperCase():"";var figureNodeSafari=null;var $repeaterItem=jQuery("#"+editor.name).closest(".InputfieldRepeaterItem");if(selection.getType()==CKEDITOR.SELECTION_TEXT){if(nodeParentName=="FIGURE"){figureNodeSafari=nodeParent}else if(nodeGrandparentName=="FIGURE"){figureNodeSafari=nodeGrandparent}if(figureNodeSafari){selection.reset();selection.removeAllRanges();figureNodeSafari.$.style.display="block";selection.selectElement(figureNodeSafari)}}if($repeaterItem.length&&$repeaterItem.find(".InputfieldImage").length){var dataPageAttr=$repeaterItem.attr("data-page");if(typeof dataPageAttr!=="undefined")page_id=parseInt(dataPageAttr)}selection.lock();editor.lockSelection();if(nodeGrandparentName=="FIGURE"){$figureWrapper=jQuery(nodeGrandparent.getOuterHtml());$figureCaption=$figureWrapper.find("figcaption");$figureWrapper.find("img").remove()}else if(nodeParentName=="FIGURE"){$figureWrapper=jQuery(nodeParent.getOuterHtml());$figureCaption=$figureWrapper.find("figcaption");$figureWrapper.find("img").remove()}if(nodeParentName==="A"){$linkWrapper=jQuery(nodeParent.getOuterHtml());$linkWrapper.find("img").remove()}if(src){imgClass=$figureWrapper?$figureWrapper.attr("class"):$node.attr("class");hidpi=imgClass&&imgClass.indexOf("hidpi")>-1;imgWidth=$node.attr("width");imgHeight=$node.attr("height");imgDescription=$node.attr("alt");imgLink=nodeParentName==="A"?nodeParent.$.href:"";var parts=src.split("/");file=parts.pop();parts=parts.reverse();page_id="";for(var n=0;n "+ProcessWire.config.InputfieldCKEditor.pwimage.selectLabel,open:function(){if(jQuery(".cke_maximized").length>0){jQuery(".ui-dialog").css("z-index",9999);jQuery(".ui-widget-overlay").css("z-index",9998)}}};var $iframe=pwModalWindow(modalUri+queryString,modalSettings,"large");$iframe.on("load",function(){var $i=$iframe.contents();var buttons;if($i.find("#selected_image").length>0){buttons=[{html:" "+ProcessWire.config.InputfieldCKEditor.pwimage.insertBtn,click:function(){function insertImage(src){var $i=$iframe.contents();var $img=jQuery("#selected_image",$i);var width=$img.attr("width");var height=$img.attr("height");var alt=jQuery("#selected_image_description",$i).val();var caption=jQuery("#selected_image_caption",$i).is(":checked")?true:false;var hidpi=jQuery("#selected_image_hidpi",$i).is(":checked")?true:false;var cls=$img.removeClass("ui-resizable No Alignment resizable_setup").removeClass("rotate90 rotate180 rotate270 rotate-90 rotate-180 rotate-270").removeClass("flip_vertical flip_horizontal").attr("class");var $linkToLarger=jQuery("#selected_image_link",$i);var link=$linkToLarger.is(":checked")?$linkToLarger.val():"";var $insertHTML=jQuery("").attr("src",src).attr("alt",alt);if(hidpi)cls+=(cls.length>0?" ":"")+"hidpi";if(caption===false)$insertHTML.addClass(cls);if(width>0&&$img.attr("data-nosize")!="1")$insertHTML.attr("width",width);if($linkWrapper){if(link&&link.length>0){$linkWrapper.attr("href",link).attr("data-cke-saved-href",link)}else if($linkToLarger.attr("data-was-checked")==1){$linkWrapper=null}if($linkWrapper!==null){$linkWrapper.append($insertHTML);$insertHTML=$linkWrapper}}else if(link&&link.length>0){var $a=jQuery("
").attr("href",link).append($insertHTML);$insertHTML=$a}if(caption){var $figure=jQuery("
");if(cls.length)$figure.addClass(cls);if(!$figureCaption){$figureCaption=jQuery("
");if(alt.length>1){$figureCaption.append(alt)}else{$figureCaption.append(ProcessWire.config.InputfieldCKEditor.pwimage.captionLabel)}}if($figureCaption)$figure.append($figureCaption);$figure.prepend($insertHTML);$insertHTML=$figure}if(nodeGrandparentName==="FIGURE"){editor.unlockSelection();selection.unlock();selection.selectElement(nodeGrandparent)}else if(nodeParentName==="A"||nodeParentName=="FIGURE"){editor.unlockSelection();selection.unlock();selection.selectElement(nodeParent)}var html=$insertHTML[0].outerHTML;if(figureNodeSafari)figureNodeSafari.remove();editor.insertHtml(html);editor.fire("change");$iframe.dialog("close")}var $i=$iframe.contents();var $img=jQuery("#selected_image",$i);$iframe.dialog("disable");$iframe.setTitle(ProcessWire.config.InputfieldCKEditor.pwimage.savingNote);$img.removeClass("resized");var width=$img.attr("width");if(!width)width=$img.width();var height=$img.attr("height");if(!height)height=$img.height();var file=$img.attr("src");var page_id=jQuery("#page_id",$i).val();var hidpi=jQuery("#selected_image_hidpi",$i).is(":checked")?1:0;var rotate=parseInt(jQuery("#selected_image_rotate",$i).val());file=file.substring(file.lastIndexOf("/")+1);var resizeURL=modalUri+"resize?id="+page_id+"&file="+file+"&width="+width+"&height="+height+"&hidpi="+hidpi+"&version="+version;if(rotate)resizeURL+="&rotate="+rotate;if($img.hasClass("flip_horizontal"))resizeURL+="&flip=h";else if($img.hasClass("flip_vertical"))resizeURL+="&flip=v";jQuery.get(resizeURL,function(data){var $div=jQuery("
").html(data);var src=$div.find("#selected_image").attr("src");insertImage(src)})}},{html:" "+ProcessWire.config.InputfieldCKEditor.pwimage.selectBtn,class:"ui-priority-secondary",click:function(){var $i=$iframe.contents();var page_id=jQuery("#page_id",$i).val();$iframe.attr("src",modalUri+"?id="+page_id+"&modal=1&version="+version);$iframe.setButtons({})}},{html:" "+ProcessWire.config.InputfieldCKEditor.pwimage.cancelBtn,class:"ui-priority-secondary",click:function(){$iframe.dialog("close")}}];$iframe.setButtons(buttons);$iframe.setTitle($i.find("title").html())}else{buttons=[];jQuery("button.pw-modal-button, button[type=submit]:visible",$i).each(function(){var $button=jQuery(this);var button={html:$button.html(),click:function(){$button.trigger("click")}};buttons.push(button);if(!$button.hasClass("pw-modal-button-visible"))$button.hide()});var cancelButton={html:" "+ProcessWire.config.InputfieldCKEditor.pwimage.cancelBtn,class:"ui-priority-secondary",click:function(){$iframe.dialog("close")}};buttons.push(cancelButton);$iframe.setButtons(buttons)}})}})(); \ No newline at end of file diff --git a/wire/modules/Inputfield/InputfieldTinyMCE/InputfieldTinyMCE.js b/wire/modules/Inputfield/InputfieldTinyMCE/InputfieldTinyMCE.js index 00341809..55616434 100644 --- a/wire/modules/Inputfield/InputfieldTinyMCE/InputfieldTinyMCE.js +++ b/wire/modules/Inputfield/InputfieldTinyMCE/InputfieldTinyMCE.js @@ -242,6 +242,12 @@ var InputfieldTinyMCE = { '&width=' + width + '&hidpi=' + hidpi + '&file=' + src; + + if(typeof ProcessWire.config.PagesVersions !== 'undefined') { + if(ProcessWire.config.PagesVersions.page == $('#Inputfield_id').val()) { + url += '&version=' + ProcessWire.config.PagesVersions.version; + } + } t.log('Resizing image to width=' + width, url); diff --git a/wire/modules/Inputfield/InputfieldTinyMCE/plugins/pwimage.js b/wire/modules/Inputfield/InputfieldTinyMCE/plugins/pwimage.js index 64b1f8f3..06914a41 100644 --- a/wire/modules/Inputfield/InputfieldTinyMCE/plugins/pwimage.js +++ b/wire/modules/Inputfield/InputfieldTinyMCE/plugins/pwimage.js @@ -126,6 +126,7 @@ function pwTinyMCE_image(editor) { var imagePageId = $('#page_id', $i).val(); var hidpi = $("#selected_image_hidpi", $i).is(":checked") ? 1 : 0; var rotate = parseInt($("#selected_image_rotate", $i).val()); + var version = 0; $iframe.dialog('disable'); $iframe.setTitle(labels.savingImage); // Saving Image @@ -136,12 +137,19 @@ function pwTinyMCE_image(editor) { file = file.substring(file.lastIndexOf('/')+1); + if(typeof ProcessWire.config.PagesVersions !== 'undefined') { + if(ProcessWire.config.PagesVersions.page == imagePageId) { + version = ProcessWire.config.PagesVersions.version; + } + } + var resizeUrl = modalUrl + 'resize' + '?id=' + imagePageId + '&file=' + file + '&width=' + width + '&height=' + height + - '&hidpi=' + hidpi; + '&hidpi=' + hidpi + + '&version=' + version; if(rotate) resizeUrl += '&rotate=' + rotate; @@ -185,7 +193,13 @@ function pwTinyMCE_image(editor) { click: function() { var $i = $iframe.contents(); var imagePageId = $('#page_id', $i).val(); - $iframe.attr('src', modalUrl + '?id=' + imagePageId + '&modal=1'); + var version = 0; + if(typeof ProcessWire.config.PagesVersions !== 'undefined') { + if(ProcessWire.config.PagesVersions.page == imagePageId) { + version = ProcessWire.config.PagesVersions.version; + } + } + $iframe.attr('src', modalUrl + '?id=' + imagePageId + '&modal=1&version=' + version); $iframe.setButtons({}); } } ]; @@ -282,6 +296,12 @@ function pwTinyMCE_image(editor) { queryString += ('&winwidth=' + ($(window).width() - 30)); + if(typeof ProcessWire.config.PagesVersions !== 'undefined') { + if(ProcessWire.config.PagesVersions.page == pageId) { + queryString += '&version=' + ProcessWire.config.PagesVersions.version; + } + } + return queryString; } @@ -362,4 +382,4 @@ tinymce.PluginManager.add('pwimage', (editor, url) => { return { getMetadata: () => ({ name: 'Image' }) }; -}); \ No newline at end of file +});