tooot/src/screens/Compose/updateText.ts

50 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-12-04 01:17:10 +01:00
import { Dispatch } from 'react'
import formatText from './formatText'
2020-12-30 00:56:25 +01:00
import { ComposeAction, ComposeState } from './utils/types'
2020-11-15 20:29:43 +01:00
const updateText = ({
2020-12-07 12:31:40 +01:00
composeState,
composeDispatch,
2020-12-04 01:17:10 +01:00
newText,
type
2020-11-15 20:29:43 +01:00
}: {
2020-12-07 12:31:40 +01:00
composeState: ComposeState
2020-12-18 23:58:53 +01:00
composeDispatch: Dispatch<ComposeAction>
2020-11-15 20:29:43 +01:00
newText: string
2020-12-04 01:17:10 +01:00
type: 'emoji' | 'suggestion'
2020-11-15 20:29:43 +01:00
}) => {
const textInput = composeState.textInputFocus.current
if (composeState[textInput].raw.length) {
const contentFront = composeState[textInput].raw.slice(
2020-12-06 23:51:13 +01:00
0,
composeState[textInput].selection.start
2020-12-06 23:51:13 +01:00
)
const contentRear = composeState[textInput].raw.slice(
composeState[textInput].selection.end
2020-12-06 23:51:13 +01:00
)
2020-12-03 22:03:06 +01:00
const whiteSpaceFront = /\s/g.test(contentFront.slice(-1))
const whiteSpaceRear = /\s/g.test(contentRear.slice(-1))
2020-12-04 01:17:10 +01:00
const newTextWithSpace = `${
whiteSpaceFront || type === 'suggestion' ? '' : ' '
}${newText}${whiteSpaceRear ? '' : ' '}`
2020-12-03 22:03:06 +01:00
2020-12-04 01:17:10 +01:00
formatText({
textInput,
2020-12-07 12:31:40 +01:00
composeDispatch,
2020-12-03 22:03:06 +01:00
content: [contentFront, newTextWithSpace, contentRear].join(''),
disableDebounce: true
})
} else {
2020-12-04 01:17:10 +01:00
formatText({
textInput,
2020-12-07 12:31:40 +01:00
composeDispatch,
2020-12-03 22:03:06 +01:00
content: `${newText} `,
disableDebounce: true
})
}
2020-11-15 20:29:43 +01:00
}
export default updateText