pinafore/src/routes/_components/compose/ComposeInput.html

249 lines
7.7 KiB
HTML
Raw Normal View History

2018-02-27 07:22:56 +01:00
<textarea
id="the-compose-box-input-{realm}"
class="compose-box-input compose-box-input-realm-{realm}"
2018-02-27 07:22:56 +01:00
placeholder="What's on your mind?"
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 07:22:56 +01:00
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>
<label for="the-compose-box-input-{realm}" class="sr-only">
What's on your mind?
</label>
<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 07:22:56 +01: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);
}
.compose-box-input-realm-dialog {
max-height: 40vh;
overflow-y: auto;
}
@media (max-width: 767px) {
.compose-box-input-realm-dialog {
max-height: 30vh;
}
}
@media (max-width: 479px) {
.compose-box-input-realm-dialog {
max-height: 20vh;
}
}
2018-02-27 07:22:56 +01:00
</style>
<script>
import { store } from '../../_store/store'
import { autosize } from '../../_thirdparty/autosize/autosize'
2018-02-27 07:22:56 +01:00
import { scheduleIdleTask } from '../../_utils/scheduleIdleTask'
import debounce from 'lodash-es/debounce'
2018-02-27 07:22:56 +01:00
import { mark, stop } from '../../_utils/marks'
2018-03-25 03:04:54 +02:00
import { selectionChange } from '../../_utils/events'
import {
clickSelectedAutosuggestionUsername,
clickSelectedAutosuggestionEmoji
} from '../../_actions/autosuggest'
import { observe } from 'svelte-extras'
import { get } from '../../_utils/lodash-lite'
2018-02-27 07:22:56 +01:00
export default {
2018-04-20 06:38:01 +02:00
oncreate () {
this.setupSyncFromStore()
this.setupSyncToStore()
this.setupAutosize()
2018-02-27 07:22:56 +01:00
},
2018-04-20 06:38:01 +02:00
ondestroy () {
this.teardownAutosize()
2018-02-27 07:22:56 +01:00
},
2018-03-01 03:45:29 +01:00
methods: {
observe,
2018-04-20 06:38:01 +02:00
setupSyncFromStore () {
let textarea = this.refs.textarea
let firstTime = true
2018-03-03 23:51:48 +01:00
this.observe('text', text => {
let { rawText } = this.get()
if (rawText !== text) {
this.set({ rawText: text })
2018-03-25 21:24:38 +02:00
// this next autosize is required to resize after
// the user clicks the "toot" button
mark('autosize.update()')
autosize.update(textarea)
stop('autosize.update()')
}
if (firstTime) {
firstTime = false
let { autoFocus } = this.get()
if (autoFocus) {
requestAnimationFrame(() => textarea.focus({ preventScroll: true }))
}
2018-03-17 03:04:48 +01:00
}
})
},
2018-04-20 06:38:01 +02:00
setupSyncToStore () {
const saveStore = 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()
this.store.setComposeData(realm, { text: rawText })
saveStore()
2018-03-03 23:58:45 +01:00
stop('observe rawText')
}, { init: false })
},
2018-04-20 06:38:01 +02:00
setupAutosize () {
let textarea = this.refs.textarea
requestAnimationFrame(() => {
mark('autosize()')
autosize(textarea)
stop('autosize()')
})
},
2018-04-20 06:38:01 +02:00
teardownAutosize () {
mark('autosize.destroy()')
autosize.destroy(this.refs.textarea)
stop('autosize.destroy()')
},
2018-04-20 06:38:01 +02:00
onBlur () {
requestAnimationFrame(() => {
this.store.setForCurrentAutosuggest({ composeFocused: false })
})
2018-03-25 03:04:54 +02:00
},
2018-04-20 06:38:01 +02:00
onFocus () {
scheduleIdleTask(() => {
let { realm } = this.get()
this.store.set({ currentComposeRealm: realm })
this.store.setForCurrentAutosuggest({ composeFocused: true })
})
2018-03-25 03:04:54 +02:00
},
2018-04-20 06:38:01 +02:00
onSelectionChange (selectionStart) {
scheduleIdleTask(() => {
this.store.setForCurrentAutosuggest({ composeSelectionStart: selectionStart })
})
2018-03-25 03:04:54 +02:00
},
2018-04-20 06:38:01 +02:00
onKeydown (e) {
2018-03-25 03:04:54 +02:00
let { keyCode } = e
2018-04-12 05:00:43 +02: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 03:04:54 +02:00
switch (keyCode) {
case 9: // tab
this.clickSelectedAutosuggestion(e)
break
2018-04-20 06:38:01 +02:00
case 13: // enter
const autosuggestionClicked = this.clickSelectedAutosuggestion(e)
if (!autosuggestionClicked && ctrlPressed) {
this.fire('postAction')
}
break
2018-03-25 03:04:54 +02:00
case 38: // up
this.incrementAutosuggestSelected(-1, e)
break
case 40: // down
this.incrementAutosuggestSelected(1, e)
break
2018-03-31 02:34:10 +02:00
case 27: // escape
this.clearAutosuggestions(e)
break
2018-03-25 03:04:54 +02:00
default:
}
},
2018-04-20 06:38:01 +02:00
clickSelectedAutosuggestion (event) {
let {
autosuggestShown,
autosuggestType
} = this.store.get()
if (!autosuggestShown) {
return false
2018-03-25 03:04:54 +02:00
}
event.preventDefault()
2018-03-31 02:34:10 +02:00
event.stopPropagation()
const clickAutosuggestedItem = async () => {
let { realm } = this.get()
/* 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()
return true
2018-03-25 03:04:54 +02:00
},
2018-04-20 06:38:01 +02:00
incrementAutosuggestSelected (increment, event) {
let {
autosuggestShown,
autosuggestSelected,
autosuggestSearchResults
} = this.store.get()
if (!autosuggestShown) {
2018-03-25 03:04:54 +02:00
return
}
autosuggestSelected += increment
if (autosuggestSelected >= 0) {
autosuggestSelected = autosuggestSelected % autosuggestSearchResults.length
2018-03-25 03:04:54 +02:00
} else {
autosuggestSelected = autosuggestSearchResults.length + autosuggestSelected
2018-03-25 03:04:54 +02:00
}
this.store.setForCurrentAutosuggest({ autosuggestSelected })
2018-03-25 03:04:54 +02:00
event.preventDefault()
2018-03-31 02:34:10 +02:00
event.stopPropagation()
},
2018-04-20 06:38:01 +02:00
clearAutosuggestions (event) {
let { autosuggestShown } = this.store.get()
if (!autosuggestShown) {
2018-03-31 02:34:10 +02:00
return
}
this.store.setForCurrentAutosuggest({
autosuggestSearchResults: [],
autosuggestSelected: 0
2018-03-31 02:34:10 +02:00
})
event.preventDefault()
event.stopPropagation()
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: {
/* 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 03:04:54 +02:00
events: {
selectionChange
}
2018-02-27 07:22:56 +01:00
}
</script>