From: Alexander Ebert Date: Fri, 10 Jul 2020 15:10:37 +0000 (+0200) Subject: Synchronizes multiple attachment instances for the same tmpHash X-Git-Tag: 5.3.0_Alpha_1~144^2~4 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=ce456f9248843c8c06d131dae4359ec9999c9837;p=GitHub%2FWoltLab%2FWCF.git Synchronizes multiple attachment instances for the same tmpHash --- diff --git a/wcfsetup/install/files/js/WCF.Attachment.js b/wcfsetup/install/files/js/WCF.Attachment.js index 39bfd3edec..6968f56586 100644 --- a/wcfsetup/install/files/js/WCF.Attachment.js +++ b/wcfsetup/install/files/js/WCF.Attachment.js @@ -87,8 +87,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.System.Event.addListener('com.woltlab.wcf.action.delete', 'attachment_' + this._editorId, $.proxy(this._removeLimitError, this)); + WCF.System.Event.addListener('com.woltlab.wcf.action.delete', 'attachment', this._onDelete.bind(this)); this._makeSortable(); @@ -153,6 +152,8 @@ WCF.Attachment.Upload = WCF.Upload.extend({ } }).bind(this)); + var syncUuid = WCF.System.Event.addListener('com.woltlab.wcf.redactor2', 'sync_' + this._tmpHash, this._sync.bind(this)); + WCF.System.Event.addListener('com.woltlab.wcf.redactor2', 'destroy_' + this._editorId, (function () { WCF.System.Event.removeAllListeners('com.woltlab.wcf.redactor2', 'submit_' + this._editorId); WCF.System.Event.removeAllListeners('com.woltlab.wcf.redactor2', 'reset_' + this._editorId); @@ -162,6 +163,7 @@ WCF.Attachment.Upload = WCF.Upload.extend({ WCF.System.Event.removeAllListeners('com.woltlab.wcf.redactor2', 'autosaveMetaData_' + this._editorId); WCF.System.Event.removeListener('com.woltlab.wcf.redactor2', 'metacode_attach_' + this._editorId, metacodeAttachUuid); + WCF.System.Event.removeListener('com.woltlab.wcf.redactor2', 'sync_' + this._tmpHash, syncUuid); }).bind(this)); } }, @@ -172,7 +174,7 @@ WCF.Attachment.Upload = WCF.Upload.extend({ * @param object data */ _editorUpload: function (data) { - var $uploadID, replace = null; + var replace = null; // show tab this._fileListSelector.closest('.messageTabMenu').messageTabMenu('showTab', 'attachments', true); @@ -189,10 +191,10 @@ WCF.Attachment.Upload = WCF.Upload.extend({ }).bind(this); if (data.file) { - $uploadID = this._upload(undefined, data.file, undefined, callbackUploadId); + this._upload(undefined, data.file, undefined, callbackUploadId); } else { - $uploadID = this._upload(undefined, undefined, data.blob, callbackUploadId); + this._upload(undefined, undefined, data.blob, callbackUploadId); replace = data.replace || null; } }, @@ -461,7 +463,7 @@ WCF.Attachment.Upload = WCF.Upload.extend({ * @see WCF.Upload._initFile() */ _initFile: function (file) { - var $li = $('
  • ' + file.name + '

    • ').data('filename', file.name); + var $li = $('
    • ' + file.name + '

      • ').data('filename', file.name); this._fileListSelector.append($li); this._fileListSelector.show(); @@ -514,6 +516,8 @@ WCF.Attachment.Upload = WCF.Upload.extend({ if (data.returnValues && data.returnValues.attachments[$internalFileID]) { attachmentData = data.returnValues.attachments[$internalFileID]; + elData($li[0], 'object-id', attachmentData.attachmentID); + // show thumbnail if (attachmentData.tinyURL) { $li.children('.fa-spinner').replaceWith($('')); @@ -542,7 +546,7 @@ WCF.Attachment.Upload = WCF.Upload.extend({ // init buttons var $buttonList = $li.find('ul').addClass('buttonGroup'); - var $deleteButton = $('
      • ' + WCF.Language.get('wcf.global.button.delete') + '
      • '); + var $deleteButton = $('
      • ' + WCF.Language.get('wcf.global.button.delete') + '
      • '); $buttonList.append($deleteButton); $li.data('objectID', attachmentData.attachmentID); @@ -550,19 +554,22 @@ WCF.Attachment.Upload = WCF.Upload.extend({ if (this._editorId) { if (attachmentData.tinyURL || (!this._useThumbnail() && attachmentData.isImage)) { if (attachmentData.thumbnailURL) { - var $insertThumbnail = $('
      • ' + WCF.Language.get('wcf.attachment.insertThumbnail') + '
      • ').appendTo($buttonList); - $insertThumbnail.children('span.button').click($.proxy(this._insert, this)); + $('
      • ' + WCF.Language.get('wcf.attachment.insertThumbnail') + '
      • ').appendTo($buttonList); } - var $insertOriginal = $('
      • ' + WCF.Language.get('wcf.attachment.insertFull') + '
      • ').appendTo($buttonList); - $insertOriginal.children('span.button').click($.proxy(this._insert, this)); + $('
      • ' + WCF.Language.get('wcf.attachment.insertFull') + '
      • ').appendTo($buttonList); } else { - var $insertPlain = $('
      • ' + WCF.Language.get('wcf.attachment.insert') + '
      • '); - $insertPlain.appendTo($buttonList).children('span.button').click($.proxy(this._insert, this)); + $('
      • ' + WCF.Language.get('wcf.attachment.insert') + '
      • ').appendTo($buttonList); } } + this._triggerSync('new', { + html: $li[0].outerHTML + }); + + this._registerEditorButtons($li[0]); + if (this._replaceOnLoad.hasOwnProperty(uploadID)) { if (!$li.hasClass('uploadFailed')) { var img = this._replaceOnLoad[uploadID]; @@ -625,6 +632,14 @@ WCF.Attachment.Upload = WCF.Upload.extend({ WCF.DOMNodeInsertedHandler.execute(); }, + _registerEditorButtons: function (attachment) { + if (this._editorId) { + elBySelAll('.jsButtonAttachmentInsertThumbnail, .jsButtonAttachmentInsertFull, .jsButtonAttachmentInsertPlain', attachment, (function(button) { + button.addEventListener('click', this._insert.bind(this)); + }).bind(this)); + } + }, + /** * Inserts an attachment into WYSIWYG editor contents. * @@ -723,5 +738,66 @@ WCF.Attachment.Upload = WCF.Upload.extend({ } }).bind(this)); } + }, + + /** + * @param {Object} data + * @param {jQuery} data.button + * @param {jQuery} data.container + */ + _onDelete: function (data) { + var objectId = elData(data.button[0], 'object-id'); + var attachment = elBySel('.formAttachmentListItem[data-object-id="' + objectId + '"]', this._fileListSelector[0]); + if (attachment !== null) { + elRemove(attachment); + } + + this._removeLimitError(data); + }, + + /** + * @param {Object} payload + * @param {Object} payload.data + * @param {Object} payload.source + * @param {string} payload.type + */ + _sync: function (payload) { + if (payload.source === this) { + return; + } + + switch (payload.type) { + case 'new': + this._syncNew(payload.data); + break; + + default: + throw new Error("Unexpected type '" + payload.type + "'"); + } + }, + + /** + * @param {Object} data + */ + _syncNew: function (data) { + require(['Dom/Util'], (function (DomUtil) { + var fragment = DomUtil.createFragmentFromHtml(data.html); + var attachment = elBySel('li', fragment); + attachment.id = ''; + + this._registerEditorButtons(attachment); + + this._fileListSelector[0].appendChild(attachment); + + elShow(this._fileListSelector[0]); + }).bind(this)); + }, + + _triggerSync: function (type, data) { + WCF.System.Event.fireEvent('com.woltlab.wcf.redactor2', 'sync_' + this._tmpHash, { + source: this, + type: type, + data: data + }); } });