Consolidate Lit repositories: figma-to-lit, figma_lit_example, lit-3d-piano, lit-calculator, lit-code-editor, lit-draggable-dom, lit-file-based-routing, lit-force-graph, lit-html-editor, lit-html-table, lit-modules, lit-native, lit-node-editor, lit-sheet-music, lit-starter-ts, lit-vscode-extension, lit-wmr, vite-lit-capacitor, vite-lit-element-starter, vite-rxdb-lit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// iosApp.swift
|
||||
// ios
|
||||
//
|
||||
// Created by Rody Davis on 6/19/21.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
@main
|
||||
struct MyApp: App {
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
Screen(
|
||||
title: "My App",
|
||||
components: [
|
||||
Component(
|
||||
tag: "my-element",
|
||||
name: "Element 1"
|
||||
),
|
||||
]
|
||||
)
|
||||
}.commands {
|
||||
// CommandMenu("Custom Menu") {
|
||||
// Button("Action 1") {
|
||||
// print("pressed menu item")
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"colors" : [
|
||||
{
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"scale" : "2x",
|
||||
"size" : "20x20"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"scale" : "3x",
|
||||
"size" : "20x20"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"scale" : "2x",
|
||||
"size" : "29x29"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"scale" : "3x",
|
||||
"size" : "29x29"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"scale" : "2x",
|
||||
"size" : "40x40"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"scale" : "3x",
|
||||
"size" : "40x40"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"scale" : "2x",
|
||||
"size" : "60x60"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"scale" : "3x",
|
||||
"size" : "60x60"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"scale" : "1x",
|
||||
"size" : "20x20"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"scale" : "2x",
|
||||
"size" : "20x20"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"scale" : "1x",
|
||||
"size" : "29x29"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"scale" : "2x",
|
||||
"size" : "29x29"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"scale" : "1x",
|
||||
"size" : "40x40"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"scale" : "2x",
|
||||
"size" : "40x40"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"scale" : "1x",
|
||||
"size" : "76x76"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"scale" : "2x",
|
||||
"size" : "76x76"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"scale" : "2x",
|
||||
"size" : "83.5x83.5"
|
||||
},
|
||||
{
|
||||
"idiom" : "ios-marketing",
|
||||
"scale" : "1x",
|
||||
"size" : "1024x1024"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
//
|
||||
// Cache.swift
|
||||
// app
|
||||
//
|
||||
// Created by Rody Davis on 6/17/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct AppBundle {
|
||||
let name: String
|
||||
let url: String
|
||||
var cacheDays = 1
|
||||
|
||||
let appSup = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
|
||||
|
||||
func get() -> String {
|
||||
// Start remote download
|
||||
self.download()
|
||||
|
||||
#if DEBUG
|
||||
#else
|
||||
// Search for cached bundle
|
||||
do {
|
||||
let url = self.appSup.appendingPathComponent(self.prefs.bundle).appendingPathExtension("js")
|
||||
let bundle: String = try String(contentsOfFile: url.path)
|
||||
return bundle
|
||||
} catch {
|
||||
print("Cached bundle not found", error)
|
||||
}
|
||||
#endif
|
||||
|
||||
// Fallback to included bundle
|
||||
do {
|
||||
let url = Bundle.main.url(forResource: name, withExtension: "js", subdirectory: "build")!
|
||||
let bundle: String = try String(contentsOfFile: url.path)
|
||||
self.save(content: bundle)
|
||||
return bundle
|
||||
} catch {
|
||||
print("Local bundle not found", error)
|
||||
}
|
||||
|
||||
// No bundle included
|
||||
return ""
|
||||
}
|
||||
|
||||
func save(content: String) {
|
||||
let fileManager = FileManager.default
|
||||
|
||||
let bundleUrl = self.appSup.appendingPathComponent(name).appendingPathExtension("js")
|
||||
|
||||
// Check if Application Support Exists
|
||||
if !fileManager.fileExists(atPath: self.appSup.path) {
|
||||
do {
|
||||
try fileManager.createDirectory(
|
||||
at: self.appSup,
|
||||
withIntermediateDirectories: true,
|
||||
attributes: nil
|
||||
)
|
||||
} catch {
|
||||
print("Could not create app support directory", error)
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to bundle included at compile time
|
||||
if fileManager.fileExists(atPath: bundleUrl.path) {
|
||||
do {
|
||||
try fileManager.removeItem(at: bundleUrl)
|
||||
} catch {
|
||||
print("Could not remove existing bundle", error)
|
||||
}
|
||||
}
|
||||
|
||||
// Update the bundle
|
||||
do {
|
||||
try content.write(to: bundleUrl, atomically: true, encoding: String.Encoding.utf8)
|
||||
} catch {
|
||||
print("Error updating bundle", error)
|
||||
}
|
||||
}
|
||||
|
||||
func download() {
|
||||
if url.isEmpty { return }
|
||||
|
||||
if let url = URL(string: url + "/" + name) {
|
||||
let key = "last-bundle-update";
|
||||
|
||||
// Check if the bundle has already been downloaded
|
||||
if let value = UserDefaults.standard.object(forKey: key) as? Date {
|
||||
if Calendar.current.daysSince(date: value) ?? -1 < cacheDays {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Try downloading a new bundle from the network
|
||||
let downloadTask = URLSession.shared.downloadTask(with: url) { (tempUrl, response, error) in
|
||||
if let tempFileUrl = tempUrl {
|
||||
do {
|
||||
let remoteBundle = try String(contentsOf: tempFileUrl)
|
||||
self.save(content: remoteBundle)
|
||||
UserDefaults.standard.set(Date(), forKey: key)
|
||||
} catch {
|
||||
print("Error downloading bundle", error)
|
||||
}
|
||||
}
|
||||
}
|
||||
downloadTask.resume()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension Calendar {
|
||||
public func daysSince(date: Date) -> Int? {
|
||||
return self.dateComponents([.day], from: date, to: Date()).day
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Events.swift
|
||||
// app
|
||||
//
|
||||
// Created by Rody Davis on 6/19/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import WebKit
|
||||
|
||||
func handleEvent(_ webview: WKWebView, message: WKScriptMessage) {
|
||||
guard let dict = message.body as? [String : AnyObject],
|
||||
let type = dict["type"] as? String else {
|
||||
return
|
||||
}
|
||||
switch type {
|
||||
case "dialog":
|
||||
let title = dict["title"] as! String
|
||||
let message = dict["message"] as! String
|
||||
showAlert(title: title, message: message) {
|
||||
webview.dispatchEvent(event: "response", detail: "WebKit")
|
||||
}
|
||||
break
|
||||
default:
|
||||
print("event", dict)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func showAlert(title: String, message: String, callback: @escaping () -> Void) -> Void {
|
||||
let alert = UIAlertController(title:title,
|
||||
message: message,
|
||||
preferredStyle: .alert)
|
||||
alert.addAction(UIAlertAction(title: "Ok", style: .default){ action -> Void in
|
||||
callback()
|
||||
})
|
||||
let vc = UIApplication.shared.windows.first?.rootViewController
|
||||
vc?.present(alert, animated: true, completion: nil)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>AppBundleName</key>
|
||||
<string>bundle.es</string>
|
||||
<key>AppUrl</key>
|
||||
<string></string>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleDisplayName</key>
|
||||
<string>My App</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<true/>
|
||||
<key>UIApplicationSceneManifest</key>
|
||||
<dict>
|
||||
<key>UIApplicationSupportsMultipleScenes</key>
|
||||
<true/>
|
||||
</dict>
|
||||
<key>UIApplicationSupportsIndirectInputEvents</key>
|
||||
<true/>
|
||||
<key>UILaunchScreen</key>
|
||||
<dict/>
|
||||
<key>UIRequiredDeviceCapabilities</key>
|
||||
<array>
|
||||
<string>armv7</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations~ipad</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationPortraitUpsideDown</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// ContentView.swift
|
||||
// ios
|
||||
//
|
||||
// Created by Rody Davis on 6/19/21.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct Screen: View {
|
||||
@StateObject var state = AppState()
|
||||
@State var title: String
|
||||
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
|
||||
let components: [Component]
|
||||
|
||||
var body: some View {
|
||||
if (components.count == 1 && title.count > 0) {
|
||||
let item = components[0]
|
||||
if (horizontalSizeClass == .compact) {
|
||||
NavigationView {
|
||||
RenderComponent(component: item)
|
||||
}.navigationTitle(title)
|
||||
} else {
|
||||
RenderComponent(component: item)
|
||||
}
|
||||
} else {
|
||||
NavigationView {
|
||||
List(components) { item in
|
||||
NavigationLink(destination: RenderComponent(component: item)
|
||||
) {
|
||||
Text(item.name)
|
||||
}
|
||||
}.navigationTitle(title)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RenderComponent: View {
|
||||
let component: Component
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geometry in
|
||||
ZStack {
|
||||
WebView(
|
||||
tag: component.tag,
|
||||
size: geometry.size
|
||||
)
|
||||
}}
|
||||
.navigationBarTitle( component.name, displayMode: .automatic)
|
||||
}
|
||||
}
|
||||
|
||||
struct Component: Codable, Identifiable {
|
||||
var id = UUID()
|
||||
let tag: String
|
||||
let name: String
|
||||
var icon: String = "phone.fill"
|
||||
var bundle: String = "bundle.es"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Config.swift
|
||||
// app
|
||||
//
|
||||
// Created by Rody Davis on 6/16/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
class AppState: ObservableObject {
|
||||
@Published var title = Bundle.main.infoDictionary!["CFBundleName"] as! String
|
||||
@Published var url = Bundle.main.infoDictionary!["AppUrl"] as! String
|
||||
@Published var bundle = Bundle.main.infoDictionary!["AppBundleName"] as! String
|
||||
@Published var hideStatusBar = false
|
||||
@Published var hideNavigationBar = false
|
||||
@Published var cacheDays = 1
|
||||
@Published var component = "my-element"
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
//
|
||||
// WebView.swift
|
||||
// app
|
||||
//
|
||||
// Created by Rody Davis on 6/16/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import WebKit
|
||||
|
||||
struct WebView: UIViewControllerRepresentable {
|
||||
let tag: String
|
||||
let size: CGSize
|
||||
var title: String = ""
|
||||
var bundle: String = "bundle.es"
|
||||
var url: String = ""
|
||||
|
||||
let webview: WKWebView = WKWebView()
|
||||
|
||||
func makeUIViewController(context: Context) -> WebViewController {
|
||||
let vc = WebViewController()
|
||||
vc.setBundle(bundle: AppBundle(name: bundle, url: url))
|
||||
vc.context = self
|
||||
return vc
|
||||
}
|
||||
|
||||
func updateUIViewController(_ webviewController: WebViewController, context: Context) {
|
||||
webviewController.resize(size: size)
|
||||
}
|
||||
}
|
||||
|
||||
class WebViewController: UIViewController, WKNavigationDelegate, WKUIDelegate, WKScriptMessageHandler, UIWindowSceneDelegate {
|
||||
lazy var context: WebView? = nil
|
||||
lazy var bundle: AppBundle? = nil
|
||||
let config = WKWebViewConfiguration()
|
||||
lazy var webview: WKWebView = WKWebView()
|
||||
|
||||
func resize(size: CGSize) {
|
||||
webview.frame = CGRect(x: 0 , y: 0, width: size.width, height: size.height)
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
webview = WKWebView(frame: .zero, configuration: config)
|
||||
webview.invalidateIntrinsicContentSize()
|
||||
webview.translatesAutoresizingMaskIntoConstraints = true
|
||||
webview.autoresizesSubviews = true
|
||||
webview.contentMode = .redraw
|
||||
webview.configuration.mediaTypesRequiringUserActionForPlayback = []
|
||||
webview.isHidden = false
|
||||
webview.configuration.allowsInlineMediaPlayback = false
|
||||
webview.scrollView.bounces = false
|
||||
webview.scrollView.isScrollEnabled = false
|
||||
webview.isOpaque = false
|
||||
webview.navigationDelegate = self
|
||||
webview.uiDelegate = self
|
||||
webview.frame = view.frame
|
||||
webview.configuration.userContentController.add(self, name: "handler")
|
||||
loadHtml()
|
||||
view.addSubview(webview)
|
||||
}
|
||||
|
||||
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
|
||||
switch keyPath {
|
||||
default:
|
||||
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
|
||||
}
|
||||
}
|
||||
|
||||
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
||||
handleEvent(webview, message: message)
|
||||
}
|
||||
|
||||
func loadHtml() {
|
||||
webview.configuration.defaultWebpagePreferences.allowsContentJavaScript = true
|
||||
addScript(source: bundle!.get())
|
||||
addScript(source: eventScript())
|
||||
webview.loadHTMLString(getHTML(title: context!.title), baseURL: nil)
|
||||
}
|
||||
|
||||
func addScript(source: String) {
|
||||
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
|
||||
webview.configuration.userContentController.addUserScript(script)
|
||||
}
|
||||
|
||||
func setBundle(bundle: AppBundle) {
|
||||
self.bundle = bundle
|
||||
}
|
||||
|
||||
func getHTML(title: String = "", slot: String = "") -> String {
|
||||
return """
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
|
||||
<title>\(title)</title>
|
||||
<style>
|
||||
body {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<\(context!.tag)>
|
||||
\(slot)
|
||||
</\(context!.tag)>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
}
|
||||
|
||||
func eventScript() -> String {
|
||||
return """
|
||||
document.addEventListener('native', (e) => {
|
||||
window.webkit.messageHandlers.handler.postMessage(e.detail);
|
||||
}, false);
|
||||
"""
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension WKWebView {
|
||||
public func dispatchEvent(event: String, detail: String) -> Void {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
let data = try! encoder.encode(["event": event, "detail": detail])
|
||||
let jsonString = String(data: data, encoding: .utf8)!
|
||||
let script = """
|
||||
const elem = document.querySelector('my-element')
|
||||
elem.dispatchEvent(new CustomEvent('response', { detail: \(jsonString) }))
|
||||
"""
|
||||
self.evaluateJavaScript(script)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>com.apple.security.app-sandbox</key>
|
||||
<true/>
|
||||
<key>com.apple.security.network.client</key>
|
||||
<true/>
|
||||
<key>com.apple.security.network.server</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
Reference in New Issue
Block a user