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
142
Issues
142
List
Board
Labels
Milestones
Merge Requests
4
Merge Requests
4
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
gargantext
purescript-gargantext
Commits
c9536adc
Commit
c9536adc
authored
May 19, 2019
by
James Laver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tons of G.U.Selection
parent
a8689d6d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
15 deletions
+59
-15
Selection.js
src/Gargantext/Utils/Selection.js
+0
-1
Selection.purs
src/Gargantext/Utils/Selection.purs
+59
-14
No files found.
src/Gargantext/Utils/Selection.js
View file @
c9536adc
exports
.
_getSelection
=
function
()
{
return
window
.
getSelection
()
||
null
;
};
exports
.
_toString
=
function
(
thing
)
{
return
thing
.
toString
();
};
src/Gargantext/Utils/Selection.purs
View file @
c9536adc
module Gargantext.Utils.Selection
( class ToString, Selection, toString, getSelection ) where
module Gargantext.Utils.Selection where
import Prelude
((<$>))
import Data.Maybe (Maybe)
import Prelude
import Data.Maybe (Maybe
, fromMaybe, maybe
)
import Data.Nullable (Nullable, toMaybe)
import DOM.Simple.Types (Element, DOMRect)
import DOM.Simple.Element as Element
import Effect (Effect)
import FFI.Simple ((.?), (..), (...))
-- | Represents a text selection
foreign import data Selection :: Type
-- foreign import data Range :: Type -- Probably coming soon
-- | Represents a single selection range
foreign import data Range :: Type
-- toString
-- Terminology:
-- Anchor: point at which the selection was started
-- Focus: point at which the selection ends
class ToString t
-- | The Node in which the anchor lies
anchorNode :: Selection -> Maybe Element
anchorNode s = s .? "anchorNode"
instance toStringSelection :: ToString Selection
-- instance toStringRange :: ToString Range
-- | The Node in which the focus lies
focusNode :: Selection -> Maybe Element
focusNode s = s .? "focusNode"
foreign import _toString :: forall t. t -> String
-- | Whether the anchor and focus are at the same point
isSelectionCollapsed :: Selection -> Boolean
isSelectionCollapsed s = s .. "isCollapsed"
rangeCount :: Selection -> Int
rangeCount s = s .. "rangeCount"
getRange :: Selection -> Int -> Effect Range
getRange s i = pure $ s ... "getRangeAt" $ [i]
-- | Renders a selection or range as a string
toString :: forall t. ToString t => t
-> String
toString = _toString
selectionToString :: Selection
-> String
selectionToString s = s ... "toString" $ []
-- getSelection
-- | Renders a range as a string
rangeToString :: Range -> String
rangeToString s = s ... "toString" $ []
foreign import _getSelection :: Effect (Nullable Selection)
-- | Whether the anchor and focus are at the same point
isRangeCollapsed :: Range -> Boolean
isRangeCollapsed r = r .. "isCollapsed"
cloneRange :: Range -> Range
cloneRange r = r ... "cloneRange" $ []
collapseRange :: Range -> Boolean -> Effect Unit
collapseRange r toStart = pure $ r ... "collapse" $ [toStart]
commonAncestorContainer :: Range -> Element
commonAncestorContainer r = r .. "commonAncestorContainer"
insertNode :: Range -> Element -> Effect Unit
insertNode r e = pure $ r ... "insertNode" $ [e]
boundingRect :: Range -> DOMRect
boundingRect r = r ... "getBoundingClientRect" $ []
-- getSelection
-- | Fetches the current text selection, if any
getSelection :: Effect (Maybe Selection)
getSelection = toMaybe <$> _getSelection
foreign import _getSelection :: Effect (Nullable Selection)
-- | Are both the start and end of the selection contained within an Element
doesSelectionLieWithin :: Selection -> Element -> Boolean
doesSelectionLieWithin sel elem = test anchorNode && test focusNode
where
test :: (Selection -> Maybe Element) -> Boolean
test f = maybe false (Element.contains elem) (f sel)
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