wasm

package
v0.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2024 License: MIT, MIT Imports: 17 Imported by: 0

Documentation

Overview

Package wasm defines several utilities to facilitate the writing of small WASM widgets in GoNB (or elsewhere).

It's based on `gowebapi`, but extends it in ways to make it ergonomic.

The variable `IsWasm` can be used to check in runtime if a program was compiled for wasm -- in case this is needed. This is the only symbol exported by this package for non-wasm builds.

It's built on top of the library `github.com/gowebapi/webapi`, a wrapper to convert most web APIs directly to Go methods -- as opposed to using the "syscall/js" indirect calling methods.

**Warning**: this is still experimental, and the API may change. Hopefully someone will improve `gowebapi` or create something new better.

Index

Constants

View Source
const (
	KeyCodeNone  = uint(0)
	KeyCodeLeft  = 37
	KeyCodeUp    = 38
	KeyCodeRight = 39
	KeyCodeDown  = 40
)

Key codes that can be used when comparing with a KeyboardEventCompatible.KeyCode()

Variables

View Source
var (
	// Doc is a shortcut to the webpage's DOM document.
	Doc *webapi.Document

	// Win is a shortcut to the webpage's DOM window.
	Win *webapi.Window
)
View Source
var IsWasm = false

IsWasm is true in WASM builds, and false otherwise.

Functions

func Alert

func Alert(msg string)

Alert opens up a pop-up with the given msg.

func Alertf

func Alertf(msg string, args ...any)

Alertf opens up a pop-up with the formatted msg.

func Append

func Append(parent, child NodeCompatible)

Append will append the child to the parent node.

func AppendHTML

func AppendHTML(parent Element, html string)

AppendHTML inserts html at the end of the current element. It's a shortcut to InsertAdjacentHTML("beforeend", html).

func AsButton

AsButton casts some type of element as an HTMLButtonElement.

func AsCanvas

AsCanvas casts some type of element as an HTMLCanvasElement.

func AsHTML

AsHTML casts some type of element as an HTMLElement.

func AsImage

AsImage casts some type of element as an HTMLImageElement.

func AsInput

AsInput casts some type of element as an HTMLInputElement.

func AsKeyboardEvent

func AsKeyboardEvent(e EventCompatible) *htmlevent.KeyboardEvent

AsKeyboardEvent casts some type of element as an HTMLKeyboardEventElement.

func AsMouseEvent

func AsMouseEvent(e EventCompatible) *htmlevent.MouseEvent

AsMouseEvent casts some type of element as an HTMLMouseEventElement.

func AsNode

func AsNode(e NodeCompatible) *dom.Node

AsNode converts any pointer to a struct that extends dom.Node, back to a *dom.Node.

func AsOption

AsOption casts some type of element as an HTMLOptionElement.

func AsScript

AsScript casts some type of element as an HTMLScriptElement.

func AsSelect

AsSelect casts some type of element as an HTMLSelectElement.

func AsSpan

AsSpan casts some type of element as an HTMLSpanElement.

func AsTR

AsTR casts some type of element as an HTMLTableRowElement.

func AsTable

AsTable casts some type of element as an HTMLTableElement.

func ById

func ById(id string) *html.HTMLElement

ById returns the HTML element with the given Id, or nil if didn't find it.

func Disable

func Disable(e HtmlElementCompatible)

Disable disables buttons.

func DiscardEvent

func DiscardEvent(e *domcore.Event)

DiscardEvent is an event handler that stops propagation and the default browser handling. Can be used as a parameter to the `On` function.

func Enable

func Enable(e HtmlElementCompatible)

Enable enables buttons.

func IsNil

func IsNil(e Compatible) bool

IsNil checks for nil interface or pointer.

func NewElem

func NewElem(tag string, attributes ...string) *dom.Element

NewElem creates a new DOM element with the tag / attributes given. Attributes can be given without a value (e.g.: `readonly`) or as a key/value pair (`width=10`), split by the first "=".

func On

func On(element EventTargetCompatible, eventType string, callback func(ev EventCompatible))

On adds a callback to the element when event type happens, using `element.AddEventListener`. All event options default to false. See OnWith() to set options.

func OnWith

func OnWith(element EventTargetCompatible, eventType string, callback func(ev EventCompatible), options EventOptions)

OnWith adds a callback to the element when eventType happens, using `element.AddEventListener`. There is no easy way to remove the event listener from Go :(

Event types: "change", "load", "click", "mouseup", etc.

func TRAddValue

func TRAddValue(tr *html.HTMLTableRowElement, value interface{}) *html.HTMLTableColElement

TRAddValue adds a cell to a row (`tr`) in a table.

func WaitForever

func WaitForever()

WaitForever waits on a channel forever, preventing the Go WASM program to ever exit. This is needed on a program that will be listening to user interaction. Use at the end of the program.

Types

type Audio

type Audio struct {
	// contains filtered or unexported fields
}

Audio is a simple wrapper over JS value that should represent a "<audio ...>" node.

func AsAudio

func AsAudio(e *html.HTMLElement) *Audio

AsAudio casts some type of element as an audio.AudioNode.

func (*Audio) Play

func (a *Audio) Play()

Play will play the audio object using Javascript functionality.

type Compatible

type Compatible interface {
	JSValue() js.Value
}

Compatible is an interface implemented by most of webapi types.

type Element

type Element interface {
	NamespaceURI() *string
	Prefix() *string
	LocalName() string
	TagName() string
	Id() string
	SetId(value string)
	ClassName() string
	SetClassName(value string)
	ClassList() *domcore.DOMTokenList
	Slot() string
	SetSlot(value string)
	Attributes() *dom.NamedNodeMap
	ShadowRoot() *dom.ShadowRoot
	InnerHTML() string
	SetInnerHTML(value string)
	OuterHTML() string
	SetOuterHTML(value string)
	ScrollTop() float64
	SetScrollTop(value float64)
	ScrollLeft() float64
	SetScrollLeft(value float64)
	ScrollWidth() int
	ScrollHeight() int
	ClientTop() int
	ClientLeft() int
	ClientWidth() int
	ClientHeight() int
	OnFullscreenChange() domcore.EventHandlerFunc
	OnFullscreenError() domcore.EventHandlerFunc
	Children() *dom.HTMLCollection
	FirstElementChild() *dom.Element
	LastElementChild() *dom.Element
	ChildElementCount() uint
	PreviousElementSibling() *dom.Element
	NextElementSibling() *dom.Element
	AssignedSlot() js.Value
	Role() *string
	SetRole(value *string)
	AriaActiveDescendant() *string
	SetAriaActiveDescendant(value *string)
	AriaAtomic() *string
	SetAriaAtomic(value *string)
	AriaAutoComplete() *string
	SetAriaAutoComplete(value *string)
	AriaBusy() *string
	SetAriaBusy(value *string)
	AriaChecked() *string
	SetAriaChecked(value *string)
	AriaColCount() *string
	SetAriaColCount(value *string)
	AriaColIndex() *string
	SetAriaColIndex(value *string)
	AriaColSpan() *string
	SetAriaColSpan(value *string)
	AriaControls() *string
	SetAriaControls(value *string)
	AriaCurrent() *string
	SetAriaCurrent(value *string)
	AriaDescribedBy() *string
	SetAriaDescribedBy(value *string)
	AriaDetails() *string
	SetAriaDetails(value *string)
	AriaDisabled() *string
	SetAriaDisabled(value *string)
	AriaErrorMessage() *string
	SetAriaErrorMessage(value *string)
	AriaExpanded() *string
	SetAriaExpanded(value *string)
	AriaFlowTo() *string
	SetAriaFlowTo(value *string)
	AriaHasPopup() *string
	SetAriaHasPopup(value *string)
	AriaHidden() *string
	SetAriaHidden(value *string)
	AriaInvalid() *string
	SetAriaInvalid(value *string)
	AriaKeyShortcuts() *string
	SetAriaKeyShortcuts(value *string)
	AriaLabel() *string
	SetAriaLabel(value *string)
	AriaLabelledBy() *string
	SetAriaLabelledBy(value *string)
	AriaLevel() *string
	SetAriaLevel(value *string)
	AriaLive() *string
	SetAriaLive(value *string)
	AriaModal() *string
	SetAriaModal(value *string)
	AriaMultiLine() *string
	SetAriaMultiLine(value *string)
	AriaMultiSelectable() *string
	SetAriaMultiSelectable(value *string)
	AriaOrientation() *string
	SetAriaOrientation(value *string)
	AriaOwns() *string
	SetAriaOwns(value *string)
	AriaPlaceholder() *string
	SetAriaPlaceholder(value *string)
	AriaPosInSet() *string
	SetAriaPosInSet(value *string)
	AriaPressed() *string
	SetAriaPressed(value *string)
	AriaReadOnly() *string
	SetAriaReadOnly(value *string)
	AriaRelevant() *string
	SetAriaRelevant(value *string)
	AriaRequired() *string
	SetAriaRequired(value *string)
	AriaRoleDescription() *string
	SetAriaRoleDescription(value *string)
	AriaRowCount() *string
	SetAriaRowCount(value *string)
	AriaRowIndex() *string
	SetAriaRowIndex(value *string)
	AriaRowSpan() *string
	SetAriaRowSpan(value *string)
	AriaSelected() *string
	SetAriaSelected(value *string)
	AriaSetSize() *string
	SetAriaSetSize(value *string)
	AriaSort() *string
	SetAriaSort(value *string)
	AriaValueMax() *string
	SetAriaValueMax(value *string)
	AriaValueMin() *string
	SetAriaValueMin(value *string)
	AriaValueNow() *string
	SetAriaValueNow(value *string)
	AriaValueText() *string
	SetAriaValueText(value *string)
	AddEventFullscreenChange(listener func(event *domcore.Event, currentTarget *dom.Element)) js.Func
	SetOnFullscreenChange(listener func(event *domcore.Event, currentTarget *dom.Element)) js.Func
	AddEventFullscreenError(listener func(event *domcore.Event, currentTarget *dom.Element)) js.Func
	SetOnFullscreenError(listener func(event *domcore.Event, currentTarget *dom.Element)) js.Func
	HasAttributes() (_result bool)
	GetAttributeNames() (_result []string)
	GetAttribute(qualifiedName string) (_result *string)
	GetAttributeNS(namespace *string, localName string) (_result *string)
	SetAttribute(qualifiedName string, value string)
	SetAttributeNS(namespace *string, qualifiedName string, value string)
	RemoveAttribute(qualifiedName string)
	RemoveAttributeNS(namespace *string, localName string)
	ToggleAttribute(qualifiedName string, force *bool) (_result bool)
	HasAttribute(qualifiedName string) (_result bool)
	HasAttributeNS(namespace *string, localName string) (_result bool)
	GetAttributeNode(qualifiedName string) (_result *dom.Attr)
	GetAttributeNodeNS(namespace *string, localName string) (_result *dom.Attr)
	SetAttributeNode(attr *dom.Attr) (_result *dom.Attr)
	SetAttributeNodeNS(attr *dom.Attr) (_result *dom.Attr)
	RemoveAttributeNode(attr *dom.Attr) (_result *dom.Attr)
	AttachShadow(init *dom.ShadowRootInit) (_result *dom.ShadowRoot)
	Closest(selectors string) (_result *dom.Element)
	Matches(selectors string) (_result bool)
	WebkitMatchesSelector(selectors string) (_result bool)
	GetElementsByTagName(qualifiedName string) (_result *dom.HTMLCollection)
	GetElementsByTagNameNS(namespace *string, localName string) (_result *dom.HTMLCollection)
	GetElementsByClassName(classNames string) (_result *dom.HTMLCollection)
	InsertAdjacentElement(where string, element *dom.Element) (_result *dom.Element)
	InsertAdjacentText(where string, data string)
	InsertAdjacentHTML(position string, text string)
	GetFragmentInformation(filter dom.FragmentFilter) (_result *dom.PromiseDeadFragmentInformation)
	ComputedStyleMap() (_result *typedom.StylePropertyMapReadOnly)
	GetClientRects() (_result *geometry.DOMRectList)
	GetBoundingClientRect() (_result *geometry.DOMRect)
	ScrollIntoView(arg *dom.Union)
	Scroll(options *view.ScrollToOptions)
	Scroll2(x float64, y float64)
	ScrollTo(options *view.ScrollToOptions)
	ScrollTo2(x float64, y float64)
	ScrollBy(options *view.ScrollToOptions)
	ScrollBy2(x float64, y float64)
	RequestFullscreen(options *dom.FullscreenOptions) (_result *javascript.PromiseVoid)
	SetPointerCapture(pointerId int)
	ReleasePointerCapture(pointerId int)
	HasPointerCapture(pointerId int) (_result bool)
	RequestPointerLock()
	GetBoxQuads(options *view.BoxQuadOptions) (_result []*geometry.DOMQuad)
	ConvertQuadFromNode(quad *geometry.DOMQuadInit, from *dom.Union, options *view.ConvertCoordinateOptions) (_result *geometry.DOMQuad)
	ConvertRectFromNode(rect *geometry.DOMRectReadOnly, from *dom.Union, options *view.ConvertCoordinateOptions) (_result *geometry.DOMQuad)
	ConvertPointFromNode(point *geometry.DOMPointInit, from *dom.Union, options *view.ConvertCoordinateOptions) (_result *geometry.DOMPoint)
	Prepend(nodes ...*dom.Union)
	Append(nodes ...*dom.Union)
	QuerySelector(selectors string) (_result *dom.Element)
	QuerySelectorAll(selectors string) (_result *dom.NodeList)
	Before(nodes ...*dom.Union)
	After(nodes ...*dom.Union)
	ReplaceWith(nodes ...*dom.Union)
	Remove()
	Animate(keyframes *javascript.Object, options *dom.Union) (_result *webani.Animation)
	GetAnimations() (_result []*webani.Animation)
}

Element is the interface implemented by webapi.Element structure.

type EventCompatible

type EventCompatible interface {
	Compatible
	Bubbles() bool
	PreventDefault()
	StopPropagation()
	StopImmediatePropagation()
}

type EventOptions

type EventOptions struct {
	Capture, Once, Passive bool
}

EventOptions are the options passed to AddEventListener used in the `OnWith` function.

type EventTargetCompatible

type EventTargetCompatible interface {
	Compatible
	AddEventListener(_type string, callback *domcore.EventListenerValue, options *domcore.Union)
}

EventTargetCompatible is the interface that all Element/Node types of the DOM implement.

type HtmlElementCompatible

type HtmlElementCompatible interface {
	Element
	Style() *cssom.CSSStyleDeclaration
}

HtmlElementCompatible is anything that behaves like a dom.HtmlElement.

type KeyValue

type KeyValue struct {
	Key, Value string
}

KeyValue is a simple structure used for different things. Key and Value are both string.

type KeyboardEventCompatible

type KeyboardEventCompatible interface {
	EventCompatible
	Key() string
	Code() string
	KeyCode() uint
	CharCode() uint

	AltKey() bool
	CtrlKey() bool
	ShiftKey() bool
	MetaKey() bool

	Repeat() bool
	IsComposing() bool
}

type MouseEventCompatible

type MouseEventCompatible interface {
	EventCompatible
	Button() int

	AltKey() bool
	CtrlKey() bool
	ShiftKey() bool

	OffsetX() float64
	OffsetY() float64
}

type NodeCompatible

type NodeCompatible interface {
	EventTargetCompatible
	AppendChild(node *dom.Node) (_result *dom.Node)
	ChildNodes() *dom.NodeList
	RemoveChild(child *dom.Node) (_result *dom.Node)
}

NodeCompatible is an interface implemented by several of the webapi types that support having sub-nodes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL