Properly removing [attach] from editor on delete
authorAlexander Ebert <ebert@woltlab.com>
Fri, 27 May 2016 15:50:45 +0000 (17:50 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 27 May 2016 15:50:52 +0000 (17:50 +0200)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabAttachment.js
wcfsetup/install/files/js/WCF.Attachment.js

index c2d1705f1419d61ebd7b45d3fff1c0904ca02d4b..02cae7b441844ac046ee77e8aff8b688ba538ce0 100644 (file)
@@ -5,6 +5,7 @@ $.Redactor.prototype.WoltLabAttachment = function() {
                init: function() {
                        require(['EventHandler'], (function(EventHandler) {
                                EventHandler.add('com.woltlab.wcf.redactor2', 'insertAttachment_' + this.$element[0].id, this.WoltLabAttachment._insert.bind(this))
+                               EventHandler.add('com.woltlab.wcf.redactor2', 'deleteAttachment_' + this.$element[0].id, this.WoltLabAttachment._delete.bind(this))
                        }).bind(this));
                },
                
@@ -18,6 +19,38 @@ $.Redactor.prototype.WoltLabAttachment = function() {
                                // non-image attachment
                                this.insert.text('[attach=' + attachmentId + '][/attach]');
                        }
+               },
+               
+               _delete: function(data) {
+                       var attachmentId = data.attachmentId;
+                       
+                       var editor = this.core.editor()[0];
+                       elBySelAll('.woltlabAttachment[data-attachment-id="' + attachmentId + '"]', editor, function(attachment) {
+                               elRemove(attachment);
+                       });
+                       
+                       // find plain text '[attach=<attachmentId>][/attach]'
+                       var needle = '[attach=' + attachmentId + '][/attach]';
+                       if (editor.textContent.indexOf(needle) !== false) {
+                               // code taken from http://stackoverflow.com/a/2579869
+                               var walker = document.createTreeWalker(
+                                       editor,
+                                       NodeFilter.SHOW_TEXT,
+                                       null,
+                                       false
+                               );
+                               
+                               var node, matches = [];
+                               while (node = walker.nextNode()) {
+                                       if (node.textContent.indexOf(needle) !== -1) {
+                                               matches.push(node);
+                                       }
+                               }
+                               
+                               for (var i = 0, length = matches.length; i < length; i++) {
+                                       matches[i].textContent = matches[i].textContent.replace(new RegExp('\\[attach=' + attachmentId + '\\]\\[\\/attach\\]', 'g'), '');
+                               }
+                       }
                }
        };
 };
\ No newline at end of file
index 0b82657337db654dd5a49267e061496294fcdce9..643fb8a0301531707f4eb2ab2b7fd58ea22d3ceb 100644 (file)
@@ -71,7 +71,7 @@ WCF.Attachment.Upload = WCF.Upload.extend({
                this._fileListSelector.find('.jsButtonAttachmentInsertThumbnail').click($.proxy(this._insert, this));
                this._fileListSelector.find('.jsButtonAttachmentInsertFull').click($.proxy(this._insert, this));
                
-               //WCF.DOMNodeRemovedHandler.addCallback('WCF.Attachment.Upload', $.proxy(this._removeLimitError, this));
+               WCF.DOMNodeRemovedHandler.addCallback('WCF.Attachment.Upload', $.proxy(this._removeLimitError, this));
                WCF.System.Event.addListener('com.woltlab.wcf.action.delete', 'attachment_' + this._editorId, $.proxy(this._removeLimitError, this));
                
                this._makeSortable();
@@ -198,8 +198,10 @@ WCF.Attachment.Upload = WCF.Upload.extend({
                        }).bind(this), 250);
                }
                
-               if (this._editorId) {
-                       $('#' + this._editorId).redactor('wbbcode.removeAttachment', data.button.data('objectID'));
+               if (this._editorId && data.button) {
+                       WCF.System.Event.fireEvent('com.woltlab.wcf.redactor2', 'deleteAttachment_' + this._editorId, {
+                               attachmentId: data.button.data('objectID')
+                       });
                }
        },