2018-02-27 06:22:56 +00:00
|
|
|
<textarea
|
2018-12-05 06:31:46 +00:00
|
|
|
id="the-compose-box-input-{realm}"
|
2019-02-14 16:46:43 +00:00
|
|
|
class="compose-box-input compose-box-input-realm-{realm}"
|
2018-02-27 06:22:56 +00:00
|
|
|
placeholder="What's on your mind?"
|
2019-05-06 05:08:54 +00:00
|
|
|
aria-describedby="compose-box-input-description-{realm}"
|
|
|
|
aria-owns="compose-autosuggest-list-{realm}"
|
|
|
|
aria-expanded={autosuggestShownForThisInput}
|
|
|
|
aria-autocomplete="both"
|
|
|
|
aria-activedescendant="{autosuggestShownForThisInput ? `compose-autosuggest-active-item-${realm}` : ''}"
|
2018-02-27 06:22:56 +00:00
|
|
|
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>
|
2018-12-05 06:31:46 +00:00
|
|
|
<label for="the-compose-box-input-{realm}" class="sr-only">
|
|
|
|
What's on your mind?
|
|
|
|
</label>
|
2019-05-06 05:08:54 +00:00
|
|
|
<span id="compose-box-input-description-{realm}" class="sr-only">
|
|
|
|
When autocomplete results are available, press up or down arrows and enter to select.
|
|
|
|
</span>
|
2018-02-27 06:22:56 +00:00
|
|
|
<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);
|
|
|
|
}
|
2019-02-14 02:38:34 +00:00
|
|
|
|
2019-02-14 16:46:43 +00:00
|
|
|
.compose-box-input-realm-dialog {
|
2019-02-14 02:38:34 +00:00
|
|
|
max-height: 40vh;
|
|
|
|
overflow-y: auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
@media (max-width: 767px) {
|
2019-02-14 16:46:43 +00:00
|
|
|
.compose-box-input-realm-dialog {
|
2019-02-14 02:38:34 +00:00
|
|
|
max-height: 30vh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@media (max-width: 479px) {
|
2019-02-14 16:46:43 +00:00
|
|
|
.compose-box-input-realm-dialog {
|
2019-02-14 02:38:34 +00:00
|
|
|
max-height: 20vh;
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 06:22:56 +00:00
|
|
|
</style>
|
|
|
|
<script>
|
|
|
|
import { store } from '../../_store/store'
|
2018-11-21 08:33:46 +00:00
|
|
|
import { autosize } from '../../_thirdparty/autosize/autosize'
|
2018-02-27 06:22:56 +00:00
|
|
|
import { scheduleIdleTask } from '../../_utils/scheduleIdleTask'
|
|
|
|
import { mark, stop } from '../../_utils/marks'
|
2018-03-25 01:04:54 +00:00
|
|
|
import { selectionChange } from '../../_utils/events'
|
2018-05-06 23:25:17 +00:00
|
|
|
import {
|
|
|
|
clickSelectedAutosuggestionUsername,
|
|
|
|
clickSelectedAutosuggestionEmoji
|
|
|
|
} from '../../_actions/autosuggest'
|
2018-04-30 15:29:04 +00:00
|
|
|
import { observe } from 'svelte-extras'
|
2019-05-06 05:08:54 +00:00
|
|
|
import { get } from '../../_utils/lodash-lite'
|
2019-07-18 04:34:53 +00:00
|
|
|
import { on } from '../../_utils/eventBus'
|
2019-08-07 16:11:34 +00:00
|
|
|
import { requestPostAnimationFrame } from '../../_utils/requestPostAnimationFrame'
|
2019-08-20 04:37:11 +00:00
|
|
|
import { throttleTimer } from '../../_utils/throttleTimer'
|
|
|
|
|
|
|
|
const updateComposeTextInStore = throttleTimer(scheduleIdleTask)
|
2018-02-27 06:22:56 +00:00
|
|
|
|
|
|
|
export default {
|
2018-04-20 04:38:01 +00:00
|
|
|
oncreate () {
|
2018-03-03 18:11:32 +00:00
|
|
|
this.setupSyncFromStore()
|
|
|
|
this.setupSyncToStore()
|
|
|
|
this.setupAutosize()
|
2019-07-18 04:34:53 +00:00
|
|
|
this.setupResize()
|
2018-02-27 06:22:56 +00:00
|
|
|
},
|
2018-04-20 04:38:01 +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-04-30 15:29:04 +00:00
|
|
|
observe,
|
2018-04-20 04:38:01 +00:00
|
|
|
setupSyncFromStore () {
|
2019-08-03 20:49:37 +00:00
|
|
|
const textarea = this.refs.textarea
|
2018-03-20 03:58:53 +00:00
|
|
|
let firstTime = true
|
2018-03-03 22:51:48 +00:00
|
|
|
this.observe('text', text => {
|
2019-08-03 20:49:37 +00:00
|
|
|
const { rawText } = this.get()
|
2018-04-19 16:37:05 +00:00
|
|
|
if (rawText !== text) {
|
2018-08-30 04:42:57 +00:00
|
|
|
this.set({ rawText: text })
|
2018-03-25 19:24:38 +00:00
|
|
|
// 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
|
2019-08-03 20:49:37 +00:00
|
|
|
const { autoFocus } = this.get()
|
2018-04-19 16:37:05 +00:00
|
|
|
if (autoFocus) {
|
2019-02-23 20:32:18 +00:00
|
|
|
requestAnimationFrame(() => textarea.focus({ preventScroll: true }))
|
2018-03-20 03:58:53 +00:00
|
|
|
}
|
2018-03-17 02:04:48 +00:00
|
|
|
}
|
2018-03-03 18:11:32 +00:00
|
|
|
})
|
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
setupSyncToStore () {
|
2019-08-20 04:37:11 +00:00
|
|
|
const { realm } = this.get()
|
2018-03-03 22:51:48 +00:00
|
|
|
this.observe('rawText', rawText => {
|
2019-08-20 04:37:11 +00:00
|
|
|
updateComposeTextInStore(() => {
|
|
|
|
mark('updateComposeTextInStore')
|
|
|
|
this.store.setComposeData(realm, { text: rawText })
|
|
|
|
this.store.save()
|
|
|
|
stop('updateComposeTextInStore')
|
|
|
|
})
|
2018-08-30 04:42:57 +00:00
|
|
|
}, { init: false })
|
2018-03-03 18:11:32 +00:00
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
setupAutosize () {
|
2019-08-03 20:49:37 +00:00
|
|
|
const textarea = this.refs.textarea
|
2019-08-07 16:11:34 +00:00
|
|
|
requestPostAnimationFrame(() => {
|
2018-03-03 18:11:32 +00:00
|
|
|
mark('autosize()')
|
2018-03-20 03:58:53 +00:00
|
|
|
autosize(textarea)
|
2018-03-03 18:11:32 +00:00
|
|
|
stop('autosize()')
|
|
|
|
})
|
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
teardownAutosize () {
|
2018-03-03 18:11:32 +00:00
|
|
|
mark('autosize.destroy()')
|
|
|
|
autosize.destroy(this.refs.textarea)
|
|
|
|
stop('autosize.destroy()')
|
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
onBlur () {
|
2019-05-06 15:34:20 +00:00
|
|
|
requestAnimationFrame(() => {
|
2018-08-30 04:42:57 +00:00
|
|
|
this.store.setForCurrentAutosuggest({ composeFocused: false })
|
2018-05-06 23:25:17 +00:00
|
|
|
})
|
2018-03-25 01:04:54 +00:00
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
onFocus () {
|
2018-05-06 23:25:17 +00:00
|
|
|
scheduleIdleTask(() => {
|
2019-08-03 20:49:37 +00:00
|
|
|
const { realm } = this.get()
|
2018-08-30 04:42:57 +00:00
|
|
|
this.store.set({ currentComposeRealm: realm })
|
|
|
|
this.store.setForCurrentAutosuggest({ composeFocused: true })
|
2018-05-06 23:25:17 +00:00
|
|
|
})
|
2018-03-25 01:04:54 +00:00
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
onSelectionChange (selectionStart) {
|
2018-05-06 23:25:17 +00:00
|
|
|
scheduleIdleTask(() => {
|
2018-08-30 04:42:57 +00:00
|
|
|
this.store.setForCurrentAutosuggest({ composeSelectionStart: selectionStart })
|
2018-05-06 23:25:17 +00:00
|
|
|
})
|
2018-03-25 01:04:54 +00:00
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
onKeydown (e) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const { 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) {
|
2019-08-20 02:08:59 +00:00
|
|
|
case 9: { // tab
|
2018-03-25 01:04:54 +00:00
|
|
|
this.clickSelectedAutosuggestion(e)
|
|
|
|
break
|
2019-08-20 02:08:59 +00:00
|
|
|
}
|
|
|
|
case 13: { // enter
|
2018-04-12 03:01:17 +00:00
|
|
|
const autosuggestionClicked = this.clickSelectedAutosuggestion(e)
|
|
|
|
if (!autosuggestionClicked && ctrlPressed) {
|
|
|
|
this.fire('postAction')
|
|
|
|
}
|
|
|
|
break
|
2019-08-20 02:08:59 +00:00
|
|
|
}
|
|
|
|
case 38: { // up
|
2018-03-25 01:04:54 +00:00
|
|
|
this.incrementAutosuggestSelected(-1, e)
|
|
|
|
break
|
2019-08-20 02:08:59 +00:00
|
|
|
}
|
|
|
|
case 40: { // down
|
2018-03-25 01:04:54 +00:00
|
|
|
this.incrementAutosuggestSelected(1, e)
|
|
|
|
break
|
2019-08-20 02:08:59 +00:00
|
|
|
}
|
|
|
|
case 27: { // escape
|
2018-03-31 00:34:10 +00:00
|
|
|
this.clearAutosuggestions(e)
|
|
|
|
break
|
2019-08-20 02:08:59 +00:00
|
|
|
}
|
2018-03-25 01:04:54 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
clickSelectedAutosuggestion (event) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const {
|
2018-05-06 23:25:17 +00:00
|
|
|
autosuggestShown,
|
|
|
|
autosuggestType
|
2018-04-19 16:37:05 +00:00
|
|
|
} = this.store.get()
|
2018-05-06 23:25:17 +00:00
|
|
|
if (!autosuggestShown) {
|
2018-04-12 03:01:17 +00:00
|
|
|
return false
|
2018-03-25 01:04:54 +00:00
|
|
|
}
|
|
|
|
event.preventDefault()
|
2018-03-31 00:34:10 +00:00
|
|
|
event.stopPropagation()
|
2019-05-06 15:34:20 +00:00
|
|
|
|
|
|
|
const clickAutosuggestedItem = async () => {
|
2019-08-03 20:49:37 +00:00
|
|
|
const { realm } = this.get()
|
2019-05-06 15:34:20 +00:00
|
|
|
/* autosuggestSelecting prevents a flash of searched content */
|
|
|
|
this.store.setForCurrentAutosuggest({ autosuggestSelecting: true })
|
|
|
|
if (autosuggestType === 'account') {
|
|
|
|
await clickSelectedAutosuggestionUsername(realm)
|
|
|
|
} else { // emoji
|
|
|
|
await clickSelectedAutosuggestionEmoji(realm)
|
|
|
|
}
|
|
|
|
this.store.setForCurrentAutosuggest({ autosuggestSelecting: false })
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no await */ clickAutosuggestedItem()
|
2018-04-12 03:01:17 +00:00
|
|
|
return true
|
2018-03-25 01:04:54 +00:00
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
incrementAutosuggestSelected (increment, event) {
|
2018-04-19 16:37:05 +00:00
|
|
|
let {
|
2018-05-06 23:25:17 +00:00
|
|
|
autosuggestShown,
|
|
|
|
autosuggestSelected,
|
|
|
|
autosuggestSearchResults
|
2018-04-19 16:37:05 +00:00
|
|
|
} = this.store.get()
|
2018-05-06 23:25:17 +00:00
|
|
|
if (!autosuggestShown) {
|
2018-03-25 01:04:54 +00:00
|
|
|
return
|
|
|
|
}
|
2018-05-06 23:25:17 +00:00
|
|
|
autosuggestSelected += increment
|
|
|
|
if (autosuggestSelected >= 0) {
|
|
|
|
autosuggestSelected = autosuggestSelected % autosuggestSearchResults.length
|
2018-03-25 01:04:54 +00:00
|
|
|
} else {
|
2018-05-06 23:25:17 +00:00
|
|
|
autosuggestSelected = autosuggestSearchResults.length + autosuggestSelected
|
2018-03-25 01:04:54 +00:00
|
|
|
}
|
2018-08-30 04:42:57 +00:00
|
|
|
this.store.setForCurrentAutosuggest({ autosuggestSelected })
|
2018-03-25 01:04:54 +00:00
|
|
|
event.preventDefault()
|
2018-03-31 00:34:10 +00:00
|
|
|
event.stopPropagation()
|
|
|
|
},
|
2018-04-20 04:38:01 +00:00
|
|
|
clearAutosuggestions (event) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const { autosuggestShown } = this.store.get()
|
2018-05-06 23:25:17 +00:00
|
|
|
if (!autosuggestShown) {
|
2018-03-31 00:34:10 +00:00
|
|
|
return
|
|
|
|
}
|
2018-05-06 23:25:17 +00:00
|
|
|
this.store.setForCurrentAutosuggest({
|
|
|
|
autosuggestSearchResults: [],
|
|
|
|
autosuggestSelected: 0
|
2018-03-31 00:34:10 +00:00
|
|
|
})
|
|
|
|
event.preventDefault()
|
|
|
|
event.stopPropagation()
|
2019-07-18 04:34:53 +00:00
|
|
|
},
|
|
|
|
setupResize () {
|
|
|
|
on('resizeComposeInput', this, realm => {
|
|
|
|
if (realm === this.get().realm) {
|
|
|
|
autosize.update(this.refs.textarea)
|
|
|
|
}
|
|
|
|
})
|
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
|
|
|
}),
|
2019-05-06 05:08:54 +00:00
|
|
|
computed: {
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
composeFocused: ({ $autosuggestData_composeFocused, $currentInstance, realm }) => (
|
|
|
|
get($autosuggestData_composeFocused, [$currentInstance, realm], false)
|
|
|
|
),
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
autosuggestShownForThisInput: ({ realm, $autosuggestShown, composeFocused }) => (
|
|
|
|
!!($autosuggestShown && composeFocused)
|
|
|
|
)
|
|
|
|
},
|
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>
|