Skip to content

Add snippet sharing support. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 10, 2015
Merged
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
4 changes: 3 additions & 1 deletion playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
<span id="controls">
<input type="button" value="Run" ng-click="run(false)" />
<input type="button" value="Format" ng-click="format()" />
<input type="button" value="Share" ng-click="share()" />
<input type="text" class="show-share-url-{{showShareUrl}}" id="share-url" value="{{shareUrl}}" />
<label><input ng-model="showGenerated" type="checkbox" />Show generated code for main package</label>
</span>
</div>

<div class="box show-generated-{{showGenerated}}" id="content">
<div class="box yellow" id="input">
<textarea ng-model="code" id="code" autocorrect="off" autocomplete="off" autocapitalize="off" spellcheck="false"></textarea>
Expand Down
10 changes: 10 additions & 0 deletions playground/playground.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ a {
padding: 10px;
}

#share-url {
width: 320px;
font-size: 16px;
border: 1px solid #ccc;
background: #eee;
}
#share-url.show-share-url-false {
display: none;
}

.show-generated-false #generated {
display: none;
}
Expand Down
93 changes: 82 additions & 11 deletions playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,48 @@ import (
"github.com/gopherjs/gopherjs/compiler"
"github.com/gopherjs/gopherjs/js"
"github.com/neelance/go-angularjs"
"honnef.co/go/js/dom"
"honnef.co/go/js/xhr"
)

type Line map[string]string

var output []Line

const snippetStoreHost = "snippets.gotools.org"

func main() {
codeReady := make(chan struct{}) // Used to synchronize when "code" value is ready.

app := angularjs.NewModule("playground", nil, nil)

app.NewController("PlaygroundCtrl", func(scope *angularjs.Scope) {
scope.Set("code", "package main\n\nimport (\n\t\"fmt\"\n\t\"github.com/gopherjs/gopherjs/js\"\n)\n\nfunc main() {\n\tfmt.Println(\"Hello, playground\")\n\tjs.Global.Call(\"alert\", \"Hello, JavaScript\")\n\tprintln(\"Hello, JS console\")\n}\n")
if strings.HasPrefix(dom.GetWindow().Location().Hash, "#/") {
id := dom.GetWindow().Location().Hash[2:]

req := xhr.NewRequest("GET", "http://"+snippetStoreHost+"/p/"+id)
req.ResponseType = xhr.ArrayBuffer
go func() {
err := req.Send(nil)
if err != nil || req.Status != 200 {
scope.Apply(func() {
scope.Set("output", []Line{Line{"type": "err", "content": `failed to load snippet "` + id + `"`}})
})
return
}

data := js.Global.Get("Uint8Array").New(req.Response).Interface().([]byte)
scope.Apply(func() {
scope.Set("code", string(data))
close(codeReady)
})
}()
} else {
scope.Set("code", "package main\n\nimport (\n\t\"fmt\"\n\t\"github.com/gopherjs/gopherjs/js\"\n)\n\nfunc main() {\n\tfmt.Println(\"Hello, playground\")\n\tjs.Global.Call(\"alert\", \"Hello, JavaScript\")\n\tprintln(\"Hello, JS console\")\n}\n")
close(codeReady)
}
scope.Set("shareUrl", "")
scope.Set("showShareUrl", false)
scope.Set("showGenerated", false)
scope.Set("generated", `(generated code will be shown here after clicking "Run")`)

Expand All @@ -42,6 +73,10 @@ func main() {
setupEnvironment(scope)

codeArea := angularjs.ElementById("code")
codeArea.On("input", func(e *angularjs.Event) {
scope.Set("showShareUrl", false)
dom.GetWindow().Location().Hash = ""
})
codeArea.On("keydown", func(e *angularjs.Event) {
toInsert := ""
switch e.KeyCode {
Expand All @@ -62,6 +97,9 @@ func main() {
}
}
if toInsert != "" {
scope.Set("showShareUrl", false)
dom.GetWindow().Location().Hash = ""

start := codeArea.Prop("selectionStart").Int()
end := codeArea.Prop("selectionEnd").Int()
code := scope.Get("code").Str()
Expand Down Expand Up @@ -118,18 +156,18 @@ func main() {
for _, p := range pkgsToLoad {
path := p

req := js.Global.Get("XMLHttpRequest").New()
req.Call("open", "GET", "pkg/"+path+".a.js", true)
req.Set("responseType", "arraybuffer")
req.Set("onload", func() {
if req.Get("status").Int() != 200 {
req := xhr.NewRequest("GET", "pkg/"+path+".a.js")
req.ResponseType = xhr.ArrayBuffer
go func() {
err := req.Send(nil)
if err != nil || req.Status != 200 {
scope.Apply(func() {
scope.Set("output", []Line{Line{"type": "err", "content": `cannot load package "` + path + `"`}})
scope.Set("output", []Line{Line{"type": "err", "content": `failed to load package "` + path + `"`}})
})
return
}

data := js.Global.Get("Uint8Array").New(req.Get("response")).Interface().([]byte)
data := js.Global.Get("Uint8Array").New(req.Response).Interface().([]byte)
packages[path], err = compiler.ReadArchive(path+".a", path, bytes.NewReader(data), importContext.Packages)
if err != nil {
scope.Apply(func() {
Expand All @@ -141,8 +179,7 @@ func main() {
if pkgsReceived == len(pkgsToLoad) {
run(loadOnly)
}
})
req.Call("send")
}()
}
return
}
Expand All @@ -163,7 +200,10 @@ func main() {
js.Global.Call("eval", js.InternalObject(jsCode.String()))
}
scope.Set("run", run)
run(true)
go func() {
<-codeReady // Wait for "code" value to be ready.
run(true)
}()

scope.Set("format", func() {
out, err := format.Source([]byte(scope.Get("code").Str()))
Expand All @@ -174,6 +214,37 @@ func main() {
scope.Set("code", string(out))
scope.Set("output", []Line{})
})

scope.Set("share", func() {
req := xhr.NewRequest("POST", "http://"+snippetStoreHost+"/share")
req.ResponseType = xhr.ArrayBuffer
go func() {
// TODO: Send as binary?
err := req.Send(scope.Get("code").Str())
if err != nil || req.Status != 200 {
scope.Apply(func() {
scope.Set("output", []Line{Line{"type": "err", "content": `failed to share snippet`}})
})
return
}

data := js.Global.Get("Uint8Array").New(req.Response).Interface().([]byte)
scope.Apply(func() {
id := string(data)

dom.GetWindow().Location().Hash = "#/" + id

scope.Set("shareUrl", dom.GetWindow().Location().Str())
scope.Set("showShareUrl", true)
// TODO: Do this better using AngularJS.
// Perhaps using http://stackoverflow.com/questions/14833326/how-to-set-focus-on-input-field/18295416.
go func() {
time.Sleep(time.Millisecond)
dom.GetWindow().Document().GetElementByID("share-url").(*dom.HTMLInputElement).Select()
}()
})
}()
})
})
}

Expand Down
Loading