Skip to content

Commit

Permalink
refactor(code): extract MarkdownViewer for reusable component
Browse files Browse the repository at this point in the history
Introduce MarkdownViewer to centralize the creation of JEditorPane for markdown rendering. Replace redundant createBaseComponent implementations in TextBlockView with calls to MarkdownViewer.createBaseComponent. Clean up related code and imports.
  • Loading branch information
phodal committed Mar 10, 2025
1 parent 3a33297 commit ef1f6b7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package com.intellij.temporary.gui.block

import cc.unitmesh.devti.gui.chat.message.ChatRole
import cc.unitmesh.devti.sketch.ui.code.MarkdownViewer
import cc.unitmesh.devti.util.parser.convertMarkdownToHtml
import com.intellij.ide.BrowserUtil
import com.intellij.openapi.util.text.StringUtil
Expand Down Expand Up @@ -64,39 +65,12 @@ class TextBlockView(private val block: MessageBlock) : MessageBlockView {
}

companion object {
fun createBaseComponent(): JEditorPane {
val jEditorPane = JEditorPane()
jEditorPane.setContentType("text/html")
val htmlEditorKit = HTMLEditorKitBuilder().withViewFactoryExtensions(
LineSpacingExtension(0.2f)
).build()
htmlEditorKit.getStyleSheet().addRule("p {margin-top: 1px}")

jEditorPane.also {
it.editorKit = htmlEditorKit
it.isEditable = false
it.putClientProperty("JEditorPane.honorDisplayProperties", true)
it.isOpaque = false
it.border = null
it.putClientProperty(
"AccessibleName",
StringUtil.unescapeXmlEntities(StringUtil.stripHtml("", " "))
)
it.text = ""
}

if (jEditorPane.caret != null) {
jEditorPane.setCaretPosition(0)
(jEditorPane.caret as? DefaultCaret)?.updatePolicy = 1
}
return jEditorPane
}
fun createBaseComponent(): JEditorPane = MarkdownViewer.createBaseComponent()
}
}


internal class LineSpacingExtension(val lineSpacing: Float) : ExtendableHTMLViewFactory.Extension {

override operator fun invoke(elem: Element, defaultView: View): View? {
return if (defaultView !is ParagraphView) null
else object : ParagraphView(elem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package com.intellij.temporary.gui.block

import cc.unitmesh.devti.gui.chat.message.ChatRole
import cc.unitmesh.devti.sketch.ui.code.MarkdownViewer
import cc.unitmesh.devti.util.parser.convertMarkdownToHtml
import com.intellij.ide.BrowserUtil
import com.intellij.openapi.util.text.StringUtil
Expand Down Expand Up @@ -64,33 +65,7 @@ class TextBlockView(private val block: MessageBlock) : MessageBlockView {
}

companion object {
fun createBaseComponent(): JEditorPane {
val jEditorPane = JEditorPane()
jEditorPane.setContentType("text/html")
val htmlEditorKit = HTMLEditorKitBuilder().withViewFactoryExtensions(
LineSpacingExtension(0.2f)
).build()
htmlEditorKit.getStyleSheet().addRule("p {margin-top: 1px}")

jEditorPane.also {
it.editorKit = htmlEditorKit
it.isEditable = false
it.putClientProperty("JEditorPane.honorDisplayProperties", true)
it.isOpaque = false
it.border = null
it.putClientProperty(
"AccessibleName",
StringUtil.unescapeXmlEntities(StringUtil.stripHtml("", " "))
)
it.text = ""
}

if (jEditorPane.caret != null) {
jEditorPane.setCaretPosition(0)
(jEditorPane.caret as? DefaultCaret)?.updatePolicy = 1
}
return jEditorPane
}
fun createBaseComponent(): JEditorPane = MarkdownViewer.createBaseComponent()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cc.unitmesh.devti.gui.chat.view


import cc.unitmesh.devti.gui.chat.message.ChatRole
import com.intellij.openapi.actionSystem.DataProvider
import com.intellij.openapi.actionSystem.ex.ActionUtil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiManager
import com.intellij.temporary.gui.block.whenDisposed
import com.intellij.testFramework.LightVirtualFile
import com.intellij.ui.JBColor
import com.intellij.ui.components.JBPanel
import com.intellij.util.concurrency.annotations.RequiresReadLock
import com.intellij.util.ui.JBEmptyBorder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cc.unitmesh.devti.sketch.ui.code

import com.intellij.openapi.util.text.StringUtil
import com.intellij.temporary.gui.block.LineSpacingExtension
import com.intellij.util.ui.HTMLEditorKitBuilder
import javax.swing.JEditorPane
import javax.swing.text.DefaultCaret

object MarkdownViewer {
fun createBaseComponent(): JEditorPane {
val jEditorPane = JEditorPane()
jEditorPane.setContentType("text/html")
val htmlEditorKit = HTMLEditorKitBuilder()
.withViewFactoryExtensions(
LineSpacingExtension(0.2f)
).build()

htmlEditorKit.getStyleSheet().addRule("p {margin-top: 1px}")

jEditorPane.also {
it.editorKit = htmlEditorKit
it.isEditable = false
it.putClientProperty("JEditorPane.honorDisplayProperties", true)
it.isOpaque = false
it.border = null
it.putClientProperty(
"AccessibleName",
StringUtil.unescapeXmlEntities(StringUtil.stripHtml("", " "))
)
it.text = ""
}

if (jEditorPane.caret != null) {
jEditorPane.setCaretPosition(0)
(jEditorPane.caret as? DefaultCaret)?.updatePolicy = 1
}

return jEditorPane
}

}

0 comments on commit ef1f6b7

Please sign in to comment.