keep cache for media for d&rd, fix #281

This commit is contained in:
Nicolas Constant 2020-05-17 16:26:33 -04:00
parent e40f5e41e5
commit 6a7d389b55
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
2 changed files with 20 additions and 2 deletions

View File

@ -17,7 +17,7 @@ import { NotificationService } from '../../services/notification.service';
import { StatusWrapper } from '../../models/common.model';
import { AccountInfo } from '../../states/accounts.state';
import { InstancesInfoService } from '../../services/instances-info.service';
import { MediaService } from '../../services/media.service';
import { MediaService, MediaWrapper } from '../../services/media.service';
import { AutosuggestSelection, AutosuggestUserActionEnum } from './autosuggest/autosuggest.component';
import { EmojiPickerComponent } from './emoji-picker/emoji-picker.component';
import { PollEditorComponent } from './poll-editor/poll-editor.component';
@ -79,8 +79,14 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
@Input('redraftedStatus')
set redraftedStatus(value: StatusWrapper) {
if (value) {
if (value) {
this.statusLoaded = false;
if(value.status && value.status.media_attachments){
for (const m of value.status.media_attachments) {
this.mediaService.addExistingMedia(new MediaWrapper(m.id, null, m));
}
}
const newLine = String.fromCharCode(13, 10);
let content = value.status.content;

View File

@ -12,6 +12,7 @@ import { NotificationService } from './notification.service';
})
export class MediaService {
mediaSubject: BehaviorSubject<MediaWrapper[]> = new BehaviorSubject<MediaWrapper[]>([]);
fileCache: { [url: string]: File } = {};
constructor(
private readonly notificationService: NotificationService,
@ -36,6 +37,7 @@ export class MediaService {
this.mastodonService.uploadMediaAttachment(account, file, null)
.then((attachment: Attachment) => {
this.fileCache[attachment.url] = file;
let currentMedias = this.mediaSubject.value;
let currentMedia = currentMedias.filter(x => x.id === uniqueId)[0];
if (currentMedia) {
@ -64,6 +66,15 @@ export class MediaService {
});
}
addExistingMedia(media: MediaWrapper){
if(!this.fileCache[media.attachment.url]) return;
media.file = this.fileCache[media.attachment.url];
let medias = this.mediaSubject.value;
medias.push(media);
this.mediaSubject.next(medias);
}
remove(media: MediaWrapper) {
let medias = this.mediaSubject.value;
let filteredMedias = medias.filter(x => x.id !== media.id);
@ -84,6 +95,7 @@ export class MediaService {
for (let media of medias) {
this.mastodonService.uploadMediaAttachment(account, media.file, media.description)
.then((attachment: Attachment) => {
this.fileCache[attachment.url] = media.file;
let currentMedias = this.mediaSubject.value;
let currentMedia = currentMedias.filter(x => x.id === media.id)[0];
if (currentMedia) {