Skip to content

Commit

Permalink
Markdown Rendering in the What's New Panel (#432)
Browse files Browse the repository at this point in the history
This is a PR for markdown file (.md) rendering and formatting for the
"What's New at Slack!" side panel in the Skate plugin. This change went
through a few iterations because I was having trouble using intelliJ's
markdown plugin in Skate, so I had done custom CSS formatting before.
However, now I was able to use MarkdownJCEFHtmlPanel to properly format
the md files. Here is what it looks like as it depends on the formatting
for the markdown plugin with intelliJ:

<img width="665" alt="Screenshot 2023-07-06 at 5 31 07 PM"
src="https://github.com/slackhq/slack-gradle-plugin/assets/67719108/d8198ce1-224d-4e31-8f47-f6c4df60776d">


<img width="466" alt="Screenshot 2023-07-06 at 6 02 59 PM"
src="https://github.com/slackhq/slack-gradle-plugin/assets/67719108/1d74e9dc-0985-4277-ac34-e158353abb4c">

<!--
  ⬆ 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!
-->

---------

Co-authored-by: Zac Sweers <[email protected]>
  • Loading branch information
kateliu20 and ZacSweers authored Jul 7, 2023
1 parent 0891b5b commit 19f163e
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 60 deletions.
2 changes: 1 addition & 1 deletion skate-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ intellij {
version.set("2022.2.5")
type.set("IC") // Target IDE Platform

plugins.set(listOf(/* Plugin Dependencies */ ))
plugins.add("org.intellij.plugins.markdown")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ object ChangelogParser {
}
}
.trimEnd()

// If the changelog substring is blank, set it as null
// If the previous entry equals to the latest entry, or if no date is found,
// use the previous entry date as the latest date instead of the current date.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ package com.slack.sgp.intellij
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.wm.ToolWindowAnchor
import com.intellij.openapi.wm.ToolWindowManager
import com.slack.sgp.intellij.ui.WhatsNewPanelFactory
import java.util.function.Supplier

interface SkateProjectService {
fun showWhatsNewWindow()
}

/**
* This file's intended purpose is to pass in the changelog file from the file path into the What's
* New UI of the Skate Plugin
*/
class SkateProjectServiceImpl(private val project: Project) : SkateProjectService {

override fun showWhatsNewWindow() {
// TODO
// Make the file configurable?
// Only show when changed
// Only show latest changes
val settings = project.service<SkatePluginSettings>()
Expand All @@ -40,13 +45,21 @@ class SkateProjectServiceImpl(private val project: Project) : SkateProjectServic
val changeLogFile = VfsUtil.findRelativeFile(projectDir, settings.whatsNewFilePath) ?: return
val changeLogString = VfsUtil.loadText(changeLogFile)
val toolWindowManager = ToolWindowManager.getInstance(project)

toolWindowManager.invokeLater {
val toolWindow =
toolWindowManager.registerToolWindow("skate-whats-new") {
stripeTitle = Supplier { "What's New in Slack!" }
anchor = ToolWindowAnchor.RIGHT
}
WhatsNewPanelFactory().createToolWindowContent(toolWindow, changeLogString)

// The Disposable is necessary to prevent a substantial memory leak while working with
// MarkdownJCEFHtmlPanel
val parentDisposable = Disposer.newDisposable()

WhatsNewPanelFactory()
.createToolWindowContent(toolWindow, project, changeLogString, parentDisposable)

toolWindow.show()
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2023 Slack Technologies, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.slack.sgp.intellij.ui

import com.intellij.openapi.Disposable
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.wm.ToolWindow
import com.intellij.testFramework.LightVirtualFile
import com.intellij.ui.content.ContentFactory
import com.intellij.ui.content.ContentManagerEvent
import com.intellij.ui.content.ContentManagerListener
import java.awt.BorderLayout
import javax.swing.JComponent
import javax.swing.JPanel
import org.intellij.lang.annotations.Language
import org.intellij.plugins.markdown.ui.preview.html.MarkdownUtil
import org.intellij.plugins.markdown.ui.preview.jcef.MarkdownJCEFHtmlPanel

/**
* The WhatsNewPanelFactory class takes the markdown file string from SkateService and displays it
* in a tool window. It uses MarkdownJCEFHtmlPanel and has dependency on intellij markdown plugin to
* properly format the markdown file and its contents
*/
class WhatsNewPanelFactory : DumbAware {

// Function that creates the tool window
fun createToolWindowContent(
toolWindow: ToolWindow,
project: Project,
markdownFileString: String,
parentDisposable: Disposable
) {

val toolWindowContent = WhatsNewPanelContent(project, markdownFileString, parentDisposable)
val content =
ContentFactory.getInstance().createContent(toolWindowContent.contentPanel, "", false)
toolWindow.contentManager.addContent(content)
toolWindow.contentManager.addContentManagerListener(
object : ContentManagerListener {
override fun contentRemoved(event: ContentManagerEvent) {
if (event.content.component == toolWindowContent.contentPanel) {
Disposer.dispose(parentDisposable)
toolWindow.contentManager.removeContentManagerListener(this)
}
}
}
)
}

private class WhatsNewPanelContent(
project: Project,
@Language("Markdown") markdownFileString: String,
parentDisposable: Disposable
) {

// Actual panel box for What's New at Slack
val contentPanel: JPanel =
JPanel().apply {
layout = BorderLayout(0, 20)
add(createWhatsNewPanel(project, markdownFileString, parentDisposable), BorderLayout.CENTER)
}

// Control Panel that takes in the current project, markdown string, and a Disposable.
private fun createWhatsNewPanel(
project: Project,
@Language("Markdown") markdownFileString: String,
parentDisposable: Disposable
): JComponent {
// to take in the parsed Changelog:
// val parsedChangelog = ChangelogParser.readFile(markdownFileString)
// then, pass in parsedChangelog instead of markdownFileString

val file = LightVirtualFile("changelog.md", markdownFileString)

val panel = MarkdownJCEFHtmlPanel(project, file)
Disposer.register(parentDisposable, panel)
val html = runReadAction {
MarkdownUtil.generateMarkdownHtml(file, markdownFileString, project)
}

panel.setHtml(html, 0)
return panel.component
}
}
}
1 change: 1 addition & 0 deletions skate-plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<!-- Product and plugin compatibility requirements.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html -->
<depends>com.intellij.modules.platform</depends>
<depends>org.intellij.plugins.markdown</depends>

<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
Expand Down

0 comments on commit 19f163e

Please sign in to comment.