1
1
import { tick } from '../../../json-crdt-patch' ;
2
- import { Anchor } from '../rga/constants' ;
3
2
import { Point } from '../rga/Point' ;
4
3
import { CursorAnchor } from '../slice/constants' ;
5
4
import { PersistedSlice } from '../slice/PersistedSlice' ;
@@ -9,6 +8,16 @@ export class Cursor<T = string> extends PersistedSlice<T> {
9
8
return this . type as CursorAnchor ;
10
9
}
11
10
11
+ /** @todo Rename to `isStartFocus`. */
12
+ public isStartFocused ( ) : boolean {
13
+ return this . type === CursorAnchor . End || this . start . cmp ( this . end ) === 0 ;
14
+ }
15
+
16
+ /** @todo Rename to `isEndFocus`. */
17
+ public isEndFocused ( ) : boolean {
18
+ return this . type === CursorAnchor . Start || this . start . cmp ( this . end ) === 0 ;
19
+ }
20
+
12
21
// ---------------------------------------------------------------- mutations
13
22
14
23
public set anchorSide ( value : CursorAnchor ) {
@@ -36,7 +45,7 @@ export class Cursor<T = string> extends PersistedSlice<T> {
36
45
* Move one of the edges of the cursor to a new point.
37
46
*
38
47
* @param point Point to set the edge to.
39
- * @param endpoint 0 for "focus", 1 for "anchor."
48
+ * @param endpoint 0 for "focus", 1 for "anchor".
40
49
*/
41
50
public setEndpoint ( point : Point < T > , endpoint : 0 | 1 = 0 ) : void {
42
51
if ( this . start === this . end ) this . end = this . end . clone ( ) ;
@@ -50,8 +59,8 @@ export class Cursor<T = string> extends PersistedSlice<T> {
50
59
51
60
public move ( move : number ) : void {
52
61
const { start, end} = this ;
53
- start . move ( move ) ;
54
- if ( start !== end ) end . move ( move ) ;
62
+ start . step ( move ) ;
63
+ if ( start !== end ) end . step ( move ) ;
55
64
this . set ( start , end ) ;
56
65
}
57
66
@@ -66,11 +75,7 @@ export class Cursor<T = string> extends PersistedSlice<T> {
66
75
*/
67
76
public collapse ( ) : void {
68
77
const deleted = this . txt . delStr ( this ) ;
69
- if ( deleted ) {
70
- const { start, rga} = this ;
71
- if ( start . anchor === Anchor . After ) this . setAfter ( start . id ) ;
72
- else this . setAfter ( start . prevId ( ) || rga . id ) ;
73
- }
78
+ if ( deleted ) this . collapseToStart ( ) ;
74
79
}
75
80
76
81
/**
@@ -87,14 +92,32 @@ export class Cursor<T = string> extends PersistedSlice<T> {
87
92
this . setAfter ( shift ? tick ( textId , shift ) : textId ) ;
88
93
}
89
94
90
- public delBwd ( ) : void {
91
- const isCollapsed = this . isCollapsed ( ) ;
92
- if ( isCollapsed ) {
93
- const range = this . txt . findCharBefore ( this . start ) ;
94
- if ( ! range ) return ;
95
- this . set ( range . start , range . end ) ;
95
+ /**
96
+ * Deletes the given number of characters from the current caret position.
97
+ * Negative values delete backwards. If the cursor selects a range, the
98
+ * range is removed and the cursor is set at the start of the range.
99
+ *
100
+ * @param step Number of characters to delete. Negative values delete
101
+ * backwards.
102
+ */
103
+ public del ( step : number = - 1 ) : void {
104
+ if ( ! this . isCollapsed ( ) ) {
105
+ this . collapse ( ) ;
106
+ return ;
96
107
}
97
- this . collapse ( ) ;
108
+ const point1 = this . start . clone ( ) ;
109
+ const point2 = point1 . clone ( ) ;
110
+ if ( step > 0 ) point2 . step ( 1 ) ;
111
+ else if ( step < 0 ) point1 . step ( - 1 ) ;
112
+ else if ( step === 0 ) {
113
+ point1 . step ( - 1 ) ;
114
+ point2 . step ( 1 ) ;
115
+ }
116
+ const txt = this . txt ;
117
+ const range = txt . range ( point1 , point2 ) ;
118
+ txt . delStr ( range ) ;
119
+ point1 . refAfter ( ) ;
120
+ this . set ( point1 ) ;
98
121
}
99
122
100
123
// ---------------------------------------------------------------- Printable
0 commit comments