1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-12 10:40:25 +09:00

Refactoring: remove legacy selection param (#795)

* remove legacy param

* add selection to store

* fixes

* tests

* fix

* fix

* tests off

* fix
This commit is contained in:
Konstantin Ivanov 2020-09-05 16:09:49 +03:00 committed by GitHub
parent 85fe958427
commit 047bd667eb
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 39 deletions

View file

@ -24,6 +24,22 @@ interface Editor {
}
}
/**
* @property id id of the focused block, empty if no block in focus
* @property selection start and end of selection, could be null
*/
data class TextSelection(
val id: String,
val selection: IntRange?
): Editor {
val isNotEmpty: Boolean get() = id != EMPTY_FOCUS
companion object {
fun empty() = TextSelection(EMPTY_FOCUS, null)
private const val EMPTY_FOCUS = ""
}
}
sealed class Cursor {
/**
* Indicates that the cursor should be placed at the beginning of a text.

View file

@ -123,7 +123,7 @@ sealed class ControlPanelMachine {
val style: Block.Content.Text.Style
) : Event()
data class OnBlockActionToolbarStyleClicked(val target: Block) : Event()
data class OnBlockActionToolbarStyleClicked(val target: Block, val selection: IntRange?) : Event()
/**
* Styling-toolbar-related events
@ -173,7 +173,7 @@ sealed class ControlPanelMachine {
object OnStop : Mentions()
}
data class OnRefresh(val target: Block?) : Event()
data class OnRefresh(val target: Block?, val selection: IntRange?) : Event()
}
/**
@ -190,18 +190,15 @@ sealed class ControlPanelMachine {
Overlap.INNER_RIGHT,
Overlap.INNER_LEFT
)
var mSelection: IntRange? = null
override val function: suspend (ControlPanelState, Event) -> ControlPanelState
get() = { state, event -> reduce(state, event) }
override suspend fun reduce(state: ControlPanelState, event: Event) = when (event) {
is Event.OnSelectionChanged -> {
mSelection = event.selection
when {
state.stylingToolbar.isVisible -> {
handleOnSelectionChangedForStylingToolbar(mSelection, event, state)
handleOnSelectionChangedForStylingToolbar(event.selection, event, state)
}
state.mentionToolbar.isVisible -> state.copy(
mentionToolbar = handleOnSelectionChangedForMentionState(
@ -231,10 +228,9 @@ sealed class ControlPanelMachine {
is Event.OnAddBlockToolbarOptionSelected -> state.copy()
is Event.OnMarkupBackgroundColorSelected -> state.copy()
is Event.OnEditorContextMenuStyleClicked -> {
mSelection = event.selection
val config = event.target.getStyleConfig(
focus = true,
selection = mSelection
selection = event.selection
)
val target = target(event.target)
val props = getMarkupLevelStylingProps(target, event.selection)
@ -276,10 +272,10 @@ sealed class ControlPanelMachine {
),
stylingToolbar = state.stylingToolbar.copy(
isVisible = true,
mode = getModeForSelection(mSelection),
mode = getModeForSelection(event.selection),
target = target(event.target),
config = event.target.getStyleConfig(true, mSelection),
props = getPropsForSelection(target, mSelection)
config = event.target.getStyleConfig(true, event.selection),
props = getPropsForSelection(target, event.selection)
)
)
}
@ -333,7 +329,7 @@ sealed class ControlPanelMachine {
return if (state.stylingToolbar.mode == StylingMode.MARKUP) {
if (event.target != null) {
val target = target(event.target)
mSelection?.let {
event.selection?.let {
val props = getMarkupLevelStylingProps(target, it)
state.copy(
stylingToolbar = state.stylingToolbar.copy(

View file

@ -1,6 +1,7 @@
package com.agileburo.anytype.presentation.page
import com.agileburo.anytype.core_ui.features.page.BlockView
import com.agileburo.anytype.domain.editor.Editor
import com.agileburo.anytype.domain.editor.Editor.Focus
import com.agileburo.anytype.presentation.page.editor.Proxy
import com.agileburo.anytype.presentation.page.editor.Store
@ -12,6 +13,7 @@ interface Editor {
val views: Store<List<BlockView>> = Store.Screen()
val focus: Store<Focus> = Store.Focus()
val details: Store.Details = Store.Details()
val textSelection: Store<Editor.TextSelection> = Store.TextSelection()
}
class Proxer(

View file

@ -98,8 +98,6 @@ class PageViewModel(
*/
private val renderizePipeline = Proxy.Subject<Document>()
private val selections = Proxy.Subject<Pair<Id, IntRange>>()
private val markupActionPipeline = Proxy.Subject<MarkupAction>()
private val titleChannel = Channel<String>()
@ -150,7 +148,7 @@ class PageViewModel(
viewModelScope.launch {
orchestrator.stores.focus.stream().collect {
if (it.isEmpty) {
clearSelections()
orchestrator.stores.textSelection.update(Editor.TextSelection.empty())
}
_focus.postValue(it.id)
}
@ -225,21 +223,30 @@ class PageViewModel(
markupActionPipeline
.stream()
.withLatestFrom(
selections
orchestrator.stores.textSelection
.stream()
.distinctUntilChanged()
.filter { (_, selection) -> selection.first != selection.last }
) { a, b -> Pair(a, b) }
.onEach { (action, selection) ->
if (action.type == Markup.Type.LINK) {
val block = blocks.first { it.id == selection.first }
val range = IntRange(
start = selection.second.first,
endInclusive = selection.second.last.dec()
)
stateData.value = ViewState.OpenLinkScreen(context, block, range)
} else {
applyMarkup(selection, action)
)
{ a, b -> Pair(a, b) }
.onEach { (action, textSelection) ->
val range = textSelection.selection
if (textSelection.isNotEmpty && range != null && range.first != range.last) {
if (action.type == Markup.Type.LINK) {
val block = blocks.first { it.id == textSelection.id }
stateData.value = ViewState.OpenLinkScreen(
pageId = context,
block = block,
range = IntRange(
start = range.first,
endInclusive = range.last.dec()
)
)
} else {
applyMarkup(
selection = Pair(textSelection.id, range),
action = action
)
}
}
}
.launchIn(viewModelScope)
@ -345,9 +352,11 @@ class PageViewModel(
.filter { it.isNotEmpty() }
.onEach {
if (focus.value != null && focus.value != context) {
val textSelection = orchestrator.stores.textSelection.current()
controlPanelInteractor.onEvent(
event = ControlPanelMachine.Event.OnRefresh(
target = blocks.find { it.id == focus.value }
target = blocks.find { it.id == focus.value },
selection = textSelection.selection
)
)
}
@ -574,7 +583,9 @@ class PageViewModel(
}
fun onSelectionChanged(id: String, selection: IntRange) {
viewModelScope.launch { selections.send(Pair(id, selection)) }
viewModelScope.launch {
orchestrator.stores.textSelection.update(Editor.TextSelection(id, selection))
}
controlPanelInteractor.onEvent(ControlPanelMachine.Event.OnSelectionChanged(id, selection))
}
@ -1064,9 +1075,11 @@ class PageViewModel(
_error.value = "Move To not implemented"
}
ActionItemType.Style -> {
val textSelection = orchestrator.stores.textSelection.current()
controlPanelInteractor.onEvent(
ControlPanelMachine.Event.OnBlockActionToolbarStyleClicked(
target = blocks.first { it.id == id }
target = blocks.first { it.id == id },
selection = textSelection.selection
)
)
dispatch(Command.PopBackStack)
@ -1321,9 +1334,11 @@ class PageViewModel(
if (orchestrator.stores.focus.current().id == context) {
_error.value = "Changing style for title currently not supported"
} else {
val textSelection = orchestrator.stores.textSelection.current()
controlPanelInteractor.onEvent(
ControlPanelMachine.Event.OnBlockActionToolbarStyleClicked(
target = blocks.first { it.id == orchestrator.stores.focus.current().id }
target = blocks.first { it.id == orchestrator.stores.focus.current().id },
selection = textSelection.selection
)
)
}
@ -2224,10 +2239,10 @@ class PageViewModel(
orchestrator.stores.focus.cancel()
orchestrator.stores.details.cancel()
orchestrator.stores.textSelection.cancel()
orchestrator.proxies.changes.cancel()
orchestrator.proxies.saves.cancel()
selections.cancel()
markupActionPipeline.cancel()
renderizePipeline.cancel()

View file

@ -68,4 +68,6 @@ interface Store<T> {
update(current().copy(details = current().details + mapOf(target to fields)))
}
}
class TextSelection : Conflated<Editor.TextSelection>(Editor.TextSelection.empty())
}

View file

@ -550,7 +550,6 @@ class ControlPanelStateReducerTest {
)
val result = runBlocking {
reducer.mSelection = selectionFirst
reducer.reduce(
state = given,
event = ControlPanelMachine.Event.OnSelectionChanged(
@ -686,7 +685,6 @@ class ControlPanelStateReducerTest {
)
val result = runBlocking {
reducer.mSelection = selectionFirst
reducer.reduce(
state = given,
event = ControlPanelMachine.Event.OnSelectionChanged(
@ -819,7 +817,8 @@ class ControlPanelStateReducerTest {
reducer.reduce(
state = given,
event = ControlPanelMachine.Event.OnBlockActionToolbarStyleClicked(
target = block
target = block,
selection = IntRange(0, 3)
)
)
}
@ -942,7 +941,8 @@ class ControlPanelStateReducerTest {
reducer.reduce(
state = given,
event = ControlPanelMachine.Event.OnBlockActionToolbarStyleClicked(
target = block
target = block,
selection = IntRange(1,1)
)
)
}
@ -1392,7 +1392,8 @@ class ControlPanelStateReducerTest {
reducer.reduce(
state = afterSelectionStateExpected,
event = ControlPanelMachine.Event.OnBlockActionToolbarStyleClicked(
target = block
target = block,
selection = IntRange(6,6)
)
)
}