Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Save message draft
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Jan 11, 2019
1 parent af33aac commit 2abd3dc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
26 changes: 26 additions & 0 deletions lib/model/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Account {
this.isRead = true
this.mentions = 0

// Save the config of channels, since channels are loaded later.
if (config.channels)
this.channelsConfig = config.channels.reduce((r, i) => { r[i.id] = i; return r }, {})

this.status = 'connecting'
this.isChannelsReady = false

Expand All @@ -42,9 +46,31 @@ class Account {
Object.assign(config, { currentChannelId: this.currentChannelId })
if (this.icon)
Object.assign(config, { icon: this.icon })
for (const messageList of this.channels.concat(this.dms)) {
const c = messageList.serialize()
if (!c)
continue
if (!config.channels)
config.channels = []
config.channels.push(c)
}
return config
}

channelsLoaded() {
if (this.channelsConfig) {
for (const messageList of this.channels.concat(this.dms)) {
const config = this.channelsConfig[messageList.id]
if (config)
messageList.deserialize(config)
}
}
this.setReadState(this.computeReadState())
this.updateMentions()
this.isChannelsReady = true
this.onUpdateChannels.dispatch(this.channels)
}

findChannelById(id) {
let channel = this.channels.find((c) => c.id == id)
if (!channel)
Expand Down
15 changes: 15 additions & 0 deletions lib/model/message-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MessageList {
this.type = type
this.id = id
this.messages = []
this.draft = null
this.mentions = 0

// Whether all messages of channel have been read.
Expand Down Expand Up @@ -53,6 +54,16 @@ class MessageList {
this.onUpdateMentions = new Signal()
}

serialize() {
if (!this.draft)
return null
return {id: this.id, draft: this.draft}
}

deserialize(config) {
this.draft = config.draft
}

markRead() {
// Clear the unread marker.
this.lastReadTs = this.latestTs
Expand Down Expand Up @@ -109,6 +120,10 @@ class MessageList {
this.messages = []
}

setDraft(draft) {
this.draft = draft
}

hasMessages() {
return this.messages.length > 0
}
Expand Down
5 changes: 1 addition & 4 deletions lib/service/slack/slack-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ class SlackAccount extends Account {
.map((c) => new SlackDirectMessage(this, c))
// Notify.
this.updatePresenceSubscription()
this.setReadState(this.computeReadState())
this.updateMentions()
this.isChannelsReady = true
this.onUpdateChannels.dispatch(this.channels)
this.channelsLoaded()
}

updatePresenceSubscription() {
Expand Down
20 changes: 16 additions & 4 deletions lib/view/chat-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class ChatBox {
}
if (this.messageList) {
this.messageList.stopReceiving()
this.messageList.setDraft(this._getMessage())
this.messagesLoaded = false
if (this.isDisplaying) {
this.isDisplaying = false
Expand Down Expand Up @@ -155,8 +156,7 @@ class ChatBox {
this.messagesLoaded = false
// Start showing the messages.
if (messageList === this.messageList) {
// TODO Remember unsent messages.
this.replyEntry.setText('')
this.replyEntry.setText(messageList.draft ? messageList.draft : '')
this._adjustEntryHeight()
const html = pageTemplate({messageList, messages})
if (process.env.WEY_DEBUG === '1')
Expand Down Expand Up @@ -293,25 +293,37 @@ class ChatBox {
this.replyEntry.setStyle({height})
}

_getMessage() {
const message = this.replyEntry.getText()
if (message.trim().length == 0 || !this.messageList || this.isSendingReply)
return null
return message
}

_handleEnter(replyEntry) {
if (gui.Event.isShiftPressed())
return true
const message = replyEntry.getText()
if (message.trim().length == 0 || !this.messageList || this.isSendingReply)
const message = this._getMessage()
if (message === null)
return false
replyEntry.setEnabled(false)
this.isSendingReply = true
const messageList = this.messageList
this.messageList
.sendMessage(message)
.then((res) => {
replyEntry.setText('')
replyEntry.setEnabled(true)
this._adjustEntryHeight()
this.isSendingReply = false
if (messageList)
messageList.draft = null
})
.catch((error) => {
// TODO Report error
console.error(error)
if (this.messageList === messageList && messageList.draft !== null)
replyEntry.setText(messageList.draft)
replyEntry.setEnabled(true)
this.isSendingReply = false
})
Expand Down

0 comments on commit 2abd3dc

Please sign in to comment.