wasm

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: Apache-2.0 Imports: 7 Imported by: 1

README

wasm Go library support

This library contains support for running Go programs under Web Assembly.

It consists of a window/DOM interface object, and simple compositor functions used for building dynamic HTML. One of the goals is to be usable with tinygo, which does not support the full range of Go's standard library.

Examples are available to show how the library can be used.

Window

Window is a simple interface to the browser DOM, allowing callbacks to be added for keyboard shortcuts, window resizing, swipe gestures:

	w := wasm.Window()
	w.OnSwipe(func (d wasm.Direction) bool {
		if d == wasm.Right {
			...
			return true // Right swipe is handled
		}
		return false // Not interested in other swipes, use default action
	})
	w.OnResize(resized)
	w.SetTitle("My window")
	w.Wait()

Compositor

The compositor allows HTML pages or fragments to be generated in a easy and consistent flow of functions. Tags are automatically closed where required, and common attributes are supported. For example:

import (
	"github.com/aamcrae/wasm"
)
...
	w := wasm.Window()
	h := wasm.NewHTML()
	h.Wr(h.H1("Title Page"))
	h.Wr(h.A(h.Href("page/index.html"), h.Id("myid"), h.Img(h.Class("image"), h.Src("flower.jpg"), h.Alt("Flower"))))
	h.Wr(h.Span(h.Class("myspan"), h.Open()) // Don't close tag
	h.Wr(h.P("This is a paragraph", h.Br(), "with a break in it)))
	h.Wr(h.Text("Combining numbers ", 12345, ", runes ", ' ', rune(0x21A7), " and strings"))
	h.Wr(h.Span(h.Close())) // Now add the closing tag for span
	w.Display(h.String())

Modifiers and conditionals are allowed so that the flow can be maintained when deeply embedded functions are used (without resorting to if/else control flow):

	// Only display title if it is not empty
	h.Wr(h.H1(h.If(len(title) > 0), title))
	h.Wr(h.A(h.Open(), h.Id("id", i), h.Href("#")))
	// complicated code to generate anchor
	h.Wr(h.A(h.Close()))

Be aware that complete syntax checking is not performed by the compositor e.g there is nothing stopping incorrect attributes being used in tags, or tags not being closed etc.

Fetcher

Fetcher is an interface to the Javascript fetch API. This interface is usable with tinygo (which does not support net/http). It allows concurrent fetching of resources:

	// Start fetching all the files required
	f1 := w.Fetcher("data/file1")
	f2 := w.Fetcher("data/file2")
	f3 := w.Fetcher("data/file3")
	...
	go func() {
		val, err := f3.Get() // Blocks until file3 is read.
		...
	}
	// Retrieve data only when available
	if f1.Ready() {
		data1, err := f1.Get()
		...
	}
	data2, err := f2.Get() // blocks until ready

Documentation

Overview

DO NOT EDIT - generated file

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Direction

type Direction int

Direction is an enum indicating the swipe direction.

const (
	Right Direction = iota
	Left
	Up
	Down
)

type HTML

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

func NewHTML

func NewHTML() *HTML

func (*HTML) A

func (h *HTML) A(elems ...any) *frag

func (*HTML) Action

func (h *HTML) Action(elems ...any) attr

func (*HTML) Alt

func (h *HTML) Alt(elems ...any) attr

func (*HTML) Area

func (h *HTML) Area(elems ...any) *frag

func (*HTML) Base

func (h *HTML) Base(elems ...any) *frag

func (*HTML) Border

func (h *HTML) Border(elems ...any) attr

func (*HTML) Br

func (h *HTML) Br(elems ...any) *frag

func (*HTML) Button

func (h *HTML) Button(elems ...any) *frag

func (*HTML) Checked

func (h *HTML) Checked(elems ...any) attr

func (*HTML) Class

func (h *HTML) Class(elems ...any) attr

func (*HTML) Close

func (h *HTML) Close() flag

For non-empty tags, generate the closing tag.

func (*HTML) Col

func (h *HTML) Col(elems ...any) *frag

func (*HTML) Content

func (h *HTML) Content(elems ...any) attr

func (*HTML) Disabled

func (h *HTML) Disabled(elems ...any) attr

func (*HTML) Div

func (h *HTML) Div(elems ...any) *frag

func (*HTML) Download

func (h *HTML) Download(elems ...any) attr

func (*HTML) Embed

func (h *HTML) Embed(elems ...any) *frag

func (*HTML) For

func (h *HTML) For(elems ...any) attr

func (*HTML) Form

func (h *HTML) Form(elems ...any) *frag

func (*HTML) H1

func (h *HTML) H1(elems ...any) *frag

func (*HTML) H2

func (h *HTML) H2(elems ...any) *frag

func (*HTML) H3

func (h *HTML) H3(elems ...any) *frag

func (*HTML) H4

func (h *HTML) H4(elems ...any) *frag

func (*HTML) H5

func (h *HTML) H5(elems ...any) *frag

func (*HTML) H6

func (h *HTML) H6(elems ...any) *frag

func (*HTML) Height

func (h *HTML) Height(elems ...any) attr

func (*HTML) Hr

func (h *HTML) Hr(elems ...any) *frag

func (*HTML) Href

func (h *HTML) Href(elems ...any) attr

func (*HTML) Id

func (h *HTML) Id(elems ...any) attr

func (*HTML) If

func (h *HTML) If(c bool) flag

If will drop this element if the condition is false

func (*HTML) Img

func (h *HTML) Img(elems ...any) *frag

func (*HTML) Input

func (h *HTML) Input(elems ...any) *frag

func (*HTML) Label

func (h *HTML) Label(elems ...any) *frag

func (*HTML) Li

func (h *HTML) Li(elems ...any) *frag
func (h *HTML) Link(elems ...any) *frag

func (*HTML) Meta

func (h *HTML) Meta(elems ...any) *frag

func (*HTML) Method

func (h *HTML) Method(elems ...any) attr

func (*HTML) Name

func (h *HTML) Name(elems ...any) attr

func (*HTML) Ol

func (h *HTML) Ol(elems ...any) *frag

func (*HTML) Onclick

func (h *HTML) Onclick(elems ...any) attr

func (*HTML) Onsubmit

func (h *HTML) Onsubmit(elems ...any) attr

func (*HTML) Open

func (h *HTML) Open() flag

For non-empty tags, do not generate the closing tag

func (*HTML) Option

func (h *HTML) Option(elems ...any) *frag

func (*HTML) P

func (h *HTML) P(elems ...any) *frag

func (*HTML) Rel

func (h *HTML) Rel(elems ...any) attr

func (*HTML) Select

func (h *HTML) Select(elems ...any) *frag

func (*HTML) Selected

func (h *HTML) Selected(elems ...any) attr

func (*HTML) Size

func (h *HTML) Size(elems ...any) attr

func (*HTML) Source

func (h *HTML) Source(elems ...any) *frag

func (*HTML) Span

func (h *HTML) Span(elems ...any) *frag

func (*HTML) Src

func (h *HTML) Src(elems ...any) attr

func (*HTML) Style

func (h *HTML) Style(elems ...any) attr

func (*HTML) Summary

func (h *HTML) Summary(elems ...any) attr

func (*HTML) Table

func (h *HTML) Table(elems ...any) *frag

func (*HTML) Tbody

func (h *HTML) Tbody(elems ...any) *frag

func (*HTML) Td

func (h *HTML) Td(elems ...any) *frag

func (*HTML) Text

func (h *HTML) Text(s ...any) string

Text composes the list of elements to a single string No tags are allowed, and a string is returned.

func (*HTML) Thead

func (h *HTML) Thead(elems ...any) *frag

func (*HTML) Title

func (h *HTML) Title(elems ...any) attr

func (*HTML) Tr

func (h *HTML) Tr(elems ...any) *frag

func (*HTML) Track

func (h *HTML) Track(elems ...any) *frag

func (*HTML) Type

func (h *HTML) Type(elems ...any) attr

func (*HTML) Ul

func (h *HTML) Ul(elems ...any) *frag

func (*HTML) Value

func (h *HTML) Value(elems ...any) attr

func (*HTML) Wbr

func (h *HTML) Wbr(elems ...any) *frag

func (*HTML) Width

func (h *HTML) Width(elems ...any) attr

func (*HTML) Wr

func (h *HTML) Wr(s ...any)

Short hand write

type Window

type Window struct {
	Width, Height                int // Width and Height of window
	Window, Document, Head, Body js.Value
	// contains filtered or unexported fields
}

Window is the main structure for interfacing to the browser

func GetWindow

func GetWindow() *Window

GetWindow creates a Window ready to interface to the browser.

func (*Window) AddJSFunction

func (w *Window) AddJSFunction(name string, f func(js.Value, []js.Value) any)

AddJSFunction registers a javascript function that invokes the Go function passed.

func (*Window) AddStyle

func (w *Window) AddStyle(s string)

AddStyle adds the CSS string to the window directly.

func (*Window) Alert

func (w *Window) Alert(a string)

Alert displays an alert box.

func (*Window) Display

func (w *Window) Display(s string)

Display sets the HTML onto the window.

func (*Window) Fetcher

func (w *Window) Fetcher(file string) *fetcher

Fetch retrieves the file from the server.

func (*Window) GetById

func (w *Window) GetById(id string) js.Value

GetById returns the named JS element.

func (*Window) Goto

func (w *Window) Goto(url string)

Goto navigates the browser to the URL.

func (*Window) LoadStyle

func (w *Window) LoadStyle(s string)

LoadStyle adds a link to read the CSS file indicated

func (*Window) OnKey

func (w *Window) OnKey(elem js.Value, f func(key string))

OnKey registers a callback to be invoked when a key is pressed on this element.

func (*Window) OnResize

func (w *Window) OnResize(f func())

OnResize registers a callback to be invoked when the window changes size.

func (*Window) OnSwipe

func (w *Window) OnSwipe(f func(Direction) bool)

OnSwipe registers a callback to be called for swipe events. If the callback handles the event, it returns true.

func (*Window) SetTitle

func (w *Window) SetTitle(title string) *Window

SetTitle sets the window title

func (*Window) Wait

func (w *Window) Wait()

Wait forces this thread to wait.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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