Skip to content

Commit

Permalink
feat: add custom emojis to picker
Browse files Browse the repository at this point in the history
  • Loading branch information
vjousse committed Jun 14, 2024
1 parent 1995409 commit f753724
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 62 deletions.
2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"rtfeldman/elm-iso8601-date-strings": "1.1.4",
"ryan-haskell/date-format": "1.0.0",
"sporto/qs": "2.0.0",
"vjousse/elm-emoji": "1.1.0",
"vjousse/elm-emoji": "2.0.1",
"vjousse/elm-mastodon-tooty": "2.1.0"
},
"indirect": {
Expand Down
1 change: 1 addition & 0 deletions src/Types.elm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type DraftMsg
| SetAutoState Menu.Msg
| ToggleSpoiler Bool
| UpdateAttachmentDescription String String
| UpdateCustomEmojis (List CustomEmoji)
| UpdateInputInformation InputInformation
| UpdateSensitive Bool
| UpdateSpoiler String
Expand Down
107 changes: 65 additions & 42 deletions src/Update/Draft.elm
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ autocompleteUpdateConfig getId =
}


pickerConfig : PickerConfig
pickerConfig =
pickerConfig : List Emojis.Emoji -> PickerConfig
pickerConfig customEmojis =
{ offsetX = 0 -- horizontal offset
, offsetY = 0 -- vertical offset
, closeOnSelect = True -- close after clicking an emoji
, customEmojis = customEmojis
, customEmojisWidth = Nothing
, customEmojisHeight = Nothing
}


Expand All @@ -93,11 +96,11 @@ empty =
, autoState = Menu.empty
, autoStartPosition = Nothing
, autoQuery = ""
, autoMaxResults = 4
, autoMaxResults = 6
, autoAccounts = []
, autoEmojis = []
, showAutoMenu = False
, emojiModel = EmojiPicker.init pickerConfig
, emojiModel = EmojiPicker.init <| pickerConfig []
}


Expand All @@ -117,8 +120,8 @@ showAutoMenu accounts emojiList atPosition query =
True


update : DraftMsg -> Account -> Model -> ( Model, Cmd Msg )
update draftMsg currentUser ({ draft } as model) =
update : DraftMsg -> Model -> ( Model, Cmd Msg )
update draftMsg ({ draft } as model) =
case draftMsg of
ClearDraft ->
( { model | draft = empty }
Expand Down Expand Up @@ -221,6 +224,28 @@ update draftMsg currentUser ({ draft } as model) =
, Cmd.none
)

UpdateCustomEmojis customEmojis ->
let
-- Convert CustomEmoji to Picker Emoji type
emojisToDisplayInPicker =
customEmojis
|> List.filter .visible_in_picker
|> List.indexedMap
(\i e ->
{ name = ":" ++ e.shortcode ++ ":"
, native = ":" ++ e.shortcode ++ ":"
, sortOrder = i
, skinVariations = Dict.fromList []
, keywords = []
, imgUrl = Just e.url
}
)

newDraft =
{ draft | emojiModel = EmojiPicker.init <| pickerConfig emojisToDisplayInPicker }
in
( { model | draft = newDraft }, Cmd.none )

UpdateSensitive sensitive ->
( { model | draft = { draft | sensitive = sensitive } }
, Cmd.none
Expand All @@ -237,30 +262,35 @@ update draftMsg currentUser ({ draft } as model) =
)

UpdateReplyTo status ->
let
newStatus =
Mastodon.Helper.getReplyPrefix currentUser status
in
( { model
| draft =
{ draft
| type_ = InReplyTo status
, status = newStatus
, sensitive = Maybe.withDefault False status.sensitive
, spoilerText =
if status.spoiler_text == "" then
Nothing
case model.currentUser of
Just currentUser ->
let
newStatus =
Mastodon.Helper.getReplyPrefix currentUser status
in
( { model
| draft =
{ draft
| type_ = InReplyTo status
, status = newStatus
, sensitive = Maybe.withDefault False status.sensitive
, spoilerText =
if status.spoiler_text == "" then
Nothing

else
Just status.spoiler_text
, visibility = status.visibility
}
}
, Cmd.batch
[ Command.focusId "status"
, Command.updateDomStatus newStatus
]
)
else
Just status.spoiler_text
, visibility = status.visibility
}
}
, Cmd.batch
[ Command.focusId "status"
, Command.updateDomStatus newStatus
]
)

_ ->
( model, Cmd.none )

UpdateInputInformation { status, selectionStart } ->
let
Expand Down Expand Up @@ -356,18 +386,11 @@ update draftMsg currentUser ({ draft } as model) =
-- Successfull request
-- Old Elm 0.18
--Task.perform identity (Task.succeed ((DraftEvent << ResetAutocomplete) True))
case model.currentUser of
Just user ->
let
( updatedModel, msgs ) =
update (ResetAutocomplete True) user newModel
in
( updatedModel, Cmd.batch [ msgs, Task.attempt (\_ -> NoOp) (Dom.focus "autocomplete-menu") ] )

Nothing ->
( newModel
, Cmd.none
)
let
( updatedModel, msgs ) =
update (ResetAutocomplete True) newModel
in
( updatedModel, Cmd.batch [ msgs, Task.attempt (\_ -> NoOp) (Dom.focus "autocomplete-menu") ] )

else
( newModel
Expand Down Expand Up @@ -456,7 +479,7 @@ update draftMsg currentUser ({ draft } as model) =
in
case maybeMsg of
Just (DraftEvent updateMsg) ->
update updateMsg currentUser newModel
update updateMsg newModel

_ ->
( newModel
Expand Down
9 changes: 1 addition & 8 deletions src/Update/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,7 @@ update msg model =
)

DraftEvent draftMsg ->
case model.currentUser of
Just user ->
Update.Draft.update draftMsg user model

Nothing ->
( model
, Cmd.none
)
Update.Draft.update draftMsg model

FilterNotifications filter ->
( { model | notificationFilter = filter }
Expand Down
18 changes: 7 additions & 11 deletions src/Update/Mastodon.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Update.Mastodon exposing (update)
import Browser.Navigation as Navigation
import Command
import InfiniteScroll
import Mastodon.ApiUrl exposing (customEmojis)
import Mastodon.Helper exposing (extractStatusId)
import Mastodon.Model exposing (..)
import Types exposing (..)
Expand Down Expand Up @@ -140,9 +141,11 @@ update msg ({ accountInfo, search } as model) =
CustomEmojis result ->
case result of
Ok { decoded } ->
( { model | customEmojis = decoded }
, Cmd.none
)
let
newModel =
{ model | customEmojis = decoded }
in
Update.Draft.update (UpdateCustomEmojis decoded) newModel

Err error ->
( { model | errors = addErrorNotification (errorText error) model }
Expand Down Expand Up @@ -583,14 +586,7 @@ update msg ({ accountInfo, search } as model) =
-- Successfull request
-- Old Elm 0.18
--Task.perform identity (Task.succeed ((DraftEvent << ResetAutocomplete) True))
case model.currentUser of
Just user ->
Update.Draft.update (ResetAutocomplete True) user newModel

Nothing ->
( newModel
, Cmd.none
)
Update.Draft.update (ResetAutocomplete True) newModel

Err error ->
( { model
Expand Down

0 comments on commit f753724

Please sign in to comment.