Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit 9d296da

Browse files
committed
created ability to make content warnings on messages which also saves draft state
1 parent 585ab1a commit 9d296da

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

lib/depject/message/html/compose.js

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const h = require('mutant/h')
22
const when = require('mutant/when')
3-
const send = require('mutant/send')
43
const resolve = require('mutant/resolve')
54
const Value = require('mutant/value')
65
const computed = require('mutant/computed')
@@ -46,14 +45,17 @@ exports.create = function (api) {
4645
'ev-dragover': onDragOver,
4746
'ev-drop': onDrop,
4847
'ev-input': function () {
49-
hasContent.set(!!textArea.value)
48+
refreshHasContent()
5049
queueSave()
5150
},
5251
'ev-blur': () => {
5352
clearTimeout(blurTimeout)
5453
blurTimeout = setTimeout(() => focused.set(false), 200)
5554
},
56-
'ev-focus': send(focused.set, true),
55+
'ev-focus': () => {
56+
clearTimeout(blurTimeout)
57+
focused.set(true)
58+
},
5759
'ev-paste': ev => {
5860
const files = ev.clipboardData && ev.clipboardData.files
5961
if (!files || !files.length) return
@@ -69,12 +71,32 @@ exports.create = function (api) {
6971
placeholder
7072
})
7173

74+
const contentWarningInput = h('input.contentWarning', {
75+
placeholder: 'content warning (optional)',
76+
'ev-input': function () {
77+
refreshHasContent()
78+
queueSave()
79+
},
80+
'ev-blur': () => {
81+
clearTimeout(blurTimeout)
82+
blurTimeout = setTimeout(() => focused.set(false), 200)
83+
},
84+
'ev-focus': () => {
85+
clearTimeout(blurTimeout)
86+
focused.set(true)
87+
}
88+
})
89+
7290
if (draftKey) {
7391
const draft = window.localStorage[`patchwork.drafts.${draftKey}`]
7492
if (draft) {
7593
textArea.value = draft
76-
hasContent.set(!!textArea.value)
7794
}
95+
const draftCW = window.localStorage[`patchwork.drafts.contentWarning.${draftKey}`]
96+
if (draftCW) {
97+
contentWarningInput.value = draftCW
98+
}
99+
refreshHasContent()
78100
}
79101

80102
const warningMessage = Value(null)
@@ -113,6 +135,7 @@ exports.create = function (api) {
113135

114136
const actions = h('section.actions', [
115137
fileInput,
138+
contentWarningInput,
116139
h('div', [
117140
when(hasContent, clearButton),
118141
publishBtn
@@ -136,7 +159,7 @@ exports.create = function (api) {
136159

137160
composer.setText = function (value) {
138161
textArea.value = value
139-
hasContent.set(!!textArea.value)
162+
refreshHasContent()
140163
}
141164

142165
return composer
@@ -148,10 +171,15 @@ exports.create = function (api) {
148171
return
149172
}
150173
textArea.value = ''
151-
hasContent.set(!!textArea.value)
174+
contentWarningInput.value = ''
175+
refreshHasContent()
152176
save()
153177
}
154178

179+
function refreshHasContent () {
180+
hasContent.set(!!textArea.value || !!contentWarningInput.value)
181+
}
182+
155183
function queueSave () {
156184
saveTimer = setTimeout(save, 1000)
157185
}
@@ -164,6 +192,11 @@ exports.create = function (api) {
164192
} else {
165193
window.localStorage[`patchwork.drafts.${draftKey}`] = textArea.value
166194
}
195+
if (!contentWarningInput.value) {
196+
delete window.localStorage[`patchwork.drafts.contentWarning.${draftKey}`]
197+
} else {
198+
window.localStorage[`patchwork.drafts.contentWarning.${draftKey}`] = contentWarningInput.value
199+
}
167200
}
168201
}
169202

@@ -245,6 +278,10 @@ exports.create = function (api) {
245278
})
246279
})
247280

281+
if (contentWarningInput.value) {
282+
content.contentWarning = contentWarningInput.value
283+
}
284+
248285
try {
249286
if (typeof prepublish === 'function') {
250287
content = prepublish(content)
@@ -271,7 +308,8 @@ exports.create = function (api) {
271308
} else {
272309
if (msg) {
273310
textArea.value = ''
274-
hasContent.set(!!textArea.value)
311+
contentWarningInput.value = ''
312+
refreshHasContent()
275313
save()
276314
}
277315
if (cb) cb(null, msg)

styles/base/compose.mcss

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ Compose {
6060
box-shadow: none
6161
}
6262
}
63-
63+
input.contentWarning {
64+
flex: 1;
65+
font-size: 120%
66+
padding: 3px 6px;
67+
border: 2px solid #ddd;
68+
padding-top: .3rem;
69+
margin-left: 5px;
70+
margin-right: 5px;
71+
min-width: 0;
72+
}
6473
(button) {
6574
-clear {
6675
margin-right: 5px

0 commit comments

Comments
 (0)