Fixed a few issues with embedded attachments and extended edit
authorAlexander Ebert <ebert@woltlab.com>
Sun, 27 Jul 2014 13:38:14 +0000 (15:38 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 27 Jul 2014 13:38:14 +0000 (15:38 +0200)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js
wcfsetup/install/files/js/WCF.Attachment.js

index 96c9c761d0a4d563a6b435bdf256bde4cb32949a..ce62f6d1d1a3d50a430d11e39fb1445bab2686d8 100644 (file)
@@ -531,8 +531,16 @@ RedactorPlugins.wbbcode = {
                // attachments
                var $attachmentUrl = this.getOption('wAttachmentUrl');
                if ($attachmentUrl) {
-                       data = data.replace(/\[attach=(\d+)\]\[\/attach\]/, function(match, attachmentID) {
-                               return '<img src="' + $attachmentUrl.replace(/987654321/, attachmentID) + '" class="redactorEmbeddedAttachment" data-attachment-id="' + attachmentID + '" />';
+                       var $imageAttachmentIDs = this._getImageAttachmentIDs();
+                       
+                       data = data.replace(/\[attach=(\d+)\]\[\/attach\]/g, function(match, attachmentID) {
+                               attachmentID = parseInt(attachmentID);
+                               
+                               if (WCF.inArray(attachmentID, $imageAttachmentIDs)) {
+                                       return '<img src="' + $attachmentUrl.replace(/987654321/, attachmentID) + '" class="redactorEmbeddedAttachment" data-attachment-id="' + attachmentID + '" />';
+                               }
+                               
+                               return match;
                        });
                }
                
@@ -657,10 +665,13 @@ RedactorPlugins.wbbcode = {
         * @param       integer         attachmentID
         */
        insertAttachment: function(attachmentID) {
+               attachmentID = parseInt(attachmentID);
                var $attachmentUrl = this.getOption('wAttachmentUrl');
                var $bbcode = '[attach=' + attachmentID + '][/attach]';
                
-               if ($attachmentUrl) {
+               var $imageAttachmentIDs = this._getImageAttachmentIDs();
+               
+               if ($attachmentUrl && WCF.inArray(attachmentID, $imageAttachmentIDs)) {
                        this.insertDynamic(
                                '<img src="' + $attachmentUrl.replace(/987654321/, attachmentID) + '" class="redactorEmbeddedAttachment" data-attachment-id="' + attachmentID + '" />',
                                $bbcode
@@ -669,5 +680,27 @@ RedactorPlugins.wbbcode = {
                else {
                        this.insertDynamic($bbcode);
                }
+       },
+       
+       /**
+        * Returns a list of attachments representing an image.
+        * 
+        * @return      array<integer>
+        */
+       _getImageAttachmentIDs: function() {
+               // WCF.Attachment.Upload may have no been initialized yet, fallback to static data
+               var $imageAttachmentIDs = this.getOption('wAttachmentImageIDs') || [ ];
+               if ($imageAttachmentIDs.length) {
+                       delete this.opts.wAttachmentImageIDs;
+                       
+                       return $imageAttachmentIDs;
+               }
+               
+               var $data = {
+                       imageAttachmentIDs: [ ]
+               };
+               WCF.System.Event.fireEvent('com.woltlab.wcf.redactor', 'getImageAttachments_' + this.$source.wcfIdentify(), $data);
+               
+               return $data.imageAttachmentIDs;
        }
 };
index 487da1800516a2dde7895a5130ba782a7f9785c3..924698a3d817902b4c4517e71e9241aefeed0013 100644 (file)
@@ -304,6 +304,7 @@ RedactorPlugins.wutil = {
         * Synchronizes editor's source textarea.
         */
        wSync: function() {
+               this.sync();
                this.$source.val(this.cleanHtml(this.$source.val()));
                this._convertFromHtml();
        }
index b1ed25edb412b2350eb2ac5d3ba50dddf2fb7690..2acaf7158e1122f1af29f93ce2da4b6afdac4a12 100644 (file)
@@ -82,6 +82,7 @@ WCF.Attachment.Upload = WCF.Upload.extend({
                        WCF.System.Event.addListener('com.woltlab.wcf.messageOptionsInline', 'submit_' + this._wysiwygContainerID, $.proxy(this._submitInline, this));
                        WCF.System.Event.addListener('com.woltlab.wcf.redactor', 'reset', $.proxy(this._reset, this));
                        WCF.System.Event.addListener('com.woltlab.wcf.redactor', 'upload_' + this._wysiwygContainerID, $.proxy(this._editorUpload, this));
+                       WCF.System.Event.addListener('com.woltlab.wcf.redactor', 'getImageAttachments_' + this._wysiwygContainerID, $.proxy(this._getImageAttachments, this));
                }
        },
        
@@ -108,6 +109,20 @@ WCF.Attachment.Upload = WCF.Upload.extend({
                data.uploadID = $uploadID;
        },
        
+       /**
+        * Sets the attachment ids representing an image.
+        * 
+        * @param       object          data
+        */
+       _getImageAttachments: function(data) {
+               this._fileListSelector.children('li').each(function(index, attachment) {
+                       var $attachment = $(attachment);
+                       if ($attachment.children('img.attachmentTinyThumbnail').length) {
+                               data.imageAttachmentIDs.push($attachment.data('objectID'));
+                       }
+               });
+       },
+       
        /**
         * Adds parameters for the inline editor.
         *