Skip to content

Commit

Permalink
Add fragment behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Jul 18, 2024
1 parent 591ac3b commit 28e608b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,47 @@ package eu.iamgio.quarkdown.ast.quarkdown

import eu.iamgio.quarkdown.ast.NestableNode
import eu.iamgio.quarkdown.ast.Node
import eu.iamgio.quarkdown.rendering.representable.RenderRepresentable
import eu.iamgio.quarkdown.rendering.representable.RenderRepresentableVisitor
import eu.iamgio.quarkdown.visitor.node.NodeVisitor

/**
* A node that, when rendered in a `Slides` environment,
* is displayed when the user attempts to go to the next slide.
* Multiple fragments in the same slide are shown in order on distinct user interactions.
* @param behavior visibility type of the fragment and how it reacts to user interactions
*/
data class SlidesFragment(
val behavior: Behavior,
override val children: List<Node>,
) : NestableNode {
override fun <T> accept(visitor: NodeVisitor<T>): T = visitor.visit(this)

/**
* Possible visibility types of a [SlidesFragment].
*/
enum class Behavior : RenderRepresentable {
/**
* Starts invisible, fades in on interaction.
*/
SHOW,

/**
* Starts visible, fade out on interaction.
*/
HIDE,

/**
* Starts visible, fade out to 50% on interaction.
*/
SEMI_HIDE,

/**
* Starts invisible, fades in on interaction, then out on the next interaction.
*/
SHOW_HIDE,
;

override fun <T> accept(visitor: RenderRepresentableVisitor<T>): T = visitor.visit(this)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.iamgio.quarkdown.rendering.html

import eu.iamgio.quarkdown.ast.quarkdown.Clipped
import eu.iamgio.quarkdown.ast.quarkdown.SlidesFragment
import eu.iamgio.quarkdown.ast.quarkdown.Stacked
import eu.iamgio.quarkdown.ast.quarkdown.TextTransformData
import eu.iamgio.quarkdown.document.page.PageMarginPosition
Expand Down Expand Up @@ -52,6 +53,14 @@ class CssRepresentableVisitor : RenderRepresentableVisitor<String> {

override fun visit(speed: Transition.Speed) = speed.kebabCaseName

override fun visit(behavior: SlidesFragment.Behavior) =
when (behavior) {
SlidesFragment.Behavior.SHOW -> "fade-in"
SlidesFragment.Behavior.HIDE -> "fade-out"
SlidesFragment.Behavior.SEMI_HIDE -> "semi-fade-out"
SlidesFragment.Behavior.SHOW_HIDE -> "fade-in-then-out"
}

override fun visit(size: TextTransformData.Size) =
when (size) {
TextTransformData.Size.TINY -> "0.5em"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class QuarkdownHtmlNodeRenderer(context: Context) : BaseHtmlNodeRenderer(context

override fun visit(node: SlidesFragment): CharSequence =
tagBuilder("p", node.children)
.attribute("class", "fragment")
.attribute("class", "fragment ${node.behavior.asCSS}")
.build()

override fun visit(node: TextTransform) =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.iamgio.quarkdown.rendering.representable

import eu.iamgio.quarkdown.ast.quarkdown.Clipped
import eu.iamgio.quarkdown.ast.quarkdown.SlidesFragment
import eu.iamgio.quarkdown.ast.quarkdown.Stacked
import eu.iamgio.quarkdown.ast.quarkdown.TextTransformData
import eu.iamgio.quarkdown.document.page.PageMarginPosition
Expand Down Expand Up @@ -35,6 +36,8 @@ interface RenderRepresentableVisitor<T> {

fun visit(speed: Transition.Speed): T

fun visit(behavior: SlidesFragment.Behavior): T

fun visit(size: TextTransformData.Size): T

fun visit(weight: TextTransformData.Weight): T
Expand Down
6 changes: 5 additions & 1 deletion stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Slides.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ fun setSlidesConfiguration(
* Creates an element that, when used in a `slides` document,
* shows its content when the user attempts to go to the next slide.
* Multiple fragments in the same slide are shown in order on distinct user interactions.
* @param behavior visibility type of the fragment and how it reacts to user interactions
* @param content content to show
* @return the fragment node
*/
fun fragment(content: InlineMarkdownContent) = SlidesFragment(content.children).wrappedAsValue()
fun fragment(
behavior: SlidesFragment.Behavior = SlidesFragment.Behavior.SHOW,
content: InlineMarkdownContent,
) = SlidesFragment(behavior, content.children).wrappedAsValue()

0 comments on commit 28e608b

Please sign in to comment.