Skip to content

Commit 46a0c17

Browse files
committed
left/right handlers for CellSelection
1 parent e7a11bd commit 46a0c17

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class CaretSelection(val layoutable: LayoutableCell, val start: Int, val end: In
4747
if (previous != null) {
4848
if (event.modifiers.shift) {
4949
val commonAncestor = layoutable.cell.commonAncestor(previous.cell)
50-
editor.changeSelection(CellSelection(commonAncestor, this))
50+
editor.changeSelection(CellSelection(commonAncestor, true, this))
5151
} else {
5252
editor.changeSelection(CaretSelection(previous, previous.cell.getMaxCaretPos()))
5353
}
@@ -68,7 +68,7 @@ class CaretSelection(val layoutable: LayoutableCell, val start: Int, val end: In
6868
if (next != null) {
6969
if (event.modifiers.shift) {
7070
val commonAncestor = layoutable.cell.commonAncestor(next.cell)
71-
editor.changeSelection(CellSelection(commonAncestor, this))
71+
editor.changeSelection(CellSelection(commonAncestor, false, this))
7272
} else {
7373
editor.changeSelection(CaretSelection(next, 0))
7474
}
@@ -81,7 +81,7 @@ class CaretSelection(val layoutable: LayoutableCell, val start: Int, val end: In
8181
}
8282
KnownKeys.ArrowUp -> {
8383
if (event.modifiers.meta) {
84-
layoutable.cell.let { editor.changeSelection(CellSelection(it, this)) }
84+
layoutable.cell.let { editor.changeSelection(CellSelection(it, true, this)) }
8585
} else {
8686
createNextPreviousLineSelection(false, desiredXPosition ?: getAbsoluteX())
8787
?.let { editor.changeSelection(it) }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ object CommonCellProperties {
5151
}
5252

5353
fun Cell.isTabTarget() = getProperty(CommonCellProperties.tabTarget)
54+
fun Cell.isSelectable() = getProperty(CommonCellProperties.selectable)

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
package org.modelix.editor
1515

16-
data class CellSelection(val cell: Cell, val downSelection: Selection?): Selection() {
16+
data class CellSelection(val cell: Cell, val directionLeft: Boolean, val previousSelection: Selection?): Selection() {
1717
fun getEditor(): EditorComponent? = cell.editorComponent
1818

1919
override fun isValid(): Boolean {
@@ -23,7 +23,7 @@ data class CellSelection(val cell: Cell, val downSelection: Selection?): Selecti
2323
override fun update(editor: EditorComponent): Selection? {
2424
return cell.data.cellReferences.asSequence()
2525
.flatMap { editor.resolveCell(it) }
26-
.map { CellSelection(it, downSelection?.update(editor)) }
26+
.map { CellSelection(it, directionLeft, previousSelection?.update(editor)) }
2727
.firstOrNull()
2828
}
2929

@@ -33,12 +33,38 @@ data class CellSelection(val cell: Cell, val downSelection: Selection?): Selecti
3333
KnownKeys.ArrowUp -> {
3434
if (event.modifiers.meta) {
3535
cell.ancestors().firstOrNull { it.getProperty(CommonCellProperties.selectable) }
36-
?.let { editor.changeSelection(CellSelection(it, this)) }
36+
?.let { editor.changeSelection(CellSelection(it, directionLeft, this)) }
3737
}
3838
}
3939
KnownKeys.ArrowDown -> {
40-
if (event.modifiers.meta && downSelection != null) {
41-
editor.changeSelection(downSelection)
40+
if (event.modifiers == Modifiers.META && previousSelection != null) {
41+
editor.changeSelection(previousSelection)
42+
}
43+
}
44+
KnownKeys.ArrowLeft, KnownKeys.ArrowRight -> {
45+
if (event.modifiers == Modifiers.SHIFT) {
46+
val isLeft = event.knownKey == KnownKeys.ArrowLeft
47+
if (isLeft == directionLeft) {
48+
cell.ancestors().firstOrNull { it.isSelectable() }
49+
?.let { editor.changeSelection(CellSelection(it, directionLeft, this)) }
50+
} else {
51+
previousSelection?.let { editor.changeSelection(it) }
52+
}
53+
} else {
54+
val caretSelection = generateSequence<Selection>(this) { (it as? CellSelection)?.previousSelection }
55+
.lastOrNull() as? CaretSelection
56+
if (caretSelection != null) {
57+
editor.changeSelection(CaretSelection(caretSelection.layoutable, caretSelection.start))
58+
} else {
59+
val tabTargets = cell.descendantsAndSelf().filter { it.isTabTarget() }
60+
if (event.knownKey == KnownKeys.ArrowLeft) {
61+
tabTargets.firstOrNull()?.layoutable()
62+
?.let { editor.changeSelection(CaretSelection(it, 0)) }
63+
} else {
64+
tabTargets.lastOrNull()?.layoutable()
65+
?.let { editor.changeSelection(CaretSelection(it, it.cell.getSelectableText()?.length ?: 0)) }
66+
}
67+
}
4268
}
4369
}
4470
else -> {}

0 commit comments

Comments
 (0)