Skip to content

Commit abf9678

Browse files
committed
up/down handler for CellSelection
Moves the caret to the next/previous line.
1 parent 3c4aff4 commit abf9678

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

editor-runtime/src/commonMain/kotlin/org/modelix/editor/CaretSelection.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.modelix.editor
22

3-
import kotlinx.html.TagConsumer
4-
import kotlinx.html.classes
5-
import kotlinx.html.div
63
import kotlin.math.max
74
import kotlin.math.min
85

@@ -78,15 +75,13 @@ class CaretSelection(val layoutable: LayoutableCell, val start: Int, val end: In
7875
}
7976
}
8077
KnownKeys.ArrowDown -> {
81-
createNextPreviousLineSelection(true, desiredXPosition ?: getAbsoluteX())
82-
?.let { editor.changeSelection(it) }
78+
selectNextPreviousLine(true)
8379
}
8480
KnownKeys.ArrowUp -> {
8581
if (event.modifiers.meta) {
8682
layoutable.cell.let { editor.changeSelection(CellSelection(it, true, this)) }
8783
} else {
88-
createNextPreviousLineSelection(false, desiredXPosition ?: getAbsoluteX())
89-
?.let { editor.changeSelection(it) }
84+
selectNextPreviousLine(false)
9085
}
9186
}
9287
KnownKeys.Tab -> {
@@ -147,6 +142,11 @@ class CaretSelection(val layoutable: LayoutableCell, val start: Int, val end: In
147142
return true
148143
}
149144

145+
fun selectNextPreviousLine(next: Boolean) {
146+
createNextPreviousLineSelection(next, desiredXPosition ?: getAbsoluteX())
147+
?.let { getEditor()?.changeSelection(it) }
148+
}
149+
150150
private fun processTypedText(typedText: String, editor: EditorComponent) {
151151
val oldText = layoutable.cell.getSelectableText() ?: ""
152152
val range = min(start, end) until max(start, end)

editor-runtime/src/commonMain/kotlin/org/modelix/editor/CellSelection.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ data class CellSelection(val cell: Cell, val directionLeft: Boolean, val previou
3434
if (event.modifiers.meta) {
3535
cell.ancestors().firstOrNull { it.getProperty(CommonCellProperties.selectable) }
3636
?.let { editor.changeSelection(CellSelection(it, directionLeft, this)) }
37+
} else {
38+
unwrapCaretSelection()?.selectNextPreviousLine(false)
3739
}
3840
}
3941
KnownKeys.ArrowDown -> {
4042
if (event.modifiers == Modifiers.META && previousSelection != null) {
4143
editor.changeSelection(previousSelection)
44+
} else {
45+
unwrapCaretSelection()?.selectNextPreviousLine(true)
4246
}
4347
}
4448
KnownKeys.ArrowLeft, KnownKeys.ArrowRight -> {
@@ -51,8 +55,7 @@ data class CellSelection(val cell: Cell, val directionLeft: Boolean, val previou
5155
previousSelection?.let { editor.changeSelection(it) }
5256
}
5357
} else {
54-
val caretSelection = generateSequence<Selection>(this) { (it as? CellSelection)?.previousSelection }
55-
.lastOrNull() as? CaretSelection
58+
val caretSelection = unwrapCaretSelection()
5659
if (caretSelection != null) {
5760
editor.changeSelection(CaretSelection(caretSelection.layoutable, caretSelection.start))
5861
} else {
@@ -73,6 +76,11 @@ data class CellSelection(val cell: Cell, val directionLeft: Boolean, val previou
7376
return true
7477
}
7578

79+
private fun unwrapCaretSelection(): CaretSelection? {
80+
return generateSequence<Selection>(this) { (it as? CellSelection)?.previousSelection }
81+
.lastOrNull() as? CaretSelection
82+
}
83+
7684
fun getLayoutables(): List<Layoutable> {
7785
val editor = getEditor() ?: return emptyList()
7886
val rootText = editor.getRootCell().layout

0 commit comments

Comments
 (0)