package appimport// EventHandler represents a function that can handle HTML events. They are// always called on the UI goroutine.typeEventHandlerfunc(ctx Context, e Event)// Event is the interface that describes a javascript event.typeEventstruct {Value// Data carries event attributes in server-side renderers, eg livepage. // It is nil in browser-based events. Data map[string]string}// PreventDefault cancels the event if it is cancelable. The default action that// belongs to the event will not occur.func ( Event) () { .Call("preventDefault")}// If several listeners are attached to the same element for the same event type,// they are called in the order in which they were added. If// StopImmediatePropagation() is invoked during one such call, no remaining// listeners will be called, either on that element or any other element.func ( Event) () { .Call("stopImmediatePropagation")}// EventOption represents an option for configuring event handlers, such as// setting scopes or marking them as passive.typeEventOptionstruct { name string value string}// EventScope returns an EventOption that adds a scope to an event handler.// This is useful in dynamic UI contexts to ensure correct mounting and// dismounting of handlers as UI elements are reordered. The scope is defined// by concatenating the provided path arguments into a unique identifier.func ( ...any) EventOption {returnEventOption{name: "scope",value: toPath(...), }}// PassiveEvent returns an EventOption that marks an event handler as passive.// Passive handlers improve performance for high-frequency events by signaling// that they will not call Event.PreventDefault(), allowing the browser to// optimize event processing.// More on passive listeners: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#using_passive_listenersfunc () EventOption {returnEventOption{name: "passive", }}type eventHandlers map[string]eventHandlerfunc ( eventHandlers) ( string, EventHandler, ...EventOption) {if != nil { [] = makeEventHandler(, , ...) }}type eventHandler struct { event string scope string passive bool goHandler EventHandler jsHandler Func close func()}func makeEventHandler( string, EventHandler, ...EventOption) eventHandler { := eventHandler{event: ,goHandler: , }for , := range {switch .name {case"scope": .scope = .valuecase"passive": .passive = true } }return}func ( eventHandler) ( eventHandler) bool {return .event == .event && .scope == .scope && .passive == .passive &&reflect.ValueOf(.goHandler).Pointer() == reflect.ValueOf(.goHandler).Pointer()}func ( eventHandler) () map[string]any {if .passive {returnmap[string]any{"passive": true, } }returnnil}func trackMousePosition( Event) { := .Get("clientX")if !.Truthy() {return } := .Get("clientY")if !.Truthy() {return }Window().setCursorPosition(.Int(), .Int())}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.