Skip to content

Commit b9ec2fa

Browse files
Minor changes
1 parent 7d1d13b commit b9ec2fa

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

src/DOM/Util/TextCursor.purs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ module DOM.Util.TextCursor
66
, isCursor, cursorAtStart, cursorAtEnd
77
, isSelection, selectionAtStart, selectionAtEnd
88
, selectAll, moveCursorToStart, moveCursorToEnd
9-
, appendLeft, appendRight
109
, modifySelected, modifyAll
11-
, insert
10+
, appendl, appendr, insert
1211
) where
1312

1413
import Prelude
@@ -114,12 +113,8 @@ _after = _Newtype <<< prop (SProxy :: SProxy "after")
114113

115114
-- | Lens for traversing/setting all three fields.
116115
_all :: Traversal' TextCursor String
117-
_all = wander trav
118-
where
119-
-- Monomorphic traverse
120-
trav :: forall m. Applicative m => (String -> m String) -> TextCursor -> m TextCursor
121-
trav f (TextCursor { before, selected, after }) =
122-
mkTextCursor <$> f before <*> f selected <*> f after
116+
_all = wander \f (TextCursor { before, selected, after }) ->
117+
mkTextCursor <$> f before <*> f selected <*> f after
123118

124119
-- | Test whether the cursor or selection touches the start.
125120
atStart :: TextCursor -> Boolean
@@ -187,14 +182,6 @@ moveCursorToEnd tc = TextCursor
187182
, after: ""
188183
}
189184

190-
-- | Prepend a string, on the left.
191-
appendLeft :: String -> TextCursor -> TextCursor
192-
appendLeft s tc = over _before (s <> _) tc
193-
194-
-- | Append a string, on the right.
195-
appendRight :: TextCursor -> String -> TextCursor
196-
appendRight tc s = over _after (_ <> s) tc
197-
198185
-- | Modify just the selected region with an endomorphism.
199186
modifySelected :: (String -> String) -> TextCursor -> TextCursor
200187
modifySelected = over _selected
@@ -204,8 +191,17 @@ modifySelected = over _selected
204191
modifyAll :: (String -> String) -> TextCursor -> TextCursor
205192
modifyAll = over _all
206193

194+
-- | Prepend a string, on the left.
195+
appendl :: String -> TextCursor -> TextCursor
196+
appendl s tc = over _before (s <> _) tc
197+
198+
-- | Append a string, on the right.
199+
appendr :: TextCursor -> String -> TextCursor
200+
appendr tc s = over _after (_ <> s) tc
201+
207202
-- | Insert a string at the cursor position. If text is selected, the insertion
208203
-- | will be part of the selection. Otherwise it is inserted before the cursor.
204+
-- Preserves normalization
209205
-- check:
210206
-- length (insert insertion textcursor)
211207
-- == length insertion + length textcursor

src/DOM/Util/TextCursor/Element.purs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ module DOM.Util.TextCursor.Element
88

99
import Prelude
1010
import Data.Maybe (Maybe(Just))
11-
import Data.Tuple (Tuple(Tuple))
1211
import Data.String (length, splitAt)
1312
import Data.Lens (Lens', (.~))
1413
import Control.Monad.Eff (Eff)
@@ -34,20 +33,20 @@ import DOM.Util.TextCursor.Element.HTML
3433

3534
-- | Helper to split a `String` at a specific position without worrying about
3635
-- | `Nothing`.
37-
splitAtTuple :: Int -> String -> Tuple String String
38-
splitAtTuple i s = case splitAt i s of
39-
Just {before, after} -> Tuple before after
40-
_ | i > 0 -> Tuple s ""
41-
| otherwise -> Tuple "" s
36+
splitAtRec :: Int -> String -> { before :: String, after :: String }
37+
splitAtRec i s = case splitAt i s of
38+
Just split -> split
39+
_ | i > 0 -> { before: s, after: "" }
40+
| otherwise -> { before: "", after: s }
4241

4342
-- | Get the `TextCursor` from a `TextCursorElement`.
4443
textCursor :: forall eff. TextCursorElement -> Eff ( dom :: DOM | eff ) TextCursor
4544
textCursor element = do
4645
val <- value element
4746
start <- selectionStart element
4847
end <- selectionEnd element
49-
let (Tuple prior after) = splitAtTuple end val
50-
let (Tuple before selected) = splitAtTuple start prior
48+
let { before: prior, after } = splitAtRec end val
49+
let { before, after: selected } = splitAtRec start prior
5150
pure $ TextCursor
5251
{ before
5352
, selected

src/DOM/Util/TextCursor/Monoid.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module DOM.Util.TextCursor.Monoid where
33
import Prelude
44
import DOM.Util.TextCursor
55
( TextCursor, empty
6-
, appendLeft, appendRight
6+
, appendl, appendr
77
, isCursor, content
88
)
99
import Data.Monoid (class Monoid)
@@ -20,10 +20,10 @@ instance rightmostMonoid :: Monoid Rightmost where mempty = Rightmost empty
2020

2121
instance leftmostSemigroup :: Semigroup Leftmost where
2222
append (Leftmost l) (Leftmost r)
23-
| isCursor l = Leftmost (content l `appendLeft` r)
24-
| otherwise = Leftmost (l `appendRight` content r)
23+
| isCursor l = Leftmost (content l `appendl` r)
24+
| otherwise = Leftmost (l `appendr` content r)
2525

2626
instance rightmostSemigroup :: Semigroup Rightmost where
2727
append (Rightmost l) (Rightmost r)
28-
| isCursor r = Rightmost (l `appendRight` content r)
29-
| otherwise = Rightmost (content l `appendLeft` r)
28+
| isCursor r = Rightmost (l `appendr` content r)
29+
| otherwise = Rightmost (content l `appendl` r)

0 commit comments

Comments
 (0)