Skip to content

Commit

Permalink
Minor simplification.
Browse files Browse the repository at this point in the history
  • Loading branch information
JPToroDev committed Jan 16, 2025
1 parent fe3e83d commit ba07619
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions Sources/Ignite/Publishing/CSSManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
//

/// A manager that generates and maintains CSS classes for media query-based styling rules.
@MainActor final class CSSManager {
@MainActor
final class CSSManager {
/// The shared instance used for managing CSS rules across the application.
static let `default` = CSSManager()

/// All themes available in the site
private var themes: [Theme] = []

/// Queue of registrations waiting for themes to be set
/// Queue of registrations waiting to be processed
private struct PendingRegistration {
let queries: [any Query]
let properties: [(String, String)]
Expand All @@ -31,53 +29,52 @@
/// A mapping of query hashes to their style properties.
private var styleProperties: [String: [(String, String)]] = [:]

/// Sets the themes and processes any pending registrations
/// - Parameter themes: Array of themes from the site
/// Processes all registrations
/// - Parameter themes: Array of themes from the site.
/// - Returns: A string containing all generated CSS rules, separated by newlines.
func generateAllRules(themes: [Theme]) -> String {
self.themes = themes
rules.removeAll()
classNames.removeAll()
styleProperties.removeAll()

// Process all pending registrations
// Process all registrations
for registration in pendingRegistrations {
register(
registration.queries,
let hash = hashForQueries(registration.queries)
let finalClassName = registration.className ?? "style-\(hash)"

classNames[hash] = finalClassName
styleProperties[hash] = registration.properties
rules[hash] = generateCSSRule(
for: registration.queries,
className: finalClassName,
properties: registration.properties,
className: registration.className
themes: themes
)
}
pendingRegistrations.removeAll()

return rules.values.joined(separator: "\n\n")
}

/// Registers a set of media queries and generates a corresponding CSS class
/// Registers a set of media queries and queues them for CSS generation
/// - Parameters:
/// - queries: Media queries that determine when styles should be applied
/// - properties: CSS property names and values to apply
/// - className: Optional specific class name to use (generates one if nil)
/// - Returns: The class name used for these styles
/// - Returns: The class name that will be used for these styles
@discardableResult
func register(
_ queries: [any Query],
properties: [(String, String)] = [("display", "none")],
className: String? = nil
) -> String {
let hash = hashForQueries(queries)

if themes.isEmpty {
// Queue the registration for later
pendingRegistrations.append(PendingRegistration(
queries: queries,
properties: properties,
className: className
))
// Return the class name that will be used
return className ?? "style-\(hash)"
}

let finalClassName = className ?? "style-\(hash)"
classNames[hash] = finalClassName
styleProperties[hash] = properties
rules[hash] = generateCSSRule(for: queries, className: finalClassName, properties: properties)

pendingRegistrations.append(PendingRegistration(
queries: queries,
properties: properties,
className: finalClassName
))

return finalClassName
}
Expand Down Expand Up @@ -105,11 +102,13 @@
/// - queries: The media queries to apply.
/// - className: The CSS class name to use.
/// - properties: The CSS properties to apply.
/// - themes: The themes to handle.
/// - Returns: A CSS rule string.
private func generateCSSRule(
for queries: [any Query],
className: String,
properties: [(String, String)]
properties: [(String, String)],
themes: [Theme]
) -> String {
var rules: [String] = []

Expand Down

0 comments on commit ba07619

Please sign in to comment.