Repository navigation
Expand file tree
/
Copy pathOutlinerRichBlockContent.swift
More file actions
589 lines (540 loc) · 22.1 KB
/
Copy pathOutlinerRichBlockContent.swift
File metadata and controls
589 lines (540 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
import SwiftUI
import LogseqModel
#if os(iOS)
import AVKit
import UIKit
import WebKit
#endif
struct OutlinerMixedRichMarkupContent: View {
@Environment(\.lgPropertyValue) private var propertyValue
let nodes: [LogseqMarkupNode]
let fallback: String
let youtubePlaybackStarts: [String: Int]
let onSeekYouTube: (String, Int) -> Void
let allowsLinkInteraction: () -> Bool
let onOpenMarkupLink: (OutlinerMarkupLink) -> Void
var onEdit: (() -> Void)? = nil
private var chunks: [[LogseqMarkupNode]] {
OutlinerRichMarkupPolicy.chunks(nodes)
}
var body: AnyView {
if !OutlinerRichMarkupPolicy.containsRich(nodes) {
return AnyView(inlineContent(nodes).accessibilityIdentifier("block.rich.inline.0"))
}
return AnyView(richContent)
}
private var richContent: some View {
let chunks = self.chunks
return VStack(alignment: .leading, spacing: 8) {
ForEach(0..<chunks.count, id: \.self) { index in
let chunk = chunks[index]
if LatexMarkupDocument.containsMath(chunk) {
NativeLatexMarkupContent(nodes: chunk, allowsLinkInteraction: allowsLinkInteraction,
onOpenMarkupLink: onOpenMarkupLink, onEdit: onEdit)
} else if chunk.count == 1,
let node = chunk.first,
OutlinerRichMarkupPolicy.isRich(node.type) {
OutlinerRichBlockContent(
node: node,
youtubePlaybackStarts: youtubePlaybackStarts,
onSeekYouTube: onSeekYouTube,
allowsLinkInteraction: allowsLinkInteraction,
onOpenMarkupLink: onOpenMarkupLink,
onEdit: onEdit
)
} else {
inlineContent(chunk)
.accessibilityIdentifier("block.rich.inline.\(index)")
}
}
}
}
@ViewBuilder private func inlineContent(_ chunk: [LogseqMarkupNode]) -> some View {
Group {
if chunk.allSatisfy({ $0.type == .text }) {
Text(verbatim: chunk.isEmpty ? fallback : chunk.map { $0.text ?? "" }.joined())
} else {
Text(OutlinerMarkupAttributedString.make(nodes: chunk, fallback: fallback, textStyle: propertyValue ? .callout : .body))
}
}
.environment(\.openURL, OpenURLAction { url in
guard allowsLinkInteraction() else { return .discarded }
guard let link = OutlinerMarkupLink(url: url) else { return .systemAction }
onOpenMarkupLink(link)
return .handled
})
}
}
enum OutlinerMarkupMarkdown {
static func requiresAttributedText(_ nodes: [LogseqMarkupNode]) -> Bool {
nodes.contains { node in
switch node.type {
case .text, .table, .codeBlock, .math, .cloze, .youtubeTimestamp, .video, .iframe, .html, .image:
return false
case .code, .emphasis, .quote, .link, .nodeReference, .tagReference:
return true
}
}
}
static func make(nodes: [LogseqMarkupNode], fallback: String) -> String {
guard !nodes.isEmpty else { return escape(fallback) }
return nodes.map(make).joined()
}
private static func make(_ node: LogseqMarkupNode) -> String {
switch node.type {
case .text:
return escape(node.text ?? "")
case .code:
return "`" + (node.text ?? "").replacingOccurrences(of: "`", with: "\\`") + "`"
case .codeBlock, .math, .cloze:
return escape(node.text ?? "")
case .table:
return node.tableRows.map { $0.map { make(nodes: $0, fallback: "") }.joined(separator: "\t") }.joined(separator: "\n")
case .youtubeTimestamp:
return escape("◷ " + (node.text ?? ""))
case .emphasis, .quote:
let children = node.children.map(make).joined()
switch node.style {
case "bold": return "**" + children + "**"
case "italic": return "_" + children + "_"
case "strikeThrough": return "~~" + children + "~~"
default: return children
}
case .link:
let label = node.children.isEmpty
? escape(node.url ?? "")
: node.children.map(make).joined()
guard let url = node.url, !url.isEmpty else { return label }
return "[" + label + "](" + escapeDestination(url) + ")"
case .nodeReference:
return nodeLink(label: "[[" + (node.title ?? "") + "]]", uuid: node.uuid)
case .tagReference:
return nodeLink(label: "#" + (node.title ?? ""), uuid: node.uuid)
case .video, .iframe, .html, .image:
return escape(node.url ?? "")
}
}
private static func nodeLink(label: String, uuid: String?) -> String {
guard let uuid, !uuid.isEmpty else { return escape(label) }
return "[" + escape(label) + "](logseq-node://" + escapeDestination(uuid) + ")"
}
private static func escape(_ value: String) -> String {
var result = value.replacingOccurrences(of: "\\", with: "\\\\")
for character in ["`", "*", "_", "{", "}", "[", "]", "<", ">", "#"] {
result = result.replacingOccurrences(of: character, with: "\\" + character)
}
return result
}
private static func escapeDestination(_ value: String) -> String {
value
.replacingOccurrences(of: "\\", with: "%5C")
.replacingOccurrences(of: "(", with: "%28")
.replacingOccurrences(of: ")", with: "%29")
.replacingOccurrences(of: " ", with: "%20")
}
}
enum OutlinerMarkupInteractionPolicy {
static func hasInteractiveContent(_ nodes: [LogseqMarkupNode]) -> Bool {
nodes.contains { node in
switch node.type {
case .table, .link, .nodeReference, .tagReference, .video, .iframe, .html, .image,
.youtubeTimestamp, .cloze:
return true
case .emphasis, .quote:
return hasInteractiveContent(node.children)
case .text, .code, .codeBlock, .math:
return false
}
}
}
}
enum OutlinerMarkupAttributedString {
static func make(nodes: [LogseqMarkupNode], fallback: String, textStyle: Font.TextStyle = .body) -> AttributedString {
guard !nodes.isEmpty else { return AttributedString(fallback) }
var result = AttributedString()
for node in nodes {
result.append(make(node, textStyle: textStyle))
}
return result
}
private static func make(_ node: LogseqMarkupNode, textStyle: Font.TextStyle) -> AttributedString {
switch node.type {
case .text:
return AttributedString(node.text ?? "")
case .code:
var value = AttributedString(node.text ?? "")
value.font = .system(textStyle, design: .monospaced)
value.backgroundColor = Color.secondary.opacity(0.12)
return value
case .codeBlock, .math, .cloze:
return AttributedString(node.text ?? "")
case .table:
var result = AttributedString()
for (rowIndex, row) in node.tableRows.enumerated() {
if rowIndex > 0 { result.append(AttributedString("\n")) }
for (cellIndex, cell) in row.enumerated() {
if cellIndex > 0 { result.append(AttributedString("\t")) }
result.append(make(nodes: cell, fallback: "", textStyle: textStyle))
}
}
return result
case .youtubeTimestamp:
return AttributedString("◷ " + (node.text ?? ""))
case .emphasis, .quote:
var value = make(nodes: node.children, fallback: "", textStyle: textStyle)
switch node.style {
case "heading-1": value.font = .title.bold()
case "heading-2": value.font = .title2.bold()
case "heading-3": value.font = .title3.bold()
case "heading-4", "heading-5", "heading-6": value.font = .headline
case "bold": value.font = .system(textStyle).bold()
case "italic": value.font = .system(textStyle).italic()
case "underline": value.underlineStyle = .single
case "strikeThrough": value.strikethroughStyle = .single
case "highlight": value.backgroundColor = Color.yellow.opacity(0.25)
default: break
}
return value
case .link:
var value = make(nodes: node.children, fallback: node.url ?? "", textStyle: textStyle)
value.link = node.url.flatMap(URL.init(string:))
return value
case .nodeReference:
var value = AttributedString("[[" + (node.title ?? "") + "]]")
if let uuid = node.uuid {
value.link = OutlinerMarkupLink.node(uuid: uuid).url
}
return value
case .tagReference:
var value = AttributedString("#" + (node.title ?? ""))
if let uuid = node.uuid {
value.link = OutlinerMarkupLink.node(uuid: uuid).url
}
return value
case .video, .iframe, .html, .image:
return AttributedString(node.url ?? "")
}
}
}
struct OutlinerRichBlockContent: View {
@Environment(\.lgPropertyValue) private var propertyValue
let node: LogseqMarkupNode
let youtubePlaybackStarts: [String: Int]
let onSeekYouTube: (String, Int) -> Void
let allowsLinkInteraction: () -> Bool
let onOpenMarkupLink: (OutlinerMarkupLink) -> Void
var onEdit: (() -> Void)? = nil
private var youtubeStartSeconds: Int? {
node.url.flatMap { youtubePlaybackStarts[$0] }
}
@ViewBuilder var body: some View {
Group {
switch node.type {
case .quote:
HStack(alignment: .top, spacing: 10) {
RoundedRectangle(cornerRadius: 1)
.fill(Color.secondary.opacity(0.45))
.frame(width: 3)
quoteText
.italic()
.foregroundStyle(.secondary)
}
case .math:
NativeLatexMarkupContent(nodes: [node], allowsLinkInteraction: allowsLinkInteraction,
onOpenMarkupLink: onOpenMarkupLink, onEdit: onEdit)
case .codeBlock:
SyntaxHighlightedCodeBlock(
code: node.text ?? "",
language: node.style
)
case .table:
OutlinerMarkupTable(
node: node,
youtubePlaybackStarts: youtubePlaybackStarts,
onSeekYouTube: onSeekYouTube,
allowsLinkInteraction: allowsLinkInteraction,
onOpenMarkupLink: onOpenMarkupLink,
onEdit: onEdit
)
case .video:
EmbeddedVideo(
node: node,
startSeconds: youtubeStartSeconds
)
case .html:
#if os(iOS)
AppleWebView(url: nil, headers: [:], html: node.text).frame(height: 220)
#else
Text(node.text ?? "")
#endif
case .image:
AsyncImage(url: node.url.flatMap(URL.init(string:))) { phase in
if let image = phase.image { image.resizable().scaledToFit() }
else if phase.error != nil { Text("Image unavailable").foregroundStyle(.secondary) }
else { ProgressView() }
}
.frame(maxWidth: .infinity, maxHeight: 240)
.clipShape(.rect(cornerRadius: 12))
.accessibilityLabel(node.text ?? "Image")
case .iframe:
EmbeddedWebContent(url: node.embedURL.flatMap { URL(string: $0) }, headers: node.mediaHeaders)
case .youtubeTimestamp:
YouTubeTimestamp(
label: node.text ?? "",
url: node.url,
seconds: node.seconds,
onSeek: onSeekYouTube
)
case .cloze:
InteractiveCloze(text: node.text ?? "")
default:
Text(verbatim: node.text ?? node.url ?? "")
}
}
.accessibilityIdentifier(contentAccessibilityIdentifier)
}
private var contentAccessibilityIdentifier: String {
if let youtubeStartSeconds {
switch node.type {
case .video:
return "block.rich.video.start.\(youtubeStartSeconds)"
case .youtubeTimestamp:
return "block.rich.youtubeTimestamp.start.\(youtubeStartSeconds)"
default:
break
}
}
return "block.rich.\(node.type.rawValue)"
}
@ViewBuilder private var quoteText: some View {
Text(OutlinerMarkupAttributedString.make(nodes: node.children, fallback: "", textStyle: propertyValue ? .callout : .body))
.environment(\.openURL, OpenURLAction { url in
guard allowsLinkInteraction() else { return .discarded }
guard let link = OutlinerMarkupLink(url: url) else { return .systemAction }
onOpenMarkupLink(link)
return .handled
})
}
}
private struct OutlinerMarkupTable: View {
@ScaledMetric(relativeTo: .body) private var cellWidth = 220.0
let node: LogseqMarkupNode
let youtubePlaybackStarts: [String: Int]
let onSeekYouTube: (String, Int) -> Void
let allowsLinkInteraction: () -> Bool
let onOpenMarkupLink: (OutlinerMarkupLink) -> Void
let onEdit: (() -> Void)?
var body: some View {
let rows = node.tableRows
let columns = rows.map(\.count).max() ?? 0
ScrollView(.horizontal) {
Grid(alignment: .topLeading, horizontalSpacing: 0, verticalSpacing: 0) {
ForEach(rows.indices, id: \.self) { rowIndex in
GridRow(alignment: .top) {
ForEach(0..<columns, id: \.self) { column in
let cell = column < rows[rowIndex].count ? rows[rowIndex][column] : []
OutlinerMixedRichMarkupContent(
nodes: cell, fallback: "", youtubePlaybackStarts: youtubePlaybackStarts,
onSeekYouTube: onSeekYouTube, allowsLinkInteraction: allowsLinkInteraction,
onOpenMarkupLink: onOpenMarkupLink, onEdit: onEdit
)
.fontWeight(rowIndex == 0 && node.header != nil ? .semibold : .regular)
.padding(10)
.frame(width: cellWidth, alignment: .topLeading)
.frame(maxHeight: .infinity, alignment: .topLeading)
.background(rowIndex == 0 && node.header != nil ? Color.secondary.opacity(0.1) : Color.clear)
.overlay(Rectangle().strokeBorder(Color.secondary.opacity(0.25), lineWidth: 0.5))
}
}
}
}
}
}
}
private struct YouTubeTimestamp: View {
let label: String
let url: String?
let seconds: Int?
let onSeek: (String, Int) -> Void
@ViewBuilder
var body: some View {
if let url, let seconds {
Button {
onSeek(url, seconds)
} label: {
timestampLabel
.foregroundStyle(.tint)
.platformTimestampHitShape()
}
.buttonStyle(.plain)
.accessibilityLabel("Seek YouTube video to " + label)
} else {
timestampLabel.foregroundStyle(.secondary)
}
}
private var timestampLabel: some View {
Text(verbatim: "◷ " + label)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
private extension View {
@ViewBuilder func platformTimestampHitShape() -> some View {
contentShape(Rectangle())
}
}
private struct InteractiveCloze: View {
let text: String
@State private var isRevealed = false
var body: some View {
Button {
isRevealed.toggle()
} label: {
Text(verbatim: isRevealed ? text : "Tap to reveal")
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(Color.secondary.opacity(0.14))
.clipShape(RoundedRectangle(cornerRadius: 6))
}
.buttonStyle(.plain)
.accessibilityLabel(isRevealed ? text : "Reveal cloze")
}
}
private struct ShowCodeLanguageKey: EnvironmentKey {
static let defaultValue = true
}
extension EnvironmentValues {
var lgShowCodeLanguage: Bool {
get { self[ShowCodeLanguageKey.self] }
set { self[ShowCodeLanguageKey.self] = newValue }
}
}
private struct SyntaxHighlightedCodeBlock: View {
@Environment(\.lgShowCodeLanguage) private var showLanguage
let code: String
let language: String?
@State private var highlighted: (code: String, language: String?, text: AttributedString)?
private var displayText: AttributedString {
if let highlighted, highlighted.code == code, highlighted.language == language { return highlighted.text }
return AttributedString(code)
}
var body: some View {
VStack(alignment: .leading, spacing: 6) {
if showLanguage, let language, !language.isEmpty {
Text(verbatim: language)
.font(.caption2.monospaced())
.foregroundStyle(.secondary)
}
ScrollView(.horizontal, showsIndicators: true) {
Text(displayText)
.font(.callout.monospaced())
.textSelection(.enabled)
}
}
.padding(10)
.background(showLanguage ? Color.secondary.opacity(0.1) : Color.clear)
.clipShape(RoundedRectangle(cornerRadius: 8))
.task(id: [code, language ?? ""]) {
highlighted = nil
let tokens = await CodeSyntaxHighlighter.shared.tokens(code: code, language: language ?? "")
guard !Task.isCancelled else { return }
#if os(iOS)
highlighted = (code, language, NativeCodeHighlighting.attributed(code, tokens: tokens))
#else
highlighted = (code, language, AttributedString(code))
#endif
}
}
}
enum NativeMediaRequest {
static func positionedURL(_ source: String?, parameter: String?, seconds: Int?) -> URL? {
guard let source, let url = URL(string: source) else { return nil }
guard let parameter, let seconds, seconds >= 0,
var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return url }
components.queryItems = (components.queryItems ?? []).filter { $0.name != parameter }
+ [URLQueryItem(name: parameter, value: String(seconds))]
return components.url
}
}
private struct EmbeddedVideo: View {
let node: LogseqMarkupNode
let startSeconds: Int?
@ViewBuilder var body: some View {
if let embedURL = NativeMediaRequest.positionedURL(node.embedURL, parameter: node.positionParameter, seconds: startSeconds) {
EmbeddedWebContent(url: embedURL, headers: node.mediaHeaders)
} else if let source = node.url, let url = URL(string: source) {
#if os(iOS)
NativeAppleVideo(url: url).frame(height: 220)
#else
Link(url.absoluteString, destination: url)
#endif
} else {
Text("Invalid video URL").foregroundStyle(.secondary)
}
}
}
#if os(iOS)
private struct NativeAppleVideo: View {
@State private var player: AVPlayer
init(url: URL) {
_player = State(initialValue: AVPlayer(url: url))
}
var body: some View {
VideoPlayer(player: player)
.onDisappear { player.pause() }
}
}
#endif
private struct EmbeddedWebContent: View {
let url: URL?
var headers: [String: String] = [:]
@ViewBuilder var body: some View {
if let url {
#if os(iOS)
AppleWebView(url: url, headers: headers)
.frame(height: 240)
#else
Link(url.absoluteString, destination: url)
#endif
} else {
Text("Invalid embed URL")
.foregroundStyle(.secondary)
}
}
}
#if os(iOS)
private struct AppleWebView: UIViewRepresentable {
let url: URL?
let headers: [String: String]
var html: String? = nil
func makeCoordinator() -> Coordinator { Coordinator() }
func makeUIView(context: Context) -> WKWebView {
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
let view = WKWebView(frame: .zero, configuration: configuration)
view.isOpaque = false
view.backgroundColor = .clear
view.scrollView.backgroundColor = .clear
return view
}
func updateUIView(_ webView: WKWebView, context: Context) {
if let html {
guard context.coordinator.loadedHTML != html else { return }
context.coordinator.loadedHTML = html
webView.loadHTMLString("<meta name='viewport' content='width=device-width,initial-scale=1'><style>:root{color-scheme:light dark}body{background:transparent;font:17px -apple-system;margin:0}img{max-width:100%}</style>" + html, baseURL: nil)
return
}
guard let url else { return }
var request = URLRequest(url: url)
for (field, value) in headers {
request.setValue(value, forHTTPHeaderField: field)
}
guard context.coordinator.loadedRequest != request else { return }
context.coordinator.loadedRequest = request
webView.load(request)
}
final class Coordinator {
var loadedRequest: URLRequest?
var loadedHTML: String?
}
}
#endif