Skip to content

Commit

Permalink
Organize source (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal authored Dec 9, 2022
1 parent 49101f6 commit 0838208
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 137 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions Sources/MarkdownUI/Views/Blocks/BlockSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,16 @@ extension BlockSequence where Data == [Block], Content == Block {
self.init(blocks) { $1 }
}
}

extension TextAlignment {
fileprivate var alignment: Alignment {
switch self {
case .leading:
return .leading
case .center:
return .center
case .trailing:
return .trailing
}
}
}
14 changes: 0 additions & 14 deletions Sources/MarkdownUI/Views/Common/TextAlignment+Alignment.swift

This file was deleted.

12 changes: 12 additions & 0 deletions Sources/MarkdownUI/Views/Environment/Environment+BaseURL.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import SwiftUI

extension EnvironmentValues {
var markdownBaseURL: URL? {
get { self[MarkdownBaseURLKey.self] }
set { self[MarkdownBaseURLKey.self] = newValue }
}
}

private struct MarkdownBaseURLKey: EnvironmentKey {
static var defaultValue: URL? = nil
}
30 changes: 30 additions & 0 deletions Sources/MarkdownUI/Views/Environment/Environment+Image.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import SwiftUI

extension View {
public func markdownImageLoader(
_ imageLoader: ImageLoader?,
forURLScheme urlScheme: String
) -> some View {
environment(\.imageLoaderRegistry[urlScheme], imageLoader)
}
}

extension EnvironmentValues {
var imageLoaderRegistry: [String: ImageLoader] {
get { self[ImageLoaderRegistryKey.self] }
set { self[ImageLoaderRegistryKey.self] = newValue }
}

var imageTransaction: Transaction {
get { self[ImageTransactionKey.self] }
set { self[ImageTransactionKey.self] = newValue }
}
}

private struct ImageLoaderRegistryKey: EnvironmentKey {
static var defaultValue: [String: ImageLoader] = [:]
}

private struct ImageTransactionKey: EnvironmentKey {
static var defaultValue = Transaction()
}
21 changes: 21 additions & 0 deletions Sources/MarkdownUI/Views/Environment/Environment+List.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import SwiftUI

extension EnvironmentValues {
var listLevel: Int {
get { self[ListLevelKey.self] }
set { self[ListLevelKey.self] = newValue }
}

var tightSpacingEnabled: Bool {
get { self[TightSpacingEnabledKey.self] }
set { self[TightSpacingEnabledKey.self] = newValue }
}
}

private struct ListLevelKey: EnvironmentKey {
static var defaultValue = 0
}

private struct TightSpacingEnabledKey: EnvironmentKey {
static var defaultValue = false
}
24 changes: 24 additions & 0 deletions Sources/MarkdownUI/Views/Environment/Environment+Theme.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import SwiftUI

extension View {
public func markdownTheme(_ theme: Theme) -> some View {
environment(\.theme, theme)
}

public func markdownTheme<V>(
_ keyPath: WritableKeyPath<Theme, V>, _ value: V
) -> some View {
environment((\EnvironmentValues.theme).appending(path: keyPath), value)
}
}

extension EnvironmentValues {
var theme: Theme {
get { self[ThemeKey.self] }
set { self[ThemeKey.self] = newValue }
}
}

private struct ThemeKey: EnvironmentKey {
static var defaultValue: Theme = .default
}
96 changes: 0 additions & 96 deletions Sources/MarkdownUI/Views/Environment/Environment.swift

This file was deleted.

26 changes: 26 additions & 0 deletions Sources/MarkdownUI/Views/Inlines/ImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,29 @@ extension ImageView {
}
}
}

extension View {
fileprivate func link(destination: String?) -> some View {
self.modifier(LinkModifier(destination: destination))
}
}

private struct LinkModifier: ViewModifier {
@Environment(\.markdownBaseURL) private var baseURL
@Environment(\.openURL) private var openURL

let destination: String?

func body(content: Content) -> some View {
if let url = self.destination.flatMap(URL.init(string:))?.relativeTo(self.baseURL) {
Button {
self.openURL(url)
} label: {
content
}
.buttonStyle(.plain)
} else {
content
}
}
}
27 changes: 0 additions & 27 deletions Sources/MarkdownUI/Views/Inlines/LinkModifier.swift

This file was deleted.

0 comments on commit 0838208

Please sign in to comment.