Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table border style now has multiple colors and selectors #327

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions Sources/MarkdownUI/Theme/BlockStyle/TableBorderStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,37 @@ import SwiftUI
///
/// ![](CustomTableBorders)
public struct TableBorderStyle {
/// The visible table borders.
public var visibleBorders: TableBorderSelector
public struct Border {
/// The visible table borders.
public var visibleBorders: TableBorderSelector

/// The table border color.
public var color: Color
/// The table border color.
public var color: Color

public init(visibleBorders: TableBorderSelector, color: Color) {
self.visibleBorders = visibleBorders
self.color = color
}
}

/// The table's list of borders
public var borders: [Border]

/// The table border stroke style.
public var strokeStyle: StrokeStyle

/// Creates a table border style with the given borders and stroke style.
/// - Parameters:
/// - borders: The visible table borders.
/// - strokeStyle: The table border stroke style.
public init(
_ borders: [Border],
strokeStyle: StrokeStyle
) {
self.borders = borders
self.strokeStyle = strokeStyle
}

/// Creates a table border style with the given visible borders, color, and stroke style.
/// - Parameters:
/// - visibleBorders: The visible table borders.
Expand All @@ -50,8 +72,7 @@ public struct TableBorderStyle {
color: Color,
strokeStyle: StrokeStyle
) {
self.visibleBorders = visibleBorders
self.color = color
self.borders = [.init(visibleBorders: visibleBorders, color: color)]
self.strokeStyle = strokeStyle
}

Expand Down
72 changes: 72 additions & 0 deletions Sources/MarkdownUI/Views/Blocks/TableBorderSelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,78 @@ extension TableBorderSelector {
}
}

/// A table border selector that selects the inside borders of a table's first row.
public static var insideBordersFirstRow: TableBorderSelector {
TableBorderSelector { tableBounds, borderWidth in
guard tableBounds.rowCount > 0 else { return [] }
let firstRowBounds = tableBounds.bounds(forRow: 0)

return [
CGRect(
origin: .init(x: firstRowBounds.minX, y: firstRowBounds.maxY - borderWidth),
size: .init(width: firstRowBounds.width, height: borderWidth)
)
] + (0..<tableBounds.columnCount - 1)
.map {
tableBounds.bounds(forColumn: $0)
.insetBy(dx: -borderWidth, dy: -borderWidth)
}
.map {
CGRect(
origin: .init(x: $0.maxX - borderWidth, y: firstRowBounds.minY),
size: .init(width: borderWidth, height: firstRowBounds.height)
)
}
}
}

/// A table border selector that selects the inside borders of a table, except for the first row.
public static var insideBordersExceptFirstRow: TableBorderSelector {
TableBorderSelector { tableBounds, borderWidth in
Self.insideHorizontalBordersExceptFirstRow.rectangles(tableBounds, borderWidth)
+ Self.insideVerticalBordersExceptFirstRow.rectangles(tableBounds, borderWidth)
}
}

/// A table border selector that selects the inside horizontal borders of a table, except for the first row.
public static var insideHorizontalBordersExceptFirstRow: TableBorderSelector {
TableBorderSelector { tableBounds, borderWidth in
let rowCount = tableBounds.rowCount
guard rowCount > 1 else { return [] }
return (1..<rowCount - 1)
.map {
tableBounds.bounds(forRow: $0)
.insetBy(dx: -borderWidth, dy: -borderWidth)
}
.map {
CGRect(
origin: .init(x: $0.minX, y: $0.maxY - borderWidth),
size: .init(width: $0.width, height: borderWidth)
)
}
}
}

/// A table border selector that selects the inside vertical borders of a table, except for the first row.
public static var insideVerticalBordersExceptFirstRow: TableBorderSelector {
TableBorderSelector { tableBounds, borderWidth in
let rowCount = tableBounds.rowCount
guard rowCount > 1 else { return [] }
let firstRowBounds = tableBounds.bounds(forRow: 0)
return (0..<tableBounds.columnCount - 1)
.map {
tableBounds.bounds(forColumn: $0)
.insetBy(dx: -borderWidth, dy: -borderWidth)
}
.map {
CGRect(
origin: .init(x: $0.maxX - borderWidth, y: firstRowBounds.maxY),
size: .init(width: borderWidth, height: $0.height - firstRowBounds.maxY)
)
}
}
}

/// A table border selector that selects the horizontal borders of a table.
public static var horizontalBorders: TableBorderSelector {
TableBorderSelector { tableBounds, borderWidth in
Expand Down
21 changes: 12 additions & 9 deletions Sources/MarkdownUI/Views/Blocks/TableBorderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ struct TableBorderView: View {

var body: some View {
ZStack(alignment: .topLeading) {
let rectangles = self.tableBorderStyle.visibleBorders.rectangles(
self.tableBounds, self.borderWidth
)
ForEach(0..<rectangles.count, id: \.self) {
let rectangle = rectangles[$0]
Rectangle()
.strokeBorder(self.tableBorderStyle.color, style: self.tableBorderStyle.strokeStyle)
.offset(x: rectangle.minX, y: rectangle.minY)
.frame(width: rectangle.width, height: rectangle.height)
ForEach(0..<self.tableBorderStyle.borders.count, id: \.self) {
let border = self.tableBorderStyle.borders[$0]
let rectangles = border.visibleBorders.rectangles(
self.tableBounds, self.borderWidth
)
ForEach(0..<rectangles.count, id: \.self) {
let rectangle = rectangles[$0]
Rectangle()
.strokeBorder(border.color, style: self.tableBorderStyle.strokeStyle)
.offset(x: rectangle.minX, y: rectangle.minY)
.frame(width: rectangle.width, height: rectangle.height)
}
}
}
}
Expand Down
Loading