Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

click to select #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/RichText/Config/Decorations.elm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import RichText.Internal.Editor exposing (Message(..), Tagger)
import RichText.Model.Element exposing (Element, annotations)
import RichText.Model.Mark exposing (Mark)
import RichText.Model.Node exposing (Path)
import RichText.Model.Selection exposing (caret)
import Set


Expand Down Expand Up @@ -229,6 +228,6 @@ selectableDecoration tagger editorNodePath elementParameters _ =
)
++ [ Html.Events.onClick
(tagger <|
SelectionEvent (Just (caret editorNodePath 0)) False
SelectElement editorNodePath
)
]
37 changes: 26 additions & 11 deletions src/RichText/Editor.elm
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ commandMap cfg =
c.commandMap


updateSelection : Maybe Selection -> Bool -> Spec -> Editor -> Editor
updateSelection maybeSelection isDomPath spec_ editor_ =
updateSelection : Maybe Selection -> Spec -> Editor -> Editor
updateSelection maybeSelection spec_ editor_ =
let
editorState =
state editor_
Expand All @@ -200,11 +200,7 @@ updateSelection maybeSelection isDomPath spec_ editor_ =
Just selection ->
let
translatedSelection =
if isDomPath then
domToEditor spec_ (State.root editorState) selection

else
Just selection
domToEditor spec_ (State.root editorState) selection
in
if isComposing editor_ then
let
Expand All @@ -217,6 +213,23 @@ updateSelection maybeSelection isDomPath spec_ editor_ =
editor_ |> withState (editorState |> withSelection translatedSelection)


selectElement : Path -> Spec -> Editor -> Editor
selectElement path _ editor_ =
let
editorState =
state editor_

selection =
case RichText.Node.next path (State.root editorState) of
Just ( b, _ ) ->
range b 0 path 0

Nothing ->
RichText.Model.Selection.caret path 0
in
editor_ |> withState (editorState |> withSelection (Just selection)) |> forceReselection


{-| The editor's internal update function. It's important that the editor process all `Message`
events with the update function so it doesn't go out of sync with the virtual DOM.

Expand All @@ -242,8 +255,11 @@ update cfg msg editor_ =
ChangeEvent change ->
updateChangeEvent change spec_ editor_

SelectionEvent selection isDomPath ->
updateSelection selection isDomPath spec_ editor_
SelectionEvent selection ->
updateSelection selection spec_ editor_

SelectElement path ->
selectElement path spec_ editor_

BeforeInputEvent inputEvent ->
BeforeInput.handleBeforeInput inputEvent commandMap_ spec_ editor_
Expand Down Expand Up @@ -504,9 +520,8 @@ selectionDecoder =

editorSelectionChangeDecoder : D.Decoder Message
editorSelectionChangeDecoder =
D.map2 SelectionEvent
D.map SelectionEvent
(D.at [ "detail" ] selectionDecoder)
(D.succeed True)


pasteWithDataDecoder : D.Decoder Message
Expand Down
4 changes: 3 additions & 1 deletion src/RichText/Internal/Editor.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import RichText.Config.Keys exposing (meta)
import RichText.Config.Spec exposing (Spec)
import RichText.Internal.Event exposing (EditorChange, InitEvent, InputEvent, KeyboardEvent, PasteEvent)
import RichText.Internal.History exposing (History, contents, empty, fromContents)
import RichText.Model.Node exposing (Path)
import RichText.Model.Selection exposing (Selection)
import RichText.Model.State exposing (State)
import RichText.State exposing (reduce, validate)
Expand Down Expand Up @@ -62,7 +63,8 @@ editor iState =
{-| The internal events that an editor has to respond to. These events should be mapped via a Tagger.
-}
type Message
= SelectionEvent (Maybe Selection) Bool
= SelectionEvent (Maybe Selection)
| SelectElement Path
| ChangeEvent EditorChange
| BeforeInputEvent InputEvent
| KeyDownEvent KeyboardEvent
Expand Down