1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-264 Editor | Scroll to table block when searching on page (#2670)

* DROID-264 scroll to block or table block

* DROID-264 scrolling with offset

Co-authored-by: konstantiniiv <ki@anytype.io>
This commit is contained in:
Konstantin Ivanov 2022-10-20 17:52:28 +02:00 committed by GitHub
parent 7babee5052
commit 4d83b5a0d0
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 16 deletions

View file

@ -803,7 +803,10 @@ open class EditorFragment : NavigationFragment<FragmentEditorBinding>(R.layout.f
vm.commands.observe(viewLifecycleOwner) { execute(it) }
vm.searchResultScrollPosition
.filter { it != EditorViewModel.NO_SEARCH_RESULT_POSITION }
.onEach { binding.recycler.smoothScrollToPosition(it) }
.onEach {
(binding.recycler.layoutManager as? LinearLayoutManager)
?.scrollToPositionWithOffset(it, dimen(R.dimen.default_toolbar_height))
}
.launchIn(lifecycleScope)
vm.syncStatus.onEach { status -> bindSyncStatus(status) }.launchIn(lifecycleScope)

View file

@ -108,6 +108,7 @@ import com.anytypeio.anytype.presentation.editor.editor.ext.updateCursorAndEditM
import com.anytypeio.anytype.presentation.editor.editor.ext.updateSelection
import com.anytypeio.anytype.presentation.editor.editor.ext.applyBordersToSelectedCells
import com.anytypeio.anytype.presentation.editor.editor.ext.findTableCellView
import com.anytypeio.anytype.presentation.editor.editor.ext.findSearchResultPosition
import com.anytypeio.anytype.presentation.editor.editor.ext.removeBordersFromCells
import com.anytypeio.anytype.presentation.editor.editor.ext.updateTableOfContentsViews
import com.anytypeio.anytype.presentation.editor.editor.listener.ListenerType
@ -2015,21 +2016,13 @@ class EditorViewModel(
val update = views.nextSearchTarget()
viewModelScope.launch { orchestrator.stores.views.update(update) }
viewModelScope.launch { renderCommand.send(Unit) }
val target = update.find { view ->
view is BlockView.Searchable && view.searchFields.any { it.isTargeted }
}
val pos = update.indexOf(target)
searchResultScrollPosition.value = pos
searchResultScrollPosition.value = update.findSearchResultPosition()
}
is SearchInDocEvent.Previous -> {
val update = views.previousSearchTarget()
viewModelScope.launch { orchestrator.stores.views.update(update) }
viewModelScope.launch { renderCommand.send(Unit) }
val target = update.find { view ->
view is BlockView.Searchable && view.searchFields.any { it.isTargeted }
}
val pos = update.indexOf(target)
searchResultScrollPosition.value = pos
searchResultScrollPosition.value = update.findSearchResultPosition()
}
is SearchInDocEvent.Cancel -> {
mode = EditorMode.Edit
@ -2043,11 +2036,7 @@ class EditorViewModel(
val update = views.nextSearchTarget()
viewModelScope.launch { orchestrator.stores.views.update(update) }
viewModelScope.launch { renderCommand.send(Unit) }
val target = update.find { view ->
view is BlockView.Searchable && view.searchFields.any { it.isTargeted }
}
val pos = update.indexOf(target)
searchResultScrollPosition.value = pos
searchResultScrollPosition.value = update.findSearchResultPosition()
}
else -> {}
}

View file

@ -1394,4 +1394,21 @@ fun List<BlockView>.findBlockTableByCellId(cellId: Id): BlockView.Table? {
}
}
return null
}
/**
* Find position of highlighted targeted text block or table block in case when cell is targeting
*/
fun List<BlockView>.findSearchResultPosition(): Int {
val target = findHighlightedTarget() ?: return -1
forEachIndexed { index, blockView ->
if (blockView is BlockView.Table) {
val textCells = blockView.getTextCells()
val cellIndex = textCells.indexOfFirst { it.id == target.id }
if (cellIndex != -1) return index
} else {
if (blockView.id == target.id) return index
}
}
return -1
}