-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
448 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...Codegen/Renderers/AttributeRenderer.swift → ...odegen/Renderers/AttributesRenderer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
Codegen/Sources/SRTCodegen/Renderers/EventListenersRenderer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
""" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
||
|
||
|
Oops, something went wrong.