2018-02-27 06:22:56 +00:00
|
|
|
<textarea
|
|
|
|
class="compose-box-input"
|
|
|
|
placeholder="What's on your mind?"
|
|
|
|
ref:textarea
|
2018-03-03 22:51:48 +00:00
|
|
|
bind:value=rawText
|
2018-03-01 02:45:29 +00:00
|
|
|
on:blur="onBlur()"
|
2018-03-25 01:04:54 +00:00
|
|
|
on:focus="onFocus()"
|
|
|
|
on:selectionChange="onSelectionChange(event)"
|
|
|
|
on:keydown="onKeydown(event)"
|
2018-02-27 06:22:56 +00:00
|
|
|
></textarea>
|
|
|
|
<style>
|
|
|
|
.compose-box-input {
|
|
|
|
grid-area: input;
|
|
|
|
margin: 10px 0 0 5px;
|
|
|
|
padding: 10px;
|
|
|
|
border: 1px solid var(--input-border);
|
|
|
|
min-height: 75px;
|
|
|
|
resize: none;
|
|
|
|
overflow: hidden;
|
|
|
|
word-wrap: break-word;
|
|
|
|
/* Text must be at least 16px or else iOS Safari zooms in */
|
|
|
|
font-size: 1.2em;
|
|
|
|
/* Hack to make Edge stretch the element all the way to the right.
|
|
|
|
* Also desktop Safari makes the gauge stretch too far to the right without it.
|
|
|
|
*/
|
|
|
|
width: calc(100% - 5px);
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script>
|
|
|
|
import { store } from '../../_store/store'
|
|
|
|
import { autosize } from '../../_utils/autosize'
|
|
|
|
import { scheduleIdleTask } from '../../_utils/scheduleIdleTask'
|
2018-04-06 00:57:36 +00:00
|
|
|
import debounce from 'lodash-es/debounce'
|
2018-02-27 06:22:56 +00:00
|
|
|
import { mark, stop } from '../../_utils/marks'
|
2018-03-25 01:04:54 +00:00
|
|
|
import { selectionChange } from '../../_utils/events'
|
|
|
|
import { clickSelectedAutosuggestionUsername } from '../../_actions/compose'
|
2018-03-25 19:24:38 +00:00
|
|
|
import { clickSelectedAutosuggestionEmoji } from '../../_actions/emoji'
|
2018-02-27 06:22:56 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
oncreate() {
|
2018-03-03 18:11:32 +00:00
|
|
|
this.setupSyncFromStore()
|
|
|
|
this.setupSyncToStore()
|
|
|
|
this.setupAutosize()
|
2018-02-27 06:22:56 +00:00
|
|
|
},
|
|
|
|
ondestroy() {
|
2018-03-03 18:11:32 +00:00
|
|
|
this.teardownAutosize()
|
2018-02-27 06:22:56 +00:00
|
|
|
},
|
2018-03-01 02:45:29 +00:00
|
|
|
methods: {
|
2018-03-03 18:11:32 +00:00
|
|
|
setupSyncFromStore() {
|
2018-03-20 03:58:53 +00:00
|
|
|
let textarea = this.refs.textarea
|
|
|
|
let firstTime = true
|
2018-03-03 22:51:48 +00:00
|
|
|
this.observe('text', text => {
|
2018-04-19 16:37:05 +00:00
|
|
|
let { rawText } = this.get()
|
|
|
|
if (rawText !== text) {
|
2018-03-25 19:24:38 +00:00
|
|
|
this.set({rawText: text})
|
|
|
|
// this next autosize is required to resize after
|
|
|
|
// the user clicks the "toot" button
|
|
|
|
mark('autosize.update()')
|
|
|
|
autosize.update(textarea)
|
|
|
|
stop('autosize.update()')
|
|
|
|
}
|
2018-03-20 03:58:53 +00:00
|
|
|
if (firstTime) {
|
|
|
|
firstTime = false
|
2018-04-19 16:37:05 +00:00
|
|
|
let { autoFocus } = this.get()
|
|
|
|
if (autoFocus) {
|
2018-04-05 02:49:05 +00:00
|
|
|
requestAnimationFrame(() => textarea.focus())
|
2018-03-20 03:58:53 +00:00
|
|
|
}
|
2018-03-17 02:04:48 +00:00
|
|
|
}
|
2018-03-03 18:11:32 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
setupSyncToStore() {
|
2018-04-10 01:30:15 +00:00
|
|
|
const saveStore = debounce(() => scheduleIdleTask(() => this.store.save()), 1000)
|
2018-03-03 18:11:32 +00:00
|
|
|
|
2018-03-03 22:51:48 +00:00
|
|
|
this.observe('rawText', rawText => {
|
2018-03-03 22:58:45 +00:00
|
|
|
mark('observe rawText')
|
2018-04-19 16:37:05 +00:00
|
|
|
let { realm } = this.get()
|
2018-03-03 22:51:48 +00:00
|
|
|
this.store.setComposeData(realm, {text: rawText})
|
2018-04-10 01:30:15 +00:00
|
|
|
saveStore()
|
2018-03-03 22:58:45 +00:00
|
|
|
stop('observe rawText')
|
2018-03-03 18:11:32 +00:00
|
|
|
}, {init: false})
|
|
|
|
},
|
|
|
|
setupAutosize() {
|
2018-03-20 03:58:53 +00:00
|
|
|
let textarea = this.refs.textarea
|
2018-03-03 18:11:32 +00:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
mark('autosize()')
|
2018-03-20 03:58:53 +00:00
|
|
|
autosize(textarea)
|
2018-03-03 18:11:32 +00:00
|
|
|
stop('autosize()')
|
|
|
|
})
|
|
|
|
},
|
|
|
|
teardownAutosize() {
|
|
|
|
mark('autosize.destroy()')
|
|
|
|
autosize.destroy(this.refs.textarea)
|
|
|
|
stop('autosize.destroy()')
|
|
|
|
},
|
2018-03-01 02:45:29 +00:00
|
|
|
onBlur() {
|
2018-03-25 01:04:54 +00:00
|
|
|
this.store.set({composeFocused: false})
|
|
|
|
},
|
|
|
|
onFocus() {
|
|
|
|
this.store.set({composeFocused: true})
|
|
|
|
},
|
|
|
|
onSelectionChange(selectionStart) {
|
|
|
|
this.store.set({composeSelectionStart: selectionStart})
|
|
|
|
},
|
|
|
|
onKeydown(e) {
|
|
|
|
let { keyCode } = e
|
2018-04-12 03:00:43 +00:00
|
|
|
// ctrl or cmd (on macs) was pressed; ctrl-enter means post a toot
|
|
|
|
const ctrlPressed = e.getModifierState('Control') || e.getModifierState('Meta')
|
2018-03-25 01:04:54 +00:00
|
|
|
switch (keyCode) {
|
|
|
|
case 9: // tab
|
|
|
|
this.clickSelectedAutosuggestion(e)
|
|
|
|
break
|
2018-04-12 03:01:17 +00:00
|
|
|
case 13: //enter
|
|
|
|
const autosuggestionClicked = this.clickSelectedAutosuggestion(e)
|
|
|
|
if (!autosuggestionClicked && ctrlPressed) {
|
|
|
|
this.fire('postAction')
|
|
|
|
}
|
|
|
|
break
|
2018-03-25 01:04:54 +00:00
|
|
|
case 38: // up
|
|
|
|
this.incrementAutosuggestSelected(-1, e)
|
|
|
|
break
|
|
|
|
case 40: // down
|
|
|
|
this.incrementAutosuggestSelected(1, e)
|
|
|
|
break
|
2018-03-31 00:34:10 +00:00
|
|
|
case 27: // escape
|
|
|
|
this.clearAutosuggestions(e)
|
|
|
|
break
|
2018-03-25 01:04:54 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
},
|
|
|
|
clickSelectedAutosuggestion(event) {
|
2018-04-19 16:37:05 +00:00
|
|
|
let {
|
|
|
|
composeAutosuggestionShown,
|
|
|
|
composeAutosuggestionType
|
|
|
|
} = this.store.get()
|
|
|
|
if (!composeAutosuggestionShown) {
|
2018-04-12 03:01:17 +00:00
|
|
|
return false
|
2018-03-25 01:04:54 +00:00
|
|
|
}
|
2018-04-19 16:37:05 +00:00
|
|
|
let { realm } = this.get()
|
|
|
|
if (composeAutosuggestionType === 'account') {
|
|
|
|
/* no await */ clickSelectedAutosuggestionUsername(realm)
|
2018-03-25 19:24:38 +00:00
|
|
|
} else { // emoji
|
2018-04-19 16:37:05 +00:00
|
|
|
/* no await */ clickSelectedAutosuggestionEmoji(realm)
|
2018-03-25 19:24:38 +00:00
|
|
|
}
|
2018-03-25 01:04:54 +00:00
|
|
|
event.preventDefault()
|
2018-03-31 00:34:10 +00:00
|
|
|
event.stopPropagation()
|
2018-04-12 03:01:17 +00:00
|
|
|
return true
|
2018-03-25 01:04:54 +00:00
|
|
|
},
|
|
|
|
incrementAutosuggestSelected(increment, event) {
|
2018-04-19 16:37:05 +00:00
|
|
|
let {
|
|
|
|
composeAutosuggestionShown,
|
|
|
|
composeAutosuggestionSelected,
|
|
|
|
composeAutosuggestionSearchResults
|
|
|
|
} = this.store.get()
|
|
|
|
if (!composeAutosuggestionShown) {
|
2018-03-25 01:04:54 +00:00
|
|
|
return
|
|
|
|
}
|
2018-04-19 16:37:05 +00:00
|
|
|
let selected = composeAutosuggestionSelected || 0
|
|
|
|
let searchResults = composeAutosuggestionSearchResults || []
|
2018-03-25 01:04:54 +00:00
|
|
|
selected += increment
|
|
|
|
if (selected >= 0) {
|
|
|
|
selected = selected % searchResults.length
|
|
|
|
} else {
|
|
|
|
selected = searchResults.length + selected
|
|
|
|
}
|
|
|
|
this.store.set({composeAutosuggestionSelected: selected})
|
|
|
|
event.preventDefault()
|
2018-03-31 00:34:10 +00:00
|
|
|
event.stopPropagation()
|
|
|
|
},
|
|
|
|
clearAutosuggestions(event) {
|
2018-04-19 16:37:05 +00:00
|
|
|
let { composeAutosuggestionShown } = this.store.get()
|
|
|
|
if (!composeAutosuggestionShown) {
|
2018-03-31 00:34:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
this.store.set({
|
|
|
|
composeAutosuggestionSearchResults: [],
|
|
|
|
composeAutosuggestionSelected: 0
|
|
|
|
})
|
|
|
|
event.preventDefault()
|
|
|
|
event.stopPropagation()
|
2018-03-01 02:45:29 +00:00
|
|
|
}
|
|
|
|
},
|
2018-02-27 06:22:56 +00:00
|
|
|
store: () => store,
|
2018-03-03 22:51:48 +00:00
|
|
|
data: () => ({
|
|
|
|
rawText: ''
|
2018-03-20 03:58:53 +00:00
|
|
|
}),
|
2018-03-25 01:04:54 +00:00
|
|
|
events: {
|
|
|
|
selectionChange
|
2018-03-20 03:58:53 +00:00
|
|
|
}
|
2018-02-27 06:22:56 +00:00
|
|
|
}
|
2018-04-12 03:01:17 +00:00
|
|
|
</script>
|