Skip to content

Commit

Permalink
events
Browse files Browse the repository at this point in the history
  • Loading branch information
omochi committed May 5, 2024
1 parent 850dbfd commit 2e8f54d
Show file tree
Hide file tree
Showing 9 changed files with 448 additions and 71 deletions.
1 change: 1 addition & 0 deletions Codegen/Sources/SRTCodegen/Def.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ struct Def: Codable {
var tagNames: [String]
var voidElements: [String]
var elementAttributes: [String: [String]]
var eventAttributes: [String]
var cssProperties: [String]

mutating func fix() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import CodegenKit

struct AttributeRenderer: Renderer {
struct AttributesRenderer: Renderer {
var def: Def

func isTarget(file: URL) -> Bool {
Expand Down
32 changes: 32 additions & 0 deletions Codegen/Sources/SRTCodegen/Renderers/EventListenersRenderer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation
import CodegenKit

struct EventListenersRenderer: Renderer {
var def: Def

func isTarget(file: URL) -> Bool {
file.lastPathComponent == "EventListeners.swift"
}

func render(template: inout CodeTemplateModule.CodeTemplate, file: URL, on runner: CodegenKit.CodegenRunner) throws {
let code = def.eventAttributes.map { (attribute) in
renderSetter(attribute: attribute)
}.joined(separator: "\n")

template["setters"] = code
}

private func renderSetter(attribute: String) -> String {
var attribute = attribute
if attribute.hasPrefix("on") {
attribute.removeFirst(2)
}

return """
public func \(renderIdentifier(attribute))(_ value: EventListener) -> EventListeners {
set("\(attribute)", to: value)
}
"""
}
}
5 changes: 3 additions & 2 deletions Codegen/Sources/SRTCodegen/SRTCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public struct SRTCodegen {
renderers: [
HTMLTagRenderer(def: def),
HTMLVoidTagRenderer(def: def),
AttributeRenderer(def: def),
StyleRenderer(def: def)
AttributesRenderer(def: def),
StyleRenderer(def: def),
EventListenersRenderer(def: def)
]
)

Expand Down
2 changes: 2 additions & 0 deletions Codegen/index.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { htmlTagNames } from "html-tag-names";
import { htmlVoidElements } from "html-void-elements";
import { htmlElementAttributes } from "html-element-attributes";
import { htmlEventAttributes } from "html-event-attributes";
import * as kcpModule from "known-css-properties";

const json = {
"tagNames": htmlTagNames,
"voidElements": htmlVoidElements,
"elementAttributes": htmlElementAttributes,
"eventAttributes": htmlEventAttributes,
"cssProperties": kcpModule.all
};

Expand Down
10 changes: 10 additions & 0 deletions Codegen/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "MIT",
"dependencies": {
"html-element-attributes": "^3.4.0",
"html-event-attributes": "^2.2.0",
"html-tag-names": "^2.1.0",
"html-void-elements": "^3.0.0",
"known-css-properties": "^0.30.0"
Expand Down
68 changes: 0 additions & 68 deletions Sources/React/HTML/EventListener.swift
Original file line number Diff line number Diff line change
@@ -1,71 +1,3 @@
import SRTDOM

public typealias EventListener = Function<Void, JSEvent>

public struct EventListeners: Hashable & RawRepresentable {
public typealias RawValue = [String: EventListener]

public init(rawValue: [String: EventListener]) {
self.rawValue = rawValue
}

public var rawValue: [String: EventListener]

public init(_ value: [String: EventListener]) {
self.rawValue = value
}

public subscript(name: String) -> EventListener? {
get { rawValue[name] }
set { rawValue[name] = newValue }
}

public var keys: some Collection<String> {
rawValue.keys
}

public func set(_ name: String, to value: EventListener) -> EventListeners {
var copy = self
copy[name] = value
return copy
}

public mutating func merge(_ other: EventListeners) {
rawValue.merge(other.rawValue) { $1 }
}
}

extension EventListeners: ExpressibleByDictionaryLiteral {
public typealias Key = String
public typealias Value = EventListener

public init(dictionaryLiteral: (Key, Value)...) {
let value = Dictionary(uniqueKeysWithValues: dictionaryLiteral)
self.init(value)
}
}

extension EventListeners: Sequence {
public typealias Element = (key: Key, value: EventListener)

public func makeIterator() -> some IteratorProtocol<Element> {
rawValue.makeIterator()
}
}

extension EventListeners {
public func click(_ x: EventListener) -> EventListeners {
set("click", to: x)
}

public func change(_ x: EventListener) -> EventListeners {
set("change", to: x)
}

public func input(_ x: EventListener) -> EventListeners {
set("input", to: x)
}
}



Loading

0 comments on commit 2e8f54d

Please sign in to comment.