Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Add Selection API #81

Open
wants to merge 5 commits 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
154 changes: 154 additions & 0 deletions src/DOM/HTML/Selection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
"use strict";

exports.anchorNode = function (selection) {
return function () {
return selection.anchorNode;
};
};

exports.anchorOffset = function (selection) {
return function () {
return selection.anchorOffset;
};
};

exports.focusNode = function (selection) {
return function () {
return selection.focusNode;
};
};

exports.focusOffset = function (selection) {
return function () {
return selection.focusOffset;
};
};

exports.isCollapsed = function (selection) {
return function () {
return selection.isCollapsed;
};
};

exports.rangeCount = function (selection) {
return function () {
return selection.rangeCount;
};
};

exports.typeImpl = function (selection) {
return function () {
return selection.type;
};
};

exports.getRangeAt = function (index) {
return function (selection) {
return function () {
return selection.getRangeAt(index);
};
};
};

exports.collapse = function (parentNode) {
return function (offset) {
return function (selection) {
return function () {
selection.collapse(parentNode, offset);
};
};
};
};

exports.extend = function (parentNode) {
return function (offset) {
return function (selection) {
return function () {
selection.extend(parentNode, offset);
};
};
};
};

exports.collapseToStart = function (selection) {
return function () {
selection.collapseToStart();
};
};

exports.collapseToEnd = function (selection) {
return function () {
selection.collapseToEnd();
};
};

exports.selectAllChildren = function (parentNode) {
return function (selection) {
return function () {
selection.selectAllChildren(parentNode);
};
};
};

exports.addRange = function (range) {
return function (selection) {
return function () {
selection.addRange(range);
};
};
};

exports.removeRange = function (range) {
return function (selection) {
return function () {
selection.removeRange(range);
};
};
};

exports.removeAllRanges = function (selection) {
return function () {
selection.removeAllRanges();
};
};

exports.deleteFromDocument = function (selection) {
return function () {
selection.deleteFromDocument();
};
};

exports.toString = function (selection) {
return function () {
return selection.toString();
};
};

exports.containsNode = function (aNode) {
return function (aPartlyContained) {
return function (selection) {
return function () {
return selection.containsNode(aNode, aPartlyContained);
};
};
};
};

exports.setBaseAndExtent = function (anchorNode) {
return function (anchorOffset) {
return function (focusNode) {
return function (focusOffset) {
return function (selection) {
return function () {
selection.setBaseAndExtent(
anchorNode,
anchorOffset,
focusNode,
focusOffset
);
};
};
};
};
};
};
149 changes: 149 additions & 0 deletions src/DOM/HTML/Selection.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
module DOM.HTML.Selection
( SelectionType
, anchorNode
, anchorOffset
, focusNode
, focusOffset
, isCollapsed
, rangeCount
, type_
) where

import Prelude
import Control.Monad.Eff (Eff)
import DOM (DOM)
import DOM.HTML.Types (Range, SELECTION, Selection)
import DOM.Node.Types (Node)
import Data.Maybe (Maybe(..), fromJust)
import Partial.Unsafe (unsafePartial)

foreign import anchorNode
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) Node

foreign import anchorOffset
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) Int

foreign import focusNode
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) Node

foreign import focusOffset
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) Int

foreign import isCollapsed
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) Boolean

foreign import rangeCount
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) Int

foreign import typeImpl
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) String

type_
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) SelectionType
type_ = unsafePartial $ typeImpl >=> pure <<< fromJust <<< toSelectionType

data SelectionType
= None
| Caret
| Range

toSelectionType :: String -> Maybe SelectionType
toSelectionType "None" = Just None
toSelectionType "Caret" = Just Caret
toSelectionType "Range" = Just Range
toSelectionType _ = Nothing

foreign import getRangeAt
:: forall eff
. Int
-> Selection
-> Eff (selection :: SELECTION | eff) Range

foreign import collapse
:: forall eff
. Node
-> Int
-> Selection
-> Eff (selection :: SELECTION | eff) Unit

foreign import extend
:: forall eff
. Node
-> Int
-> Selection
-> Eff (selection :: SELECTION | eff) Unit

foreign import collapseToStart
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) Unit

foreign import collapseToEnd
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) Unit

foreign import selectAllChildren
:: forall eff
. Node
-> Selection
-> Eff (selection :: SELECTION | eff) Unit

foreign import addRange
:: forall eff
. Range
-> Selection
-> Eff (selection :: SELECTION | eff) Unit

foreign import removeRange
:: forall eff
. Range
-> Selection
-> Eff (selection :: SELECTION | eff) Unit

foreign import removeAllRanges
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) Unit

foreign import deleteFromDocument
:: forall eff
. Selection
-> Eff (dom :: DOM, selection :: SELECTION | eff) Unit

foreign import toString
:: forall eff
. Selection
-> Eff (selection :: SELECTION | eff) String

foreign import containsNode
:: forall eff
. Node
-> Boolean
-> Selection
-> Eff (selection :: SELECTION | eff) Boolean

foreign import setBaseAndExtent
:: forall eff
. Node
-> Int
-> Node
-> Int
-> Selection
-> Eff (selection :: SELECTION | eff) Unit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all of these latter functions should perhaps have the Selection argument first for "normal" PS argument ordering?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, disregard that, I think it probably is the right way around now.

I'll let you comment on that either way and then will merge :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now you've got me wondering!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's tricky with this one, as it's usually "the thing that is being operated on goes last" - but due to the nature of this API, the selection is acting on a thing - that's what tripped me up.

I'm fine with it either way, just let me know what you want to do, now I've sown the seeds of doubt 😆

9 changes: 9 additions & 0 deletions src/DOM/HTML/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ module DOM.HTML.Types
( Navigator
, Location
, History
, Range
, Selection
, URL
, Window
, ALERT
, CONFIRM
, HISTORY
, PROMPT
, SELECTION
, WINDOW
, windowToEventTarget
, HTMLDocument
Expand Down Expand Up @@ -232,6 +235,10 @@ foreign import data Window :: Type

foreign import data History :: Type

foreign import data Range :: Type

foreign import data Selection :: Type

foreign import data URL :: Type

foreign import data ALERT :: Effect
Expand All @@ -242,6 +249,8 @@ foreign import data PROMPT :: Effect

foreign import data CONFIRM :: Effect

foreign import data SELECTION :: Effect

foreign import data WINDOW :: Effect

windowToEventTarget :: Window -> EventTarget
Expand Down
6 changes: 6 additions & 0 deletions src/DOM/HTML/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,9 @@ exports._cancelIdleCallback = function(id) {
};
};
};

exports.getSelection = function (window) {
return function () {
return window.getSelection();
};
};
4 changes: 3 additions & 1 deletion src/DOM/HTML/Window.purs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module DOM.HTML.Window

import Control.Monad.Eff (Eff)
import DOM (DOM)
import DOM.HTML.Types (ALERT, CONFIRM, HISTORY, HTMLDocument, History, Location, Navigator, PROMPT, WINDOW, Window, URL)
import DOM.HTML.Types (ALERT, CONFIRM, HISTORY, HTMLDocument, History, Location, Navigator, PROMPT, SELECTION, Selection, WINDOW, Window, URL)
import DOM.WebStorage.Types (Storage)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
Expand Down Expand Up @@ -143,3 +143,5 @@ foreign import _cancelIdleCallback :: forall eff. Int -> Window -> Eff (dom :: D

cancelIdleCallback :: forall eff. RequestIdleCallbackId -> Window -> Eff (dom :: DOM | eff) Unit
cancelIdleCallback idAF = _cancelIdleCallback (unwrap idAF)

foreign import getSelection :: forall eff. Window -> Eff (selection :: SELECTION | eff) Selection