From 1c0a625192d5292de33e5c75cf78110dbe6a3444 Mon Sep 17 00:00:00 2001 From: wangdl Date: Thu, 18 Jun 2026 21:19:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20"=E9=98=85=E8=AF=BB=E5=8E=9F?= =?UTF-8?q?=E6=96=87=E4=BB=B6"=20button=20to=20KnowledgeDetailPage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Blue gradient button at bottom of plain text content view. Writes content to temp .md file and navigates to MaterialReaderView, which triggers reading event collection. Co-Authored-By: Claude Opus 4.7 --- .../Features/Library/LibrarySubpages.swift | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift b/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift index 997c14a..d8026a6 100644 --- a/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift +++ b/AIStudyApp/AIStudyApp/Features/Library/LibrarySubpages.swift @@ -1109,6 +1109,28 @@ struct KnowledgeDetailPage: View { .padding(16).background(Color.zxFill004).clipShape(RoundedRectangle(cornerRadius: 14)) } Text(content).font(.system(size: 14)).foregroundColor(Color.zxF0).lineSpacing(6) + + // 阅读原文件 button + if let path = vm.localPath ?? (try? writeTempFile(content: content, itemId: item.id)), let mt = vm.detectedType ?? detectTypeFromContent(content, sourceType: item.sourceType) { + NavigationLink(value: Route.materialReader( + materialId: item.id, + filePath: path, + materialType: mt, + knowledgeBaseId: item.knowledgeBaseId, + title: item.title + )) { + HStack(spacing: 8) { + Image(systemName: "book") + Text("阅读原文件") + } + .font(.system(size: 15, weight: .bold)) + .foregroundColor(.white) + .frame(maxWidth: .infinity).frame(height: 48) + .background(LinearGradient(colors: [Color.zxPurple, Color.zxAccent], startPoint: .leading, endPoint: .trailing)) + .clipShape(RoundedRectangle(cornerRadius: 14)) + } + .padding(.top, 8) + } }.padding(.horizontal, 20).padding(.top, 8).padding(.bottom, 100) }.scrollIndicators(.hidden) } @@ -1452,3 +1474,30 @@ struct EditKnowledgePage: View { }.navigationBarTitleDisplayMode(.inline).toolbarBackground(.hidden, for: .navigationBar).toolbar(.hidden, for: .tabBar) } } + +// MARK: - Helpers + +private func writeTempFile(content: String, itemId: String) throws -> String { + let dir = FileManager.default.temporaryDirectory + let url = dir.appendingPathComponent("kd_text_\(itemId).md") + try content.write(to: url, atomically: true, encoding: .utf8) + return url.path +} + +private func detectTypeFromContent(_ content: String, sourceType: String?) -> MaterialType? { + if let st = sourceType { + switch st.lowercased() { + case "markdown", "md": return .markdown + case "txt", "text": return .text + case "pdf": return .pdf + case "epub": return .epub + case "docx", "doc": return .word + case "xlsx", "xls": return .excel + case "pptx", "ppt": return .powerPoint + default: break + } + } + // Detect from content + if content.hasPrefix("#") || content.contains("```") { return .markdown } + return .text +}