Some improvements, as found while investigating #322

This commit is contained in:
the-djmaze 2022-04-13 10:48:14 +02:00
parent 5fb78bcd80
commit c5cf9fc71d
3 changed files with 43 additions and 116 deletions

View file

@ -1,4 +1,3 @@
import { pInt } from 'Common/Utils';
import { FileInfo } from 'Common/File'; import { FileInfo } from 'Common/File';
import { AbstractModel } from 'Knoin/AbstractModel'; import { AbstractModel } from 'Knoin/AbstractModel';
@ -59,39 +58,6 @@ export class ComposeAttachmentModel extends AbstractModel {
}); });
} }
static fromAttachment(item)
{
const attachment = new ComposeAttachmentModel(
item.download,
item.fileName,
item.estimatedSize,
item.isInline(),
item.isLinked(),
item.cid,
item.contentLocation
);
attachment.fromMessage = true;
return attachment;
}
/**
* @param {FetchJsonComposeAttachment} json
* @returns {boolean}
*/
initByUploadJson(json) {
let bResult = false;
if (json) {
this.fileName(json.Name);
this.size(undefined === json.Size ? 0 : pInt(json.Size));
this.tempName(undefined === json.TempName ? '' : json.TempName);
this.isInline = false;
bResult = true;
}
return bResult;
}
/** /**
* @returns {string} * @returns {string}
*/ */

View file

@ -1203,7 +1203,6 @@ export class ComposePopupView extends AbstractViewPopup {
clickElement: dom.querySelector('#composeUploadButton'), clickElement: dom.querySelector('#composeUploadButton'),
dragAndDropElement: dom.querySelector('.b-attachment-place') dragAndDropElement: dom.querySelector('.b-attachment-place')
}), }),
uploadCache = {},
attachmentSizeLimit = pInt(SettingsGet('AttachmentLimit')); attachmentSizeLimit = pInt(SettingsGet('AttachmentLimit'));
oJua oJua
@ -1224,14 +1223,7 @@ export class ComposePopupView extends AbstractViewPopup {
this.dragAndDropVisible(false); this.dragAndDropVisible(false);
}) })
.on('onProgress', (id, loaded, total) => { .on('onProgress', (id, loaded, total) => {
let item = uploadCache[id]; let item = this.getAttachmentById(id);
if (!item) {
item = this.getAttachmentById(id);
if (item) {
uploadCache[id] = item;
}
}
if (item) { if (item) {
item.progress(Math.floor((loaded / total) * 100)); item.progress(Math.floor((loaded / total) * 100));
} }
@ -1239,15 +1231,15 @@ export class ComposePopupView extends AbstractViewPopup {
.on('onSelect', (sId, oData) => { .on('onSelect', (sId, oData) => {
this.dragAndDropOver(false); this.dragAndDropOver(false);
const fileName = undefined === oData.FileName ? '' : oData.FileName.toString(), const
size = pInt(oData.Size, null), size = pInt(oData.Size, null),
attachment = new ComposeAttachmentModel(sId, fileName, size); attachment = new ComposeAttachmentModel(
sId,
oData.FileName ? oData.FileName.toString() : '',
size
);
attachment.cancel = this.cancelAttachmentHelper(sId, oJua); this.addAttachment(attachment, 1, oJua);
this.attachments.push(attachment);
this.attachmentsArea();
if (0 < size && 0 < attachmentSizeLimit && attachmentSizeLimit < size) { if (0 < size && 0 < attachmentSizeLimit && attachmentSizeLimit < size) {
attachment attachment
@ -1261,15 +1253,8 @@ export class ComposePopupView extends AbstractViewPopup {
return true; return true;
}) })
.on('onStart', (id) => { .on('onStart', id => {
let item = uploadCache[id]; let item = this.getAttachmentById(id);
if (!item) {
item = this.getAttachmentById(id);
if (item) {
uploadCache[id] = item;
}
}
if (item) { if (item) {
item item
.waiting(false) .waiting(false)
@ -1302,12 +1287,10 @@ export class ComposePopupView extends AbstractViewPopup {
.waiting(false) .waiting(false)
.uploading(false) .uploading(false)
.complete(true); .complete(true);
attachment.fileName(attachmentJson.Name);
attachment.initByUploadJson(attachmentJson); attachment.size(attachmentJson.Size ? pInt(attachmentJson.Size) : 0);
} attachment.tempName(attachmentJson.TempName ? attachmentJson.TempName : '');
attachment.isInline = false;
if (undefined === uploadCache[id]) {
delete uploadCache[id];
} }
} }
}); });
@ -1363,16 +1346,6 @@ export class ComposePopupView extends AbstractViewPopup {
return this.attachments.find(item => item && id === item.id); return this.attachments.find(item => item && id === item.id);
} }
cancelAttachmentHelper(id, oJua) {
return () => {
const attachment = this.getAttachmentById(id);
if (attachment) {
this.attachments.remove(attachment);
oJua && oJua.cancel(id);
}
};
}
/** /**
* @returns {Object} * @returns {Object}
*/ */
@ -1396,18 +1369,22 @@ export class ComposePopupView extends AbstractViewPopup {
temp = '.eml' === temp.slice(-4).toLowerCase() ? temp : temp + '.eml'; temp = '.eml' === temp.slice(-4).toLowerCase() ? temp : temp + '.eml';
const attachment = new ComposeAttachmentModel(message.requestHash, temp, message.size()); const attachment = new ComposeAttachmentModel(message.requestHash, temp, message.size());
attachment.fromMessage = true; attachment.fromMessage = true;
attachment.cancel = this.cancelAttachmentHelper(message.requestHash); attachment.complete(true);
attachment this.addAttachment(attachment);
.waiting(false)
.uploading(true)
.complete(true);
this.attachments.push(attachment);
} }
} }
addAttachment(attachment, view, oJua) {
oJua || attachment.waiting(false).uploading(true);
attachment.cancel = () => {
this.attachments.remove(attachment);
oJua && oJua.cancel(attachment.id);
};
this.attachments.push(attachment);
view && this.attachmentsArea();
}
/** /**
* @param {string} url * @param {string} url
* @param {string} name * @param {string} name
@ -1416,18 +1393,7 @@ export class ComposePopupView extends AbstractViewPopup {
*/ */
addAttachmentHelper(url, name, size) { addAttachmentHelper(url, name, size) {
const attachment = new ComposeAttachmentModel(url, name, size); const attachment = new ComposeAttachmentModel(url, name, size);
this.addAttachment(attachment, 1);
attachment.fromMessage = false;
attachment.cancel = this.cancelAttachmentHelper(url);
attachment
.waiting(false)
.uploading(true)
.complete(false);
this.attachments.push(attachment);
this.attachmentsArea();
return attachment; return attachment;
} }
@ -1456,14 +1422,17 @@ export class ComposePopupView extends AbstractViewPopup {
} }
if (add) { if (add) {
const attachment = ComposeAttachmentModel.fromAttachment(item); const attachment = new ComposeAttachmentModel(
attachment.cancel = this.cancelAttachmentHelper(item.download); item.download,
attachment item.fileName,
.waiting(false) item.estimatedSize,
.uploading(true) item.isInline(),
.complete(false); item.isLinked(),
item.cid,
this.attachments.push(attachment); item.contentLocation
);
attachment.fromMessage = true;
this.addAttachment(attachment);
} }
}); });
} }

18
vendors/jua/jua.js vendored
View file

@ -242,7 +242,7 @@
oEvent.files || oEvent.dataTransfer.files, oEvent.files || oEvent.dataTransfer.files,
oFile => { oFile => {
if (oFile) { if (oFile) {
self.addNewFile(oFile); self.addFile(oFile);
timerStop(); timerStop();
} }
}, },
@ -286,18 +286,10 @@
/** /**
* @param {Object} oFileInfo * @param {Object} oFileInfo
*/ */
addNewFile(oFileInfo) addFile(oFileInfo)
{ {
this.addFile('jua-uid-' + Jua.randomId(16) + '-' + (Date.now().toString()), oFileInfo); const sUid = 'jua-uid-' + Jua.randomId(16) + '-' + (Date.now().toString()),
} fOnSelect = this.getEvent('onSelect');
/**
* @param {string} sUid
* @param {Object} oFileInfo
*/
addFile(sUid, oFileInfo)
{
const fOnSelect = this.getEvent('onSelect');
if (oFileInfo && (!fOnSelect || (false !== fOnSelect(sUid, oFileInfo)))) if (oFileInfo && (!fOnSelect || (false !== fOnSelect(sUid, oFileInfo))))
{ {
this.oUids[sUid] = true; this.oUids[sUid] = true;
@ -404,7 +396,7 @@
oInput.addEventListener('input', () => { oInput.addEventListener('input', () => {
const fFileCallback = oFile => { const fFileCallback = oFile => {
self.addNewFile(oFile); self.addFile(oFile);
setTimeout(() => { setTimeout(() => {
oInput.remove(); oInput.remove();
oClickElement.removeEventListener('click', onClick); oClickElement.removeEventListener('click', onClick);