Spaces:
Sleeping
Sleeping
File size: 4,155 Bytes
13555f3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useState} from 'react'
import Select from 'react-select'
import {CSSObject} from '@emotion/serialize'
import {getSelectBaseStyle} from '../../theme'
import * as registry from './blocks/'
import {ContentType} from './blocks/types'
type Props = {
onChange: (value: string) => void
onChangeType: (blockType: ContentType) => void
onSave: (value: string, blockType: string) => void
value: string
}
const baseStyles = getSelectBaseStyle()
const styles = {
...baseStyles,
control: (provided: CSSObject): CSSObject => ({
...provided,
width: '100%',
height: '100%',
display: 'flex',
background: 'rgb(var(--center-channel-bg-rgb))',
color: 'rgb(var(--center-channel-color-rgb))',
flexDirection: 'row',
}),
input: (provided: CSSObject): CSSObject => ({
...provided,
background: 'rgb(var(--center-channel-bg-rgb))',
color: 'rgb(var(--center-channel-color-rgb))',
}),
menu: (provided: CSSObject): CSSObject => ({
...provided,
minWidth: '100%',
width: 'max-content',
background: 'rgb(var(--center-channel-bg-rgb))',
left: '0',
marginBottom: '0',
}),
menuPortal: (provided: CSSObject): CSSObject => ({
...provided,
zIndex: 999,
}),
}
export default function RootInput(props: Props) {
const [showMenu, setShowMenu] = useState(false)
return (
<Select
styles={styles}
components={{DropdownIndicator: () => null, IndicatorSeparator: () => null}}
className='RootInput'
placeholder={'Introduce your text or your slash command'}
autoFocus={true}
menuIsOpen={showMenu}
menuPortalTarget={document.getElementById('focalboard-root-portal')}
menuPosition={'fixed'}
options={registry.list()}
getOptionValue={(ct: ContentType) => ct.slashCommand}
getOptionLabel={(ct: ContentType) => ct.slashCommand + ' Creates a new ' + ct.displayName + ' block.'}
filterOption={(option: any, inputValue: string): boolean => {
return inputValue.startsWith(option.value) || option.value.startsWith(inputValue)
}}
inputValue={props.value}
onInputChange={(inputValue: string) => {
props.onChange(inputValue)
if (inputValue.startsWith('/')) {
setShowMenu(true)
} else {
setShowMenu(false)
}
}}
onChange={(ct: ContentType|null) => {
if (ct) {
const args = props.value.split(' ').slice(1)
ct.runSlashCommand(props.onChangeType, props.onChange, ...args)
}
}}
onBlur={() => {
const command = props.value.trimStart().split(' ')[0]
const block = registry.getBySlashCommandPrefix(command)
if (command === '' || !block) {
props.onSave(props.value, 'text')
props.onChange('')
}
}}
onFocus={(e: React.FocusEvent) => {
const target = e.currentTarget
target.scrollIntoView({block: 'center'})
}}
onKeyDown={(e) => {
if (e.key === 'Escape') {
props.onSave('', 'text')
props.onChange('')
}
if (e.key === 'Enter') {
const command = props.value.trimStart().split(' ')[0]
const block = registry.getBySlashCommandPrefix(command)
if (command === '' || !block) {
e.preventDefault()
e.stopPropagation()
props.onSave(props.value, 'text')
props.onChange('')
}
}
}}
/>
)
}
|