Skip to content

Commit 4ea154d

Browse files
leandroBorgesFerreiraLeandro Ferreira
andauthored
Adding keyboard command for list items (#531)
Co-authored-by: Leandro Ferreira <[email protected]>
1 parent ab36495 commit 4ea154d

File tree

5 files changed

+45
-30
lines changed

5 files changed

+45
-30
lines changed

application/composeApp/src/jvmMain/kotlin/io/writeopia/desktop/MainDesktop.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ private fun ApplicationScope.App(onCloseRequest: () -> Unit = ::exitApplication)
179179
false
180180
}
181181

182+
KeyboardCommands.isListEvent(keyEvent) -> {
183+
sendEvent(KeyboardEvent.LIST)
184+
false
185+
}
186+
182187
else -> false
183188
}
184189
}

application/core/utils/src/commonMain/kotlin/io/writeopia/common/utils/keyboard/KeyboardCommands.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,9 @@ object KeyboardCommands {
9292
keyEvent.isCommandTrigger() &&
9393
keyEvent.key.keyCode == Key.E.keyCode &&
9494
keyEvent.type == KeyEventType.KeyUp
95+
96+
fun isListEvent(keyEvent: KeyEvent) =
97+
keyEvent.isCommandTrigger() &&
98+
keyEvent.key.keyCode == Key.Minus.keyCode &&
99+
keyEvent.type == KeyEventType.KeyUp
95100
}

application/features/editor/src/commonMain/kotlin/io/writeopia/editor/features/editor/viewmodel/NoteEditorKmpViewModel.kt

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ class NoteEditorKmpViewModel(
130130
showSearch()
131131
}
132132

133+
KeyboardEvent.LIST -> {
134+
writeopiaManager.addListItem()
135+
}
136+
133137
else -> {}
134138
}
135139
}
@@ -142,15 +146,15 @@ class NoteEditorKmpViewModel(
142146

143147
private var isDarkTheme: Boolean = true
144148

145-
private val _showSearch = MutableStateFlow(false)
149+
private val showSearch = MutableStateFlow(false)
146150
private val _searchText = MutableStateFlow("")
147-
private val _currentSearchIndex = MutableStateFlow(0)
148-
private val _totalSearchResults = MutableStateFlow(0)
151+
private val _currentSearchIndexState = MutableStateFlow(0)
152+
private val _totalSearchResultsState = MutableStateFlow(0)
149153

150-
override val showSearchState: StateFlow<Boolean> = _showSearch.asStateFlow()
154+
override val showSearchState: StateFlow<Boolean> = showSearch.asStateFlow()
151155
override val searchText: StateFlow<String> = _searchText.asStateFlow()
152-
override val currentSearchIndexState: StateFlow<Int> = _currentSearchIndex.asStateFlow()
153-
override val totalSearchResultsState: StateFlow<Int> = _totalSearchResults.asStateFlow()
156+
override val currentSearchIndexState: StateFlow<Int> = _currentSearchIndexState.asStateFlow()
157+
override val totalSearchResultsState: StateFlow<Int> = _totalSearchResultsState.asStateFlow()
154158

155159
private val hasLinesSelection = writeopiaManager.onEditPositions
156160
.map { it.isNotEmpty() }
@@ -228,7 +232,7 @@ class NoteEditorKmpViewModel(
228232
private val _shouldGoToNextScreen = MutableStateFlow(false)
229233
override val shouldGoToNextScreen = _shouldGoToNextScreen.asStateFlow()
230234

231-
private val _expandedFolders = MutableStateFlow(setOf<String>())
235+
private val expandedFolders = MutableStateFlow(setOf<String>())
232236

233237
override val isEditState: StateFlow<EditState> by lazy {
234238
writeopiaManager.onEditPositions.map { set ->
@@ -257,8 +261,8 @@ class NoteEditorKmpViewModel(
257261
.stateIn(viewModelScope, SharingStarted.Lazily, "")
258262
}
259263

260-
private val _sideMenuTab = MutableStateFlow(SideMenuTab.NONE)
261-
override val sideMenuTabState: StateFlow<SideMenuTab> = _sideMenuTab.asStateFlow()
264+
private val _sideMenuTabState = MutableStateFlow(SideMenuTab.NONE)
265+
override val sideMenuTabState: StateFlow<SideMenuTab> = _sideMenuTabState.asStateFlow()
262266

263267
/**
264268
* This property defines if the document is favorite
@@ -278,16 +282,16 @@ class NoteEditorKmpViewModel(
278282
writeopiaManager.toDraw,
279283
findsOfSearch,
280284
searchText,
281-
_showSearch,
282-
_currentSearchIndex
285+
showSearch,
286+
_currentSearchIndexState
283287
) { drawState, finds, query, showSearch, currentSearchIndex ->
284288
if (finds.isEmpty() && showSearch) return@combine drawState.copy(focus = null)
285289
if (finds.isEmpty()) return@combine drawState
286290

287291
val mutableStories = drawState.stories.toMutableList()
288292
val activeFindPosition =
289293
if (finds.size > currentSearchIndex) finds.elementAt(currentSearchIndex) else null
290-
_totalSearchResults.value = finds.size
294+
_totalSearchResultsState.value = finds.size
291295

292296
if (activeFindPosition != null) {
293297
writeopiaManager.scrollToPosition(activeFindPosition)
@@ -360,7 +364,7 @@ class NoteEditorKmpViewModel(
360364
authRepository.listenForWorkspace()
361365
.flatMapLatest { workspace ->
362366
combine(
363-
_expandedFolders,
367+
expandedFolders,
364368
folderRepository.listenForFoldersByParentId(
365369
"root",
366370
workspace.id
@@ -385,7 +389,7 @@ class NoteEditorKmpViewModel(
385389
}.stateIn(viewModelScope, SharingStarted.Lazily, emptyList())
386390

387391
override fun changeSideMenu(tab: SideMenuTab) {
388-
_sideMenuTab.value = tab
392+
_sideMenuTabState.value = tab
389393
}
390394

391395
override fun deleteSelection() {
@@ -485,7 +489,7 @@ class NoteEditorKmpViewModel(
485489

486490
override fun onAddListItemClick() {
487491
if (!isEditable.value) return
488-
writeopiaManager.onListItemClicked()
492+
writeopiaManager.addListItem()
489493
}
490494

491495
override fun onAddCodeBlockClick() {
@@ -581,10 +585,10 @@ class NoteEditorKmpViewModel(
581585
}
582586

583587
override fun expandFolder(folderId: String) {
584-
val expanded = _expandedFolders.value
588+
val expanded = expandedFolders.value
585589
if (expanded.contains(folderId)) {
586590
viewModelScope.launch(Dispatchers.Default) {
587-
_expandedFolders.value = expanded - folderId
591+
expandedFolders.value = expanded - folderId
588592
}
589593
} else {
590594
viewModelScope.launch {
@@ -593,7 +597,7 @@ class NoteEditorKmpViewModel(
593597
folderId,
594598
workspace.id
595599
)
596-
_expandedFolders.value = expanded + folderId
600+
expandedFolders.value = expanded + folderId
597601
}
598602
}
599603
}
@@ -739,15 +743,15 @@ class NoteEditorKmpViewModel(
739743
}
740744

741745
override fun showSearch() {
742-
_showSearch.value = true
746+
showSearch.value = true
743747
}
744748

745749
override fun hideSearch() {
746750
viewModelScope.launch {
747-
_showSearch.value = false
751+
showSearch.value = false
748752
delay(100)
749753
_searchText.value = ""
750-
_currentSearchIndex.value = 0
754+
_currentSearchIndexState.value = 0
751755
}
752756
}
753757

@@ -756,16 +760,16 @@ class NoteEditorKmpViewModel(
756760
_searchText.value = query
757761
val finds = findsOfSearch.first().toList().sorted()
758762
if (finds.isEmpty()) {
759-
_currentSearchIndex.value = 0
760-
_totalSearchResults.value = 0
763+
_currentSearchIndexState.value = 0
764+
_totalSearchResultsState.value = 0
761765
return@launch
762766
}
763767

764768
val currentPosition = writeopiaManager.currentStory.value.focus
765769
?: writeopiaManager.currentStory.value.selection.position
766770
val newIndex = finds.indexOfFirst { it >= currentPosition }
767771

768-
_currentSearchIndex.value = if (newIndex != -1) newIndex else 0
772+
_currentSearchIndexState.value = if (newIndex != -1) newIndex else 0
769773
}
770774
}
771775

@@ -774,8 +778,8 @@ class NoteEditorKmpViewModel(
774778
val total = findsOfSearch.first().size
775779
if (total == 0) return@launch
776780

777-
val currentIndex = _currentSearchIndex.value
778-
_currentSearchIndex.value = (currentIndex - 1 + total) % total
781+
val currentIndex = _currentSearchIndexState.value
782+
_currentSearchIndexState.value = (currentIndex - 1 + total) % total
779783
}
780784
}
781785

@@ -784,8 +788,8 @@ class NoteEditorKmpViewModel(
784788
val total = findsOfSearch.first().size
785789
if (total == 0) return@launch
786790

787-
val currentIndex = _currentSearchIndex.value
788-
_currentSearchIndex.value = (currentIndex + 1) % total
791+
val currentIndex = _currentSearchIndexState.value
792+
_currentSearchIndexState.value = (currentIndex + 1) % total
789793
}
790794
}
791795

writeopia_ui/src/commonMain/kotlin/io/writeopia/ui/keyboard/KeyboardEvent.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ enum class KeyboardEvent {
1818
REDO,
1919
ACCEPT_AI,
2020
EQUATION,
21-
SEARCH
21+
SEARCH,
22+
LIST,
2223
}

writeopia_ui/src/commonMain/kotlin/io/writeopia/ui/manager/WriteopiaStateManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ class WriteopiaStateManager(
461461
/**
462462
* Click lister when user clicks in the menu to add a list item
463463
*/
464-
fun onListItemClicked() {
464+
fun addListItem() {
465465
if (!isEditable) return
466466
val onEdit = _onEditPositions.value
467467

0 commit comments

Comments
 (0)