270 lines
13 KiB
Kotlin
270 lines
13 KiB
Kotlin
package com.gh.base
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.app.Activity
|
|
import android.content.ClipboardManager
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.text.TextUtils
|
|
import android.view.View
|
|
import android.webkit.JavascriptInterface
|
|
import butterknife.OnClick
|
|
import com.gh.common.util.DialogUtils
|
|
import com.gh.common.util.MtaHelper
|
|
import com.gh.common.view.RichEditor
|
|
import com.gh.gamecenter.R
|
|
import com.gh.gamecenter.entity.GameEntity
|
|
import com.gh.gamecenter.entity.MyVideoEntity
|
|
import com.gh.gamecenter.qa.editor.GameActivity
|
|
import com.gh.gamecenter.qa.editor.InsertAnswerWrapperActivity
|
|
import com.gh.gamecenter.qa.editor.InsertArticleWrapperActivity
|
|
import com.gh.gamecenter.qa.editor.VideoActivity
|
|
import com.gh.gamecenter.qa.entity.AnswerEntity
|
|
import com.gh.gamecenter.qa.entity.ArticleEntity
|
|
import com.gh.gamecenter.qa.entity.EditorInsertEntity
|
|
import com.halo.assistant.HaloApp
|
|
import com.lightgame.utils.Utils
|
|
import com.lightgame.view.CheckableImageView
|
|
import kotterknife.bindView
|
|
|
|
abstract class BaseRichEditorActivity : ToolBarActivity() {
|
|
|
|
val mRichEditor by bindView<RichEditor>(R.id.rich_editor)
|
|
|
|
private val mEditorFont by bindView<CheckableImageView>(R.id.editor_font)
|
|
private val mEditorLink by bindView<CheckableImageView>(R.id.editor_link)
|
|
private val mEditorParagraph by bindView<CheckableImageView>(R.id.editor_paragraph)
|
|
private val mEditorFontBold by bindView<CheckableImageView>(R.id.editor_font_bold)
|
|
private val mEditorFontItalic by bindView<CheckableImageView>(R.id.editor_font_italic)
|
|
private val mEditorFontStrikeThrough by bindView<CheckableImageView>(R.id.editor_font_strikethrough)
|
|
private val mEditorParagraphH1 by bindView<CheckableImageView>(R.id.editor_paragraph_h1)
|
|
private val mEditorParagraphH2 by bindView<CheckableImageView>(R.id.editor_paragraph_h2)
|
|
private val mEditorParagraphH3 by bindView<CheckableImageView>(R.id.editor_paragraph_h3)
|
|
private val mEditorParagraphH4 by bindView<CheckableImageView>(R.id.editor_paragraph_h4)
|
|
private val mEditorParagraphQuote by bindView<CheckableImageView>(R.id.editor_paragraph_quote)
|
|
private val mEditorFontContainer by bindView<View>(R.id.editor_font_container)
|
|
private val mEditorParagraphContainer by bindView<View>(R.id.editor_paragraph_container)
|
|
private val mEditorLinkContainer by bindView<View>(R.id.editor_link_container)
|
|
private val mEditorInsertDetail by bindView<View>(R.id.editor_insert_detail)
|
|
|
|
private var mCurrentParagraphStyle = ""
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
DialogUtils.fixWebViewKeyboardNotWorking(this)
|
|
if (resultCode != Activity.RESULT_OK) return
|
|
var insertData: EditorInsertEntity? = null
|
|
when (requestCode) {
|
|
INSERT_ANSWER_CODE -> {
|
|
val answer = data?.getParcelableExtra<AnswerEntity>(AnswerEntity::class.java.simpleName)
|
|
if (answer != null) insertData = EditorInsertEntity.transform(answer)
|
|
}
|
|
INSERT_ARTICLE_CODE -> {
|
|
val article = data?.getParcelableExtra<ArticleEntity>(ArticleEntity::class.java.simpleName)
|
|
if (article != null) insertData = EditorInsertEntity.transform(article)
|
|
}
|
|
INSERT_GAME_CODE -> {
|
|
val game = data?.getParcelableExtra<GameEntity>(GameEntity::class.java.simpleName)
|
|
if (game != null) insertData = EditorInsertEntity.transform(game)
|
|
}
|
|
VideoActivity.INSERT_VIDEO_CODE -> {
|
|
val video = data?.getParcelableExtra<MyVideoEntity>(MyVideoEntity::class.java.simpleName)
|
|
if (video != null) mRichEditor.insertCustomVideo(video)
|
|
return
|
|
}
|
|
}
|
|
|
|
mRichEditor.insertCustomStyleLink(insertData)
|
|
}
|
|
|
|
@SuppressLint("AddJavascriptInterface")
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
mRichEditor.setPadding(20, 15, 20, 15)
|
|
// 防止个别手机在Js里无法获取粘贴内容
|
|
mRichEditor.addJavascriptInterface(OnPasteListener(), "onPasteListener")
|
|
mRichEditor.addJavascriptInterface(OnCursorChangeListener(), "OnCursorChangeListener")
|
|
mRichEditor.setInputEnabled(true)
|
|
}
|
|
|
|
@OnClick(R.id.editor_image, R.id.editor_font, R.id.editor_link, R.id.editor_paragraph,
|
|
R.id.editor_font_bold, R.id.editor_font_italic, R.id.editor_font_strikethrough,
|
|
R.id.editor_paragraph_h1, R.id.editor_paragraph_h2, R.id.editor_paragraph_h3,
|
|
R.id.editor_paragraph_h4, R.id.editor_font_container, R.id.editor_paragraph_container,
|
|
R.id.editor_paragraph_quote, R.id.editor_link_answer, R.id.editor_link_article,
|
|
R.id.editor_link_game, R.id.editor_link_video)
|
|
fun onRichClick(view: View) {
|
|
when (view.id) {
|
|
R.id.editor_font -> {
|
|
mEditorFont.isChecked = !mEditorFont.isChecked
|
|
mEditorParagraph.isChecked = false
|
|
mEditorLink.isChecked = false
|
|
mEditorFontContainer.visibility = if (mEditorFont.isChecked) View.VISIBLE else View.GONE
|
|
mEditorParagraphContainer.visibility = if (!mEditorFont.isChecked) View.VISIBLE else View.GONE
|
|
mEditorLinkContainer.visibility = if (!mEditorFont.isChecked) View.VISIBLE else View.GONE
|
|
mEditorInsertDetail.visibility = mEditorFontContainer.visibility
|
|
}
|
|
R.id.editor_paragraph -> {
|
|
mEditorParagraph.isChecked = !mEditorParagraph.isChecked
|
|
mEditorFont.isChecked = false
|
|
mEditorLink.isChecked = false
|
|
mEditorParagraphContainer.visibility = if (mEditorParagraph.isChecked) View.VISIBLE else View.GONE
|
|
mEditorFontContainer.visibility = if (!mEditorParagraph.isChecked) View.VISIBLE else View.GONE
|
|
mEditorLinkContainer.visibility = if (!mEditorParagraph.isChecked) View.VISIBLE else View.GONE
|
|
mEditorInsertDetail.visibility = mEditorParagraphContainer.visibility
|
|
}
|
|
R.id.editor_link -> {
|
|
mEditorLink.isChecked = !mEditorLink.isChecked
|
|
mEditorFont.isChecked = false
|
|
mEditorParagraph.isChecked = false
|
|
mEditorLinkContainer.visibility = if (mEditorLink.isChecked) View.VISIBLE else View.GONE
|
|
mEditorParagraphContainer.visibility = if (!mEditorLink.isChecked) View.VISIBLE else View.GONE
|
|
mEditorFontContainer.visibility = if (!mEditorLink.isChecked) View.VISIBLE else View.GONE
|
|
mEditorInsertDetail.visibility = mEditorLinkContainer.visibility
|
|
}
|
|
R.id.editor_font_bold -> {
|
|
mEditorFontBold.isChecked = !mEditorFontBold.isChecked
|
|
mRichEditor.setBold()
|
|
if (mEditorFontBold.isChecked) {
|
|
MtaHelper.onEvent(mtaEventName(), "文字样式", "文字样式-加粗")
|
|
}
|
|
}
|
|
R.id.editor_font_italic -> {
|
|
mEditorFontItalic.isChecked = !mEditorFontItalic.isChecked
|
|
mRichEditor.setItalic()
|
|
if (mEditorFontItalic.isChecked) {
|
|
MtaHelper.onEvent(mtaEventName(), "文字样式", "文字样式-斜体")
|
|
}
|
|
}
|
|
R.id.editor_font_strikethrough -> {
|
|
mEditorFontStrikeThrough.isChecked = !mEditorFontStrikeThrough.isChecked
|
|
mRichEditor.setStrikeThrough()
|
|
|
|
if (mEditorFontStrikeThrough.isChecked) {
|
|
MtaHelper.onEvent(mtaEventName(), "文字样式", "文字样式-删除线")
|
|
}
|
|
}
|
|
R.id.editor_paragraph_h1 -> {
|
|
if (mEditorParagraphH1.isChecked) {
|
|
mRichEditor.formatBlock()
|
|
} else {
|
|
MtaHelper.onEvent(mtaEventName(), "段落样式", "段落样式-1级标题")
|
|
mRichEditor.setHeading(1)
|
|
}
|
|
mEditorParagraphH1.isChecked = !mEditorParagraphH1.isChecked
|
|
}
|
|
R.id.editor_paragraph_h2 -> {
|
|
if (mEditorParagraphH2.isChecked) {
|
|
mRichEditor.formatBlock()
|
|
} else {
|
|
MtaHelper.onEvent(mtaEventName(), "段落样式", "段落样式-2级标题")
|
|
mRichEditor.setHeading(2)
|
|
}
|
|
mEditorParagraphH2.isChecked = !mEditorParagraphH2.isChecked
|
|
}
|
|
R.id.editor_paragraph_h3 -> {
|
|
if (mEditorParagraphH3.isChecked) {
|
|
mRichEditor.formatBlock()
|
|
} else {
|
|
MtaHelper.onEvent(mtaEventName(), "段落样式", "段落样式-3级标题")
|
|
mRichEditor.setHeading(3)
|
|
}
|
|
mEditorParagraphH3.isChecked = !mEditorParagraphH3.isChecked
|
|
}
|
|
R.id.editor_paragraph_h4 -> {
|
|
if (mEditorParagraphH4.isChecked) {
|
|
mRichEditor.formatBlock()
|
|
} else {
|
|
MtaHelper.onEvent(mtaEventName(), "段落样式", "段落样式-4级标题")
|
|
mRichEditor.setHeading(4)
|
|
}
|
|
mEditorParagraphH4.isChecked = !mEditorParagraphH4.isChecked
|
|
}
|
|
R.id.editor_paragraph_quote -> {
|
|
if (mEditorParagraphQuote.isChecked) {
|
|
mRichEditor.formatBlock()
|
|
} else {
|
|
MtaHelper.onEvent(mtaEventName(), "段落样式", "段落样式-引用")
|
|
mRichEditor.setBlockquote()
|
|
}
|
|
mEditorParagraphQuote.isChecked = !mEditorParagraphQuote.isChecked
|
|
}
|
|
R.id.editor_link_answer -> {
|
|
MtaHelper.onEvent(mtaEventName(), "插入链接", "插入链接-回答")
|
|
startActivityForResult(InsertAnswerWrapperActivity.getIntent(this), INSERT_ANSWER_CODE)
|
|
}
|
|
R.id.editor_link_article -> {
|
|
MtaHelper.onEvent(mtaEventName(), "插入链接", "插入链接-文章")
|
|
startActivityForResult(InsertArticleWrapperActivity.getIntent(this), INSERT_ARTICLE_CODE)
|
|
}
|
|
R.id.editor_link_game -> {
|
|
MtaHelper.onEvent(mtaEventName(), "插入链接", "插入链接-游戏")
|
|
startActivityForResult(GameActivity.getIntent(this, GameActivity.INSERT_GAME_TITLE), INSERT_GAME_CODE)
|
|
}
|
|
R.id.editor_link_video -> {
|
|
MtaHelper.onEvent(mtaEventName(), "插入链接", "插入链接-视频")
|
|
startActivityForResult(VideoActivity.getIntent(this), VideoActivity.INSERT_VIDEO_CODE)
|
|
}
|
|
}
|
|
}
|
|
|
|
private inner class OnCursorChangeListener {
|
|
@JavascriptInterface
|
|
fun onElements(elements: String) {
|
|
Utils.log("-----------------------")
|
|
Utils.log(elements)
|
|
Utils.log(mRichEditor.html)
|
|
Utils.log("-----------------------")
|
|
|
|
mCurrentParagraphStyle = when {
|
|
elements.contains(ELEMENT_PARAGRAPH_QUOTE) -> ELEMENT_PARAGRAPH_QUOTE
|
|
elements.contains(ELEMENT_PARAGRAPH_P) -> ELEMENT_PARAGRAPH_P
|
|
else -> ""
|
|
}
|
|
|
|
mBaseHandler.post {
|
|
mEditorFontBold.isChecked = elements.contains(ELEMENT_NAME_BOLD)
|
|
mEditorFontItalic.isChecked = elements.contains(ELEMENT_NAME_ITALIC)
|
|
mEditorFontStrikeThrough.isChecked = elements.contains(ELEMENT_NAME_STRIKE)
|
|
mEditorParagraphH1.isChecked = elements.contains(ELEMENT_PARAGRAPH_H1)
|
|
mEditorParagraphH2.isChecked = elements.contains(ELEMENT_PARAGRAPH_H2)
|
|
mEditorParagraphH3.isChecked = elements.contains(ELEMENT_PARAGRAPH_H3)
|
|
mEditorParagraphH4.isChecked = elements.contains(ELEMENT_PARAGRAPH_H4)
|
|
mEditorParagraphQuote.isChecked = elements.contains(ELEMENT_PARAGRAPH_QUOTE)
|
|
}
|
|
}
|
|
}
|
|
|
|
private inner class OnPasteListener {
|
|
@JavascriptInterface
|
|
fun onPaste() {
|
|
val clipboard =
|
|
HaloApp.getInstance().application.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
|
val clipText = clipboard.text.toString()
|
|
if (!TextUtils.isEmpty(clipText)) {
|
|
// 替换换行符号否则 插入失败
|
|
val text = clipText.replace("[ ]".toRegex(), " ").replace("[\r\n]".toRegex(), "<br/>")
|
|
mBaseHandler.post { mRichEditor.insertHtml(text) }
|
|
}
|
|
}
|
|
}
|
|
|
|
abstract fun mtaEventName(): String
|
|
|
|
companion object {
|
|
const val ELEMENT_NAME_BOLD = " b "
|
|
const val ELEMENT_NAME_ITALIC = " i "
|
|
const val ELEMENT_NAME_STRIKE = " strike "
|
|
const val ELEMENT_PARAGRAPH_H1 = " h1 "
|
|
const val ELEMENT_PARAGRAPH_H2 = " h2 "
|
|
const val ELEMENT_PARAGRAPH_H3 = " h3 "
|
|
const val ELEMENT_PARAGRAPH_H4 = " h4 "
|
|
const val ELEMENT_PARAGRAPH_P = " p "
|
|
const val ELEMENT_PARAGRAPH_QUOTE = " blockquote "
|
|
const val INSERT_ANSWER_CODE = 411
|
|
const val INSERT_ARTICLE_CODE = 412
|
|
const val INSERT_GAME_CODE = 413
|
|
}
|
|
} |