Просмотр исходного кода

[新增] 文字选择菜单可进入编辑正文界面 #457

HapeLee 11 месяцев назад
Родитель
Сommit
14c309d5c1

+ 14 - 10
app/src/main/java/io/legado/app/ui/book/read/ContentEditDialog.kt

@@ -41,8 +41,11 @@ class ContentEditDialog : BaseBottomSheetDialogFragment(R.layout.dialog_content_
     val binding by viewBinding(DialogContentEditBinding::bind)
     val viewModel by viewModels<ContentEditViewModel>()
 
+    private val targetOffset: Int
+        get() = arguments?.getInt("start_position", -1).takeIf { it != -1 }
+            ?: ReadBook.durChapterPos
+
     override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
-        //binding.toolBar.setBackgroundColor(primaryColor)
         binding.toolBar.title = ReadBook.curTextChapter?.title
         initMenu()
         binding.toolBar.setOnClickListener {
@@ -67,12 +70,11 @@ class ContentEditDialog : BaseBottomSheetDialogFragment(R.layout.dialog_content_
                 val layout = binding.contentView.layout ?: return@post
                 val targetY = binding.contentView.top +
                         layout.getLineTop(
-                            layout.getLineForOffset(ReadBook.durChapterPos)
+                            layout.getLineForOffset(targetOffset)
                         )
 
                 binding.scrollView.smoothScrollTo(0, targetY)
-
-                highlightCurrentLineTwice()
+                highlightSelectedTextTwice()
             }
         }
 
@@ -117,14 +119,16 @@ class ContentEditDialog : BaseBottomSheetDialogFragment(R.layout.dialog_content_
         }
     }
 
-    private fun highlightCurrentLineTwice() {
+    private fun highlightSelectedTextTwice() {
         val editText = binding.contentView
-        val layout = editText.layout ?: return
-        val offset = ReadBook.durChapterPos
-        val lineIndex = layout.getLineForOffset(offset)
+        val content = editText.text ?: return
+
+        val start = arguments?.getInt("start_position", -1) ?: -1
+        val selectedText = arguments?.getString("selected_text") ?: ""
+
+        if (start == -1 || selectedText.isEmpty()) return
 
-        val start = layout.getLineStart(lineIndex)
-        val end = layout.getLineEnd(lineIndex)
+        val end = (start + selectedText.length).coerceAtMost(content.length)
 
         val spannable = editText.text as Spannable
         val span =

+ 22 - 0
app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt

@@ -971,6 +971,15 @@ class ReadBookActivity : BaseReadBookActivity(),
                 return true
             }
 
+            R.id.menu_edit -> {
+                val startPos = binding.readView.getSelectTextPos() + ReadBook.durChapterPos - 13
+                showDialogFragment<ContentEditDialog> {
+                    putInt("start_position", startPos)
+                    putString("selected_text", binding.readView.getSelectText())
+                }
+                return true
+            }
+
             R.id.menu_replace -> {
                 val scopes = arrayListOf<String>()
                 ReadBook.book?.name?.let {
@@ -1006,6 +1015,19 @@ class ReadBookActivity : BaseReadBookActivity(),
         return false
     }
 
+    private fun onEditTextAction(selectedText: String, startPos: Int) {
+        val bundle = Bundle().apply {
+            putString("selected_text", selectedText)
+            putInt("start_position", startPos)
+        }
+
+        val dialog = ContentEditDialog().apply {
+            arguments = bundle
+        }
+
+        dialog.show(supportFragmentManager, "ContentEditDialog")
+    }
+
     /**
      * 文本选择菜单操作完成
      */

+ 8 - 0
app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt

@@ -674,6 +674,14 @@ class ReadView(context: Context, attrs: AttributeSet) :
         return curPage.selectedText
     }
 
+    fun getSelectTextPos(): Int {
+        val selectStartPos = curPage.selectStartPos
+        return curPage.textPage.getPosByLineColumn(
+            selectStartPos.lineIndex,
+            selectStartPos.columnIndex
+        )
+    }
+
     fun getCurVisiblePage(): TextPage {
         return curPage.getCurVisiblePage()
     }

+ 4 - 0
app/src/main/res/menu/content_select_action.xml

@@ -10,6 +10,10 @@
         android:id="@+id/menu_copy"
         android:title="@android:string/copy" />
 
+    <item
+        android:id="@+id/menu_edit"
+        android:title="@string/edit" />
+
     <item
         android:id="@+id/menu_bookmark"
         android:title="@string/bookmark" />