use separate models, commit once

This commit is contained in:
Eric Bailey
2023-10-13 14:52:58 -05:00
parent 7ee34b6a12
commit 35dd618d83
4 changed files with 19 additions and 10 deletions
+4 -1
View File
@@ -244,6 +244,10 @@ export const ComposePost = observer(function ComposePost({
}) })
if (replyTo && replyTo.uri) track('Post:Reply') if (replyTo && replyTo.uri) track('Post:Reply')
// save outline tags
tags.forEach(tag => tagsAutocompleteModel.commitRecentTag(tag))
// save inline tags
for (const facet of richtext.facets || []) { for (const facet of richtext.facets || []) {
for (const feature of facet.features) { for (const feature of facet.features) {
if (AppBskyRichtextFacet.isTag(feature)) { if (AppBskyRichtextFacet.isTag(feature)) {
@@ -396,7 +400,6 @@ export const ComposePost = observer(function ComposePost({
placeholder={selectTextInputPlaceholder} placeholder={selectTextInputPlaceholder}
suggestedLinks={suggestedLinks} suggestedLinks={suggestedLinks}
autocompleteView={autocompleteView} autocompleteView={autocompleteView}
tagsAutocompleteModel={tagsAutocompleteModel}
autoFocus={true} autoFocus={true}
setRichText={setRichText} setRichText={setRichText}
onPhotoPasted={onPhotoPasted} onPhotoPasted={onPhotoPasted}
@@ -34,6 +34,7 @@ import {useTheme} from 'lib/ThemeContext'
import {isUriImage} from 'lib/media/util' import {isUriImage} from 'lib/media/util'
import {downloadAndResize} from 'lib/media/manip' import {downloadAndResize} from 'lib/media/manip'
import {POST_IMG_MAX} from 'lib/constants' import {POST_IMG_MAX} from 'lib/constants'
import {useStores} from 'state/index'
export interface TextInputRef { export interface TextInputRef {
focus: () => void focus: () => void
@@ -45,7 +46,6 @@ interface TextInputProps extends ComponentProps<typeof RNTextInput> {
placeholder: string placeholder: string
suggestedLinks: Set<string> suggestedLinks: Set<string>
autocompleteView: UserAutocompleteModel autocompleteView: UserAutocompleteModel
tagsAutocompleteModel: TagsAutocompleteModel
setRichText: (v: RichText | ((v: RichText) => RichText)) => void setRichText: (v: RichText | ((v: RichText) => RichText)) => void
onPhotoPasted: (uri: string) => void onPhotoPasted: (uri: string) => void
onPressPublish: (richtext: RichText) => Promise<void> onPressPublish: (richtext: RichText) => Promise<void>
@@ -64,7 +64,6 @@ export const TextInput = forwardRef(function TextInputImpl(
placeholder, placeholder,
suggestedLinks, suggestedLinks,
autocompleteView, autocompleteView,
tagsAutocompleteModel,
setRichText, setRichText,
onPhotoPasted, onPhotoPasted,
onSuggestedLinksChanged, onSuggestedLinksChanged,
@@ -73,10 +72,15 @@ export const TextInput = forwardRef(function TextInputImpl(
}: TextInputProps, }: TextInputProps,
ref, ref,
) { ) {
const store = useStores()
const pal = usePalette('default') const pal = usePalette('default')
const textInput = useRef<PasteInputRef>(null) const textInput = useRef<PasteInputRef>(null)
const textInputSelection = useRef<Selection>({start: 0, end: 0}) const textInputSelection = useRef<Selection>({start: 0, end: 0})
const theme = useTheme() const theme = useTheme()
const tagsAutocompleteModel = React.useMemo(
() => new TagsAutocompleteModel(store),
[store],
)
React.useImperativeHandle(ref, () => ({ React.useImperativeHandle(ref, () => ({
focus: () => textInput.current?.focus(), focus: () => textInput.current?.focus(),
@@ -210,7 +214,6 @@ export const TextInput = forwardRef(function TextInputImpl(
onChangeText( onChangeText(
insertTagAt(richtext.text, textInputSelection.current?.start || 0, tag), insertTagAt(richtext.text, textInputSelection.current?.start || 0, tag),
) )
tagsAutocompleteModel.commitRecentTag(tag)
tagsAutocompleteModel.setActive(false) tagsAutocompleteModel.setActive(false)
}, },
[onChangeText, richtext, tagsAutocompleteModel], [onChangeText, richtext, tagsAutocompleteModel],
@@ -20,6 +20,7 @@ import {Emoji} from './web/EmojiPicker.web'
import {LinkDecorator} from './web/LinkDecorator' import {LinkDecorator} from './web/LinkDecorator'
import {generateJSON} from '@tiptap/html' import {generateJSON} from '@tiptap/html'
import {Tags, createTagsAutocomplete} from './web/Tags' import {Tags, createTagsAutocomplete} from './web/Tags'
import {useStores} from 'state/index'
export interface TextInputRef { export interface TextInputRef {
focus: () => void focus: () => void
@@ -31,7 +32,6 @@ interface TextInputProps {
placeholder: string placeholder: string
suggestedLinks: Set<string> suggestedLinks: Set<string>
autocompleteView: UserAutocompleteModel autocompleteView: UserAutocompleteModel
tagsAutocompleteModel: TagsAutocompleteModel
setRichText: (v: RichText | ((v: RichText) => RichText)) => void setRichText: (v: RichText | ((v: RichText) => RichText)) => void
onPhotoPasted: (uri: string) => void onPhotoPasted: (uri: string) => void
onPressPublish: (richtext: RichText) => Promise<void> onPressPublish: (richtext: RichText) => Promise<void>
@@ -47,7 +47,6 @@ export const TextInput = React.forwardRef(function TextInputImpl(
placeholder, placeholder,
suggestedLinks, suggestedLinks,
autocompleteView, autocompleteView,
tagsAutocompleteModel,
setRichText, setRichText,
onPhotoPasted, onPhotoPasted,
onPressPublish, onPressPublish,
@@ -56,6 +55,11 @@ export const TextInput = React.forwardRef(function TextInputImpl(
TextInputProps, TextInputProps,
ref, ref,
) { ) {
const store = useStores()
const tagsAutocompleteModel = React.useMemo(
() => new TagsAutocompleteModel(store),
[store],
)
const modeClass = useColorSchemeStyle('ProseMirror-light', 'ProseMirror-dark') const modeClass = useColorSchemeStyle('ProseMirror-light', 'ProseMirror-dark')
const extensions = React.useMemo( const extensions = React.useMemo(
() => [ () => [
@@ -112,9 +112,8 @@ const Autocomplete = forwardRef<AutocompleteRef, ListProps>(
*/ */
// @ts-ignore // @ts-ignore
command({tag, punctuation}) command({tag, punctuation})
autocompleteModel.commitRecentTag(tag)
}, },
[command, autocompleteModel], [command],
) )
const selectItem = React.useCallback( const selectItem = React.useCallback(
@@ -142,7 +141,7 @@ const Autocomplete = forwardRef<AutocompleteRef, ListProps>(
if (event.key === 'Enter') { if (event.key === 'Enter') {
if (!props.items.length) { if (!props.items.length) {
// no items, use whatever the user typed // no items, use whatever the user typed
commit(props.autocompleteModel.query) commit(autocompleteModel.query)
} else { } else {
selectItem(selectedIndex) selectItem(selectedIndex)
} }
@@ -150,7 +149,7 @@ const Autocomplete = forwardRef<AutocompleteRef, ListProps>(
} }
if (event.key === ' ') { if (event.key === ' ') {
commit(props.autocompleteModel.query) commit(autocompleteModel.query)
return true return true
} }