├── .gitignore ├── LICENSE.md ├── PlotPlayground.xcworkspace ├── Package │ ├── Package.swift │ └── Sources │ │ └── PlotPlayground │ │ └── WKWebView+Component.swift ├── Playground.playground │ ├── Contents.swift │ ├── Resources │ │ └── Styles.css │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ └── contents.xcworkspacedata │ └── timeline.xctimeline ├── contents.xcworkspacedata └── xcshareddata │ └── IDEWorkspaceChecks.plist └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | .swiftpm 4 | *.xcodeproj 5 | xcuserdata/ 6 | Package.resolved -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 John Sundell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /PlotPlayground.xcworkspace/Package/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.4 2 | 3 | /** 4 | * PlotPlayground 5 | * Copyright (c) John Sundell 2021 6 | * MIT license, see LICENSE file for details 7 | */ 8 | 9 | import PackageDescription 10 | 11 | let package = Package( 12 | name: "PlotPlayground", 13 | products: [ 14 | .library(name: "PlotPlayground", targets: ["PlotPlayground"]) 15 | ], 16 | dependencies: [ 17 | .package( 18 | name: "Plot", 19 | url: "https://github.com/johnsundell/plot.git", 20 | from: "0.9.0" 21 | ) 22 | ], 23 | targets: [ 24 | .target( 25 | name: "PlotPlayground", 26 | dependencies: ["Plot"] 27 | ) 28 | ] 29 | ) 30 | -------------------------------------------------------------------------------- /PlotPlayground.xcworkspace/Package/Sources/PlotPlayground/WKWebView+Component.swift: -------------------------------------------------------------------------------- 1 | /** 2 | * PlotPlayground 3 | * Copyright (c) John Sundell 2021 4 | * MIT license, see LICENSE file for details 5 | */ 6 | 7 | import Plot 8 | import WebKit 9 | 10 | public extension WKWebView { 11 | convenience init(frame: CGRect, 12 | component: Component, 13 | cssURL: URL, 14 | baseURL: URL) throws { 15 | self.init(frame: frame) 16 | let css = try String(contentsOf: cssURL) 17 | let html = HTML(head: [.style(css)]) { component } 18 | loadHTMLString(html.render(), baseURL: baseURL) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /PlotPlayground.xcworkspace/Playground.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | /** 2 | * PlotPlayground 3 | * Copyright (c) John Sundell 2021 4 | * MIT license, see LICENSE file for details 5 | */ 6 | 7 | import Plot 8 | import PlotPlayground 9 | import WebKit 10 | import PlaygroundSupport 11 | 12 | // Defining our main HTML page: 13 | 14 | struct WebPage: Component { 15 | var body: Component { 16 | Div { 17 | Image(logoURL).class("logo") 18 | H1("A Plot-powered web page") 19 | Paragraph(welcomeText) 20 | 21 | H2("Some useful links") 22 | LinkList() 23 | 24 | H2("A form") 25 | Form(url: "/") { 26 | TextField(placeholder: "First name") 27 | TextField(placeholder: "Last name") 28 | SubmitButton("Send") 29 | } 30 | 31 | H2("A table") 32 | Table { 33 | TableRow { 34 | Text("Building") 35 | Text("HTML pages") 36 | } 37 | TableRow { 38 | Text("in Swift") 39 | Text("is quite nice!") 40 | } 41 | } 42 | } 43 | } 44 | 45 | private var logoURL: URLRepresentable { 46 | "https://raw.githubusercontent.com/JohnSundell/Plot/master/Logo.png" 47 | } 48 | 49 | private var welcomeText: String { 50 | """ 51 | Welcome to this Plot-powered web page built \ 52 | entirely in Swift! 53 | """ 54 | } 55 | } 56 | 57 | // A custom component that renders a series of links: 58 | 59 | struct LinkList: Component { 60 | var body: Component { 61 | List { 62 | Link("Plot", 63 | url: "https://github.com/johnsundell/plot" 64 | ) 65 | Link("Swift by Sundell", 66 | url: "https://swiftbysundell.com" 67 | ) 68 | Link("The official Swift website", 69 | url: "https://swift.org" 70 | ) 71 | } 72 | .listStyle(.ordered) 73 | } 74 | } 75 | 76 | // Showing our web page in the playground's live view: 77 | 78 | PlaygroundPage.current.liveView = try WKWebView( 79 | frame: CGRect( 80 | origin: .zero, 81 | size: CGSize(width: 500, height: 700) 82 | ), 83 | component: WebPage(), 84 | cssURL: Bundle.main.url( 85 | forResource: "Styles", 86 | withExtension: "css" 87 | )!, 88 | baseURL: Bundle.main.resourceURL! 89 | ) 90 | -------------------------------------------------------------------------------- /PlotPlayground.xcworkspace/Playground.playground/Resources/Styles.css: -------------------------------------------------------------------------------- 1 | /** 2 | * PlotPlayground 3 | * Copyright (c) John Sundell 2021 4 | * MIT license, see LICENSE file for details 5 | */ 6 | 7 | body { 8 | font-family: -apple-system, sans-serif; 9 | padding: 10px; 10 | } 11 | 12 | .logo { 13 | max-height: 80px; 14 | } 15 | 16 | table td { 17 | border: 1px solid black; 18 | padding: 5px; 19 | } 20 | -------------------------------------------------------------------------------- /PlotPlayground.xcworkspace/Playground.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /PlotPlayground.xcworkspace/Playground.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /PlotPlayground.xcworkspace/Playground.playground/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /PlotPlayground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /PlotPlayground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PlotPlayground 2 | 3 | A Swift playground that comes pre-loaded with [Plot](https://github.com/JohnSundell/Plot), so that you can quickly try out the library and its new, SwiftUI-like API for building HTML pages. 4 | 5 | **To get started, follow these steps:** 6 | 7 | 1. Make sure that you have the latest, non-beta version of Xcode installed. 8 | 2. Clone the project to your local machine. 9 | 3. Open `PlotPlayground.xcworkspace` in Xcode. 10 | 4. Make sure that the `Playground` item is selected in the left-hand navigator pane. 11 | 5. Build the project using `⌘ + B`. 12 | 6. The playground should now automatically start running, but if it doesn’t, press the play button (▶️) at the bottom of the Xcode UI to start it manually. 13 | 7. Have fun exploring Plot! 😀 14 | 15 | To learn more about Plot, please check out [its README](https://github.com/JohnSundell/Plot). 16 | --------------------------------------------------------------------------------