This repository has been archived by the owner on Oct 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Add Selection API #81
Open
paulyoung
wants to merge
5
commits into
purescript-deprecated:master
Choose a base branch
from
paulyoung:paulyoung/selection-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
29ab77f
Add Selection API
paulyoung f1fc376
Add missing semicolon
paulyoung caeb70f
Merge remote-tracking branch 'upstream/master' into paulyoung/selecti…
paulyoung 08c7af4
Drop the `Partial` constraint via `unsafePartial`
paulyoung ff53e77
Merge branch 'master' into paulyoung/selection-api
paulyoung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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 😆