From 79a14ad5b815148ccffec1cb28a2d4ebf746541f Mon Sep 17 00:00:00 2001 From: Evgenii Kozlov Date: Mon, 28 Sep 2020 16:51:09 +0300 Subject: [PATCH] Fix/code block in multi select mode (#927) --- CHANGELOG.md | 4 ++ .../features/editor/holders/other/Code.kt | 23 ++++++- .../core_ui/features/page/BlockAdapter.kt | 1 + .../core_ui/features/page/BlockView.kt | 5 +- ..._block_code_multi_select_mode_selector.xml | 1 - .../res/layout/item_block_code_snippet.xml | 61 +++++++++++------- .../item_block_code_snippet_preview.xml | 63 ++++++++++++------- .../core_utils/ext/AndroidExtension.kt | 3 +- .../page/render/DefaultBlockViewRenderer.kt | 8 ++- .../editor/EditorBackspaceNestedDeleteTest.kt | 6 +- 10 files changed, 121 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af80fd9970..17a2658e65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Version 0.0.50 (WIP) +### New features 🚀 + +* Code block in multi-select and scroll-and-move modes (#892) + ### Design & UX 🔳 * Changed object names in add-block and turn-into panels (#752) diff --git a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/editor/holders/other/Code.kt b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/editor/holders/other/Code.kt index 772cfb3961..ecfd174ed3 100644 --- a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/editor/holders/other/Code.kt +++ b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/editor/holders/other/Code.kt @@ -2,6 +2,9 @@ package com.anytypeio.anytype.core_ui.features.editor.holders.other import android.text.Editable import android.view.View +import android.widget.FrameLayout +import androidx.core.view.updateLayoutParams +import com.anytypeio.anytype.core_ui.R import com.anytypeio.anytype.core_ui.features.editor.holders.`interface`.TextHolder import com.anytypeio.anytype.core_ui.features.page.BlockView import com.anytypeio.anytype.core_ui.features.page.BlockViewDiffUtil @@ -10,6 +13,7 @@ import com.anytypeio.anytype.core_ui.features.page.ListenerType import com.anytypeio.anytype.core_ui.tools.DefaultTextWatcher import com.anytypeio.anytype.core_ui.widgets.text.EditorLongClickListener import com.anytypeio.anytype.core_ui.widgets.text.TextInputWidget +import com.anytypeio.anytype.core_utils.ext.dimen import kotlinx.android.synthetic.main.item_block_code_snippet.view.* import timber.log.Timber @@ -25,8 +29,10 @@ class Code(view: View) : BlockViewHolder(view), TextHolder { onTextChanged: (String, Editable) -> Unit, onSelectionChanged: (String, IntRange) -> Unit, onFocusChanged: (String, Boolean) -> Unit, - clicked: (ListenerType) -> Unit + clicked: (ListenerType) -> Unit, + onTextInputClicked: (String) -> Unit ) { + indentize(item) if (item.mode == BlockView.Mode.READ) { content.setText(item.text) enableReadMode() @@ -61,6 +67,17 @@ class Code(view: View) : BlockViewHolder(view), TextHolder { } content.selectionWatcher = { onSelectionChanged(item.id, it) } } + + content.setOnClickListener { onTextInputClicked(item.id) } + } + + fun indentize(item: BlockView.Indentable) { + itemView.snippetContainer.updateLayoutParams { + apply { + val extra = item.indent * dimen(R.dimen.indent) + leftMargin = 0 + extra + } + } } fun processChangePayload( @@ -98,6 +115,10 @@ class Code(view: View) : BlockViewHolder(view), TextHolder { if (payload.focusChanged()) { setFocus(item) } + + if (payload.isIndentChanged) { + indentize(item) + } } override fun select(item: BlockView.Selectable) { diff --git a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/page/BlockAdapter.kt b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/page/BlockAdapter.kt index 6ca3bd560a..57b61dc756 100644 --- a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/page/BlockAdapter.kt +++ b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/page/BlockAdapter.kt @@ -778,6 +778,7 @@ class BlockAdapter( onSelectionChanged = onSelectionChanged, onFocusChanged = onFocusChanged, clicked = onClickListener, + onTextInputClicked = onTextInputClicked ) } is File -> { diff --git a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/page/BlockView.kt b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/page/BlockView.kt index 884fd080e5..f185f29ec0 100644 --- a/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/page/BlockView.kt +++ b/core-ui/src/main/java/com/anytypeio/anytype/core_ui/features/page/BlockView.kt @@ -436,8 +436,9 @@ sealed class BlockView : ViewType, Parcelable { var text: String, override val mode: Mode = Mode.EDIT, override var isFocused: Boolean = false, - override val isSelected: Boolean = false - ) : BlockView(), Permission, Selectable, Focusable { + override val isSelected: Boolean = false, + override val indent: Int = 0 + ) : BlockView(), Permission, Selectable, Focusable, Indentable { override fun getViewType() = HOLDER_CODE_SNIPPET } diff --git a/core-ui/src/main/res/drawable/item_block_code_multi_select_mode_selector.xml b/core-ui/src/main/res/drawable/item_block_code_multi_select_mode_selector.xml index 8e4f381067..9c6ae9ee18 100644 --- a/core-ui/src/main/res/drawable/item_block_code_multi_select_mode_selector.xml +++ b/core-ui/src/main/res/drawable/item_block_code_multi_select_mode_selector.xml @@ -1,5 +1,4 @@ - \ No newline at end of file diff --git a/core-ui/src/main/res/layout/item_block_code_snippet.xml b/core-ui/src/main/res/layout/item_block_code_snippet.xml index 7ca2ffd8d5..986310efce 100644 --- a/core-ui/src/main/res/layout/item_block_code_snippet.xml +++ b/core-ui/src/main/res/layout/item_block_code_snippet.xml @@ -1,31 +1,48 @@ - + android:paddingBottom="6dp" + android:paddingStart="12dp" + android:paddingEnd="12dp"> - - - + android:layout_marginTop="1dp" + android:layout_marginBottom="1dp" + android:orientation="vertical"> - \ No newline at end of file + + + + + + \ No newline at end of file diff --git a/core-ui/src/main/res/layout/item_block_code_snippet_preview.xml b/core-ui/src/main/res/layout/item_block_code_snippet_preview.xml index 9389c35e51..8aa934ce08 100644 --- a/core-ui/src/main/res/layout/item_block_code_snippet_preview.xml +++ b/core-ui/src/main/res/layout/item_block_code_snippet_preview.xml @@ -1,28 +1,47 @@ - + android:id="@+id/root" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_marginStart="8dp" + android:layout_marginEnd="8dp" + android:background="@drawable/item_block_code_multi_select_mode_selector" + android:paddingTop="6dp" + android:layout_marginTop="1dp" + android:layout_marginBottom="1dp" + android:paddingBottom="6dp" + android:paddingStart="4dp" + android:paddingEnd="4dp"> - - - + android:layout_marginTop="1dp" + android:layout_marginBottom="1dp" + android:orientation="vertical"> - \ No newline at end of file + + + + + + \ No newline at end of file diff --git a/core-utils/src/main/java/com/anytypeio/anytype/core_utils/ext/AndroidExtension.kt b/core-utils/src/main/java/com/anytypeio/anytype/core_utils/ext/AndroidExtension.kt index f718e4535e..0654cb6075 100644 --- a/core-utils/src/main/java/com/anytypeio/anytype/core_utils/ext/AndroidExtension.kt +++ b/core-utils/src/main/java/com/anytypeio/anytype/core_utils/ext/AndroidExtension.kt @@ -180,13 +180,14 @@ fun TextView.getCursorOffsetY(): Int? = } } -fun View.indentize(indent: Int, defIndent: Int, margin: Int) = +fun View.indentize(indent: Int, defIndent: Int, margin: Int) { updateLayoutParams { apply { val extra = indent * defIndent leftMargin = margin + extra } } +} fun Fragment.clipboard() : ClipboardManager { return requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager diff --git a/presentation/src/main/java/com/anytypeio/anytype/presentation/page/render/DefaultBlockViewRenderer.kt b/presentation/src/main/java/com/anytypeio/anytype/presentation/page/render/DefaultBlockViewRenderer.kt index c877d8cc14..490118c067 100644 --- a/presentation/src/main/java/com/anytypeio/anytype/presentation/page/render/DefaultBlockViewRenderer.kt +++ b/presentation/src/main/java/com/anytypeio/anytype/presentation/page/render/DefaultBlockViewRenderer.kt @@ -223,7 +223,7 @@ class DefaultBlockViewRenderer( } Content.Text.Style.CODE_SNIPPET -> { counter.reset() - result.add(code(mode, block, content, focus)) + result.add(code(mode, block, content, focus, indent)) if (block.children.isNotEmpty()) { result.addAll( render( @@ -469,12 +469,14 @@ class DefaultBlockViewRenderer( mode: EditorMode, block: Block, content: Content.Text, - focus: Focus + focus: Focus, + indent: Int ): BlockView.Code = BlockView.Code( mode = if (mode == EditorMode.EDITING) BlockView.Mode.EDIT else BlockView.Mode.READ, id = block.id, text = content.text, - isFocused = block.id == focus.id + isFocused = block.id == focus.id, + indent = indent ) private fun highlight( diff --git a/presentation/src/test/java/com/anytypeio/anytype/presentation/page/editor/EditorBackspaceNestedDeleteTest.kt b/presentation/src/test/java/com/anytypeio/anytype/presentation/page/editor/EditorBackspaceNestedDeleteTest.kt index afa04f184d..b594d74e8a 100644 --- a/presentation/src/test/java/com/anytypeio/anytype/presentation/page/editor/EditorBackspaceNestedDeleteTest.kt +++ b/presentation/src/test/java/com/anytypeio/anytype/presentation/page/editor/EditorBackspaceNestedDeleteTest.kt @@ -378,7 +378,7 @@ class EditorBackspaceNestedDeleteTest : EditorPresentationTestSetup() { content = Block.Content.Text( text = MockDataFactory.randomString(), marks = emptyList(), - style = Block.Content.Text.Style.CODE_SNIPPET + style = Block.Content.Text.Style.BULLET ) ) @@ -478,9 +478,11 @@ class EditorBackspaceNestedDeleteTest : EditorPresentationTestSetup() { isFocused = false, text = parent.content().text ), - BlockView.Code( + BlockView.Text.Bulleted( + indent = 1, id = child1.id, isFocused = true, + cursor = child1.content().text.length, text = child1.content().text ), BlockView.Upload.Picture(