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

This commit is contained in:
HapeLee committed 2025-12-13 02:17:53 +08:00
1 parent 89adcdc32a
commit 14c309d5c1
4 files changed
+48 -10

No files matched your search

@@ -41,8 +41,11 @@ class ContentEditDialog : BaseBottomSheetDialogFragment(R.layout.dialog_content_
val binding by viewBinding(DialogContentEditBinding::bind) val binding by viewBinding(DialogContentEditBinding::bind)
val viewModel by viewModels<ContentEditViewModel>() 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?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
//binding.toolBar.setBackgroundColor(primaryColor)
binding.toolBar.title = ReadBook.curTextChapter?.title binding.toolBar.title = ReadBook.curTextChapter?.title
initMenu() initMenu()
binding.toolBar.setOnClickListener { binding.toolBar.setOnClickListener {
@@ -67,12 +70,11 @@ class ContentEditDialog : BaseBottomSheetDialogFragment(R.layout.dialog_content_
val layout = binding.contentView.layout ?: return@post val layout = binding.contentView.layout ?: return@post
val targetY = binding.contentView.top + val targetY = binding.contentView.top +
layout.getLineTop( layout.getLineTop(
layout.getLineForOffset(ReadBook.durChapterPos) layout.getLineForOffset(targetOffset)
) )
binding.scrollView.smoothScrollTo(0, targetY) binding.scrollView.smoothScrollTo(0, targetY)
highlightSelectedTextTwice()
highlightCurrentLineTwice()
} }
} }
@@ -117,14 +119,16 @@ class ContentEditDialog : BaseBottomSheetDialogFragment(R.layout.dialog_content_
} }
} }
private fun highlightCurrentLineTwice() { private fun highlightSelectedTextTwice() {
val editText = binding.contentView val editText = binding.contentView
val layout = editText.layout ?: return val content = editText.text ?: return
val offset = ReadBook.durChapterPos
val lineIndex = layout.getLineForOffset(offset)
val start = layout.getLineStart(lineIndex) val start = arguments?.getInt("start_position", -1) ?: -1
val end = layout.getLineEnd(lineIndex) val selectedText = arguments?.getString("selected_text") ?: ""
if (start == -1 || selectedText.isEmpty()) return
val end = (start + selectedText.length).coerceAtMost(content.length)
val spannable = editText.text as Spannable val spannable = editText.text as Spannable
val span = val span =
@@ -971,6 +971,15 @@ class ReadBookActivity : BaseReadBookActivity(),
return true 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 -> { R.id.menu_replace -> {
val scopes = arrayListOf<String>() val scopes = arrayListOf<String>()
ReadBook.book?.name?.let { ReadBook.book?.name?.let {
@@ -1006,6 +1015,19 @@ class ReadBookActivity : BaseReadBookActivity(),
return false 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")
}
/** /**
* 文本选择菜单操作完成 * 文本选择菜单操作完成
*/ */
@@ -674,6 +674,14 @@ class ReadView(context: Context, attrs: AttributeSet) :
return curPage.selectedText return curPage.selectedText
} }
fun getSelectTextPos(): Int {
val selectStartPos = curPage.selectStartPos
return curPage.textPage.getPosByLineColumn(
selectStartPos.lineIndex,
selectStartPos.columnIndex
)
}
fun getCurVisiblePage(): TextPage { fun getCurVisiblePage(): TextPage {
return curPage.getCurVisiblePage() return curPage.getCurVisiblePage()
} }
@@ -10,6 +10,10 @@
android:id="@+id/menu_copy" android:id="@+id/menu_copy"
android:title="@android:string/copy" /> android:title="@android:string/copy" />
<item
android:id="@+id/menu_edit"
android:title="@string/edit" />
<item <item
android:id="@+id/menu_bookmark" android:id="@+id/menu_bookmark"
android:title="@string/bookmark" /> android:title="@string/bookmark" />