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

Add Static HTML Renderer and Documentation #204

Merged
merged 14 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 25 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ let package = Package(
name: "TokamakShim",
targets: ["TokamakShim"]
),
.library(
name: "TokamakStatic",
carson-katri marked this conversation as resolved.
Show resolved Hide resolved
targets: ["TokamakStatic"]
),
.executable(
name: "TokamakStaticDemo",
targets: ["TokamakStaticDemo"]
),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand All @@ -40,7 +48,10 @@ let package = Package(
// in packages which this package depends on.
.target(
name: "TokamakCore",
dependencies: ["OpenCombine", "Runtime"]
dependencies: [
.byName(name: "OpenCombine", condition: .when(platforms: [.wasi])),
"Runtime",
]
),
.target(
name: "TokamakDemo",
Expand All @@ -54,6 +65,19 @@ let package = Package(
name: "TokamakShim",
dependencies: [.target(name: "TokamakDOM", condition: .when(platforms: [.wasi]))]
),
.target(
name: "TokamakStatic",
dependencies: [
"TokamakCore",
"TokamakDOM",
]
carson-katri marked this conversation as resolved.
Show resolved Hide resolved
),
.target(
name: "TokamakStaticDemo",
dependencies: [
"TokamakStatic",
]
),
.target(
name: "TokamakTestRenderer",
dependencies: ["TokamakCore"]
Expand Down
4 changes: 4 additions & 0 deletions Sources/TokamakCore/Environment/EnvironmentObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
// Created by Carson Katri on 7/7/20.
//

#if os(WASI)
import OpenCombine
#else
import Combine
#endif

@propertyWrapper public struct EnvironmentObject<ObjectType>: ObservedProperty,
EnvironmentReader
Expand Down
4 changes: 4 additions & 0 deletions Sources/TokamakCore/Environment/EnvironmentValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if os(WASI)
import OpenCombine
#else
import Combine
#endif

public struct EnvironmentValues: CustomStringConvertible {
public var description: String {
Expand Down
4 changes: 4 additions & 0 deletions Sources/TokamakCore/MountedViews/MountedCompositeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
// Created by Max Desiatov on 03/12/2018.
//

#if os(WASI)
import OpenCombine
#else
import Combine
#endif
import Runtime

final class MountedCompositeView<R: Renderer>: MountedView<R>, Hashable {
Expand Down
7 changes: 6 additions & 1 deletion Sources/TokamakCore/State/ObservedObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if os(WASI)
import OpenCombine

public typealias ObservableObject = OpenCombine.ObservableObject
public typealias Published = OpenCombine.Published
#else
import Combine
public typealias ObservableObject = Combine.ObservableObject
public typealias Published = Combine.Published
#endif

protocol ObservedProperty {
var objectWillChange: AnyPublisher<(), Never> { get }
Expand Down
2 changes: 1 addition & 1 deletion Sources/TokamakDOM/Resources/TokamakStyles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

let tokamakStyles = """
public let tokamakStyles = """
._tokamak-stack > * {
flex-shrink: 0;
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/TokamakDOM/Styles/ToggleStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public struct DefaultToggleStyle: ToggleStyle {
public func makeBody(configuration: Configuration) -> some View {
CheckboxToggleStyle().makeBody(configuration: configuration)
}

public init() {}
}

public struct CheckboxToggleStyle: ToggleStyle {
Expand Down
10 changes: 5 additions & 5 deletions Sources/TokamakDOM/Views/HTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import TokamakCore

public typealias Listener = (JSObjectRef) -> ()

protocol AnyHTML {
public protocol AnyHTML {
var innerHTML: String? { get }
var tag: String { get }
var attributes: [String: String] { get }
Expand Down Expand Up @@ -56,9 +56,9 @@ extension AnyHTML {
}

public struct HTML<Content>: View, AnyHTML where Content: View {
let tag: String
let attributes: [String: String]
let listeners: [String: Listener]
public let tag: String
public let attributes: [String: String]
public let listeners: [String: Listener]
let content: Content

public init(
Expand All @@ -73,7 +73,7 @@ public struct HTML<Content>: View, AnyHTML where Content: View {
self.content = content()
}

var innerHTML: String? { nil }
public var innerHTML: String? { nil }

public var body: Never {
neverBody("HTML")
Expand Down
8 changes: 4 additions & 4 deletions Sources/TokamakDOM/Views/Spacers/Divider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import TokamakCore

extension Divider: AnyHTML {
var innerHTML: String? { nil }
var tag: String { "hr" }
var attributes: [String: String] {
public var innerHTML: String? { nil }
public var tag: String { "hr" }
public var attributes: [String: String] {
[
"style": """
width: 100%; height: 0; margin: 0;
Expand All @@ -29,5 +29,5 @@ extension Divider: AnyHTML {
]
}

var listeners: [String: Listener] { [:] }
public var listeners: [String: Listener] { [:] }
}
6 changes: 3 additions & 3 deletions Sources/TokamakDOM/Views/Text/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ extension Text: AnyHTML {
}
}

var tag: String { "span" }
var attributes: [String: String] {
public var tag: String { "span" }
public var attributes: [String: String] {
var font: Font?
var color: Color?
var italic: Bool = false
Expand Down Expand Up @@ -160,5 +160,5 @@ extension Text: AnyHTML {
]
}

var listeners: [String: Listener] { [:] }
public var listeners: [String: Listener] { [:] }
}
87 changes: 87 additions & 0 deletions Sources/TokamakStatic/Core.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Carson Katri on 7/20/20.
//

import TokamakCore

// MARK: Environment & State

public typealias Environment = TokamakCore.Environment

// MARK: Modifiers & Styles

public typealias ViewModifier = TokamakCore.ViewModifier
public typealias ModifiedContent = TokamakCore.ModifiedContent

public typealias DefaultListStyle = TokamakCore.DefaultListStyle
public typealias PlainListStyle = TokamakCore.PlainListStyle
public typealias InsetListStyle = TokamakCore.InsetListStyle
public typealias GroupedListStyle = TokamakCore.GroupedListStyle
public typealias InsetGroupedListStyle = TokamakCore.InsetGroupedListStyle

// MARK: Shapes

public typealias Shape = TokamakCore.Shape

public typealias Capsule = TokamakCore.Capsule
public typealias Circle = TokamakCore.Circle
public typealias Ellipse = TokamakCore.Ellipse
public typealias Path = TokamakCore.Path
public typealias Rectangle = TokamakCore.Rectangle
public typealias RoundedRectangle = TokamakCore.RoundedRectangle

// MARK: Primitive values

public typealias Color = TokamakCore.Color
public typealias Font = TokamakCore.Font

public typealias CGAffineTransform = TokamakCore.CGAffineTransform
public typealias CGPoint = TokamakCore.CGPoint
public typealias CGRect = TokamakCore.CGRect
public typealias CGSize = TokamakCore.CGSize

// MARK: Views

public typealias Divider = TokamakCore.Divider
public typealias ForEach = TokamakCore.ForEach
public typealias GridItem = TokamakCore.GridItem
public typealias Group = TokamakCore.Group
public typealias HStack = TokamakCore.HStack
public typealias LazyHGrid = TokamakCore.LazyHGrid
public typealias LazyVGrid = TokamakCore.LazyVGrid
public typealias List = TokamakCore.List
public typealias ScrollView = TokamakCore.ScrollView
public typealias Section = TokamakCore.Section
public typealias Spacer = TokamakCore.Spacer
public typealias Text = TokamakCore.Text
public typealias VStack = TokamakCore.VStack
public typealias ZStack = TokamakCore.ZStack

// MARK: Special Views

public typealias View = TokamakCore.View
public typealias AnyView = TokamakCore.AnyView
public typealias EmptyView = TokamakCore.EmptyView

// MARK: Misc

// FIXME: I would put this inside TokamakCore, but for
// some reason it doesn't get exported with the typealias
extension Text {
public static func + (lhs: Self, rhs: Self) -> Self {
_concatenating(lhs: lhs, rhs: rhs)
}
}
111 changes: 111 additions & 0 deletions Sources/TokamakStatic/StaticRenderer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Carson Katri on 7/20/20.
//

import TokamakCore
import TokamakDOM

public final class HTMLTarget: Target {
var html: AnyHTML
var children: [HTMLTarget] = []

init<V: View>(_ view: V,
_ html: AnyHTML) {
self.html = html
super.init(view)
}
}

extension HTMLTarget {
var outerHTML: String {
"""
<\(html.tag)\(html.attributes.isEmpty ? "" : " ")\
\(html.attributes.map { #"\#($0)="\#($1)""# }.joined(separator: " "))>\
\(html.innerHTML ?? "")\
\(children.map(\.outerHTML).joined(separator: "\n"))\
</\(html.tag)>
"""
}
}

struct HTMLBody: AnyHTML {
let tag: String = "body"
let innerHTML: String? = nil
let attributes: [String: String] = [:]
let listeners: [String: Listener] = [:]
}

public final class StaticRenderer: Renderer {
public private(set) var reconciler: StackReconciler<StaticRenderer>?

var rootTarget: HTMLTarget

public var html: String {
"""
<html>
<head>
<style>
\(tokamakStyles)
</style>
</head>
\(rootTarget.outerHTML)
</html>
"""
}

public init<V: View>(_ view: V) {
rootTarget = HTMLTarget(view, HTMLBody())
reconciler = StackReconciler(
view: view,
target: rootTarget,
renderer: self,
environment: EnvironmentValues()
) { _ in
fatalError("Stateful apps cannot be created with TokamakStatic")
}
}

public func mountTarget(to parent: HTMLTarget, with host: MountedHost) -> HTMLTarget? {
guard let html = mapAnyView(
host.view,
transform: { (html: AnyHTML) in html }
) else {
// handle cases like `TupleView`
if mapAnyView(host.view, transform: { (view: ParentView) in view }) != nil {
return parent
}

return nil
}

let node = HTMLTarget(host.view, html)
parent.children.append(node)
return node
}

public func update(target: HTMLTarget, with host: MountedHost) {
fatalError("Stateful apps cannot be created with TokamakStatic")
}

public func unmount(
target: HTMLTarget,
from parent: HTMLTarget,
with host: MountedHost,
completion: @escaping () -> ()
) {
fatalError("Stateful apps cannot be created with TokamakStatic")
}
}
Loading