Skip to content

Commit

Permalink
🖥 Improve LivePreview declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaelIsaev committed Dec 26, 2022
1 parent b7c0df0 commit 8381d95
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 13 deletions.
13 changes: 7 additions & 6 deletions Sources/Web/WebApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,16 @@ open class WebApp: _PreviewableApp {
guard let classType = Bundle.main.classNamed(className) as? WebPreview.Type else {
fatalError("Class \(className) doesn't conform to WebberPreview")
}
Localization.shared.defaultLanguage = classType.language
Localization.shared.currentLanguage = classType.language
guard let b64html = classType.html.data(using: .utf8)?.base64EncodedString() else {

let previewData = classType.result

guard let b64html = previewData.html.data(using: .utf8)?.base64EncodedString() else {
fatalError("Unable to encode into b64html")
}
result.previews.append(.init(
width: classType.width,
height: classType.height,
title: classType.title,
width: previewData.width,
height: previewData.height,
title: previewData.title,
module: passedPreview.module,
class: passedPreview.class,
html: b64html
Expand Down
3 changes: 3 additions & 0 deletions Sources/Web/WebberPreview/RenderBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ struct _RenderBuilderContent: Preview.Content {

public enum Item {
case none
case language(Language)
case title(WebPreview.Title)
case size(WebPreview.Size)
case item(WebPreviewRenderable)
case items([Item])
}
Expand Down
116 changes: 109 additions & 7 deletions Sources/Web/WebberPreview/WebPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,132 @@
import Foundation
import WebFoundation

extension Language: RenderBuilderContent {
public var renderBuilderContent: Preview.Item { .language(self) }
}

open class WebPreview {
@available(*, deprecated, message: "🧨 This variable is deprecated and not working. Please switch to `Language.en` in the @Preview content body declaration.")
open class var language: Language { .en }
@available(*, deprecated, message: "🧨 This variable is deprecated and not working. Please switch to `Title(\"\")` in the @Preview content body declaration.")
open class var title: String { "" }
@available(*, deprecated, message: "🧨 This variable is deprecated and not working. Please switch to `Size(640, 480)` in the @Preview content body declaration.")
open class var width: UInt { 0 }
@available(*, deprecated, message: "🧨 This variable is deprecated and not working. Please switch to `Size(640, 480)` in the @Preview content body declaration.")
open class var height: UInt { 0 }

@Preview open class var content: Preview.Content { _RenderBuilderContent(renderBuilderContent: .none) }

public struct Title: RenderBuilderContent {
public var renderBuilderContent: Preview.Item { .title(self) }

let title: String

public init (_ title: String) {
self.title = title
}
}

public struct Size: RenderBuilderContent {
public var renderBuilderContent: Preview.Item { .size(self) }

let width: UInt
let height: UInt

public init (_ width: UInt, _ height: UInt) {
self.width = width
self.height = height
}
}

#if os(macOS)
@objc class var html: String { _html }
class PreviewData: NSObject {
let title: String
let width: UInt
let height: UInt
let html: String

init (
title: String,
width: UInt,
height: UInt,
html: String
) {
self.title = title
self.width = width
self.height = height
self.html = html
}
}
#else
class var html: String { _html }
class PreviewData {
let title: String
let width: UInt
let height: UInt
let html: String

init (
title: String,
width: UInt,
height: UInt,
html: String
) {
self.title = title
self.width = width
self.height = height
self.html = html
}
}
#endif

private class var _html: String {
#if os(macOS)
@objc class var result: PreviewData { _result }
#else
class var result: PreviewData { _result }
#endif

private class var _result: PreviewData {
var title: String = ""
var width: UInt = 0
var height: UInt = 0

var items: [WebPreviewRenderable] = []
func parseRenderBuilderLanguage(_ item: Preview.Item) {
switch item {
case .language(let v):
Localization.shared.defaultLanguage = v
Localization.shared.currentLanguage = v
case .items(let v):
v.forEach { parseRenderBuilderLanguage($0) }
default: break
}
}
func parseRenderBuilderItem(_ item: Preview.Item) {
switch item {
case .items(let v): v.forEach { parseRenderBuilderItem($0) }
case .item(let v): items.append(v)
case .none: break
case .language:
break
case .title(let v):
title = v.title
case .size(let size):
width = size.width
height = size.height
case .items(let v):
v.forEach { parseRenderBuilderItem($0) }
case .item(let v):
items.append(v)
case .none:
break
}
}
parseRenderBuilderLanguage(content.renderBuilderContent)
parseRenderBuilderItem(content.renderBuilderContent)
return items.map { $0.renderPreview() }.joined()
let html: String = items.map { $0.renderPreview() }.joined()

return PreviewData(
title: title,
width: width,
height: height,
html: html
)
}
}

0 comments on commit 8381d95

Please sign in to comment.