Skip to content

Commit

Permalink
Add fallback Html panel support for markdown rendering (#522)
Browse files Browse the repository at this point in the history
Android Studio frustratingly ships with a broken version of the embedded
markdown plugin, which prevents use of the modern JCEF API we were
using. This adds a fallback implementation for when that's not available
and just uses a simpler html panel. Code blocks don't work and it's a
little plain, but it's otherwise ok.

<img width="1512" alt="Screenshot 2023-08-10 at 12 56 05 AM"
src="https://github.com/slackhq/slack-gradle-plugin/assets/1361086/6df182be-dcdd-49c8-b01c-12a1052c1b08">


<!--
  ⬆ Put your description above this! ⬆

  Please be descriptive and detailed.
  
Please read our [Contributing
Guidelines](https://github.com/tinyspeck/slack-gradle-plugin/blob/main/.github/CONTRIBUTING.md)
and [Code of Conduct](https://slackhq.github.io/code-of-conduct).

Don't worry about deleting this, it's not visible in the PR!
-->
  • Loading branch information
ZacSweers authored Aug 10, 2023
1 parent ab1014d commit 891922d
Showing 1 changed file with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.slack.sgp.intellij.ui
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
Expand All @@ -26,6 +27,12 @@ import com.intellij.testFramework.LightVirtualFile
import com.intellij.ui.content.ContentFactory
import com.intellij.ui.content.ContentManagerEvent
import com.intellij.ui.content.ContentManagerListener
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.dsl.gridLayout.HorizontalAlign
import com.intellij.ui.dsl.gridLayout.VerticalAlign
import com.intellij.ui.jcef.JBCefApp
import com.intellij.util.ui.HtmlPanel
import com.intellij.util.ui.JBUI
import com.slack.sgp.intellij.ChangelogJournal
import com.slack.sgp.intellij.ChangelogParser
import java.awt.BorderLayout
Expand Down Expand Up @@ -71,6 +78,8 @@ class WhatsNewPanelFactory : DumbAware {
parentDisposable: Disposable
) {

private val logger = logger<WhatsNewPanelContent>()

// Actual panel box for "What's New in Slack!"
val contentPanel: JPanel =
JPanel().apply {
Expand All @@ -91,15 +100,49 @@ class WhatsNewPanelFactory : DumbAware {

val file = LightVirtualFile("changelog.md", changeLogContent.changeLogString ?: "")

val panel = MarkdownJCEFHtmlPanel(project, file)
Disposer.register(parentDisposable, panel)

val html = runReadAction {
MarkdownUtil.generateMarkdownHtml(file, changeLogContent.changeLogString ?: "", project)
}

panel.setHtml(html, 0)
return panel.component
// We have to support two different modes here: the modern JCEF-based one and the legacy
// Legacy is because Android Studio ships with a broken markdown plugin that doesn't work with
// JCEF
// https://issuetracker.google.com/issues/159933628#comment19
val panel =
if (JBCefApp.isSupported()) {
logger.debug("Using JCEFHtmlPanelProvider")
MarkdownJCEFHtmlPanel(project, file)
.apply {
Disposer.register(parentDisposable, this)
setHtml(html, 0)
}
.component
} else {
logger.debug("Using HtmlPanel")
val htmlPanel =
object : HtmlPanel() {
init {
isVisible = true
// Padding
border = JBUI.Borders.empty(16)
update()
}

override fun getBody(): String {
return html
}
}
panel {
row {
cell(htmlPanel)
.horizontalAlign(HorizontalAlign.FILL)
.verticalAlign(VerticalAlign.FILL)
}
.resizableRow()
}
}

return panel
}
}
}

0 comments on commit 891922d

Please sign in to comment.