Skip to content

Commit

Permalink
修复 markdown 水平滚动阻碍垂直滚动问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Apr 7, 2024
1 parent 1eb2ef7 commit baffd88
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 24 deletions.
4 changes: 4 additions & 0 deletions SwiftPamphletApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
0896FB9227BA486900676B7F /* 145.md in Resources */ = {isa = PBXBuildFile; fileRef = 0896FB9127BA486900676B7F /* 145.md */; };
08A4FDC227B25A140068E5BC /* 141.md in Resources */ = {isa = PBXBuildFile; fileRef = 08A4FDC127B25A140068E5BC /* 141.md */; };
08A9E19F2BC18EF400A73764 /* APIVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E19E2BC18EF400A73764 /* APIVM.swift */; };
08A9E1A22BC25D0700A73764 /* ViewComponetMarkdown.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08A9E1A12BC25D0700A73764 /* ViewComponetMarkdown.swift */; };
08AEAEDD277EA64900B969E2 /* DBDevNoti.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08AEAEDC277EA64900B969E2 /* DBDevNoti.swift */; };
08AEAEE1277EA70500B969E2 /* DBRepoStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08AEAEE0277EA70500B969E2 /* DBRepoStore.swift */; };
08AEAEF1277F09D000B969E2 /* IntroView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08AEAEF0277F09D000B969E2 /* IntroView.swift */; };
Expand Down Expand Up @@ -475,6 +476,7 @@
0896FB9127BA486900676B7F /* 145.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = 145.md; sourceTree = "<group>"; };
08A4FDC127B25A140068E5BC /* 141.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = 141.md; sourceTree = "<group>"; };
08A9E19E2BC18EF400A73764 /* APIVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIVM.swift; sourceTree = "<group>"; };
08A9E1A12BC25D0700A73764 /* ViewComponetMarkdown.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewComponetMarkdown.swift; sourceTree = "<group>"; };
08AEAEDC277EA64900B969E2 /* DBDevNoti.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DBDevNoti.swift; sourceTree = "<group>"; };
08AEAEE0277EA70500B969E2 /* DBRepoStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DBRepoStore.swift; sourceTree = "<group>"; };
08AEAEF0277F09D000B969E2 /* IntroView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntroView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1211,6 +1213,7 @@
isa = PBXGroup;
children = (
086A5F512744EF4C00FECE02 /* ViewComponent.swift */,
08A9E1A12BC25D0700A73764 /* ViewComponetMarkdown.swift */,
08F14B432BBE2865005B46CC /* ViewComponentImage.swift */,
0805F4952BAAABEA0008BB52 /* ViewStyle.swift */,
);
Expand Down Expand Up @@ -1742,6 +1745,7 @@
08448F0B2797F73200B61353 /* PlayArchitecture.swift in Sources */,
08038767276700F100519B15 /* CCYRESTfulAPI.swift in Sources */,
08522BD827CF344B005FF059 /* PlaySliderView.swift in Sources */,
08A9E1A22BC25D0700A73764 /* ViewComponetMarkdown.swift in Sources */,
0887A59A2BA28F6D00131359 /* CSGuideView.swift in Sources */,
086A5F362744ED9600FECE02 /* RepoView.swift in Sources */,
086A5F302744ED8600FECE02 /* IssuesListFromCustomView.swift in Sources */,
Expand Down
22 changes: 15 additions & 7 deletions SwiftPamphletApp/GitHubAPI/APIVM/APIVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class APIRepoVM {
var name: String = ""
var repo: RepoModel = RepoModel()
var commits: [CommitModel] = [CommitModel]()
var issues: [IssueModel] = [IssueModel]()

init(name: String) {
self.name = name
Expand All @@ -21,17 +22,16 @@ final class APIRepoVM {
func updateAllData() async {
await obtainRepos()
await obtainCommits()
await obtainIssues()
}

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

// https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28
Expand All @@ -40,13 +40,21 @@ final class APIRepoVM {
do {
let (data, _) = try await URLSession.shared.data(for: GitHubReq.req("repos/\(name)/commits"))
commits = try GitHubReq.jsonDecoder().decode([CommitModel].self, from: data)
} catch {
print("问题是:\(error)")
}
} catch { print("问题是:\(error)") }
}

// https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28
@MainActor
func obtainIssues() async {
do {
let (data, _) = try await URLSession.shared.data(for: GitHubReq.req("repos/\(name)/issues"))
issues = try GitHubReq.jsonDecoder().decode([IssueModel].self, from: data)
} catch { print("问题是:\(error)") }
}

}

// github api 入口 https://api.github.com
class GitHubReq {
static func jsonDecoder() -> JSONDecoder {
let de = JSONDecoder()
Expand Down
10 changes: 1 addition & 9 deletions SwiftPamphletApp/GitHubAPI/Developer/EditDeveloper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,12 @@ struct EditDeveloper: View {
.tabItem {
Text("新提交")
}
// .onAppear {
// Task {
// await repoVM.obtainCommits()
// }
// }
.tag(1)

IssuesView(issues: vmRepo.issues, repo: vmRepo.repo)
IssuesView(issues: repoVM.issues, repo: repoVM.repo)
.tabItem {
Text("议题列表")
}
.onAppear {
vmRepo.doing(.inIssues)
}
.tag(2)

IssueEventsView(issueEvents: vmRepo.issueEvents, repo: vmRepo.repo)
Expand Down
9 changes: 1 addition & 8 deletions SwiftPamphletApp/ViewComponet/ViewComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,7 @@ struct SideBarLabel: View {
}
}

// MARK: - MarkdownUI
struct MarkdownView: View {
var s: String
var body: some View {
Markdown(s)
.markdownTheme(.gitHub)
}
}


// MARK: - 共享菜单
struct ShareView: View {
Expand Down
212 changes: 212 additions & 0 deletions SwiftPamphletApp/ViewComponet/ViewComponetMarkdown.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
//
// ViewComponetMarkdown.swift
// SwiftPamphletApp
//
// Created by Ming Dai on 2024/4/7.
//

import Foundation
import SwiftUI
import MarkdownUI

// MARK: - MarkdownUI
struct MarkdownView: View {
var s: String
var body: some View {
Markdown(s)
.markdownTheme(.gitHubCustom)
}
}

extension Theme {
/// A theme that mimics the GitHub style.
///
/// Style | Preview
/// --- | ---
/// Inline text | ![](GitHubInlines)
/// Headings | ![](GitHubHeading)
/// Blockquote | ![](GitHubBlockquote)
/// Code block | ![](GitHubCodeBlock)
/// Image | ![](GitHubImage)
/// Task list | ![](GitHubTaskList)
/// Bulleted list | ![](GitHubNestedBulletedList)
/// Numbered list | ![](GitHubNumberedList)
/// Table | ![](GitHubTable)
public static let gitHubCustom = Theme()
.text {
ForegroundColor(.text)
BackgroundColor(.background)
FontSize(16)
}
.code {
FontFamilyVariant(.monospaced)
FontSize(.em(0.85))
BackgroundColor(.secondaryBackground)
}
.strong {
FontWeight(.semibold)
}
.link {
ForegroundColor(.link)
}
.heading1 { configuration in
VStack(alignment: .leading, spacing: 0) {
configuration.label
.relativePadding(.bottom, length: .em(0.3))
.relativeLineSpacing(.em(0.125))
.markdownMargin(top: 24, bottom: 16)
.markdownTextStyle {
FontWeight(.semibold)
FontSize(.em(2))
}
Divider().overlay(Color.divider)
}
}
.heading2 { configuration in
VStack(alignment: .leading, spacing: 0) {
configuration.label
.relativePadding(.bottom, length: .em(0.3))
.relativeLineSpacing(.em(0.125))
.markdownMargin(top: 24, bottom: 16)
.markdownTextStyle {
FontWeight(.semibold)
FontSize(.em(1.5))
}
Divider().overlay(Color.divider)
}
}
.heading3 { configuration in
configuration.label
.relativeLineSpacing(.em(0.125))
.markdownMargin(top: 24, bottom: 16)
.markdownTextStyle {
FontWeight(.semibold)
FontSize(.em(1.25))
}
}
.heading4 { configuration in
configuration.label
.relativeLineSpacing(.em(0.125))
.markdownMargin(top: 24, bottom: 16)
.markdownTextStyle {
FontWeight(.semibold)
}
}
.heading5 { configuration in
configuration.label
.relativeLineSpacing(.em(0.125))
.markdownMargin(top: 24, bottom: 16)
.markdownTextStyle {
FontWeight(.semibold)
FontSize(.em(0.875))
}
}
.heading6 { configuration in
configuration.label
.relativeLineSpacing(.em(0.125))
.markdownMargin(top: 24, bottom: 16)
.markdownTextStyle {
FontWeight(.semibold)
FontSize(.em(0.85))
ForegroundColor(.tertiaryText)
}
}
.paragraph { configuration in
configuration.label
.fixedSize(horizontal: false, vertical: true)
.relativeLineSpacing(.em(0.25))
.markdownMargin(top: 0, bottom: 16)
}
.blockquote { configuration in
HStack(spacing: 0) {
RoundedRectangle(cornerRadius: 6)
.fill(Color.border)
.relativeFrame(width: .em(0.2))
configuration.label
.markdownTextStyle { ForegroundColor(.secondaryText) }
.relativePadding(.horizontal, length: .em(1))
}
.fixedSize(horizontal: false, vertical: true)
}
.codeBlock { configuration in
configuration.label
.fixedSize(horizontal: false, vertical: true)
.relativeLineSpacing(.em(0.225))
.markdownTextStyle {
FontFamilyVariant(.monospaced)
FontSize(.em(0.85))
}
.padding(16)
.background(Color.secondaryBackground)
.clipShape(RoundedRectangle(cornerRadius: 6))
.markdownMargin(top: 0, bottom: 16)
}
.listItem { configuration in
configuration.label
.markdownMargin(top: .em(0.25))
}
.taskListMarker { configuration in
Image(systemName: configuration.isCompleted ? "checkmark.square.fill" : "square")
.symbolRenderingMode(.hierarchical)
.foregroundStyle(Color.checkbox, Color.checkboxBackground)
.imageScale(.small)
.relativeFrame(minWidth: .em(1.5), alignment: .trailing)
}
.table { configuration in
configuration.label
.fixedSize(horizontal: false, vertical: true)
.markdownTableBorderStyle(.init(color: .border))
.markdownTableBackgroundStyle(
.alternatingRows(Color.background, Color.secondaryBackground)
)
.markdownMargin(top: 0, bottom: 16)
}
.tableCell { configuration in
configuration.label
.markdownTextStyle {
if configuration.row == 0 {
FontWeight(.semibold)
}
BackgroundColor(nil)
}
.fixedSize(horizontal: false, vertical: true)
.padding(.vertical, 6)
.padding(.horizontal, 13)
.relativeLineSpacing(.em(0.25))
}
.thematicBreak {
Divider()
.relativeFrame(height: .em(0.25))
.overlay(Color.border)
.markdownMargin(top: 24, bottom: 24)
}
}

extension Color {
fileprivate static let text = Color(
light: Color(rgba: 0x0606_06ff), dark: Color(rgba: 0xfbfb_fcff)
)
fileprivate static let secondaryText = Color(
light: Color(rgba: 0x6b6e_7bff), dark: Color(rgba: 0x9294_a0ff)
)
fileprivate static let tertiaryText = Color(
light: Color(rgba: 0x6b6e_7bff), dark: Color(rgba: 0x6d70_7dff)
)
fileprivate static let background = Color(
light: .white, dark: Color(rgba: 0x1819_1dff)
)
fileprivate static let secondaryBackground = Color(
light: Color(rgba: 0xf7f7_f9ff), dark: Color(rgba: 0x2526_2aff)
)
fileprivate static let link = Color(
light: Color(rgba: 0x2c65_cfff), dark: Color(rgba: 0x4c8e_f8ff)
)
fileprivate static let border = Color(
light: Color(rgba: 0xe4e4_e8ff), dark: Color(rgba: 0x4244_4eff)
)
fileprivate static let divider = Color(
light: Color(rgba: 0xd0d0_d3ff), dark: Color(rgba: 0x3334_38ff)
)
fileprivate static let checkbox = Color(rgba: 0xb9b9_bbff)
fileprivate static let checkboxBackground = Color(rgba: 0xeeee_efff)
}

0 comments on commit baffd88

Please sign in to comment.