pinafore/routes/_components/compose/ComposeInput.html

150 lines
4.4 KiB
HTML
Raw Normal View History

2018-02-27 07:22:56 +01:00
<textarea
class="compose-box-input"
placeholder="What's on your mind?"
ref:textarea
2018-03-03 23:51:48 +01:00
bind:value=rawText
2018-03-01 03:45:29 +01:00
on:blur="onBlur()"
2018-03-25 03:04:54 +02:00
on:focus="onFocus()"
on:selectionChange="onSelectionChange(event)"
on:keydown="onKeydown(event)"
2018-02-27 07:22:56 +01: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'
import debounce from 'lodash/debounce'
import { mark, stop } from '../../_utils/marks'
2018-03-25 03:04:54 +02:00
import { selectionChange } from '../../_utils/events'
import { clickSelectedAutosuggestionUsername } from '../../_actions/compose'
2018-02-27 07:22:56 +01:00
export default {
oncreate() {
this.setupSyncFromStore()
this.setupSyncToStore()
this.setupAutosize()
2018-02-27 07:22:56 +01:00
},
ondestroy() {
this.teardownAutosize()
2018-02-27 07:22:56 +01:00
},
2018-03-01 03:45:29 +01:00
methods: {
setupSyncFromStore() {
let textarea = this.refs.textarea
let firstTime = true
2018-03-03 23:51:48 +01:00
this.observe('text', text => {
this.set({rawText: text})
if (firstTime) {
firstTime = false
if (this.get('autoFocus')) {
textarea.focus()
}
} else {
mark('autosize.update()')
autosize.update(textarea)
stop('autosize.update()')
2018-03-17 03:04:48 +01:00
}
})
},
setupSyncToStore() {
const saveText = debounce(() => scheduleIdleTask(() => this.store.save()), 1000)
2018-03-03 23:51:48 +01:00
this.observe('rawText', rawText => {
2018-03-03 23:58:45 +01:00
mark('observe rawText')
let realm = this.get('realm')
2018-03-03 23:51:48 +01:00
this.store.setComposeData(realm, {text: rawText})
saveText()
2018-03-03 23:58:45 +01:00
stop('observe rawText')
}, {init: false})
},
setupAutosize() {
let textarea = this.refs.textarea
requestAnimationFrame(() => {
mark('autosize()')
autosize(textarea)
stop('autosize()')
})
},
teardownAutosize() {
mark('autosize.destroy()')
autosize.destroy(this.refs.textarea)
stop('autosize.destroy()')
},
2018-03-01 03:45:29 +01:00
onBlur() {
2018-03-25 03:04:54 +02:00
this.store.set({composeFocused: false})
},
onFocus() {
this.store.set({composeFocused: true})
},
onSelectionChange(selectionStart) {
this.store.set({composeSelectionStart: selectionStart})
},
onKeydown(e) {
let { keyCode } = e
switch (keyCode) {
case 9: // tab
case 13: //enter
this.clickSelectedAutosuggestion(e)
break
case 38: // up
this.incrementAutosuggestSelected(-1, e)
break
case 40: // down
this.incrementAutosuggestSelected(1, e)
break
default:
}
},
clickSelectedAutosuggestion(event) {
let autosuggestionShown = this.store.get('composeAutosuggestionShown')
if (!autosuggestionShown) {
return
}
clickSelectedAutosuggestionUsername(this.get('realm'))
event.preventDefault()
},
incrementAutosuggestSelected(increment, event) {
let autosuggestionShown = this.store.get('composeAutosuggestionShown')
if (!autosuggestionShown) {
return
}
let selected = this.store.get('composeAutosuggestionSelected') || 0
let searchResults = this.store.get('composeAutosuggestionSearchResults') || []
selected += increment
if (selected >= 0) {
selected = selected % searchResults.length
} else {
selected = searchResults.length + selected
}
this.store.set({composeAutosuggestionSelected: selected})
event.preventDefault()
2018-03-01 03:45:29 +01:00
}
},
2018-02-27 07:22:56 +01:00
store: () => store,
2018-03-03 23:51:48 +01:00
data: () => ({
rawText: ''
}),
computed: {
postedStatusForRealm: ($postedStatusForRealm) => $postedStatusForRealm
2018-03-25 03:04:54 +02:00
},
events: {
selectionChange
}
2018-02-27 07:22:56 +01:00
}
</script>