Skip to content

Commit

Permalink
feat: keep state of the emoji start
Browse files Browse the repository at this point in the history
  • Loading branch information
vjousse committed Jun 9, 2024
1 parent 0ed2f87 commit 10224c5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
17 changes: 15 additions & 2 deletions src/Types.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Types exposing
( AccountInfo
, AutocompleteType(..)
, Confirm
, CurrentAccountView(..)
, CurrentView(..)
Expand Down Expand Up @@ -145,6 +146,11 @@ type ScrollElement
| ScrollNotifications


type AutocompleteType
= AccountAuto
| EmojiAuto


type Msg
= AddFavorite Status
| AskConfirm String Msg Msg
Expand Down Expand Up @@ -239,14 +245,21 @@ type alias Draft =
, attachments : List Attachment
, mediaUploading : Bool
, statusLength : Int
, autocompleteType : Maybe AutocompleteType

-- Autocomplete values
, autoState : Menu.State
, autoCursorPosition : Int
, autoAtPosition : Maybe Int

-- Index of the @ or : for the user/emoji autoComplete
, autoStartPosition : Maybe Int
, autoQuery : String
, autoMaxResults : Int

-- List of the accounts to display in autoComplete
, autoAccounts : List Account

-- List of the emojis to display in autoComplete
, autoEmojis : List CustomEmoji
, showAutoMenu : Bool

-- EmojiPicker state
Expand Down
60 changes: 41 additions & 19 deletions src/Update/Draft.elm
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ empty =
, attachments = []
, mediaUploading = False
, statusLength = 0
, autocompleteType = Nothing
, autoState = Menu.empty
, autoAtPosition = Nothing
, autoStartPosition = Nothing
, autoQuery = ""
, autoCursorPosition = 0
, autoMaxResults = 4
, autoAccounts = []
, autoEmojis = []
, showAutoMenu = False
, emojiModel = EmojiPicker.init pickerConfig
}
Expand Down Expand Up @@ -238,41 +239,61 @@ update draftMsg currentUser ({ draft } as model) =
stringToPos =
String.slice 0 selectionStart status

atPosition =
getQuery position =
case position of
Just p ->
String.slice p (String.length stringToPos) stringToPos

_ ->
""

( startAutocompletePosition, autocompleteType, query ) =
case String.right 1 stringToPos of
"@" ->
Just selectionStart
( Just selectionStart, Just AccountAuto, getQuery <| Just selectionStart )

":" ->
case model.draft.autocompleteType of
-- End of an already existing emoji
Just EmojiAuto ->
( model.draft.autoStartPosition, Just EmojiAuto, getQuery model.draft.autoStartPosition )

-- New Emoji
_ ->
( Just selectionStart, Just EmojiAuto, getQuery <| Just selectionStart )

-- Space is pressed, cancel auto state
" " ->
Nothing
( Nothing, Nothing, "" )

-- Middle of a word
_ ->
model.draft.autoAtPosition

query =
case atPosition of
Just position ->
String.slice position (String.length stringToPos) stringToPos
case model.draft.autocompleteType of
Nothing ->
( Nothing, Nothing, "" )

Nothing ->
""
autoType ->
( model.draft.autoStartPosition
, autoType
, getQuery model.draft.autoStartPosition
)

newDraft =
{ draft
| status = status
, statusLength = String.length status
, autoCursorPosition = selectionStart
, autoAtPosition = atPosition
, autoStartPosition = startAutocompletePosition
, autoQuery = query
, autocompleteType = autocompleteType
, showAutoMenu =
showAutoMenu
draft.autoAccounts
draft.autoAtPosition
draft.autoStartPosition
draft.autoQuery
}
in
( { model | draft = newDraft }
, if query /= "" && atPosition /= Nothing then
, if query /= "" && startAutocompletePosition /= Nothing && autocompleteType == Just AccountAuto then
Command.searchAccounts (List.head model.clients) query model.draft.autoMaxResults False

else
Expand All @@ -286,7 +307,7 @@ update draftMsg currentUser ({ draft } as model) =
|> List.head

newStatus =
case draft.autoAtPosition of
case draft.autoStartPosition of
Just atPosition ->
String.Extra.replaceSlice
(case account of
Expand All @@ -306,7 +327,8 @@ update draftMsg currentUser ({ draft } as model) =
newDraft =
{ draft
| status = newStatus
, autoAtPosition = Nothing
, autoStartPosition = Nothing
, autocompleteType = Nothing
, autoQuery = ""
, autoState = Menu.empty
, autoAccounts = []
Expand Down
2 changes: 1 addition & 1 deletion src/Update/Mastodon.elm
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ update msg ({ accountInfo, search } as model) =
| showAutoMenu =
Update.Draft.showAutoMenu
decoded
draft.autoAtPosition
draft.autoStartPosition
draft.autoQuery
, autoAccounts = decoded
}
Expand Down

0 comments on commit 10224c5

Please sign in to comment.