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

This commit is contained in:
HapeLee
2025-12-13 02:17:53 +08:00
parent 89adcdc32a
commit 14c309d5c1
4 changed files with 48 additions and 10 deletions
@@ -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 = layout.getLineStart(lineIndex)
val end = layout.getLineEnd(lineIndex)
val start = arguments?.getInt("start_position", -1) ?: -1
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 span =
@@ -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")
}
/**
* 文本选择菜单操作完成
*/
@@ -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()
}