diff --git a/.idea/modules.xml b/.idea/modules.xml
index 57c1c34e1999..465398a55da3 100644
--- a/.idea/modules.xml
+++ b/.idea/modules.xml
@@ -526,6 +526,7 @@
+
diff --git a/platform/build-scripts/src/org/jetbrains/intellij/build/CommunityLibraryLicenses.kt b/platform/build-scripts/src/org/jetbrains/intellij/build/CommunityLibraryLicenses.kt
index 4d6a28e74318..a7c7b922dbf0 100644
--- a/platform/build-scripts/src/org/jetbrains/intellij/build/CommunityLibraryLicenses.kt
+++ b/platform/build-scripts/src/org/jetbrains/intellij/build/CommunityLibraryLicenses.kt
@@ -583,7 +583,7 @@ object CommunityLibraryLicenses {
)
.apache("https://github.com/JetBrains/jewel/blob/master/LICENSE")
.suppliedByOrganizations(Suppliers.JETBRAINS),
- LibraryLicense(name = "Jetbrains Jewel Markdown LaF Standalone",
+ LibraryLicense(name = "Jetbrains Jewel Markdown IDE LaF Bridge",
url = "https://github.com/JetBrains/jewel",
libraryName= "jetbrains-jewel-markdown-laf-bridge-styling",
)
diff --git a/platform/compose/api-dump.txt b/platform/compose/api-dump.txt
index e69de29bb2d1..b710189eef48 100644
--- a/platform/compose/api-dump.txt
+++ b/platform/compose/api-dump.txt
@@ -0,0 +1,2 @@
+f:com.intellij.platform.compose.JBComposePanelKt
+- *sf:JBComposePanel(kotlin.jvm.functions.Function2):javax.swing.JComponent
diff --git a/platform/compose/src/com/intellij/platform/compose/JBComposePanel.kt b/platform/compose/src/com/intellij/platform/compose/JBComposePanel.kt
index 3b2ddc3800c4..c9b584fb2e59 100644
--- a/platform/compose/src/com/intellij/platform/compose/JBComposePanel.kt
+++ b/platform/compose/src/com/intellij/platform/compose/JBComposePanel.kt
@@ -22,7 +22,7 @@ import javax.swing.JComponent
@Suppress("FunctionName")
@OptIn(ExperimentalComposeUiApi::class, ExperimentalJewelApi::class)
@Experimental
-internal fun JBComposePanel(
+fun JBComposePanel(
content: @Composable () -> Unit
): JComponent {
if (ApplicationManager.getApplication().isInternal) {
diff --git a/plugins/markdown/compose/intellij.markdown.compose.preview.iml b/plugins/markdown/compose/intellij.markdown.compose.preview.iml
new file mode 100644
index 000000000000..6788c802dc3e
--- /dev/null
+++ b/plugins/markdown/compose/intellij.markdown.compose.preview.iml
@@ -0,0 +1,71 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ $MAVEN_REPOSITORY$/org/jetbrains/compose/compiler/compiler-hosted/1.5.14/compiler-hosted-1.5.14.jar
+
+
+ plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=1.9.25
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 44f90aace7a3f8b9c811294f7705a4591d169f13b5d2299eed9f922be30dafce
+
+
+ 422b42dab33648dcb2b413a06707e100c6e70470918a42a1d760e4b02a1401a3
+
+
+ 3417909f2997bc8c61d90d64c6af29f4a3f5b6729caceaef97221ceac93df814
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/plugins/markdown/compose/src/main/kotlin/com/intellij/markdown/compose/preview/ComposePanelProvider.kt b/plugins/markdown/compose/src/main/kotlin/com/intellij/markdown/compose/preview/ComposePanelProvider.kt
new file mode 100644
index 000000000000..1c7f080203a6
--- /dev/null
+++ b/plugins/markdown/compose/src/main/kotlin/com/intellij/markdown/compose/preview/ComposePanelProvider.kt
@@ -0,0 +1,33 @@
+// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
+package com.intellij.markdown.compose.preview
+
+import com.intellij.idea.AppMode
+import com.intellij.openapi.project.Project
+import com.intellij.openapi.util.registry.Registry
+import com.intellij.openapi.vfs.VirtualFile
+import org.intellij.plugins.markdown.ui.preview.MarkdownHtmlPanel
+import org.intellij.plugins.markdown.ui.preview.MarkdownHtmlPanelProvider
+import org.intellij.plugins.markdown.ui.preview.MarkdownHtmlPanelProvider.AvailabilityInfo
+import org.intellij.plugins.markdown.ui.preview.MarkdownHtmlPanelProvider.ProviderInfo
+import org.jetbrains.jewel.foundation.ExperimentalJewelApi
+
+@OptIn(ExperimentalJewelApi::class)
+private class ComposePanelProvider : MarkdownHtmlPanelProvider() {
+
+ override fun createHtmlPanel(): MarkdownHtmlPanel {
+ return MarkdownComposePanel()
+ }
+
+ override fun createHtmlPanel(project: Project, virtualFile: VirtualFile): MarkdownHtmlPanel {
+ return MarkdownComposePanel(project, virtualFile)
+ }
+
+ override fun isAvailable(): AvailabilityInfo {
+ if (Registry.`is`("markdown.experimental.use.compose.for.preview", false) && !AppMode.isRemoteDevHost()) {
+ return AvailabilityInfo.AVAILABLE
+ }
+ return AvailabilityInfo.UNAVAILABLE
+ }
+
+ override fun getProviderInfo() = ProviderInfo("Compose-based", ComposePanelProvider::class.java.name)
+}
diff --git a/plugins/markdown/compose/src/main/kotlin/com/intellij/markdown/compose/preview/MarkdownComposePanel.kt b/plugins/markdown/compose/src/main/kotlin/com/intellij/markdown/compose/preview/MarkdownComposePanel.kt
new file mode 100644
index 000000000000..9cb6a2d34a9b
--- /dev/null
+++ b/plugins/markdown/compose/src/main/kotlin/com/intellij/markdown/compose/preview/MarkdownComposePanel.kt
@@ -0,0 +1,115 @@
+// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
+package com.intellij.markdown.compose.preview
+
+import androidx.compose.foundation.ScrollState
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.rememberScrollState
+import androidx.compose.foundation.rememberScrollbarAdapter
+import androidx.compose.foundation.verticalScroll
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.collectAsState
+import androidx.compose.runtime.getValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.unit.dp
+import com.intellij.ide.BrowserUtil
+import com.intellij.openapi.project.Project
+import com.intellij.openapi.util.UserDataHolder
+import com.intellij.openapi.util.UserDataHolderBase
+import com.intellij.openapi.vfs.VirtualFile
+import com.intellij.platform.compose.JBComposePanel
+import org.intellij.plugins.markdown.ui.preview.MarkdownHtmlPanel
+import org.intellij.plugins.markdown.ui.preview.MarkdownHtmlPanelEx
+import org.intellij.plugins.markdown.ui.preview.MarkdownUpdateHandler
+import org.intellij.plugins.markdown.ui.preview.MarkdownUpdateHandler.PreviewRequest
+import org.jetbrains.annotations.ApiStatus
+import org.jetbrains.jewel.foundation.ExperimentalJewelApi
+import org.jetbrains.jewel.intui.markdown.bridge.ProvideMarkdownStyling
+import org.jetbrains.jewel.markdown.Markdown
+import org.jetbrains.jewel.ui.component.VerticalScrollbar
+import javax.swing.JComponent
+
+@ExperimentalJewelApi
+class MarkdownComposePanel(
+ private val project: Project?,
+ private val virtualFile: VirtualFile?,
+ private val updateHandler: MarkdownUpdateHandler = MarkdownUpdateHandler.Debounced()
+) : MarkdownHtmlPanelEx, UserDataHolder by UserDataHolderBase() {
+
+ constructor() : this(null, null)
+
+ private val panelComponent by lazy {
+ JBComposePanel {
+ // TODO temporary styling, we will likely need our own in the future for JCEF-like rendering
+ ProvideMarkdownStyling {
+ MarkdownPanel()
+ }
+ }
+ }
+
+ @Suppress("FunctionName")
+ @Composable
+ private fun MarkdownPanel() {
+ Box {
+ val scrollState = rememberScrollState(0)
+ MarkdownPreviewPanel(scrollState)
+ VerticalScrollbar(
+ modifier = Modifier
+ .align(Alignment.CenterEnd),
+ adapter = rememberScrollbarAdapter(scrollState),
+ )
+ }
+ }
+
+ @Suppress("FunctionName")
+ @Composable
+ private fun MarkdownPreviewPanel(scrollState: ScrollState) {
+ val request by updateHandler.requests.collectAsState(null)
+ (request as? PreviewRequest.Update)?.let {
+ Markdown(
+ it.content,
+ modifier = Modifier
+ .fillMaxWidth()
+ .padding(8.dp)
+ .verticalScroll(scrollState),
+ enabled = true,
+ onUrlClick = { url -> BrowserUtil.open(url) },
+ )
+ }
+ }
+
+ override fun setHtml(html: String, initialScrollOffset: Int, document: VirtualFile?) {
+ updateHandler.setContent(html, initialScrollOffset, document)
+ }
+
+ override fun reloadWithOffset(offset: Int) {
+ updateHandler.reloadWithOffset(offset)
+ }
+
+ override fun getComponent(): JComponent {
+ return panelComponent
+ }
+
+ override fun dispose() {
+ }
+
+ @ApiStatus.Experimental
+ override fun getProject(): Project? = project
+
+ @ApiStatus.Experimental
+ override fun getVirtualFile(): VirtualFile? = virtualFile
+
+ override fun addScrollListener(listener: MarkdownHtmlPanel.ScrollListener) {
+ }
+
+ override fun removeScrollListener(listener: MarkdownHtmlPanel.ScrollListener) {
+ }
+
+ override fun scrollToMarkdownSrcOffset(offset: Int, smooth: Boolean) {
+ }
+
+ override fun scrollBy(horizontalUnits: Int, verticalUnits: Int) {
+ }
+}
diff --git a/plugins/markdown/compose/src/main/resources/intellij.markdown.compose.preview.xml b/plugins/markdown/compose/src/main/resources/intellij.markdown.compose.preview.xml
new file mode 100644
index 000000000000..a5a32bbe490b
--- /dev/null
+++ b/plugins/markdown/compose/src/main/resources/intellij.markdown.compose.preview.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/plugins/markdown/core/plugin-content.yaml b/plugins/markdown/core/plugin-content.yaml
index 113b627c8ba1..befda54b0098 100644
--- a/plugins/markdown/core/plugin-content.yaml
+++ b/plugins/markdown/core/plugin-content.yaml
@@ -9,4 +9,15 @@
- name: intellij.markdown.images
- name: intellij.markdown.xml
- name: intellij.markdown.model
- - name: intellij.markdown.spellchecker
\ No newline at end of file
+ - name: intellij.markdown.spellchecker
+- name: lib/modules/intellij.markdown.compose.preview.jar
+ contentModules:
+ - name: intellij.markdown.compose.preview
+ libraries:
+ jetbrains-jewel-markdown-laf-bridge-styling:
+ - name: $MAVEN_REPOSITORY$/org/jetbrains/jewel/jewel-markdown-ide-laf-bridge-styling-241/0.19.7/jewel-markdown-ide-laf-bridge-styling-241-0.19.7.jar
+ size: 28320
+ - name: $MAVEN_REPOSITORY$/org/jetbrains/jewel/jewel-markdown-core-241/0.19.7/jewel-markdown-core-241-0.19.7.jar
+ size: 294476
+ - name: $MAVEN_REPOSITORY$/org/commonmark/commonmark/0.22.0/commonmark-0.22.0.jar
+ size: 193845
\ No newline at end of file
diff --git a/plugins/markdown/core/resources/META-INF/plugin.xml b/plugins/markdown/core/resources/META-INF/plugin.xml
index aa744521caf1..5a231706a129 100644
--- a/plugins/markdown/core/resources/META-INF/plugin.xml
+++ b/plugins/markdown/core/resources/META-INF/plugin.xml
@@ -17,6 +17,7 @@
+
(State()) {
+class MarkdownPreviewSettings: SimplePersistentStateComponent(State()) {
class State: BaseState() {
var fontSize by property(defaultFontSize)
}
diff --git a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownHtmlPanelProvider.java b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownHtmlPanelProvider.java
index e4845243ceff..e9588b1aed44 100644
--- a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownHtmlPanelProvider.java
+++ b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownHtmlPanelProvider.java
@@ -40,6 +40,10 @@ public abstract class MarkdownHtmlPanelProvider {
public abstract @NotNull ProviderInfo getProviderInfo();
+ public @NotNull SourceTextPreprocessor getSourceTextPreprocessor() {
+ return SourceTextPreprocessor.getDefault();
+ }
+
public static @NotNull List getProviders() {
return EP_NAME.getExtensionList();
}
diff --git a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownPreviewFileEditor.kt b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownPreviewFileEditor.kt
index 7d262dc5b4b3..fc6271596f06 100644
--- a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownPreviewFileEditor.kt
+++ b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownPreviewFileEditor.kt
@@ -37,7 +37,6 @@ import kotlinx.coroutines.launch
import org.intellij.plugins.markdown.MarkdownBundle
import org.intellij.plugins.markdown.settings.MarkdownExtensionsSettings
import org.intellij.plugins.markdown.settings.MarkdownSettings
-import org.intellij.plugins.markdown.ui.preview.html.MarkdownUtil.generateMarkdownHtml
import org.intellij.plugins.markdown.ui.preview.jcef.MarkdownJCEFHtmlPanel
import org.intellij.plugins.markdown.util.MarkdownPluginScope
import org.jetbrains.annotations.ApiStatus.Internal
@@ -204,10 +203,12 @@ class MarkdownPreviewFileEditor(
return
}
- val html = readAction { generateMarkdownHtml(file, document.text, project) }
+ val settings = MarkdownSettings.getInstance(project)
+ val textPreprocessor = retrievePanelProvider(settings).sourceTextPreprocessor
+ lastRenderedHtml = readAction {
+ textPreprocessor.preprocessText(project, document, file)
+ }
- val currentHtml = "$html"
- lastRenderedHtml = currentHtml
val editor = mainEditor.firstOrNull() ?: return
writeIntentReadAction {
val offset = editor.caretModel.offset
@@ -229,7 +230,8 @@ class MarkdownPreviewFileEditor(
@RequiresEdt
private suspend fun attachHtmlPanel() {
val settings = MarkdownSettings.getInstance(project)
- val panel = retrievePanelProvider(settings).createHtmlPanel(project, file)
+ val panelProvider = retrievePanelProvider(settings)
+ val panel = panelProvider.createHtmlPanel(project, file)
this.panel = panel
htmlPanelWrapper.add(panel.component, BorderLayout.CENTER)
if (htmlPanelWrapper.isShowing) htmlPanelWrapper.validate()
diff --git a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownUpdateHandler.kt b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownUpdateHandler.kt
new file mode 100644
index 000000000000..6086d67b3cac
--- /dev/null
+++ b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownUpdateHandler.kt
@@ -0,0 +1,50 @@
+// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
+package org.intellij.plugins.markdown.ui.preview
+
+import com.intellij.openapi.vfs.VirtualFile
+import kotlinx.coroutines.FlowPreview
+import kotlinx.coroutines.channels.BufferOverflow
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.MutableSharedFlow
+import kotlinx.coroutines.flow.debounce
+import kotlin.time.Duration
+import kotlin.time.Duration.Companion.milliseconds
+
+abstract class MarkdownUpdateHandler {
+
+ abstract val requests: Flow
+ protected abstract fun addRequest(request: PreviewRequest): Boolean
+
+ fun setContent(content: String, initialScrollOffset: Int, document: VirtualFile?) {
+ doRequest(PreviewRequest.Update(content, initialScrollOffset, document))
+ }
+
+ fun reloadWithOffset(offset: Int) {
+ doRequest(PreviewRequest.ReloadWithOffset(offset))
+ }
+
+ private fun doRequest(request: PreviewRequest) {
+ check(addRequest(request))
+ }
+
+ sealed interface PreviewRequest {
+ data class Update(
+ val content: String,
+ val initialScrollOffset: Int,
+ val document: VirtualFile?
+ ) : PreviewRequest
+
+ data class ReloadWithOffset(val offset: Int) : PreviewRequest
+ }
+
+ @OptIn(FlowPreview::class)
+ class Debounced(private val debounceTimeout: Duration = 20.milliseconds) : MarkdownUpdateHandler() {
+
+ private val _updateViewRequests = MutableSharedFlow(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
+
+ override val requests: Flow
+ get() = _updateViewRequests.debounce(debounceTimeout)
+
+ override fun addRequest(request: PreviewRequest): Boolean = _updateViewRequests.tryEmit(request)
+ }
+}
diff --git a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/SourceTextPreprocessor.kt b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/SourceTextPreprocessor.kt
new file mode 100644
index 000000000000..1dde1dc1bb9b
--- /dev/null
+++ b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/SourceTextPreprocessor.kt
@@ -0,0 +1,19 @@
+// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
+package org.intellij.plugins.markdown.ui.preview
+
+import com.intellij.openapi.editor.Document
+import com.intellij.openapi.project.Project
+import com.intellij.openapi.vfs.VirtualFile
+
+interface SourceTextPreprocessor {
+ fun preprocessText(project: Project, document: Document, file: VirtualFile): String
+
+ companion object {
+ @JvmStatic
+ val default: SourceTextPreprocessor = object : SourceTextPreprocessor {
+ override fun preprocessText(project: Project, document: Document, file: VirtualFile): String {
+ return document.text
+ }
+ }
+ }
+}
diff --git a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/jcef/HtmlSourceTextPreprocessor.kt b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/jcef/HtmlSourceTextPreprocessor.kt
new file mode 100644
index 000000000000..d70ddd6f92d2
--- /dev/null
+++ b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/jcef/HtmlSourceTextPreprocessor.kt
@@ -0,0 +1,15 @@
+// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
+package org.intellij.plugins.markdown.ui.preview.jcef
+
+import com.intellij.openapi.editor.Document
+import com.intellij.openapi.project.Project
+import com.intellij.openapi.vfs.VirtualFile
+import org.intellij.plugins.markdown.ui.preview.SourceTextPreprocessor
+import org.intellij.plugins.markdown.ui.preview.html.MarkdownUtil.generateMarkdownHtml
+
+class HtmlSourceTextPreprocessor : SourceTextPreprocessor {
+ override fun preprocessText(project: Project, document: Document, file: VirtualFile): String {
+ val html = generateMarkdownHtml(file, document.text, project)
+ return "$html"
+ }
+}
diff --git a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/jcef/JCEFHtmlPanelProvider.java b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/jcef/JCEFHtmlPanelProvider.java
index 4c0362a9d5b3..f7e4f1cb8fa9 100644
--- a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/jcef/JCEFHtmlPanelProvider.java
+++ b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/jcef/JCEFHtmlPanelProvider.java
@@ -7,10 +7,9 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.jcef.JBCefApp;
import org.intellij.plugins.markdown.ui.preview.MarkdownHtmlPanel;
import org.intellij.plugins.markdown.ui.preview.MarkdownHtmlPanelProvider;
+import org.intellij.plugins.markdown.ui.preview.SourceTextPreprocessor;
import org.jetbrains.annotations.NotNull;
-import javax.swing.*;
-
public final class JCEFHtmlPanelProvider extends MarkdownHtmlPanelProvider {
@NotNull
@@ -39,6 +38,11 @@ public final class JCEFHtmlPanelProvider extends MarkdownHtmlPanelProvider {
return new ProviderInfo("JCEF Browser", JCEFHtmlPanelProvider.class.getName());
}
+ @Override
+ public @NotNull SourceTextPreprocessor getSourceTextPreprocessor() {
+ return new HtmlSourceTextPreprocessor();
+ }
+
public static boolean canBeUsed() {
return !AppMode.isRemoteDevHost() && JBCefApp.isSupported();
}
diff --git a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/jcef/MarkdownJCEFHtmlPanel.kt b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/jcef/MarkdownJCEFHtmlPanel.kt
index 3c3575f3b350..d5ba42d3f043 100644
--- a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/jcef/MarkdownJCEFHtmlPanel.kt
+++ b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/jcef/MarkdownJCEFHtmlPanel.kt
@@ -24,10 +24,7 @@ import com.intellij.ui.jcef.JCEFHtmlPanel
import com.intellij.util.application
import com.intellij.util.net.NetUtils
import kotlinx.coroutines.*
-import kotlinx.coroutines.channels.BufferOverflow
-import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collectLatest
-import kotlinx.coroutines.flow.debounce
import org.cef.browser.CefBrowser
import org.cef.browser.CefFrame
import org.cef.handler.CefRequestHandlerAdapter
@@ -41,6 +38,7 @@ import org.intellij.plugins.markdown.extensions.MarkdownConfigurableExtension
import org.intellij.plugins.markdown.settings.MarkdownPreviewSettings
import org.intellij.plugins.markdown.settings.MarkdownSettingsConfigurable.Companion.fontSizeOptions
import org.intellij.plugins.markdown.ui.preview.*
+import org.intellij.plugins.markdown.ui.preview.MarkdownUpdateHandler.PreviewRequest
import org.intellij.plugins.markdown.ui.preview.jcef.impl.*
import org.intellij.plugins.markdown.ui.preview.jcef.zoomIndicator.PreviewZoomIndicatorManager
import org.intellij.plugins.markdown.util.MarkdownApplicationScope
@@ -101,6 +99,8 @@ class MarkdownJCEFHtmlPanel(
styles.map { PreviewStaticServer.getStaticUrl(resourceProvider, it) }
)
+ private val updateHandler = MarkdownUpdateHandler.Debounced()
+
private fun buildIndexContent(): String {
// language=HTML
return """
@@ -126,18 +126,6 @@ class MarkdownJCEFHtmlPanel(
private val coroutineScope = project?.let(MarkdownPluginScope::createChildScope) ?: MarkdownApplicationScope.createChildScope()
- private sealed interface PreviewRequest {
- data class Update(
- val content: String,
- val initialScrollOffset: Int,
- val document: VirtualFile?,
- ) : PreviewRequest
-
- data class ReloadWithOffset(val offset: Int) : PreviewRequest
- }
-
- private val updateViewRequests = MutableSharedFlow(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
-
private val projectRoot = coroutineScope.async(context = Dispatchers.Default) {
if (virtualFile != null && project != null) {
BaseProjectDirectories.getInstance(project).getBaseDirectoryFor(virtualFile)
@@ -186,7 +174,7 @@ class MarkdownJCEFHtmlPanel(
val fileSchemeResourcesProcessor = createFileSchemeResourcesProcessor(projectRoot)
loadIndexContent()
- updateViewRequests.debounce(20.milliseconds).collectLatest { request ->
+ updateHandler.requests.collectLatest { request ->
when (request) {
is PreviewRequest.Update -> {
val (html, initialScrollOffset, document) = request
@@ -239,7 +227,7 @@ class MarkdownJCEFHtmlPanel(
}
override fun setHtml(html: String, initialScrollOffset: Int, document: VirtualFile?) {
- check(updateViewRequests.tryEmit(PreviewRequest.Update(content = html, initialScrollOffset, document)))
+ updateHandler.setContent(html, initialScrollOffset, document)
}
@ApiStatus.Internal
@@ -253,7 +241,7 @@ class MarkdownJCEFHtmlPanel(
}
override fun reloadWithOffset(offset: Int) {
- check(updateViewRequests.tryEmit(PreviewRequest.ReloadWithOffset(offset)))
+ updateHandler.reloadWithOffset(offset)
}
override fun dispose() {
diff --git a/plugins/markdown/plugin/intellij.markdown.plugin.iml b/plugins/markdown/plugin/intellij.markdown.plugin.iml
index 59658a08d40c..f43fb3b2dcd0 100644
--- a/plugins/markdown/plugin/intellij.markdown.plugin.iml
+++ b/plugins/markdown/plugin/intellij.markdown.plugin.iml
@@ -12,5 +12,6 @@
+
\ No newline at end of file