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

Editor | Tech | Doc provider (#2078)

This commit is contained in:
Evgenii Kozlov 2022-02-02 18:23:09 +03:00 committed by GitHub
parent 105fa8c977
commit 5e5a34f824
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 13 deletions

View file

@ -1,5 +1,6 @@
package com.anytypeio.anytype.presentation.editor
import com.anytypeio.anytype.core_models.Document
import com.anytypeio.anytype.core_models.Id
import com.anytypeio.anytype.domain.editor.Editor
import com.anytypeio.anytype.domain.editor.Editor.Focus
@ -26,6 +27,7 @@ interface Editor {
}
class Storage {
val document: DocumentProvider = DocumentProvider.Default()
val views: Store<List<BlockView>> = Store.Screen()
val focus: Store<Focus> = Store.Focus()
val details: Store.Details = Store.Details()
@ -60,4 +62,16 @@ interface Editor {
data class OnSelectionChanged(val id: Id, val selection: IntRange) : Text()
}
}
interface DocumentProvider {
fun get(): Document
fun update(document: Document)
fun clear()
class Default : DocumentProvider {
var doc: Document = emptyList()
override fun get(): Document = doc
override fun update(document: Document) { this.doc = document }
override fun clear() { this.doc = emptyList() }
}
}
}

View file

@ -201,7 +201,7 @@ class EditorViewModel(
/**
* Current document
*/
var blocks: Document = emptyList()
val blocks: Document get() = orchestrator.stores.document.get()
private val _focus: MutableLiveData<Id> = MutableLiveData()
val focus: LiveData<Id> = _focus
@ -323,7 +323,7 @@ class EditorViewModel(
// do nothing
}
}
blocks = reduce(blocks, event)
orchestrator.stores.document.update(reduce(blocks, event))
}
if (BuildConfig.DEBUG) {
Timber.d("Blocks after handling events: $blocks")
@ -415,12 +415,13 @@ class EditorViewModel(
range = selection.second
)
blocks = blocks.map { block ->
val update = blocks.map { block ->
if (block.id != target.id)
block
else
new
}
orchestrator.stores.document.update(update)
refresh()
@ -436,12 +437,13 @@ class EditorViewModel(
private fun rerenderingBlocks(block: Block) =
viewModelScope.launch {
blocks = blocks.map {
val update = blocks.map {
if (it.id != block.id)
it
else
block
}
orchestrator.stores.document.update(update)
refresh()
}
@ -543,12 +545,13 @@ class EditorViewModel(
.onEach { if (it == pendingTextUpdate) pendingTextUpdate = null }
.filterNotNull()
.onEach { update ->
blocks = blocks.map { block ->
val updated = blocks.map { block ->
if (block.id == update.target) {
block.updateText(update)
} else
block
}
orchestrator.stores.document.update(updated)
}
.map { update ->
Intent.Text.UpdateText(
@ -986,7 +989,8 @@ class EditorViewModel(
val block = blocks.first { it.id == target }
val content = block.content<Content.Text>()
blocks = blocks.updateTextContent(target, text, marks)
val update = blocks.updateTextContent(target, text, marks)
orchestrator.stores.document.update(update)
viewModelScope.launch {
orchestrator.proxies.saves.send(null)
@ -1030,10 +1034,12 @@ class EditorViewModel(
marks = marks
)
blocks = blocks.replace(
val update = blocks.replace(
replacement = { old -> old.copy(content = content) }
) { block -> block.id == id }
orchestrator.stores.document.update(update)
if (content.isList() || content.isToggle()) {
handleEndlineEnterPressedEventForListItem(content, id)
} else {
@ -1113,7 +1119,7 @@ class EditorViewModel(
) {
Timber.d("onNonEmptyBlockBackspaceClicked, id:[$id] text:[$text] marks:[$marks]")
blocks = blocks.map { block ->
val update = blocks.map { block ->
if (block.id == id) {
block.copy(
content = block.content<Content.Text>().copy(
@ -1126,6 +1132,8 @@ class EditorViewModel(
}
}
orchestrator.stores.document.update(update)
viewModelScope.launch {
orchestrator.proxies.saves.send(null)
orchestrator.proxies.changes.send(null)
@ -1551,13 +1559,15 @@ class EditorViewModel(
param = null
)
blocks = blocks.map { block ->
val update = blocks.map { block ->
if (block.id != target.id)
block
else
new
}
orchestrator.stores.document.update(update)
viewModelScope.launch { refresh() }
viewModelScope.launch {
@ -2009,7 +2019,7 @@ class EditorViewModel(
Timber.d("onCheckboxClicked, view:[$view]")
blocks = blocks.map { block ->
val update = blocks.map { block ->
if (block.id == view.id) {
block.copy(
content = block.content<Content.Text>().copy(
@ -2021,6 +2031,8 @@ class EditorViewModel(
}
}
orchestrator.stores.document.update(update)
val store = orchestrator.stores.views
viewModelScope.launch {
@ -2049,7 +2061,7 @@ class EditorViewModel(
Timber.d("onTitleCheckboxClicked, view:[$view]")
blocks = blocks.map { block ->
val update = blocks.map { block ->
if (block.id == view.id) {
block.copy(
content = block.content<Content.Text>().copy(
@ -2061,6 +2073,8 @@ class EditorViewModel(
}
}
orchestrator.stores.document.update(update)
val store = orchestrator.stores.views
viewModelScope.launch {
@ -4177,12 +4191,14 @@ class EditorViewModel(
text: String,
marks: List<Content.Text.Mark>
) {
blocks = blocks.updateTextContent(
val update = blocks.updateTextContent(
target = targetId,
text = text,
marks = marks
)
orchestrator.stores.document.update(update)
//send new text to Middleware
viewModelScope.launch {
orchestrator.proxies.saves.send(null)
@ -5072,13 +5088,15 @@ class EditorViewModel(
mentionTrigger = mentionTrigger
)
blocks = blocks.map { block ->
val update = blocks.map { block ->
if (block.id != target.id)
block
else
new
}
orchestrator.stores.document.update(update)
viewModelScope.launch {
val position = mentionFrom + name.length + 1
orchestrator.stores.focus.update(