From caf492269073595a5760a5154b4fba7a149b16b0 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 27 May 2016 17:50:45 +0200 Subject: [PATCH] Properly removing [attach] from editor on delete --- .../redactor2/plugins/WoltLabAttachment.js | 33 +++++++++++++++++++ wcfsetup/install/files/js/WCF.Attachment.js | 8 +++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabAttachment.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabAttachment.js index c2d1705f14..02cae7b441 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabAttachment.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabAttachment.js @@ -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=][/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 diff --git a/wcfsetup/install/files/js/WCF.Attachment.js b/wcfsetup/install/files/js/WCF.Attachment.js index 0b82657337..643fb8a030 100644 --- a/wcfsetup/install/files/js/WCF.Attachment.js +++ b/wcfsetup/install/files/js/WCF.Attachment.js @@ -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') + }); } }, -- 2.20.1