From 1042386bfb40b692ee06d33172b4065677353a88 Mon Sep 17 00:00:00 2001 From: WindowsMEMZ Date: Sat, 24 Feb 2024 15:42:10 +0800 Subject: [PATCH] rev: Reverted dynamic link detect because of UI issue --- DarockBili.xcodeproj/project.pbxproj | 20 ++--- Localizable.xcstrings | 1 + MeowBili/Others/LinkDetectText.swift | 110 ++++++++++++++------------- 3 files changed, 69 insertions(+), 62 deletions(-) diff --git a/DarockBili.xcodeproj/project.pbxproj b/DarockBili.xcodeproj/project.pbxproj index dc7334af2..4b40dade3 100644 --- a/DarockBili.xcodeproj/project.pbxproj +++ b/DarockBili.xcodeproj/project.pbxproj @@ -1461,7 +1461,7 @@ ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO; CODE_SIGN_ENTITLEMENTS = "MeowBili/MeowBili Vision App.entitlements"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 842; + CURRENT_PROJECT_VERSION = 846; DEVELOPMENT_ASSET_PATHS = "\"MeowBili/Preview Content\""; DEVELOPMENT_TEAM = B57D8PP775; ENABLE_PREVIEWS = YES; @@ -1496,7 +1496,7 @@ ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = NO; CODE_SIGN_ENTITLEMENTS = "MeowBili/MeowBili Vision App.entitlements"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 842; + CURRENT_PROJECT_VERSION = 846; DEVELOPMENT_ASSET_PATHS = "\"MeowBili/Preview Content\""; DEVELOPMENT_TEAM = B57D8PP775; ENABLE_PREVIEWS = YES; @@ -1533,7 +1533,7 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = MeowBili/MeowBili.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 842; + CURRENT_PROJECT_VERSION = 846; DEVELOPMENT_ASSET_PATHS = "\"MeowBili/Preview Content\""; DEVELOPMENT_TEAM = B57D8PP775; ENABLE_PREVIEWS = YES; @@ -1580,7 +1580,7 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_ENTITLEMENTS = MeowBili/MeowBili.entitlements; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 842; + CURRENT_PROJECT_VERSION = 846; DEVELOPMENT_ASSET_PATHS = "\"MeowBili/Preview Content\""; DEVELOPMENT_TEAM = B57D8PP775; ENABLE_PREVIEWS = YES; @@ -1624,7 +1624,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIconWatch; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 842; + CURRENT_PROJECT_VERSION = 846; DEVELOPMENT_ASSET_PATHS = "\"MeowBili/Preview\\ Content\""; DEVELOPMENT_TEAM = B57D8PP775; ENABLE_PREVIEWS = YES; @@ -1660,7 +1660,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIconWatch; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 842; + CURRENT_PROJECT_VERSION = 846; DEVELOPMENT_ASSET_PATHS = "\"MeowBili/Preview\\ Content\""; DEVELOPMENT_TEAM = B57D8PP775; ENABLE_PREVIEWS = YES; @@ -1699,7 +1699,7 @@ CODE_SIGN_ENTITLEMENTS = "MeowBili/MeowBili Mac App.entitlements"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 842; + CURRENT_PROJECT_VERSION = 846; DEVELOPMENT_ASSET_PATHS = "\"MeowBili/Preview Content\""; DEVELOPMENT_TEAM = B57D8PP775; ENABLE_HARDENED_RUNTIME = YES; @@ -1732,7 +1732,7 @@ CODE_SIGN_ENTITLEMENTS = "MeowBili/MeowBili Mac App.entitlements"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 842; + CURRENT_PROJECT_VERSION = 846; DEVELOPMENT_ASSET_PATHS = "\"MeowBili/Preview Content\""; DEVELOPMENT_TEAM = B57D8PP775; ENABLE_HARDENED_RUNTIME = YES; @@ -1761,7 +1761,7 @@ isa = XCBuildConfiguration; buildSettings = { CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 842; + CURRENT_PROJECT_VERSION = 846; DEVELOPMENT_TEAM = B57D8PP775; GENERATE_INFOPLIST_FILE = YES; IPHONEOS_DEPLOYMENT_TARGET = 16.0; @@ -1784,7 +1784,7 @@ isa = XCBuildConfiguration; buildSettings = { CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 842; + CURRENT_PROJECT_VERSION = 846; DEVELOPMENT_TEAM = B57D8PP775; GENERATE_INFOPLIST_FILE = YES; IPHONEOS_DEPLOYMENT_TARGET = 16.0; diff --git a/Localizable.xcstrings b/Localizable.xcstrings index ccb6d598f..624e76681 100644 --- a/Localizable.xcstrings +++ b/Localizable.xcstrings @@ -8644,6 +8644,7 @@ } }, "文本加载失败" : { + "extractionState" : "stale", "localizations" : { "en" : { "stringUnit" : { diff --git a/MeowBili/Others/LinkDetectText.swift b/MeowBili/Others/LinkDetectText.swift index 8795114f9..f74869845 100644 --- a/MeowBili/Others/LinkDetectText.swift +++ b/MeowBili/Others/LinkDetectText.swift @@ -25,47 +25,53 @@ struct LinkDetectText: View { @State var markdownText = try! AttributedString(markdown: "") @State var linkColor = Color.blue @State var error: Error? + @State var isFinishedLoad = false var body: some View { - Group { - if let error { - VStack(alignment: .leading, content: { - Text("文本加载失败") - Text(error.localizedDescription) - .font(.footnote) - }) - } else { - Text(markdownText) - .tint(linkColor) - #if os(watchOS) - //拦截,然后用暗礁浏览器打开 - .environment(\.openURL, OpenURLAction(handler: { url in - //加一个小动画,因为下一步操作【真机上】会卡1秒 - bling() - // Source: https://www.reddit.com/r/apple/comments/rcn2h7/comment/hnwr8do/ - let session = ASWebAuthenticationSession( - url: url, - callbackURLScheme: nil - ) { _, _ in - } - - // Makes the "Watch App Wants To Use example.com to Sign In" popup not show up - session.prefersEphemeralWebBrowserSession = true - - session.start() - return .handled - })) - //iOS端可以直接用系统浏览器打开链接,不需要拦截 - #endif - } - } - .onAppear { - loadText(inputURL: inputURL) - } - .onChange(of: inputURL, perform: { newValue in - loadText(inputURL: newValue) - }) - .animation(.smooth, value: markdownText) - +// Group { +// if let error { +// VStack(alignment: .leading, content: { +// Text("文本加载失败") +// Text(error.localizedDescription) +// .font(.footnote) +// }) +// } else { +// Text(markdownText) +// .tint(linkColor) +// #if os(watchOS) +// //拦截,然后用暗礁浏览器打开 +// .environment(\.openURL, OpenURLAction(handler: { url in +// //加一个小动画,因为下一步操作【真机上】会卡1秒 +// bling() +// // Source: https://www.reddit.com/r/apple/comments/rcn2h7/comment/hnwr8do/ +// let session = ASWebAuthenticationSession( +// url: url, +// callbackURLScheme: nil +// ) { _, _ in +// } +// +// // Makes the "Watch App Wants To Use example.com to Sign In" popup not show up +// session.prefersEphemeralWebBrowserSession = true +// +// session.start() +// return .handled +// })) +// //iOS端可以直接用系统浏览器打开链接,不需要拦截 +// #endif +// } +// } +// .onAppear { +// if !isFinishedLoad { +// loadText(inputURL: inputURL) +// isFinishedLoad = true +// } +// } +// .onChange(of: inputURL, perform: { newValue in +// loadText(inputURL: newValue) +// }) +// .animation(.smooth, value: markdownText) + // FIXME: The view commented shows empty view when fast scrolling in the dynamic page. + // Tip: Reverted on February 24, 2024 + Text(inputURL) } func loadText(inputURL: String) { let markdownURLs = detectURLs(in: inputURL) @@ -88,19 +94,19 @@ struct LinkDetectText: View { }) } func detectURLs(in text: String) -> String { - let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) - let matches = detector?.matches(in: text, options: [], range: NSRange(location: 0, length: text.utf16.count)) - - var markdownString = text - if let matches = matches { - for match in matches { - if let url = match.url { - let link = "[\(url.absoluteString)](\(url.absoluteString))" - markdownString = markdownString.replacingOccurrences(of: url.absoluteString, with: link) - } + let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) + let matches = detector?.matches(in: text, options: [], range: NSRange(location: 0, length: text.utf16.count)) + + var markdownString = text + if let matches = matches { + for match in matches { + if let url = match.url { + let link = "[\(url.absoluteString)](\(url.absoluteString))" + markdownString = markdownString.replacingOccurrences(of: url.absoluteString, with: link) } } - - return markdownString } + + return markdownString + } }