Skip to content

Commit baffd88

Browse files
committed
修复 markdown 水平滚动阻碍垂直滚动问题
1 parent 1eb2ef7 commit baffd88

File tree

5 files changed

+233
-24
lines changed

5 files changed

+233
-24
lines changed

SwiftPamphletApp.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
0896FB9227BA486900676B7F /* 145.md in Resources */ = {isa = PBXBuildFile; fileRef = 0896FB9127BA486900676B7F /* 145.md */; };
201201
08A4FDC227B25A140068E5BC /* 141.md in Resources */ = {isa = PBXBuildFile; fileRef = 08A4FDC127B25A140068E5BC /* 141.md */; };
202202
08A9E19F2BC18EF400A73764 /* APIVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E19E2BC18EF400A73764 /* APIVM.swift */; };
203+
08A9E1A22BC25D0700A73764 /* ViewComponetMarkdown.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E1A12BC25D0700A73764 /* ViewComponetMarkdown.swift */; };
203204
08AEAEDD277EA64900B969E2 /* DBDevNoti.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08AEAEDC277EA64900B969E2 /* DBDevNoti.swift */; };
204205
08AEAEE1277EA70500B969E2 /* DBRepoStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08AEAEE0277EA70500B969E2 /* DBRepoStore.swift */; };
205206
08AEAEF1277F09D000B969E2 /* IntroView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08AEAEF0277F09D000B969E2 /* IntroView.swift */; };
@@ -475,6 +476,7 @@
475476
0896FB9127BA486900676B7F /* 145.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = 145.md; sourceTree = "<group>"; };
476477
08A4FDC127B25A140068E5BC /* 141.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = 141.md; sourceTree = "<group>"; };
477478
08A9E19E2BC18EF400A73764 /* APIVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIVM.swift; sourceTree = "<group>"; };
479+
08A9E1A12BC25D0700A73764 /* ViewComponetMarkdown.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewComponetMarkdown.swift; sourceTree = "<group>"; };
478480
08AEAEDC277EA64900B969E2 /* DBDevNoti.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DBDevNoti.swift; sourceTree = "<group>"; };
479481
08AEAEE0277EA70500B969E2 /* DBRepoStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DBRepoStore.swift; sourceTree = "<group>"; };
480482
08AEAEF0277F09D000B969E2 /* IntroView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntroView.swift; sourceTree = "<group>"; };
@@ -1211,6 +1213,7 @@
12111213
isa = PBXGroup;
12121214
children = (
12131215
086A5F512744EF4C00FECE02 /* ViewComponent.swift */,
1216+
08A9E1A12BC25D0700A73764 /* ViewComponetMarkdown.swift */,
12141217
08F14B432BBE2865005B46CC /* ViewComponentImage.swift */,
12151218
0805F4952BAAABEA0008BB52 /* ViewStyle.swift */,
12161219
);
@@ -1742,6 +1745,7 @@
17421745
08448F0B2797F73200B61353 /* PlayArchitecture.swift in Sources */,
17431746
08038767276700F100519B15 /* CCYRESTfulAPI.swift in Sources */,
17441747
08522BD827CF344B005FF059 /* PlaySliderView.swift in Sources */,
1748+
08A9E1A22BC25D0700A73764 /* ViewComponetMarkdown.swift in Sources */,
17451749
0887A59A2BA28F6D00131359 /* CSGuideView.swift in Sources */,
17461750
086A5F362744ED9600FECE02 /* RepoView.swift in Sources */,
17471751
086A5F302744ED8600FECE02 /* IssuesListFromCustomView.swift in Sources */,

SwiftPamphletApp/GitHubAPI/APIVM/APIVM.swift

+15-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ final class APIRepoVM {
1313
var name: String = ""
1414
var repo: RepoModel = RepoModel()
1515
var commits: [CommitModel] = [CommitModel]()
16+
var issues: [IssueModel] = [IssueModel]()
1617

1718
init(name: String) {
1819
self.name = name
@@ -21,17 +22,16 @@ final class APIRepoVM {
2122
func updateAllData() async {
2223
await obtainRepos()
2324
await obtainCommits()
25+
await obtainIssues()
2426
}
2527

26-
// https://docs.github.com/zh/rest/repos/repos?apiVersion=2022-11-28
28+
// https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28
2729
@MainActor
2830
func obtainRepos() async {
2931
do {
3032
let (data, _) = try await URLSession.shared.data(for: GitHubReq.req("repos/\(name)"))
3133
repo = try GitHubReq.jsonDecoder().decode(RepoModel.self, from: data)
32-
} catch {
33-
print("问题是:\(error)")
34-
}
34+
} catch { print("问题是:\(error)") }
3535
}
3636

3737
// https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28
@@ -40,13 +40,21 @@ final class APIRepoVM {
4040
do {
4141
let (data, _) = try await URLSession.shared.data(for: GitHubReq.req("repos/\(name)/commits"))
4242
commits = try GitHubReq.jsonDecoder().decode([CommitModel].self, from: data)
43-
} catch {
44-
print("问题是:\(error)")
45-
}
43+
} catch { print("问题是:\(error)") }
44+
}
45+
46+
// https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28
47+
@MainActor
48+
func obtainIssues() async {
49+
do {
50+
let (data, _) = try await URLSession.shared.data(for: GitHubReq.req("repos/\(name)/issues"))
51+
issues = try GitHubReq.jsonDecoder().decode([IssueModel].self, from: data)
52+
} catch { print("问题是:\(error)") }
4653
}
4754

4855
}
4956

57+
// github api 入口 https://api.github.com
5058
class GitHubReq {
5159
static func jsonDecoder() -> JSONDecoder {
5260
let de = JSONDecoder()

SwiftPamphletApp/GitHubAPI/Developer/EditDeveloper.swift

+1-9
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,12 @@ struct EditDeveloper: View {
107107
.tabItem {
108108
Text("新提交")
109109
}
110-
// .onAppear {
111-
// Task {
112-
// await repoVM.obtainCommits()
113-
// }
114-
// }
115110
.tag(1)
116111

117-
IssuesView(issues: vmRepo.issues, repo: vmRepo.repo)
112+
IssuesView(issues: repoVM.issues, repo: repoVM.repo)
118113
.tabItem {
119114
Text("议题列表")
120115
}
121-
.onAppear {
122-
vmRepo.doing(.inIssues)
123-
}
124116
.tag(2)
125117

126118
IssueEventsView(issueEvents: vmRepo.issueEvents, repo: vmRepo.repo)

SwiftPamphletApp/ViewComponet/ViewComponent.swift

+1-8
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,7 @@ struct SideBarLabel: View {
9292
}
9393
}
9494

95-
// MARK: - MarkdownUI
96-
struct MarkdownView: View {
97-
var s: String
98-
var body: some View {
99-
Markdown(s)
100-
.markdownTheme(.gitHub)
101-
}
102-
}
95+
10396

10497
// MARK: - 共享菜单
10598
struct ShareView: View {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
//
2+
// ViewComponetMarkdown.swift
3+
// SwiftPamphletApp
4+
//
5+
// Created by Ming Dai on 2024/4/7.
6+
//
7+
8+
import Foundation
9+
import SwiftUI
10+
import MarkdownUI
11+
12+
// MARK: - MarkdownUI
13+
struct MarkdownView: View {
14+
var s: String
15+
var body: some View {
16+
Markdown(s)
17+
.markdownTheme(.gitHubCustom)
18+
}
19+
}
20+
21+
extension Theme {
22+
/// A theme that mimics the GitHub style.
23+
///
24+
/// Style | Preview
25+
/// --- | ---
26+
/// Inline text | ![](GitHubInlines)
27+
/// Headings | ![](GitHubHeading)
28+
/// Blockquote | ![](GitHubBlockquote)
29+
/// Code block | ![](GitHubCodeBlock)
30+
/// Image | ![](GitHubImage)
31+
/// Task list | ![](GitHubTaskList)
32+
/// Bulleted list | ![](GitHubNestedBulletedList)
33+
/// Numbered list | ![](GitHubNumberedList)
34+
/// Table | ![](GitHubTable)
35+
public static let gitHubCustom = Theme()
36+
.text {
37+
ForegroundColor(.text)
38+
BackgroundColor(.background)
39+
FontSize(16)
40+
}
41+
.code {
42+
FontFamilyVariant(.monospaced)
43+
FontSize(.em(0.85))
44+
BackgroundColor(.secondaryBackground)
45+
}
46+
.strong {
47+
FontWeight(.semibold)
48+
}
49+
.link {
50+
ForegroundColor(.link)
51+
}
52+
.heading1 { configuration in
53+
VStack(alignment: .leading, spacing: 0) {
54+
configuration.label
55+
.relativePadding(.bottom, length: .em(0.3))
56+
.relativeLineSpacing(.em(0.125))
57+
.markdownMargin(top: 24, bottom: 16)
58+
.markdownTextStyle {
59+
FontWeight(.semibold)
60+
FontSize(.em(2))
61+
}
62+
Divider().overlay(Color.divider)
63+
}
64+
}
65+
.heading2 { configuration in
66+
VStack(alignment: .leading, spacing: 0) {
67+
configuration.label
68+
.relativePadding(.bottom, length: .em(0.3))
69+
.relativeLineSpacing(.em(0.125))
70+
.markdownMargin(top: 24, bottom: 16)
71+
.markdownTextStyle {
72+
FontWeight(.semibold)
73+
FontSize(.em(1.5))
74+
}
75+
Divider().overlay(Color.divider)
76+
}
77+
}
78+
.heading3 { configuration in
79+
configuration.label
80+
.relativeLineSpacing(.em(0.125))
81+
.markdownMargin(top: 24, bottom: 16)
82+
.markdownTextStyle {
83+
FontWeight(.semibold)
84+
FontSize(.em(1.25))
85+
}
86+
}
87+
.heading4 { configuration in
88+
configuration.label
89+
.relativeLineSpacing(.em(0.125))
90+
.markdownMargin(top: 24, bottom: 16)
91+
.markdownTextStyle {
92+
FontWeight(.semibold)
93+
}
94+
}
95+
.heading5 { configuration in
96+
configuration.label
97+
.relativeLineSpacing(.em(0.125))
98+
.markdownMargin(top: 24, bottom: 16)
99+
.markdownTextStyle {
100+
FontWeight(.semibold)
101+
FontSize(.em(0.875))
102+
}
103+
}
104+
.heading6 { configuration in
105+
configuration.label
106+
.relativeLineSpacing(.em(0.125))
107+
.markdownMargin(top: 24, bottom: 16)
108+
.markdownTextStyle {
109+
FontWeight(.semibold)
110+
FontSize(.em(0.85))
111+
ForegroundColor(.tertiaryText)
112+
}
113+
}
114+
.paragraph { configuration in
115+
configuration.label
116+
.fixedSize(horizontal: false, vertical: true)
117+
.relativeLineSpacing(.em(0.25))
118+
.markdownMargin(top: 0, bottom: 16)
119+
}
120+
.blockquote { configuration in
121+
HStack(spacing: 0) {
122+
RoundedRectangle(cornerRadius: 6)
123+
.fill(Color.border)
124+
.relativeFrame(width: .em(0.2))
125+
configuration.label
126+
.markdownTextStyle { ForegroundColor(.secondaryText) }
127+
.relativePadding(.horizontal, length: .em(1))
128+
}
129+
.fixedSize(horizontal: false, vertical: true)
130+
}
131+
.codeBlock { configuration in
132+
configuration.label
133+
.fixedSize(horizontal: false, vertical: true)
134+
.relativeLineSpacing(.em(0.225))
135+
.markdownTextStyle {
136+
FontFamilyVariant(.monospaced)
137+
FontSize(.em(0.85))
138+
}
139+
.padding(16)
140+
.background(Color.secondaryBackground)
141+
.clipShape(RoundedRectangle(cornerRadius: 6))
142+
.markdownMargin(top: 0, bottom: 16)
143+
}
144+
.listItem { configuration in
145+
configuration.label
146+
.markdownMargin(top: .em(0.25))
147+
}
148+
.taskListMarker { configuration in
149+
Image(systemName: configuration.isCompleted ? "checkmark.square.fill" : "square")
150+
.symbolRenderingMode(.hierarchical)
151+
.foregroundStyle(Color.checkbox, Color.checkboxBackground)
152+
.imageScale(.small)
153+
.relativeFrame(minWidth: .em(1.5), alignment: .trailing)
154+
}
155+
.table { configuration in
156+
configuration.label
157+
.fixedSize(horizontal: false, vertical: true)
158+
.markdownTableBorderStyle(.init(color: .border))
159+
.markdownTableBackgroundStyle(
160+
.alternatingRows(Color.background, Color.secondaryBackground)
161+
)
162+
.markdownMargin(top: 0, bottom: 16)
163+
}
164+
.tableCell { configuration in
165+
configuration.label
166+
.markdownTextStyle {
167+
if configuration.row == 0 {
168+
FontWeight(.semibold)
169+
}
170+
BackgroundColor(nil)
171+
}
172+
.fixedSize(horizontal: false, vertical: true)
173+
.padding(.vertical, 6)
174+
.padding(.horizontal, 13)
175+
.relativeLineSpacing(.em(0.25))
176+
}
177+
.thematicBreak {
178+
Divider()
179+
.relativeFrame(height: .em(0.25))
180+
.overlay(Color.border)
181+
.markdownMargin(top: 24, bottom: 24)
182+
}
183+
}
184+
185+
extension Color {
186+
fileprivate static let text = Color(
187+
light: Color(rgba: 0x0606_06ff), dark: Color(rgba: 0xfbfb_fcff)
188+
)
189+
fileprivate static let secondaryText = Color(
190+
light: Color(rgba: 0x6b6e_7bff), dark: Color(rgba: 0x9294_a0ff)
191+
)
192+
fileprivate static let tertiaryText = Color(
193+
light: Color(rgba: 0x6b6e_7bff), dark: Color(rgba: 0x6d70_7dff)
194+
)
195+
fileprivate static let background = Color(
196+
light: .white, dark: Color(rgba: 0x1819_1dff)
197+
)
198+
fileprivate static let secondaryBackground = Color(
199+
light: Color(rgba: 0xf7f7_f9ff), dark: Color(rgba: 0x2526_2aff)
200+
)
201+
fileprivate static let link = Color(
202+
light: Color(rgba: 0x2c65_cfff), dark: Color(rgba: 0x4c8e_f8ff)
203+
)
204+
fileprivate static let border = Color(
205+
light: Color(rgba: 0xe4e4_e8ff), dark: Color(rgba: 0x4244_4eff)
206+
)
207+
fileprivate static let divider = Color(
208+
light: Color(rgba: 0xd0d0_d3ff), dark: Color(rgba: 0x3334_38ff)
209+
)
210+
fileprivate static let checkbox = Color(rgba: 0xb9b9_bbff)
211+
fileprivate static let checkboxBackground = Color(rgba: 0xeeee_efff)
212+
}

0 commit comments

Comments
 (0)