|
| 1 | +public extension HTMLAttribute where Tag: HTMLTrait.Attributes.Global { |
| 2 | + static func on(_ event: HTMLAttributeValue.MouseEvent, _ script: String) -> Self { .init(on: event, script: script) } |
| 3 | + static func on(_ event: HTMLAttributeValue.FormEvent, _ script: String) -> Self { .init(on: event, script: script) } |
| 4 | + static func on(_ event: HTMLAttributeValue.KeyboardEvent, _ script: String) -> Self { .init(on: event, script: script) } |
| 5 | +} |
| 6 | + |
| 7 | +// TODO: window events, drag events, media events (more scoped) |
| 8 | + |
| 9 | +public extension HTMLAttributeValue { |
| 10 | + struct MouseEvent: HTMLEventName { |
| 11 | + public var rawValue: String |
| 12 | + public init(rawValue: String) { |
| 13 | + self.rawValue = rawValue |
| 14 | + } |
| 15 | + |
| 16 | + public static var click: Self { .init(rawValue: "click") } |
| 17 | + public static var dblclick: Self { .init(rawValue: "dblclick") } |
| 18 | + public static var mousedown: Self { .init(rawValue: "mousedown") } |
| 19 | + public static var mousemove: Self { .init(rawValue: "mousemove") } |
| 20 | + public static var mouseout: Self { .init(rawValue: "mouseout") } |
| 21 | + public static var mouseover: Self { .init(rawValue: "mouseover") } |
| 22 | + public static var mouseup: Self { .init(rawValue: "mouseup") } |
| 23 | + public static var wheel: Self { .init(rawValue: "wheel") } |
| 24 | + } |
| 25 | + |
| 26 | + struct KeyboardEvent: HTMLEventName { |
| 27 | + public var rawValue: String |
| 28 | + public init(rawValue: String) { |
| 29 | + self.rawValue = rawValue |
| 30 | + } |
| 31 | + |
| 32 | + public static var keydown: Self { .init(rawValue: "keydown") } |
| 33 | + public static var keypress: Self { .init(rawValue: "keypress") } |
| 34 | + public static var keyup: Self { .init(rawValue: "keyup") } |
| 35 | + } |
| 36 | + |
| 37 | + struct FormEvent: HTMLEventName { |
| 38 | + public var rawValue: String |
| 39 | + public init(rawValue: String) { |
| 40 | + self.rawValue = rawValue |
| 41 | + } |
| 42 | + |
| 43 | + public static var blur: Self { .init(rawValue: "blur") } |
| 44 | + public static var change: Self { .init(rawValue: "change") } |
| 45 | + public static var contextmenu: Self { .init(rawValue: "contextmenu") } |
| 46 | + public static var focus: Self { .init(rawValue: "focus") } |
| 47 | + public static var input: Self { .init(rawValue: "input") } |
| 48 | + public static var invalid: Self { .init(rawValue: "invalid") } |
| 49 | + public static var reset: Self { .init(rawValue: "reset") } |
| 50 | + public static var search: Self { .init(rawValue: "search") } |
| 51 | + public static var select: Self { .init(rawValue: "select") } |
| 52 | + public static var submit: Self { .init(rawValue: "submit") } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +protocol HTMLEventName: RawRepresentable {} |
| 57 | + |
| 58 | +extension HTMLAttribute { |
| 59 | + init(on eventName: some HTMLEventName, script: String) { |
| 60 | + self.init(name: "on\(eventName.rawValue)", value: script) |
| 61 | + } |
| 62 | +} |
0 commit comments