Skip to content

Instantly share code, notes, and snippets.

@coreyhaines
Last active August 25, 2022 05:11
Show Gist options
  • Save coreyhaines/cf40b7dca8916b77878c97fdb5c8184e to your computer and use it in GitHub Desktop.
Save coreyhaines/cf40b7dca8916b77878c97fdb5c8184e to your computer and use it in GitHub Desktop.
type Editable
module Editable exposing (..)
type Editable ofType
= NotEditing { value : ofType }
| Editing { originalValue : ofType, buffer : ofType }
value : Editable ofType -> ofType
value editable =
case editable of
NotEditing { value } ->
value
Editing { originalValue } ->
originalValue
bufferValue : Editable ofType -> ofType
bufferValue editable =
case editable of
NotEditing { value } ->
value
Editing { buffer } ->
buffer
setBuffer : Editable ofType -> ofType -> Editable ofType
setBuffer editable newBuffer =
case editable of
NotEditing _ ->
editable
Editing values ->
Editing { values | buffer = newBuffer }
newEditing : ofType -> Editable ofType
newEditing value =
Editing { originalValue = value, buffer = value }
newNotEditing : ofType -> Editable ofType
newNotEditing value =
NotEditing { value = value }
startEditing : Editable ofType -> Editable ofType
startEditing editable =
case editable of
NotEditing { value } ->
newEditing value
Editing _ ->
editable
finishEditing : Editable ofType -> Editable ofType
finishEditing editable =
case editable of
NotEditing _ ->
editable
Editing { buffer } ->
NotEditing { value = buffer }
cancelEditing : Editable ofType -> Editable ofType
cancelEditing editable =
case editable of
NotEditing _ ->
editable
Editing { originalValue } ->
NotEditing { value = originalValue }
isEditing : Editable ofType -> Bool
isEditing editable =
case editable of
NotEditing _ ->
False
Editing _ ->
True
hasChanged : Editable comparable -> Bool
hasChanged editable =
case editable of
NotEditing _ ->
False
Editing { originalValue, buffer } ->
originalValue /= buffer
StartEditingNotebookTitle ->
( { model
| notebookTitle = Editable.startEditing model.notebookTitle
}
, Dom.focus "notebook-title-editor" |> Task.attempt (always NoOp)
)
CancelEditingNotebookTitle ->
( { model
| notebookTitle = Editable.cancelEditing model.notebookTitle
}
, Cmd.none
)
UpdateEditingNotebookTitle title ->
( { model
| notebookTitle = Types.editableSetBuffer model.notebookTitle title
}
, Cmd.none
)
SaveEditingNotebookTitle ->
let
cmd =
saveNotebookTitleCmd model
updatedValue =
case model.notebookTitle of
NotEditing { value } ->
model.notebookTitle
Editing { buffer } ->
NotEditing { value = buffer }
in
( { model | notebookTitle = updatedValue }
, cmd
)
SaveNotebookTitleFailure err ->
( model
, Cmd.map IrnMsg <| Irn.showUserAlert "Error Updating Title"
)
SaveNotebookTitleSuccess notebook ->
( { model | notebookTitle = NotEditing { value = notebook.title } }
, Cmd.map IrnMsg <| Irn.showUserAlert "Notebook Title Updated"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment