Skip to content

Commit

Permalink
Upgrade the package (#119)
Browse files Browse the repository at this point in the history
* Make the storage manager public

To make the environment for htmlkit work, the manager needs to be cached in the application storage.

* Add the press modifier to the text component

* Add the environment value to the content rendering

In a specific case the environment value didn't  get caught by the renderer.

* Fix the styling of the divider component

* Add a dropdown component

* Revise the card component

* Remove the css from the grid item

The grid should only be for alignment.

* Add a scrollview component

* Add a carousel component

* Extend the carousel component

* Add more component tests

* Fix test failing

* Add motion to the carousel by adding javascript

* Add autoplay to the carousel

* Fix tests

* Add a test for the symbol component

* Add the possibility to disable views

* Fix the css classes for the position index

* Add a few new symbols

* Add more symbols

* Add the possibility to hide views

* Revise the input fields to accept a placeholder value

* Add the submit event to the form component

* Add comments

* Refactor code and rearrange files

* Add form validation

* Fix some sort of things

* Change the js initialization for the carousel component

* Add a textpad component
  • Loading branch information
mattesmohr authored Feb 28, 2023
1 parent 4041787 commit c811b23
Show file tree
Hide file tree
Showing 83 changed files with 1,522 additions and 253 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ let package = Package(
dependencies: [
.product(name: "Lingo", package: "Lingo"),
.product(name: "Collections", package: "swift-collections")
]
],
exclude: ["Abstraction/README.md", "Framework/README.md"]
),
.target(
name: "HTMLKitConverter",
Expand Down
10 changes: 10 additions & 0 deletions Sources/HTMLKit/Abstraction/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Abstraction

This directory contains the HTML abstraction.

### Attributes

### Elements

### Tokens

Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import Foundation

/// A type, that manages the environment storage
internal class Manager {
public class Manager {

/// The storage of the environment
internal var storage: [AnyKeyPath: Any]
private var storage: [AnyKeyPath: Any]

/// Initiates a manager
internal init() {
public init() {

self.storage = [:]
}

/// Retrieves an item from storage by its path
internal func retrieve(for path: AnyKeyPath) -> Any? {
public func retrieve(for path: AnyKeyPath) -> Any? {

if let value = self.storage[path] {
return value
Expand All @@ -23,7 +23,7 @@ internal class Manager {
}

/// Adds und updates an item to the storage
internal func upsert<T>(_ value: T, for path: AnyKeyPath) {
public func upsert<T>(_ value: T, for path: AnyKeyPath) {
self.storage[path] = value
}
}
17 changes: 17 additions & 0 deletions Sources/HTMLKit/Framework/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Framework

This directory contains sources for

### Builders

### Environment

### Extensions

### Helpers

### Localization

### Primitives

### Rendering
12 changes: 10 additions & 2 deletions Sources/HTMLKit/Framework/Rendering/Renderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ public class Renderer {
self.lingo = lingo
}

public func add<T>(model: T) where T: Encodable {
manager.upsert(model, for: \T.self)
/// Initiates the renderer.
public init(lingo: Lingo? = nil, manager: Manager) {

self.environment = Environment()
self.manager = manager
self.lingo = lingo
}

/// Renders a view
Expand Down Expand Up @@ -112,6 +116,10 @@ public class Renderer {
result += try render(modifier: modifier)
}

if let value = content as? EnvironmentValue {
result += try render(value: value)
}

if let element = content as? String {
result += element
}
Expand Down
49 changes: 48 additions & 1 deletion Sources/HTMLKitComponents/Actions.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
/*
Abstract:
The file contains the actions for the components.
The file contains the action stencils for the components.
*/

import Foundation

/// A collection of actions, that can be triggered by events.
public enum Actions {

/// Shows the target.
case show(_ target: String)

/// Hides the target.
case hide(_ target: String)

/// Animates the target.
case animate(_ target: String)

/// Opens the target.
case open(_ target: String)

/// Closes the target.
case close(_ target: String)

/// Validates the form.
case valdiate(_ target: String, _ rules: [Validator])

public var description: String {

switch self {
case .valdiate(_, _):
return "validate"
default:
return "default"
}
}

/// The script for the action.
public var script: String {

switch self {
Expand All @@ -28,26 +54,47 @@ public enum Actions {

case .close(let target):
return close(target)

case .valdiate(let target, let rules):
return validate(target, rules)
}
}

/// Returns a show action stencil.
private func show(_ target: String) -> String {
return "$('#\(target)').show();"
}

/// Returns a hide action stencil.
private func hide(_ target: String) -> String {
return "$('#\(target)').hide();"
}

/// Returns a animate action stencil.
private func animate(_ target: String) -> String {
return "$('#\(target)').animate();"
}

/// Returns a open action stencil.
private func open(_ target: String) -> String {
return "$('#\(target)').open();"
}

/// Returns a close action stencil.
private func close(_ target: String) -> String {
return "$('#\(target)').close();"
}

/// Returns a close action stencil.
private func validate(_ target: String, _ validators: [Validator]) -> String {

if let data = try? JSONEncoder().encode(validators) {

if let result = String(data: data, encoding: .utf8) {
return "$('#\(target)').validate('\(result)');"
}
}

return "$('#\(target)').validate('[]');"
}
}
18 changes: 18 additions & 0 deletions Sources/HTMLKitComponents/Components/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ extension Button: ButtonModifier {
public func backgroundColor(_ color: Tokens.BackgroundColor) -> Button {
return self.mutate(backgroundcolor: color.rawValue)
}

public func disabled(_ condition: Bool) -> Button {

if condition {
return self.mutate(state: Tokens.ViewState.disabled.rawValue)
}

return self
}
}

extension Button: PressModifier {
Expand Down Expand Up @@ -153,4 +162,13 @@ extension LinkButton: ButtonModifier {
public func backgroundColor(_ color: Tokens.BackgroundColor) -> LinkButton {
return self.mutate(backgroundcolor: color.rawValue)
}

public func disabled(_ condition: Bool) -> LinkButton {

if condition {
return self.mutate(state: Tokens.ViewState.disabled.rawValue)
}

return self
}
}
44 changes: 40 additions & 4 deletions Sources/HTMLKitComponents/Components/Card.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
/*
Abstract:
The file contains a card component.
*/

import HTMLKit

public struct Card: View, Modifiable {
/// A component that distinguish content.
public class Card: View {

/// The header of the card.
public var header: [Content]?

internal var content: [Content]
/// The content of the card.
public var content: [Content]

/// The classes of the content.
internal var classes: [String]

/// Creates a card.
public init(@ContentBuilder<Content> content: () -> [Content]) {

self.content = content()
self.classes = ["card"]
}

/// Creates a card.
public init(@ContentBuilder<Content> content: () -> [Content],
@ContentBuilder<Content> header: () -> [Content]) {

self.content = content()
self.header = header()
self.classes = ["card"]
}

/// Creates a card.
internal init(header: [Content]?, content: [Content], classes: [String]) {

self.header = header
self.content = content
self.classes = classes
}

public var body: Content {
Division {
content
Division {
header
}
.class("card-header")
Division {
content
}
.class("card-body")
}
.class(self.classes.joined(separator: " "))
.class(classes.joined(separator: " "))
}
}
114 changes: 114 additions & 0 deletions Sources/HTMLKitComponents/Components/Carousel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Abstract:
The file contains a carousel component.
*/

import HTMLKit

/// A compnonent that cycles through an amount of views.
public struct Carousel: View {

/// The indication for the carousel.
internal var indication: [Content]

/// The content of the carousel.
internal var content: [Content]

/// The classes of the carousel.
internal var classes: [String]

/// Creates a carousel.
public init(@ContentBuilder<Content> content: () -> [Content],
@ContentBuilder<Content> indication: () -> [Content]) {

self.content = content()
self.indication = indication()
self.classes = ["carousel"]
}

/// Creates a carousel.
internal init(indication: [Content], content: [Content], classes: [String]) {

self.indication = indication
self.content = content
self.classes = classes
}

public var body: Content {
Division {
Division {
content
}
.class("carousel-content")
Division {
indication
}
.class("carousel-indication")
}
.class(classes.joined(separator: " "))
}
}

public struct Slide: View, Identifiable, Modifiable {

internal var id: String?

internal var source: String

internal var classes: [String]

internal var caption: [Content]

public init(source: String, @ContentBuilder<Content> caption: () -> [Content]) {

self.source = source
self.caption = caption()
self.classes = ["slide"]
}

internal init(id: String?, source: String, caption: [Content], classes: [String]) {

self.id = id
self.source = source
self.caption = caption
self.classes = classes
}

public var body: Content {
Division {
Division {
HTMLKit.Image()
.source(source)
}
.class("slide-thumbnail")
Division {
caption
}
.class("slide-caption")
}
.class(classes.joined(separator: " "))
.modify(unwrap: id) {
$0.id($1)
}
}

public func tag(_ value: String) -> Slide {
return self.mutate(id: value)
}
}

public struct Indicator: View {

internal var tag: String

public init(for tag: String) {
self.tag = "#" + tag
}

public var body: Content {
Anchor {
}
.class("indicator")
.reference(tag)
}
}
Loading

0 comments on commit c811b23

Please sign in to comment.