Skip to content

Commit 578231c

Browse files
committed
feat(markdown): add theme-aware styling for code blocks
Set code block background color and font based on the current IDE theme. This ensures consistent appearance with the editor's theme.
1 parent 39779b5 commit 578231c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/code/MarkdownViewer.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package cc.unitmesh.devti.sketch.ui.code
22

3+
import com.intellij.openapi.editor.colors.EditorColorsManager
34
import com.intellij.openapi.util.text.StringUtil
45
import com.intellij.temporary.gui.block.LineSpacingExtension
6+
import com.intellij.ui.BrowserHyperlinkListener
7+
import com.intellij.ui.ColorUtil
58
import com.intellij.util.ui.HTMLEditorKitBuilder
9+
import com.intellij.util.ui.UIUtil
610
import javax.swing.JEditorPane
711
import javax.swing.text.DefaultCaret
812

@@ -15,6 +19,14 @@ object MarkdownViewer {
1519
LineSpacingExtension(0.2f)
1620
).build()
1721

22+
val backgroundColor = UIUtil.getPanelBackground()
23+
val schemeForCurrentUITheme = EditorColorsManager.getInstance().schemeForCurrentUITheme
24+
val editorFontName = schemeForCurrentUITheme.editorFontName
25+
val editorFontSize = schemeForCurrentUITheme.editorFontSize
26+
val fontFamilyAndSize =
27+
"font-family:'" + editorFontName + "'; font-size:" + editorFontSize + "pt;"
28+
val backgroundColorCss = "background-color: #" + ColorUtil.toHex(backgroundColor) + ";"
29+
htmlEditorKit.getStyleSheet().addRule("code { $backgroundColorCss$fontFamilyAndSize}")
1830
htmlEditorKit.getStyleSheet().addRule("p {margin-top: 1px}")
1931

2032
jEditorPane.also {
@@ -35,6 +47,7 @@ object MarkdownViewer {
3547
(jEditorPane.caret as? DefaultCaret)?.updatePolicy = 1
3648
}
3749

50+
jEditorPane.addHyperlinkListener(BrowserHyperlinkListener.INSTANCE);
3851
return jEditorPane
3952
}
4053

0 commit comments

Comments
 (0)