Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
purescript-gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grégoire Locqueville
purescript-gargantext
Commits
2e27a8c4
Commit
2e27a8c4
authored
May 20, 2019
by
James Laver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert "Overhaul G.C.Annotation.AnnotatedField to use Selection position for placement"
This reverts commit
65e9e0cd
.
parent
095c1b53
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
66 deletions
+36
-66
AnnotatedField.purs
src/Gargantext/Components/Annotation/AnnotatedField.purs
+36
-66
No files found.
src/Gargantext/Components/Annotation/AnnotatedField.purs
View file @
2e27a8c4
...
@@ -13,25 +13,18 @@ module Gargantext.Components.Annotation.AnnotatedField where
...
@@ -13,25 +13,18 @@ module Gargantext.Components.Annotation.AnnotatedField where
import Prelude
import Prelude
import Data.Map as Map
import Data.Map as Map
import Data.Maybe (Maybe(..), maybe, maybe', fromMaybe)
import Data.Maybe ( Maybe(..), maybe, maybe' )
import Data.Lens (Lens', lens)
import Data.Lens ( Lens', lens )
import Data.Nullable (Nullable, null, toMaybe)
import Data.Traversable ( traverse_ )
import Data.Ord (max)
import Data.Tuple ( Tuple(..) )
import Data.Traversable (traverse, traverse_)
import Data.Tuple.Nested ( (/\) )
import Data.Tuple (Tuple(..))
import Data.Tuple.Nested ((/\))
import DOM.Simple as DOM
import DOM.Simple.Console
import DOM.Simple.Console
import DOM.Simple.Document ( document )
import DOM.Simple.Document as Document
import DOM.Simple.Node as Node
import DOM.Simple.Types (Element)
import DOM.Simple.Element as Element
import DOM.Simple.Event as DE
import DOM.Simple.Event as DE
import Effect (
Effect
)
import Effect (
Effect
)
import Effect.Uncurried (
EffectFn1,
mkEffectFn1)
import Effect.Uncurried (mkEffectFn1)
import Reactix as R
import Reactix as R
import Reactix.DOM.HTML as HTML
import Reactix.DOM.HTML as HTML
import Reactix.SyntheticEvent as E
import Gargantext.Types ( TermList(..) )
import Gargantext.Types ( TermList(..) )
import Gargantext.Components.Annotation.Utils ( termClass )
import Gargantext.Components.Annotation.Utils ( termClass )
...
@@ -56,66 +49,43 @@ annotatedFieldComponent = R.hooksComponent "AnnotatedField" cpt
...
@@ -56,66 +49,43 @@ annotatedFieldComponent = R.hooksComponent "AnnotatedField" cpt
runs props =
runs props =
HTML.div { className: "annotated-field-runs" } (map annotateRun $ compile props)
HTML.div { className: "annotated-field-runs" } (map annotateRun $ compile props)
cpt props _ = do
cpt props _ = do
menu <- R.useState $ \_ -> pure Nothing
menu /\ setMenu <- R.useState $ \_ -> pure Nothing
root <- R.useRef null
let wrapperProps =
useAnnotationEffect root menu props.ngrams
{ className: "annotated-field-wrapper"
pure $ HTML.div { className } [ maybeAddMenu menu (runs props) ]
, onContextMenu: mkEffectFn1 (maybeShowMenu setMenu props.ngrams) }
className = "annotated-field-wrapper"
pure $ HTML.div wrapperProps [ maybeAddMenu setMenu (runs props) menu]
useAnnotationEffect
:: R.Ref (Nullable Element)
-> R.State (Maybe AnnotationMenu)
-> NgramsTable
-> R.Hooks Unit
useAnnotationEffect rootRef menu ngrams =
R.useLayoutEffect1 (R.readNullableRef rootRef) h
where
h _ =
case R.readNullableRef rootRef of
Just root -> do
let handler = onSelectionChange root menu ngrams
DOM.addEventListener document "selectionchange" handler
pure $ \_ -> DOM.removeEventListener document "selectionchange" handler
Nothing -> pure $ \_ -> pure unit
-- | TODO: handle multiple ranges
onSelectionChange
:: Element
-> R.State (Maybe AnnotationMenu)
-> NgramsTable
-> Element.Callback DE.SelectionEvent
onSelectionChange root (_ /\ setMenu) ngrams =
Element.callback $ \event ->
Sel.getSelection >>= traverse (getMenu event) >>= traverse_ setMenu
where
getMenu event sel = getMenu' event sel (Sel.selectionToString sel)
getMenu' event sel sel'
| not (selEmpty sel sel') = do
range <- Sel.getRange sel 0
if not (liesWithin $ Sel.commonAncestorContainer range)
then pure Nothing
else do
let rect = Sel.boundingRect range
DE.preventDefault event
-- top and right are the most pessimistic values because the menu is biased
-- towards being positioned above and to the right of the cursor
pure $ Just { x: rect.top, y: rect.right, list: findNgram ngrams sel' }
| true = pure Nothing
liesWithin = Element.contains root
selEmpty _ "" = true
selEmpty sel _ = Sel.rangeCount sel > 0 && not (Sel.isSelectionCollapsed sel)
maybeAddMenu
maybeAddMenu
::
R.State (Maybe AnnotationMenu
)
::
(Maybe AnnotationMenu -> Effect Unit
)
-> R.Element
-> R.Element
-> Maybe AnnotationMenu
-> R.Element
-> R.Element
maybeAddMenu
((Just props) /\ setMenu) e
= annotationMenu setMenu props <> e
maybeAddMenu
setMenu e (Just props)
= annotationMenu setMenu props <> e
maybeAddMenu _ e = e
maybeAddMenu _ e
_
= e
compile :: Record Props -> Array Run
compile :: Record Props -> Array Run
compile props = runs props.text
compile props = runs props.text
where runs = maybe [] (highlightNgrams props.ngrams)
where runs = maybe [] (highlightNgrams props.ngrams)
maybeShowMenu
:: forall t
. (Maybe AnnotationMenu -> Effect Unit)
-> NgramsTable
-> E.SyntheticEvent DE.MouseEvent
-> Effect Unit
maybeShowMenu setMenu ngrams event = do
s <- Sel.getSelection
case s of
Just sel -> do
case Sel.toString sel of
"" -> pure unit
sel' -> do
let x = E.clientX event
let y = E.clientY event
E.preventDefault event
setMenu $ Just { x, y, list: findNgram ngrams sel' }
Nothing -> pure unit
findNgram :: NgramsTable -> String -> Maybe TermList
findNgram :: NgramsTable -> String -> Maybe TermList
findNgram _ _ = Nothing
findNgram _ _ = Nothing
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment