Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Build
run: docker build -t hackpad .
- name: Publish
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.16
go-version: 1.20.x
- name: Lint
run: make lint

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.16
go-version: 1.20.x
- name: Test
run: make test
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
SHELL := /usr/bin/env bash
GO_VERSION = 1.16.6
GO_VERSION = 1.20
GOROOT =
PATH := ${PWD}/cache/go/bin:${PWD}/cache/go/misc/wasm:${PATH}
GOOS = js
GOARCH = wasm
export
LINT_VERSION=1.27.0
LINT_VERSION=1.52.2

.PHONY: serve
serve:
Expand Down Expand Up @@ -71,7 +71,7 @@ cache/go${GO_VERSION}: cache
git clone \
--depth 1 \
--single-branch \
--branch hackpad-go${GO_VERSION} \
--branch hackpad/release-branch.go${GO_VERSION} \
https://github.com/hack-pad/go.git \
"$$TMP"; \
pushd "$$TMP/src"; \
Expand Down
1 change: 1 addition & 0 deletions cmd/editor/css/css.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package css
Expand Down
1 change: 1 addition & 0 deletions cmd/editor/dom/document.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package dom
Expand Down
13 changes: 10 additions & 3 deletions cmd/editor/dom/element.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package dom
Expand Down Expand Up @@ -111,14 +112,20 @@ func (e *Element) QuerySelectorAll(query string) []*Element {
return sliceFromArray(e.elem.Call("querySelectorAll", query))
}

func (e *Element) AddEventListener(name string, listener EventListener) {
e.elem.Call("addEventListener", name, js.FuncOf(func(this js.Value, args []js.Value) interface{} {
func (e *Element) RemoveEventListener(name string, listener js.Func) {
e.elem.Call("removeEventListener", name, listener)
}

func (e *Element) AddEventListener(name string, listener EventListener) js.Func {
listenerFunc := js.FuncOf(func(_ js.Value, args []js.Value) interface{} {
defer common.CatchExceptionHandler(func(err error) {
log.Error("recovered from panic: ", err, "\n", string(debug.Stack()))
})
listener(args[0])
return nil
}))
})
e.elem.Call("addEventListener", name, listenerFunc)
return listenerFunc
}

func (e *Element) Focus() {
Expand Down
1 change: 1 addition & 0 deletions cmd/editor/dom/rect.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package dom
Expand Down
1 change: 1 addition & 0 deletions cmd/editor/dom/window.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package dom
Expand Down
21 changes: 11 additions & 10 deletions cmd/editor/editor.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//go:build js
// +build js

package main

import (
"io/ioutil"
"os"
"syscall/js"

Expand All @@ -13,19 +13,20 @@ import (
)

// editorJSFunc is a JS function that opens on a JS element and returns a JS object with the following spec:
// {
// getContents() string
// setContents(string)
// getCursorIndex() int
// setCursorIndex(int)
// }
//
// {
// getContents() string
// setContents(string)
// getCursorIndex() int
// setCursorIndex(int)
// }
type editorJSFunc js.Value

func (e editorJSFunc) New(elem *dom.Element) ide.Editor {
editor := &jsEditor{
titleChan: make(chan string, 1),
}
editor.elem = js.Value(e).Invoke(elem, js.FuncOf(editor.onEdit))
editor.elem = js.Value(e).Invoke(elem.JSValue(), js.FuncOf(editor.onEdit))
return editor
}

Expand All @@ -43,7 +44,7 @@ func (j *jsEditor) onEdit(js.Value, []js.Value) interface{} {
if err == nil {
perm = info.Mode()
}
err = ioutil.WriteFile(j.filePath, []byte(contents), perm)
err = os.WriteFile(j.filePath, []byte(contents), perm)
if err != nil {
log.Error("Failed to write file contents: ", err)
}
Expand All @@ -62,7 +63,7 @@ func (j *jsEditor) CurrentFile() string {
}

func (j *jsEditor) ReloadFile() error {
contents, err := ioutil.ReadFile(j.filePath)
contents, err := os.ReadFile(j.filePath)
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/editor/ide/command.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package ide
Expand All @@ -16,15 +17,15 @@ const (
)

// runGoProcess optimizes runProcess by skipping the wait time for listing PATH directories on startup
func (w *window) runGoProcess(console TaskConsole, args ...string) promise.Promise {
func (w *window) runGoProcess(console TaskConsole, args ...string) promise.JS {
return w.runRawProcess(console, goBinaryPath, "go", args...)
}

func (w *window) runProcess(console TaskConsole, name string, args ...string) promise.Promise {
func (w *window) runProcess(console TaskConsole, name string, args ...string) promise.JS {
return w.runRawProcess(console, name, name, args...)
}

func (w *window) runRawProcess(console TaskConsole, fullPath, name string, args ...string) promise.Promise {
func (w *window) runRawProcess(console TaskConsole, fullPath, name string, args ...string) promise.JS {
resolve, reject, prom := promise.New()
go func() {
var success bool
Expand Down Expand Up @@ -69,6 +70,6 @@ func (w *window) startProcess(console TaskConsole, rawPath, name string, args ..

func (w *window) runPlayground(console TaskConsole) {
w.runGoProcess(console, "build", "-v", ".").Then(func(_ interface{}) interface{} {
return w.runProcess(console, "./playground")
return w.runProcess(console, "./playground").JSValue()
})
}
1 change: 1 addition & 0 deletions cmd/editor/ide/console.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package ide
Expand Down
3 changes: 2 additions & 1 deletion cmd/editor/ide/dropdown.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package ide
Expand Down Expand Up @@ -33,7 +34,7 @@ func newDropdown(attachTo, content *dom.Element) *dropdown {
attached: attachTo,
}
dom.GetDocument().AddEventListener("click", func(event js.Value) {
if !event.Call("composedPath").Call("includes", drop).Bool() {
if !event.Call("composedPath").Call("includes", drop.JSValue()).Bool() {
drop.Close()
}
})
Expand Down
1 change: 1 addition & 0 deletions cmd/editor/ide/editor.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package ide
Expand Down
1 change: 1 addition & 0 deletions cmd/editor/ide/settings.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package ide
Expand Down
1 change: 1 addition & 0 deletions cmd/editor/ide/tab.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package ide
Expand Down
1 change: 1 addition & 0 deletions cmd/editor/ide/tab_pane.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package ide
Expand Down
38 changes: 18 additions & 20 deletions cmd/editor/ide/window.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//go:build js
// +build js

package ide

import (
_ "embed"
"go/format"
"io/ioutil"
"os"
"strings"
"syscall/js"

Expand Down Expand Up @@ -99,7 +100,7 @@ func New(elem *dom.Element, editorBuilder EditorBuilder, consoleBuilder ConsoleB
}

go func() {
src, err := ioutil.ReadFile(path)
src, err := os.ReadFile(path)
if err != nil {
log.Errorf("Failed to read Go file %q: %v", path, err)
return
Expand All @@ -109,7 +110,7 @@ func New(elem *dom.Element, editorBuilder EditorBuilder, consoleBuilder ConsoleB
log.Errorf("Failed to format Go file %q: %v", path, err)
return
}
err = ioutil.WriteFile(path, out, 0)
err = os.WriteFile(path, out, 0)
if err != nil {
log.Errorf("Failed to write Go file %q: %v", path, err)
return
Expand Down Expand Up @@ -183,11 +184,12 @@ func (w *window) makeDefaultEditor(id int, title, contents *dom.Element) Tabber

title.SetInnerHTML(`<input type="text" class="editor-file-picker" placeholder="file_name.go" spellcheck=false />`)
inputElem := title.QuerySelector("input")
dom.QueueMicrotask(func() {
inputElem.Focus() // run focus on next run loop so opening a file immediately doesn't trigger onblur
})
inputElem.Focus()

keydownFn := func(event js.Value) {
blurListener := inputElem.AddEventListener("blur", func(js.Value) {
w.editorsPane.closeTabID(id)
})
title.AddEventListener("keydown", func(event js.Value) {
if event.Get("key").String() != "Enter" {
return
}
Expand All @@ -199,19 +201,15 @@ func (w *window) makeDefaultEditor(id int, title, contents *dom.Element) Tabber
if fileName == "" {
return
}
title.SetInnerText("New file")
err := editor.OpenFile(fileName)
if err != nil {
log.Error(err)
}
w.editorsPane.focusID(id)
}
title.AddEventListener("keydown", keydownFn)
inputElem.AddEventListener("blur", func(event js.Value) {
titleText := title.InnerText()
if titleText != "New file" {
w.editorsPane.closeTabID(id)
}
inputElem.RemoveEventListener("blur", blurListener)
title.SetInnerText("New file") // setting inner text triggers onblur because the input HTML is about to be removed
go func() {
err := editor.OpenFile(fileName)
if err != nil {
log.Error(err)
}
w.editorsPane.focusID(id)
}()
})
return editor
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/editor/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//go:build js
// +build js

package main

import (
"flag"
"io/ioutil"
"os"
"syscall/js"

Expand Down Expand Up @@ -85,7 +85,7 @@ func main() {
fmt.Println("Hello from Wasm!", datasize.Gigabytes(4))
}
`
err := ioutil.WriteFile("main.go", []byte(mainGoContents), 0600)
err := os.WriteFile("main.go", []byte(mainGoContents), 0600)
if err != nil {
log.Error("Failed to write to main.go: ", err)
return
Expand Down
7 changes: 4 additions & 3 deletions cmd/editor/plaineditor/editor.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//go:build js
// +build js

package plaineditor

import (
"io/ioutil"
"os"
"syscall/js"

"github.com/hack-pad/hackpad/cmd/editor/dom"
Expand Down Expand Up @@ -50,7 +51,7 @@ func (e *textAreaEditor) CurrentFile() string {
}

func (e *textAreaEditor) ReloadFile() error {
contents, err := ioutil.ReadFile(e.filePath)
contents, err := os.ReadFile(e.filePath)
if err != nil {
return err
}
Expand All @@ -59,7 +60,7 @@ func (e *textAreaEditor) ReloadFile() error {
}

func (e *textAreaEditor) edited(newContents func() string) {
err := ioutil.WriteFile(e.filePath, []byte(newContents()), 0600)
err := os.WriteFile(e.filePath, []byte(newContents()), 0600)
if err != nil {
log.Errorf("Failed to write %s: %s", e.filePath, err.Error())
return
Expand Down
13 changes: 7 additions & 6 deletions cmd/editor/plaineditor/typer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package plaineditor
Expand Down Expand Up @@ -42,7 +43,7 @@ func handleKeydown(event js.Value) {
selectionEnd := target.Get("selectionEnd").Int()
key := event.Get("key").String()
code := event.Get("code").String()
//metaKey := event.Get("metaKey").Bool()
// metaKey := event.Get("metaKey").Bool()

preventDefault := func() {
event.Call("preventDefault")
Expand All @@ -59,11 +60,11 @@ func handleKeydown(event js.Value) {

if code == KeyEnter {
// TODO restore cmd+enter triggering run button
//if metaKey {
//preventDefault()
//runPlayground()
//return
//}
// if metaKey {
// preventDefault()
// runPlayground()
// return
// }

lastNewLine := strings.LastIndexByte(slice(text, 0, selectionStart), '\n')
if lastNewLine != -1 {
Expand Down
1 change: 1 addition & 0 deletions cmd/editor/taskconsole/console.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build js
// +build js

package taskconsole
Expand Down
Loading