[Client] Better draft management
This commit is contained in:
		
							parent
							
								
									736b972bcb
								
							
						
					
					
						commit
						bde46c4cff
					
				
					 1 changed files with 30 additions and 18 deletions
				
			
		| 
						 | 
					@ -341,10 +341,10 @@
 | 
				
			||||||
				: '投稿';
 | 
									: '投稿';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		this.draftId = this.repost
 | 
							this.draftId = this.repost
 | 
				
			||||||
			? 'draft-repost-' + this.repost.id
 | 
								? 'repost:' + this.repost.id
 | 
				
			||||||
			: this.inReplyToPost
 | 
								: this.inReplyToPost
 | 
				
			||||||
				? 'draft-reply-' + this.inReplyToPost.id
 | 
									? 'reply:' + this.inReplyToPost.id
 | 
				
			||||||
				: 'draft';
 | 
									: 'post';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		this.on('mount', () => {
 | 
							this.on('mount', () => {
 | 
				
			||||||
			this.refs.uploader.on('uploaded', file => {
 | 
								this.refs.uploader.on('uploaded', file => {
 | 
				
			||||||
| 
						 | 
					@ -359,15 +359,14 @@
 | 
				
			||||||
			this.autocomplete.attach();
 | 
								this.autocomplete.attach();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// 書きかけの投稿を復元
 | 
								// 書きかけの投稿を復元
 | 
				
			||||||
			let draft = localStorage.getItem(this.draftId);
 | 
								const draft = JSON.parse(localStorage.getItem('drafts') || '{}')[this.draftId];
 | 
				
			||||||
			if (draft) {
 | 
								if (draft) {
 | 
				
			||||||
				draft = JSON.parse(draft);
 | 
									this.refs.text.value = draft.data.text;
 | 
				
			||||||
				this.refs.text.value = draft.text;
 | 
									this.files = draft.data.files;
 | 
				
			||||||
				this.files = draft.files;
 | 
									if (draft.data.poll) {
 | 
				
			||||||
				if (draft.poll) {
 | 
					 | 
				
			||||||
					this.poll = true;
 | 
										this.poll = true;
 | 
				
			||||||
					this.update();
 | 
										this.update();
 | 
				
			||||||
					this.refs.poll.set(draft.poll);
 | 
										this.refs.poll.set(draft.data.poll);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				this.trigger('change-files', this.files);
 | 
									this.trigger('change-files', this.files);
 | 
				
			||||||
				this.update();
 | 
									this.update();
 | 
				
			||||||
| 
						 | 
					@ -487,8 +486,8 @@
 | 
				
			||||||
				poll: this.poll ? this.refs.poll.get() : undefined
 | 
									poll: this.poll ? this.refs.poll.get() : undefined
 | 
				
			||||||
			}).then(data => {
 | 
								}).then(data => {
 | 
				
			||||||
				this.clear();
 | 
									this.clear();
 | 
				
			||||||
 | 
									this.removeDraft();
 | 
				
			||||||
				this.trigger('post');
 | 
									this.trigger('post');
 | 
				
			||||||
				localStorage.removeItem(this.draftId);
 | 
					 | 
				
			||||||
				this.notify(this.repost
 | 
									this.notify(this.repost
 | 
				
			||||||
					? 'Repostしました!'
 | 
										? 'Repostしました!'
 | 
				
			||||||
					: this.inReplyToPost
 | 
										: this.inReplyToPost
 | 
				
			||||||
| 
						 | 
					@ -512,17 +511,30 @@
 | 
				
			||||||
		};
 | 
							};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		this.on('update', () => {
 | 
							this.on('update', () => {
 | 
				
			||||||
			this.save();
 | 
								this.saveDraft();
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		this.save = () => {
 | 
							this.saveDraft = () => {
 | 
				
			||||||
			const context = {
 | 
								const data = JSON.parse(localStorage.getItem('drafts') || '{}');
 | 
				
			||||||
				text: this.refs.text.value,
 | 
					 | 
				
			||||||
				files: this.files,
 | 
					 | 
				
			||||||
				poll: this.poll && this.refs.poll ? this.refs.poll.get() : undefined
 | 
					 | 
				
			||||||
			};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
			localStorage.setItem(this.draftId, JSON.stringify(context));
 | 
								data[this.draftId] = {
 | 
				
			||||||
 | 
									updated_at: new Date(),
 | 
				
			||||||
 | 
									data: {
 | 
				
			||||||
 | 
										text: this.refs.text.value,
 | 
				
			||||||
 | 
										files: this.files,
 | 
				
			||||||
 | 
										poll: this.poll && this.refs.poll ? this.refs.poll.get() : undefined
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								localStorage.setItem('drafts', JSON.stringify(data));
 | 
				
			||||||
 | 
							};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							this.removeDraft = () => {
 | 
				
			||||||
 | 
								const data = JSON.parse(localStorage.getItem('drafts') || '{}');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								delete data[this.draftId];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								localStorage.setItem('drafts', JSON.stringify(data));
 | 
				
			||||||
		};
 | 
							};
 | 
				
			||||||
	</script>
 | 
						</script>
 | 
				
			||||||
</mk-post-form>
 | 
					</mk-post-form>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue