@@ -13,6 +13,7 @@ public struct AsyncCodeBlock: View {
1313 )
1414
1515 var dimmedCharacterCount : Int = 0
16+ var code : String ?
1617 private var highlightedCode = [ NSAttributedString] ( )
1718 private var foregroundColor : Color = . primary
1819 private( set) var commonPrecedingSpaceCount = 0
@@ -69,19 +70,23 @@ public struct AsyncCodeBlock: View {
6970 @PerceptionIgnored private var debounceFunction : DebounceFunction < AsyncCodeBlock > ?
7071 @PerceptionIgnored private var highlightTask : Task < Void , Error > ?
7172
72- init ( ) { }
73+ init ( ) {
74+ debounceFunction = . init( duration: 0.1 , block: { view in
75+ self . highlight ( for: view)
76+ } )
77+ }
7378
7479 func highlight( debounce: Bool , for view: AsyncCodeBlock ) {
7580 if debounce {
76- Task { await debounceFunction ? ( view) }
81+ Task { @ MainActor in await debounceFunction ? ( view) }
7782 } else {
7883 highlight ( for: view)
7984 }
8085 }
8186
8287 private func highlight( for view: AsyncCodeBlock ) {
8388 highlightTask? . cancel ( )
84- let code = view. code
89+ let code = self . code ?? view. code
8590 let language = view. language
8691 let scenario = view. scenario
8792 let brightMode = view. colorScheme != . dark
@@ -197,7 +202,8 @@ public struct AsyncCodeBlock: View {
197202 storage. dimmedCharacterCount = dimmedCharacterCount
198203 storage. highlight ( debounce: false , for: self )
199204 }
200- . onChange ( of: code) { _ in
205+ . onChange ( of: code) { code in
206+ storage. code = code // But why do we need this? Time to learn some SwiftUI!
201207 storage. highlight ( debounce: true , for: self )
202208 }
203209 . onChange ( of: colorScheme) { _ in
0 commit comments