mirror of
				https://github.com/yattee/yattee.git
				synced 2025-11-04 06:32:03 +00:00 
			
		
		
		
	make links in description clickable
Adds clickable links to macOS. I it is a naive approach, but it works. Probably closes #51
This commit is contained in:
		@@ -61,7 +61,7 @@ struct VideoDescription: View {
 | 
			
		||||
        #if !os(iOS)
 | 
			
		||||
            Group {
 | 
			
		||||
                if #available(macOS 12, *) {
 | 
			
		||||
                    Text(description)
 | 
			
		||||
                    DescriptionWithLinks(description: description, detailsSize: detailsSize)
 | 
			
		||||
                        .frame(maxWidth: .infinity, alignment: .leading)
 | 
			
		||||
                        .lineLimit(shouldExpand ? 500 : Self.collapsedLines)
 | 
			
		||||
                    #if !os(tvOS)
 | 
			
		||||
@@ -79,6 +79,33 @@ struct VideoDescription: View {
 | 
			
		||||
        #endif
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // If possibe convert URLs to clickable links
 | 
			
		||||
    #if os(macOS)
 | 
			
		||||
    @available(macOS 12, *)
 | 
			
		||||
    struct DescriptionWithLinks: View {
 | 
			
		||||
        let description: String
 | 
			
		||||
        let detailsSize: CGSize?
 | 
			
		||||
        let separators = CharacterSet(charactersIn: " \n")
 | 
			
		||||
 | 
			
		||||
        var formattedString: AttributedString {
 | 
			
		||||
            var attrString = AttributedString(description)
 | 
			
		||||
            let words = description.unicodeScalars.split(whereSeparator: separators.contains).map(String.init)
 | 
			
		||||
            words.forEach { word in
 | 
			
		||||
                if (word.hasPrefix("https://") || word.hasPrefix("http://")), let url = URL(string: String(word)) {
 | 
			
		||||
                    if let range = attrString.range(of: word) {
 | 
			
		||||
                        attrString[range].link = url
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return attrString
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        var body: some View {
 | 
			
		||||
                Text(formattedString)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    #endif
 | 
			
		||||
 | 
			
		||||
    @ViewBuilder var keywords: some View {
 | 
			
		||||
        if showKeywords {
 | 
			
		||||
            ScrollView(.horizontal, showsIndicators: showScrollIndicators) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user