git ssb

10+

Matt McKegg / patchwork



Tree: 833e6272fdc03999e6a10c74b5adff86a11d8d39

Files: 833e6272fdc03999e6a10c74b5adff86a11d8d39 / modules / message / html / compose.js

4505 bytesRaw
1var h = require('mutant/h')
2var when = require('mutant/when')
3var send = require('mutant/send')
4var resolve = require('mutant/resolve')
5var Value = require('mutant/value')
6var computed = require('mutant/computed')
7var nest = require('depnest')
8var mentions = require('ssb-mentions')
9var extend = require('xtend')
10var addSuggest = require('suggest-box')
11
12exports.needs = nest({
13 'blob.html.input': 'first',
14 'profile.async.suggest': 'first',
15 'channel.async.suggest': 'first',
16 'message.async.publish': 'first',
17 'emoji.sync.names': 'first',
18 'emoji.sync.url': 'first'
19})
20
21exports.gives = nest('message.html.compose')
22
23exports.create = function (api) {
24 return nest('message.html.compose', function ({shrink = true, meta, prepublish, placeholder = 'Write a message'}, cb) {
25 var files = []
26 var filesById = {}
27 var focused = Value(false)
28 var hasContent = Value(false)
29 var publishing = Value(false)
30 var getProfileSuggestions = api.profile.async.suggest()
31 var getChannelSuggestions = api.channel.async.suggest()
32
33 var blurTimeout = null
34
35 var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => {
36 if (!shrink || hasContent) {
37 return true
38 } else {
39 return focused
40 }
41 })
42
43 var textArea = h('textarea', {
44 'ev-input': function () {
45 hasContent.set(!!textArea.value)
46 },
47 'ev-blur': () => {
48 clearTimeout(blurTimeout)
49 blurTimeout = setTimeout(() => focused.set(false), 200)
50 },
51 'ev-focus': send(focused.set, true),
52 disabled: publishing,
53 placeholder
54 })
55
56 var fileInput = api.blob.html.input(file => {
57 files.push(file)
58 filesById[file.link] = file
59
60 var embed = file.type.indexOf('image/') === 0 ? '!' : ''
61 var pos = textArea.selectionStart
62 var before = textArea.value.slice(0, pos)
63 var after = textArea.value.slice(pos)
64
65 var spacer = embed ? '\n' : ' '
66 if (before && !before.endsWith(spacer)) before += spacer
67 if (!after.startsWith(spacer)) after = spacer + after
68
69 textArea.value = `${before}![${file.name}](${file.link})${after}`
70 console.log('added:', file)
71 })
72
73 fileInput.onclick = function () {
74 hasContent.set(true)
75 }
76
77 var publishBtn = h('button', {
78 'ev-click': publish,
79 disabled: publishing
80 }, when(publishing, 'Publishing...', 'Publish'))
81
82 var actions = h('section.actions', [
83 fileInput,
84 publishBtn
85 ])
86
87 var composer = h('Compose', {
88 classList: [
89 when(expanded, '-expanded', '-contracted')
90 ]
91 }, [
92 textArea,
93 actions
94 ])
95
96 addSuggest(textArea, (inputText, cb) => {
97 if (inputText[0] === '@') {
98 cb(null, getProfileSuggestions(inputText.slice(1)))
99 } else if (inputText[0] === '#') {
100 cb(null, getChannelSuggestions(inputText.slice(1)))
101 } else if (inputText[0] === ':') {
102 // suggest emojis
103 var word = inputText.slice(1)
104 if (word[word.length - 1] === ':') {
105 word = word.slice(0, -1)
106 }
107 // TODO: when no emoji typed, list some default ones
108 cb(null, api.emoji.sync.names().filter(function (name) {
109 return name.slice(0, word.length) === word
110 }).slice(0, 100).map(function (emoji) {
111 return {
112 image: api.emoji.sync.url(emoji),
113 title: emoji,
114 subtitle: emoji,
115 value: ':' + emoji + ':'
116 }
117 }))
118 }
119 }, {cls: 'SuggestBox'})
120
121 return composer
122
123 // scoped
124
125 function publish () {
126 publishing.set(true)
127
128 var content = extend(resolve(meta), {
129 text: textArea.value,
130 mentions: mentions(textArea.value).map(mention => {
131 // merge markdown-detected mention with file info
132 var file = filesById[mention.link]
133 if (file) {
134 if (file.type) mention.type = file.type
135 if (file.size) mention.size = file.size
136 }
137 return mention
138 })
139 })
140
141 try {
142 if (typeof prepublish === 'function') {
143 content = prepublish(content)
144 }
145 } catch (err) {
146 publishing.set(false)
147 if (cb) cb(err)
148 else throw err
149 }
150
151 return api.message.async.publish(content, done)
152
153 function done (err, msg) {
154 publishing.set(false)
155 if (err) throw err
156 else if (msg) textArea.value = ''
157 if (cb) cb(err, msg)
158 }
159 }
160 })
161}
162

Built with git-ssb-web