Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

support for wasm #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions karma/go/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"fmt"
"github.com/albrow/vdom"
"github.com/gopherjs/gopherjs/js"
"honnef.co/go/js/dom"
"strconv"
"strings"

"github.com/gopherjs/gopherjs/js"
"github.com/gowasm/vdom"
dom "github.com/yml/go-js-dom"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions karma/go/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"github.com/JohannWeging/jasmine"
"github.com/albrow/vdom"
"honnef.co/go/js/dom"
"github.com/gowasm/vdom"
dom "github.com/yml/go-js-dom"
)

var (
Expand Down
32 changes: 21 additions & 11 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package vdom

import (
"fmt"
"github.com/gopherjs/gopherjs/js"
"honnef.co/go/js/dom"

"github.com/gopherjs/gopherwasm/js"
dom "github.com/gowasm/go-js-dom"
)

var document dom.Document

func init() {
// We only want to initialize document if we are running in the browser.
// We can detect this by checking if the document is defined.
if js.Global != nil && js.Global.Get("document") != js.Undefined {
if js.Global() != js.Null() && js.Global().Get("document") != js.Undefined() {
document = dom.GetWindow().Document()
}
}
Expand Down Expand Up @@ -47,22 +48,20 @@ type Append struct {
// Patch satisfies the Patcher interface and applies the change to the
// actual DOM.
func (p *Append) Patch(root dom.Element) error {
// fmt.Println("Got parent: ", p.Parent)
// fmt.Println("Parent == nil: ", p.Parent == nil)
// fmt.Println("Parent != nil: ", p.Parent != nil)
fmt.Println("APPEND PATCH")
var parent dom.Node
if p.Parent != nil {
// fmt.Println("Finding parent in DOM")
fmt.Println("Finding parent in DOM")
parent = findInDOM(p.Parent, root)
} else {
// fmt.Println("Setting parent as root")
fmt.Println("Setting parent as root")
parent = root
}
// fmt.Println("Computed parent: ", parent)
fmt.Println("Computed parent: ", parent)
child := createForDOM(p.Child)
// fmt.Println("Created child: ", child)
fmt.Println("Created child: ", child)
parent.AppendChild(child)
// fmt.Println("Successfully appended")
fmt.Println("Successfully appended")
return nil
}

Expand All @@ -75,6 +74,7 @@ type Replace struct {
// Patch satisfies the Patcher interface and applies the change to the
// actual DOM.
func (p *Replace) Patch(root dom.Element) error {
fmt.Println("REPLACE PATCH")
var parent dom.Node
if p.Old.Parent() != nil {
parent = findInDOM(p.Old.Parent(), root)
Expand All @@ -95,6 +95,7 @@ type Remove struct {
// Patch satisfies the Patcher interface and applies the change to the
// actual DOM.
func (p *Remove) Patch(root dom.Element) error {
fmt.Println("REMOVE PATCH")
var parent dom.Node
if p.Node.Parent() != nil {
parent = findInDOM(p.Node.Parent(), root)
Expand Down Expand Up @@ -135,6 +136,7 @@ type SetAttr struct {
// Patch satisfies the Patcher interface and applies the change to the
// actual DOM.
func (p *SetAttr) Patch(root dom.Element) error {
fmt.Println("SET ATTR PATCH")
self := findInDOM(p.Node, root).(dom.Element)
self.SetAttribute(p.Attr.Name, p.Attr.Value)
return nil
Expand All @@ -150,6 +152,7 @@ type RemoveAttr struct {
// Patch satisfies the Patcher interface and applies the change to the
// actual DOM.
func (p *RemoveAttr) Patch(root dom.Element) error {
fmt.Println("REMOVE ATTR PATCH")
self := findInDOM(p.Node, root).(dom.Element)
self.RemoveAttribute(p.AttrName)
return nil
Expand All @@ -160,6 +163,8 @@ func (p *RemoveAttr) Patch(root dom.Element) error {
// starting point.
func findInDOM(node Node, root dom.Element) dom.Node {
el := root.ChildNodes()[node.Index()[0]]
fmt.Println("ELEMENT:", el)
fmt.Println("NODE.INDEX", node.Index())
for _, i := range node.Index()[1:] {
el = el.ChildNodes()[i]
}
Expand All @@ -171,6 +176,7 @@ func findInDOM(node Node, root dom.Element) dom.Node {
func createForDOM(node Node) dom.Node {
switch node.(type) {
case *Element:
fmt.Println("Creating an element")
vEl := node.(*Element)
el := document.CreateElement(vEl.Name)
for _, attr := range vEl.Attrs {
Expand All @@ -179,10 +185,14 @@ func createForDOM(node Node) dom.Node {
el.SetInnerHTML(string(vEl.InnerHTML()))
return el
case *Text:

fmt.Println("Creating a text")
vText := node.(*Text)
textNode := document.CreateTextNode(string(vText.Value))
return textNode
case *Comment:

fmt.Println("Creating a comment")
vComment := node.(*Comment)
commentNode := document.Underlying().Call("createComment", string(vComment.Value))
return dom.WrapNode(commentNode)
Expand Down