Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct DemonstrationView: View {
@AppStorage(SampleSettings.preferStreamedMarkdownKey) private var preferStreamedMarkdown = true
@AppStorage(SampleSettings.appearanceModeKey) private var appearanceMode = AppearanceMode.device
@AppStorage(SampleSettings.markdownThemeKey) private var markdownTheme = SampleMarkdownTheme.automatic
@AppStorage(SampleSettings.streamingTextAnimationKey) private var streamingTextAnimation = SampleStreamingTextAnimation.characterStreaming

let demonstration: Demonstration
let markdownText: String
Expand All @@ -29,14 +30,22 @@ struct DemonstrationView: View {
if preferStreamedMarkdown {
StreamedMarkdownView(
source: viewModel,
config: demonstration.renderConfig(theme: markdownTheme, isStreaming: true),
config: demonstration.renderConfig(
theme: markdownTheme,
isStreaming: true,
streamingTextAnimation: streamingTextAnimation
),
listener: listener
)
.id(streamedContentID)
} else {
MarkdownView(
text: markdownText,
config: demonstration.renderConfig(theme: markdownTheme, isStreaming: false),
config: demonstration.renderConfig(
theme: markdownTheme,
isStreaming: false,
streamingTextAnimation: streamingTextAnimation
),
listener: listener
)
.id(staticContentID)
Expand Down Expand Up @@ -106,6 +115,12 @@ struct DemonstrationView: View {
Text(mode.displayName).tag(mode)
}
}

Picker("Streaming Text", selection: $streamingTextAnimation) {
ForEach(SampleStreamingTextAnimation.allCases) { animation in
Text(animation.displayName).tag(animation)
}
}
} label: {
Image(systemName: "circle.righthalf.filled")
.accessibilityLabel("Appearance")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ final class DemonstrationViewModel: ObservableObject, StreamedMarkdownSource {

init(
text: String,
chunkSize: Int = 48,
chunkInterval: TimeInterval = 0.2
chunkSize: Int = 24,
chunkInterval: TimeInterval = 0.15
) {
self.fullText = text
self.chunkSize = max(1, chunkSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,15 @@ enum Demonstration: String, CaseIterable, Identifiable, Hashable {
}
}

func renderConfig(theme: SampleMarkdownTheme, isStreaming: Bool) -> MarkdownRenderConfig {
theme.renderConfig(for: self, isStreaming: isStreaming)
func renderConfig(
theme: SampleMarkdownTheme,
isStreaming: Bool,
streamingTextAnimation: SampleStreamingTextAnimation
) -> MarkdownRenderConfig {
theme.renderConfig(
for: self,
textAnimation: isStreaming ? streamingTextAnimation.renderAnimation : .none
)
}

var automaticBackgroundColor: Color {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ enum RobotoTheme {
// MARK: - Config

static let renderConfig: MarkdownRenderConfig = MarkdownRenderConfig(
shouldAnimateText: false,
textAnimation: .none,
blockQuoteStyle: .init(
textFonts: textFonts(size: 16, lineHeight: 24),
textColor: mutedForeground
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ enum SampleMarkdownTheme: String, CaseIterable, Identifiable {
}
}

func renderConfig(for demonstration: Demonstration, isStreaming: Bool) -> MarkdownRenderConfig {
func renderConfig(
for demonstration: Demonstration,
textAnimation: MarkdownRenderConfig.TextAnimation
) -> MarkdownRenderConfig {
resolvedConfig(for: demonstration)
.withTextContextMenu(value: demonstration.customContextMenu)
.withShouldAnimateText(value: isStreaming)
.withTextAnimation(textAnimation)
.withImageConfig(ImageConfig(
enabled: true,
allowedImageTypes: [.remote(allowedDomains: ["markdownguide.org"]), .assetCatalog, .bundledResource]
Expand Down Expand Up @@ -88,7 +91,7 @@ enum SampleMarkdownTheme: String, CaseIterable, Identifiable {

private static func paletteConfig(_ palette: Palette) -> MarkdownRenderConfig {
MarkdownRenderConfig(
shouldAnimateText: false,
textAnimation: .none,
blockQuoteStyle: .init(
textFonts: MarkdownRenderConfig.defaultBlockQuoteStyle.textFonts,
textColor: palette.secondaryForeground
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@
//

import SwiftUI
import SwiftStreamingMarkdown

enum SampleSettings {
static let preferStreamedMarkdownKey = "preferStreamedMarkdown"
static let appearanceModeKey = "appearanceMode"
static let markdownThemeKey = "markdownTheme"
static let streamingTextAnimationKey = "streamingTextAnimation"
}

enum SampleStreamingTextAnimation: String, CaseIterable, Identifiable {
case characterStreaming
case standardFade

var id: String { rawValue }

var displayName: String {
switch self {
case .characterStreaming: "Character Streaming"
case .standardFade: "Standard Fade"
}
}

var renderAnimation: MarkdownRenderConfig.TextAnimation {
switch self {
case .characterStreaming: .characterStreaming
case .standardFade: .fade
}
}
}

enum AppearanceMode: String, CaseIterable, Identifiable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ struct SettingsView: View {
@AppStorage(SampleSettings.preferStreamedMarkdownKey) private var preferStreamedMarkdown = true
@AppStorage(SampleSettings.appearanceModeKey) private var appearanceMode = AppearanceMode.device
@AppStorage(SampleSettings.markdownThemeKey) private var markdownTheme = SampleMarkdownTheme.automatic
@AppStorage(SampleSettings.streamingTextAnimationKey) private var streamingTextAnimation = SampleStreamingTextAnimation.characterStreaming

var body: some View {
Form {
Toggle("Streamed", isOn: $preferStreamedMarkdown)
Picker("Streaming Text", selection: $streamingTextAnimation) {
ForEach(SampleStreamingTextAnimation.allCases) { animation in
Text(animation.displayName).tag(animation)
}
}
.pickerStyle(.menu)
Picker("Markdown Theme", selection: $markdownTheme) {
ForEach(SampleMarkdownTheme.allCases) { theme in
Text(theme.displayName).tag(theme)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ by composing the `withXxx` helpers on `.default`:

```swift
let config = MarkdownRenderConfig.default
.withShouldAnimateText(value: true)
.withTextAnimation(.characterStreaming)
.withHeadingStyle(value: MarkdownRenderConfig.defaultHeadingStyle)
.withParagraphStyle(value: MarkdownRenderConfig.defaultParagraphStyle)
```
Expand Down
Loading
Loading