-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Markdown styling usage, and add semantics support (#397)
* Fix Markdown functions not using provided styling * Add semantics support to Markdown, move to right package Usage in tests: ```kotlin fun SemanticsNodeInteraction.assertMarkdownEquals( expected: String ): SemanticsNodeInteraction = assert(hasMarkdownExactly(expected)) private fun hasMarkdownExactly(expected: String): SemanticsMatcher = SemanticsMatcher.expectValue(RawMarkdown, expected) ``` * Update API dump
- Loading branch information
Showing
7 changed files
with
162 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
markdown/core/src/main/kotlin/org/jetbrains/jewel/markdown/Markdown.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package org.jetbrains.jewel.markdown | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.LazyListState | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.foundation.lazy.rememberLazyListState | ||
import androidx.compose.foundation.text.selection.SelectionContainer | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.semantics.semantics | ||
import androidx.compose.ui.unit.dp | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.intellij.lang.annotations.Language | ||
import org.jetbrains.jewel.foundation.ExperimentalJewelApi | ||
import org.jetbrains.jewel.foundation.theme.JewelTheme | ||
import org.jetbrains.jewel.markdown.extensions.markdownBlockRenderer | ||
import org.jetbrains.jewel.markdown.extensions.markdownProcessor | ||
import org.jetbrains.jewel.markdown.extensions.markdownStyling | ||
import org.jetbrains.jewel.markdown.processing.MarkdownProcessor | ||
import org.jetbrains.jewel.markdown.rendering.DefaultMarkdownBlockRenderer | ||
import org.jetbrains.jewel.markdown.rendering.MarkdownBlockRenderer | ||
import org.jetbrains.jewel.markdown.rendering.MarkdownStyling | ||
|
||
@ExperimentalJewelApi | ||
@Composable | ||
public fun Markdown( | ||
@Language("Markdown") markdown: String, | ||
modifier: Modifier = Modifier, | ||
selectable: Boolean = false, | ||
enabled: Boolean = true, | ||
renderingDispatcher: CoroutineDispatcher = Dispatchers.Default, | ||
onUrlClick: (String) -> Unit = {}, | ||
onTextClick: () -> Unit = {}, | ||
markdownStyling: MarkdownStyling = JewelTheme.markdownStyling, | ||
processor: MarkdownProcessor = JewelTheme.markdownProcessor, | ||
blockRenderer: MarkdownBlockRenderer = DefaultMarkdownBlockRenderer(markdownStyling), | ||
) { | ||
var markdownBlocks by remember { mutableStateOf(emptyList<MarkdownBlock>()) } | ||
LaunchedEffect(markdown, processor) { | ||
markdownBlocks = | ||
withContext(renderingDispatcher) { processor.processMarkdownDocument(markdown) } | ||
} | ||
|
||
Markdown( | ||
markdownBlocks = markdownBlocks, | ||
markdown = markdown, | ||
modifier = modifier, | ||
selectable = selectable, | ||
enabled = enabled, | ||
onUrlClick = onUrlClick, | ||
onTextClick = onTextClick, | ||
markdownStyling = markdownStyling, | ||
blockRenderer = blockRenderer, | ||
) | ||
} | ||
|
||
@ExperimentalJewelApi | ||
@Composable | ||
public fun Markdown( | ||
markdownBlocks: List<MarkdownBlock>, | ||
markdown: String, | ||
modifier: Modifier = Modifier, | ||
enabled: Boolean = true, | ||
selectable: Boolean = false, | ||
onUrlClick: (String) -> Unit = {}, | ||
onTextClick: () -> Unit = {}, | ||
markdownStyling: MarkdownStyling = JewelTheme.markdownStyling, | ||
blockRenderer: MarkdownBlockRenderer = DefaultMarkdownBlockRenderer(markdownStyling), | ||
) { | ||
if (selectable) { | ||
SelectionContainer(modifier.semantics { rawMarkdown = markdown }) { | ||
Column(verticalArrangement = Arrangement.spacedBy(markdownStyling.blockVerticalSpacing)) { | ||
for (block in markdownBlocks) { | ||
blockRenderer.render(block, enabled, onUrlClick, onTextClick) | ||
} | ||
} | ||
} | ||
} else { | ||
Column( | ||
modifier.semantics { rawMarkdown = markdown }, | ||
verticalArrangement = Arrangement.spacedBy(markdownStyling.blockVerticalSpacing), | ||
) { | ||
for (block in markdownBlocks) { | ||
blockRenderer.render(block, enabled, onUrlClick, onTextClick) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@ExperimentalJewelApi | ||
@Composable | ||
public fun LazyMarkdown( | ||
markdownBlocks: List<MarkdownBlock>, | ||
modifier: Modifier = Modifier, | ||
contentPadding: PaddingValues = PaddingValues(0.dp), | ||
state: LazyListState = rememberLazyListState(), | ||
enabled: Boolean = true, | ||
selectable: Boolean = false, | ||
onUrlClick: (String) -> Unit = {}, | ||
onTextClick: () -> Unit = {}, | ||
markdownStyling: MarkdownStyling = JewelTheme.markdownStyling, | ||
blockRenderer: MarkdownBlockRenderer = JewelTheme.markdownBlockRenderer, | ||
) { | ||
if (selectable) { | ||
SelectionContainer(modifier) { | ||
LazyColumn( | ||
state = state, | ||
contentPadding = contentPadding, | ||
verticalArrangement = Arrangement.spacedBy(markdownStyling.blockVerticalSpacing), | ||
) { | ||
items(markdownBlocks) { block -> | ||
blockRenderer.render(block, enabled, onUrlClick, onTextClick) | ||
} | ||
} | ||
} | ||
} else { | ||
LazyColumn( | ||
modifier = modifier, | ||
state = state, | ||
contentPadding = contentPadding, | ||
verticalArrangement = Arrangement.spacedBy(markdownStyling.blockVerticalSpacing), | ||
) { | ||
items(markdownBlocks) { block -> | ||
blockRenderer.render(block, enabled, onUrlClick, onTextClick) | ||
} | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
markdown/core/src/main/kotlin/org/jetbrains/jewel/markdown/Semantics.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.jetbrains.jewel.markdown | ||
|
||
import androidx.compose.ui.semantics.SemanticsPropertyKey | ||
import androidx.compose.ui.semantics.SemanticsPropertyReceiver | ||
|
||
public val RawMarkdown: SemanticsPropertyKey<String> = SemanticsPropertyKey("RawMarkdown") | ||
public var SemanticsPropertyReceiver.rawMarkdown: String by RawMarkdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters